2

I tried the following programs on Linux system using gcc compiler, both are giving different outputs. Can anyone explain the results?

Program 1:

#include<stdio.h>

int i=10;

int add(){
  printf("hai");
  i=i+1;
}

void main(){
  int k;
  k=add();
  printf("%d",k);
}

Program 2:

#include<stdio.h>

int add(){
      int i=10;
      printf("hai");
      i=i+1;
}

void main(){
     int k;
     k=add();

     printf("%d",k);
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Vasu
  • 265
  • 1
  • 10
  • 18

4 Answers4

4

add is supposed to return an int but you have no return statement at the end of the function so you are invoking undefined behavior and so we can not reason about the results. The C99 draft standard in section 6.9.1 Function definitions paragraph 12 says:

If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

Probably not directly related but still important is that main should return and int not void, the C99 draft standard in section 5.1.2.2.1 Program startup paragraph 1 says:

It shall be defined with a return type of int and with no parameters:

  int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be
used, as they are local to the function in which they are declared):

   int main(int argc, char *argv[]) { /* ... */ }
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • good to know you have given some answer, but what i heard is like printf is returning some value so it will be stored in register, and this can taken by the calling function as we see in program2. do u know anything about this ? @shafik – Vasu Sep 12 '13 at 14:41
  • @user2773010: That's not defined behavior, don't count on it. – Hasturkun Sep 12 '13 at 14:47
  • @user2773010 When you are dealing with undefined behavior you can sometimes make good guesses about what will happen but it will be compiler, platform etc.. dependent and it will only be a guess. [Deep C and C++](http://www.slideshare.net/olvemaudal/deep-c) is a good intro but please only do this type of stuff to learn. – Shafik Yaghmour Sep 12 '13 at 14:47
2

Your program has undefined behavior, the function add is supposed to returned int but has no return statement to actually return a value. From the code, I'm guessing what you want is:

int i = 10;
int add(){
  printf("hai");
  i=i+1;
  return i;  //here
}

The other one is similar, try it and see the result.

Another undefined behavior is void main, as many posts in SO have already stated, you should always use int main(void). Look here for a good discussion on this.

Community
  • 1
  • 1
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
1

In Both programs you did not return any thing from add() function. And k did not initialized before .

return value from add() is any unexpected value.

either try to print i value or else add return statement in function add()

try 1

#include<stdio.h>
void add();
int i=10;
void add(){
  i=i+1;
}
void main(){
add(); 
printf("%d",i); 
//i is global variable so changes made in function will gets effectes here and prints 11
}  

try 2

#include<stdio.h>
int add();
int i=10;
int add(){
  return i+1; //return i+1 
}
void main(){
int k;
k=add();
printf("%d",k); //here k value will modify but i value does not. print i value also
}

try 3

#include<stdio.h>
void add();
void add(){
  int i=10; //i is local to add() function.
  i=i+1;     
}
void main(){
int i=10;
add();
printf("%d",i); //now i value wont change because i is local to main
}
Gangadhar
  • 10,248
  • 3
  • 31
  • 50
0

'add' function does not return any value. What got returned may be a garbage value, which may be different for the two programs or even 2 invocations of the same program.

user1969104
  • 2,340
  • 14
  • 15