Is “int a;” a declaration or definition in C and in C++?

后端 未结 3 1308
情深已故
情深已故 2020-12-30 09:44

Is int a; a declaration or definition in C? Is there any difference in C++?

I was always thinking it\'s just a declaration, until today...

Is i

3条回答
  •  难免孤独
    2020-12-30 10:00

    Consider:

    int a;
    

    It's a definition as well as a declaration.

    Consider the following example...

    int i;
    printf("%d", i);
    

    Some junk value will be printed. Which obviously means it's got a memory location.

    If you want to just declare a variable and not define it then go for "extern" keyword.

    Thus extern int b; is just a declaration and not a definition.

    Example:

    extern int var;
    int main(void)
    {
        var = 10;
        return 0;
    }
    

    Thus the above program will throw an error as "var" was not defined anywhere.

提交回复
热议问题