8,286 views
1 votes
1 votes
#include <stdio.h>

func(a,b);

void  main(){
    printf("hello");
    return 0;
}



here the code runs although it shows warning , so how is it that although func has no return type mentioned,it works .

Also the return type of main is void but I am returning a value 0 , so then why is there no error here ?

2 Answers

Best answer
3 votes
3 votes

First of all everything that is not correct is not ERROR in C. C language has evolved from 1970s and C89 (ANSI) was the first standard followed by C99 and now C11. These standards were introduced based on current architectures to ensure best performance for C code. But backward compatibility is always an issue and no one wants an old code just not compiling. 

Return type of a function is assumed to be "int" in C. But this assumption is removed from C99 standard on wards. Compiler might not show error for this, but you can tell compiler to do this. See the compiler warnings for the above code:

gcc func.c 
func.c:2:1: warning: data definition has no type or storage class
 func(a,b);
 ^
func.c:2:1: warning: parameter names (without types) in function declaration
func.c: In function ‘main’:
func.c:6:1: warning: ‘return’ with a value, in function returning void
 return 0;
 ^

Just giving "-Werror" option to gcc

gcc -Werror func.c 
func.c:2:1: error: data definition has no type or storage class [-Werror]
 func(a,b);
 ^
func.c:2:1: error: parameter names (without types) in function declaration [-Werror]
func.c: In function ‘main’:
func.c:6:1: error: ‘return’ with a value, in function returning void [-Werror]
 return 0;
 ^
cc1: all warnings being treated as errors

All warnings became error. 

Now, as per standard, main must always return "int". 

selected by
0 votes
0 votes
The default return type for  c language function is integer.so your fuction is taken by the compiler as returning and integer and about the void main() you cannot return a value form a function which is having a void type that's why you compiler is showing warning and is ignoring the return statement of your main.

Related questions

2 votes
2 votes
3 answers
1
Laahithyaa VS asked Sep 9, 2023
948 views
. What will be the value returned by the following function, when it is called with 11?recur (int num){if ((num / 2)! = 0 ) return (recur (num/2) *10+num%2);else return 1...
1 votes
1 votes
3 answers
3
jverma asked May 23, 2022
1,068 views
#include <stdio.h>int f(int n){ static int r = 0; if (n <= 0) return 1; r=n; return f(n-1) + r;}int main() { printf("output is %d", f(5)); return 0;}Ou...
0 votes
0 votes
0 answers
4
Mizuki asked Dec 9, 2018
316 views
Are these return types valid for a function in C? Would any of these result in error, or simply will be ignored?1 const void f()2 extern void f()3 static void f()