1,056 views
2 votes
2 votes
#include <stdio.h>
int main(){
    int k = m();
    printf("%d ",k);
}
void m(){
    printf("hello\n");
}

https://gateoverflow.in/?qa=blob&qa_blobid=15997855123266011837

i think code should throws compiler error because no declaration of m() before calling it.so compiler assume return type must be int but is void.but code compile and run correctly.please explain

2 Answers

0 votes
0 votes

In C99 strict, we can not do this. In C89/C90 or gcc 4.8.4+, we need not need to pre-declare (or prototype) a function; it would be implicitly declared as function taking undefined list of arguments and having return type 'int'.
Although type conpflict 'warning' would occur.

Below the results using gcc 4.8.4

In of case c99 strict : compilation gives errors:

prog.c: In function 'main':
prog.c:3:10: error: implicit declaration of function 'm' [-Werror=implicit-function-declaration]
  int k = m();
          ^
prog.c: At top level:
prog.c:6:6: error: conflicting types for 'm' [-Werror]
 void m(){
      ^
prog.c:3:10: note: previous implicit declaration of 'm' was here
  int k = m();
          ^
cc1: all warnings being treated as errors

Related questions

0 votes
0 votes
1 answer
2
Desert_Warrior asked May 16, 2016
2,358 views
#include<stdio.h int main() { int a = 5; int b = ++a * a++; printf("%d ",b); return 0; }(a) 25 (b) 30 (c) 36 (d) Undefined Behavior
0 votes
0 votes
2 answers
3
Desert_Warrior asked May 16, 2016
8,914 views
#include<stdio.h int main() { int a = 5; switch(a) { default: a = 4; case 6: a ; case 5: a = a+1; case 1: a = a-1; } printf("%d \n",a); return 0; }(a) 5 (b) 4 (c) 3 (d) N...