What is the difference between declaring a variable and defining a variable? Ans. Declaring a variable means describing its type to the compiler but not allocating any space for it. Defining a variable means declaring it and also allocating space to hold the variable. You can also initialize a variable at the time it is defined. Here is a declaration of a variable and a structure, and two variable definitions, one with initialization: extern int decl1; /* this is a declaration */ struct decl2 { int member; }; /* this just declares the type--no variable mentioned */ int def1 = 8; /* this is a definition */ int def2; /* this is a definition */ To put it another way, a declaration says to the compiler, 'Somewhere in my program will be a variable with this name, and this is what type it is.' A definition says, 'Right here is this variable with this name and this type.' A variable can be declared many times, but it must be defined exactly once. For this reason, definitions do not belong in header files, where they might get #included into more than one place in your program. - Study24x7
Social learning Network
20 Mar 2023 12:18 PM study24x7 study24x7

What is the difference between declaring a variable and defining a variable? Ans. Declaring a variable means describing its type to the compiler but not allocating any space for it. Defining a variable means declaring it and also allocating space to hold the variable. You can al...

See more

study24x7
Write a comment
Related Questions
500+   more Questions to answer
Most Related Articles