Does anyone know why the main function returns 9?

Asked 2 years ago, Updated 2 years ago, 161 views

ISO/IEC 9899:201x Committee Draft — April 12, 2011 viewed in 5.1.2.2.3 Program termination

..reaching the } that terminates the main function returns a value of 0.

It says that when the return value is particularly stable in main(), 0 is automatically returned, but this code returns another value.

#include<stdio.h>
int sum(int a,int b)
{
return (a + b);
}

int main()
{
    int a=10;
    int b=5;
    int ans;    
    ans=sum(a,b);
    printf("sum is %d",ans);
}

Compile:

gcc test.c
./a.out sum is 15 echo $? 9/0 should come, but 9 came out

linux c return main

2022-09-21 18:16

1 Answers

..reaching the } that terminates the main function returns a value of 0.

This rule has been set as standard C since 1999, and there is no decision on what value will come out if the return is not specified until C90.

If you specify the C99 compiler with -std=c99 in gcc, you will get 0.


2022-09-21 18:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.