Vasu
Vasu

Reputation: 365

why these two programs giving different outputs?

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);
}

Upvotes: 2

Views: 145

Answers (4)

Shafik Yaghmour
Shafik Yaghmour

Reputation: 158599

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[]) { /* ... */ }

Upvotes: 4

Yu Hao
Yu Hao

Reputation: 122493

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.

Upvotes: 2

Gangadhar
Gangadhar

Reputation: 10526

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
}

Upvotes: 1

user1969104
user1969104

Reputation: 2420

'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.

Upvotes: 0

Related Questions