Hi folks,

I'm trying following code..

#include<stdio.h>

int calsum(int a,int b,int c);

int main(){

    int x,y,z,sum=0;

    printf("Enter the 3 numbers:\n");

    scanf("%d%d%d",&x,&y,&z);

    sum=calsum(x,y,z);

    printf("The vlaue of sum is :%d\n",sum);
    return 0;

}



int calsum(int a,int b,int c){

    int d=0;

    printf("The value of d is:%d",d);

    d=a+b+c;
}

** (1) **
When I compile with

gcc -o test_cal test_cal.c

It not giving any warning and when I execute it, it is returning actual summation to the main.
Program works perfectly.

** (2) **
But when I compile with

gcc -Wall -Werror -o test_cal test_cal.c

It is giving me a warning..

cc1: warnings being treated as errors
test_cal.c: In function ‘calsum’:
test_cal.c:20: error: control reaches end of non-void function

I'm using gcc version 4.4.4 and Fedora 13. Can anybody explain me, how the code works in 1st case?

The answer is in your error message

cc1: warnings being treated as errors
test_cal.c: In function ‘calsum’:
test_cal.c:20: error: control reaches end of non-void function

Since function calsum is non-void(means returns something), so you should add a return statement.

return d;

Then why it is not giving any warning with

gcc -o test_cal test_cal.c

Then why it is not giving any warning with

gcc -o test_cal test_cal.c

Well that's not mine (or gcc's) problem, it's your job to follow good programming practices.

Well that's not mine (or gcc's) problem, it's your job to follow good programming practices.

Agree. Its not GCC's problem. But how it is returning value to main, even we are not returning in code.

I guess it just adds ret statement and pops whatever is in the eax register.

But how it is returning value to main, even we are not returning in code.

You lied to the compiler and it's doing what you asked. The following says that calsum() returns an int:

int calsum(int a,int b,int c)

Whether you actually return a value or not, the compiler will allow you to use the return value as if something were returned because of that lie. In other words, this compiles:

sum=calsum(x,y,z);

However, since you didn't actually return a value, the value is likely whatever was already sitting in that spot in memory when calsum() returns. In other words, it's garbage. You can think of it as if the compiler internally translates calsum() to this:

int calsum(int a,int b,int c){
    int return_value;

/* User code... */
    int d=0;
 
    printf("The value of d is:%d",d);
 
    d=a+b+c;
/* End user code... */

    return return_value;
}

Notice how return_value is uninitialized. Now, if you had a return statement:

int calsum(int a,int b,int c){
    int d=0;
 
    printf("The value of d is:%d",d);
 
    d=a+b+c;

    return d;
}

Then return_value would be initialized in our pretend translation based on knowledge of how the compiler works:

int calsum(int a,int b,int c){
    int return_value;

/* User code... */
    int d=0;
 
    printf("The value of d is:%d",d);
 
    d=a+b+c;

    return_value = d;
/* End user code... */

    return return_value;
}

Dear Diwakar and Narue,

Thanks for the solution. I understood how the compiler works in case of return value.

THANKS!!

I guess it just adds ret statement and pops whatever is in the eax register.

Sorry, there was a typo in the above line. I mean,pops whatever is in the stack to the eax register(i.e.last pushed value).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.