0

As a beginner I have learned that only variable definition is allotted a memory in C. But for the following program the output is 0x7ffd12792034

#include<stdio.h>


int main(char args[], int vargs)
{
  int max;
  printf("%p\n", &max);
}
7
  • 2
    int max; is a variable definition. Commented May 30, 2017 at 1:37
  • @user2357112 what would be the function declaration then? I thought a declaration is something which only provides the name and type and not the value, and definition is declaration + value. Am I wrong ?
    – smartnerd
    Commented May 30, 2017 at 1:42
  • 2
    int main(char args[], int vargs) --> int main(int argc, char *args[]) or int main(void)
    – BLUEPIXY
    Commented May 30, 2017 at 1:46
  • 2
    A variable definition does not need to provide an initial value. Commented May 30, 2017 at 1:48
  • 1
    It sounds like you're trying to understand the distinction between declarations and definitions. But the line you have there is definitely a definition, so it does allocate space. As a rough rule of thumb, it's a declaration if it includes the extern keyword, otherwise it's a definition. (This rule isn't perfect, but it's a start.) Commented May 30, 2017 at 1:48

3 Answers 3

3

A local variable is likely to sit on the call stack (but sometimes the compiler would optimize to put it only in some processor register or even forget it entirely). Your int max; is a local variable definition. Its initial value is indeterminate, which practically means that it holds whatever was in the memory location (or the register) before.

Your program is printing the address of that local variable, which is on the call stack.

Because of ASLR the actual value of that address might change (or not) from one program execution to the next. It is implementation specific.

BTW, you should enable all warnings and debug information when you compile. If you use GCC, you should compile with gcc -Wall -Wextra -g. You would then have some warnings at least, in particular because your main function has the wrong signature. It should be int main(int argc, char**argv) and the runtime environment guarantee that argc is at least 1 and that the argv array is NULL terminated, with argc arguments which are non-NULL unaliased strings.

Beware of undefined behavior.

2
  • Oh, so you mean in a function a local variable is always defined?
    – smartnerd
    Commented May 30, 2017 at 1:44
  • 1
    No, variable whose address is taken could sit in some register. They are likely to have a slot in the call stack. Commented May 30, 2017 at 1:58
1

You've actually defined a variable in this case, not declared one.

If you had used the extern keyword, you would have a declaration. But because you didn't, you have a definition.

An initializer such as int max = 1; is not necessary to have a definition. The value will be unspecified until it is assigned one later, but it's still a definition.

If you declared the variable at file scope without an initializer, you would have a tentative definition. You could then have a full definition with an initializer later, but it would have to match the type of the tentative definition.

7
  • But I read that all the functions and variables in C have extern prepended to them by default?
    – smartnerd
    Commented May 30, 2017 at 1:50
  • @smartnerd Not sure what you read, but I think you may have misinterpreted it. Commented May 30, 2017 at 1:55
  • 1
    @smartnerd A function declaration is extern by default. Those are easy to distinguish because they end with a ; while a function definition has a body. For a variable you should specify extern for a declaration.
    – dbush
    Commented May 30, 2017 at 1:57
  • Function definitions also have external linkage by default
    – M.M
    Commented May 30, 2017 at 1:58
  • @SteveSummit Here. geeksforgeeks.org/understanding-extern-keyword-in-c Third para...last third line.
    – smartnerd
    Commented May 30, 2017 at 2:45
0

You are printing an address in your memory which is occupied for int max and ready to get initialize. By typing int max you already have the place where the variable will be stored if you give it a value.

Not the answer you're looking for? Browse other questions tagged or ask your own question.