33

In this c program

a=8;
main()
{
    printf("%d", a);
}   

variable a has been declared without any data type and still this program compiles successfully and gives the desired output.
output ::

8  

see it on ideone.
but, when i declared the same variable inside main, it gives compilation error.

main()
{
     a=8;
    printf("%d", a);
}  

output ::

prog.c:2: warning: return type defaults to ‘int’
prog.c: In function ‘main’:
prog.c:3: error: ‘a’ undeclared (first use in this function)
prog.c:3: error: (Each undeclared identifier is reported only once
prog.c:3: error: for each function it appears in.)
prog.c:4: warning: implicit declaration of function ‘printf’
prog.c:4: warning: incompatible implicit declaration of built-in function ‘printf’  

see here.

How the first program is working but the second?

Eight
  • 4,194
  • 5
  • 30
  • 51

6 Answers6

28

What you see here is the "Implicit Int Rule" at work. Simply put the rule says:

"A variable declared without an explicit type name is assumed to be of type int."

Note that this rule was revoked in the c99 Standard[Ref 1].However, depending on your compiler and its settings, the first example might compile with a warning, or will fail to compile(with strict compilation settings)

If you compile your first example with strict settings adhering to c99 Standard the compiler will tell you the root cause.

check here.

cc1: warnings being treated as errors
prog.c:1: error: data definition has no type or storage class
prog.c:1: error: type defaults to ‘int’ in declaration of ‘a’
prog.c:3: error: return type defaults to ‘int’
prog.c: In function ‘main’:
prog.c:4: error: implicit declaration of function ‘printf’
prog.c:4: error: incompatible implicit declaration of built-in function ‘printf’

EDIT:

why does the first example work but second does not?

Note the emphasis on the words "variable declared" in the rule.

In first example, since the statement is at the global scope it is treated as an Implicit declaration, and the Implicit Int Rule gets applied to it.

In Second example, the statement acts as an Assignment and not a Declaration. Since there is no declaration the Implicit int rule does not apply here. In the absence of any type the compiler cannot determine what is the type of a and hence reports the error.


[Ref 1]

C99 Standard: Foreword
Para 5:

This edition replaces the previous edition, ISO/IEC 9899:1990, as amended and corrected by ISO/IEC 9899/COR1:1994, ISO/IEC 9899/COR2:1995, and ISO/IEC 9899/AMD1:1995.
Major changes from the previous edition include:
.....
.....
— remove implicit int
.....
.....

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 1
    I do feel it has value. For a more complete answer, though, you might explain that in the second example, `a = 8` is an assignment, not a declaration. – sczizzo Jun 16 '12 at 14:44
  • For the first program, you said `main()` function defaults to `int`. But it compiled successfully even when i just called `return;`(without any value) in main. How is that possible? – nitish712 Aug 17 '13 at 17:18
  • 2
    @nitish712: You will need to compile your program by enabling all compiler warnings. `-Wall` in case you are using gcc. C and C++ standards do not require compilers to check whether a function returns a value in all code paths & so by default compilers will not provide this diagnostic, You need to explicitly tell the compiler to do so.Also, note that w.r.t functions `main` is though a function is given a special treatment by the standards in some regards, You may want to check the standards for these rules. – Alok Save Aug 18 '13 at 05:37
8

a=8; outside of the function looks like a declaration statment, where a is the declaration specifier and = 8 is the initializer. C used to allow to omit types in declaration specifiers and defaults to int in that case. (Since C99, this is no longer allowed.)

However, in a function, a=8; looks like an expression statement (an assignment expression) and symbol a isn't resolved. (Note that you don't have expression statements outside of functions.)

If you tried to do, for instance static a = 8;, then it would again look like a declaration, and should compile because the type would default to int.

Anyway, don't rely on that :-) Write your types clearly.

Kos
  • 70,399
  • 25
  • 169
  • 233
3

It's because in C, any variable / function is implicitly int.

This is the same reason that you can use register instead of register int, or unsigned instead of unsigned int, auto instead of auto int, and static instead of static int. I personally always explicitly qualify my variables with int, but whether you do or not is your option.

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
0

Your variable does have a data type (int) even though you didn't write the data type.

It is bad practice to take advantage of that feature.

Apparently your compiler doesn't allow that to happen inside a function. That makes sense because if it did allow "a=8;" in a function it would be very hard to catch certain typoes.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
0

In C++11, there is the auto keyword.Unfortunately, C and C++ are strongly-typed languages, whose require defining a strict type for the each declared variable. Some compliers assume int as default , but it's their well-mind and it should not be used, because it's acknowledged as a wrong programming habit.

0x6B6F77616C74
  • 2,559
  • 7
  • 38
  • 65
0

even a function parameter can be written as implicit int. example:

fun(n){printf("%d",n);}

The above code works fine but once a fixed datatype is included in parameters,like

fun(n,char c){ printf("%d",n);}

It shows error: error: expected ')' before 'char'|

skyconfusion
  • 123
  • 8
  • This is a separate issue to the question. `fun(n)` introduces a K&R-style function definition – M.M Jul 25 '17 at 04:56