A simple C problem.....

Hey ppl,

I have a problem related to a C program.

#include
#include


 int sum(int a,int b)
 {
    int c=a+b;
 }




 int main()
  {
    int a=5,b=2;
    printf("%d",sum(a,b));
    getch();
    return 0;
  }
Now, surprisingly the output is 7. I am not returning anything from the function. It should return garbage value. But how am i getting the correct output????

Replies

  • Reya
    Reya
    If i run the program using online compiler it returns the output as 0.Whereas in devcpp it returns 7.
  • Pensu
    Pensu
    @praveena: exactly my point....how it can print 7 while i am not returning anything.
  • Bharath51
    Bharath51
    i think....it defaultly return last operation performed by the compiler......
    i tried this....
    as
    a+b;
    a-b;
    a*b;
    a/b;

    the last statement "a/b" returned to the function......so the c compiler default return some thing......that is last operation performed.....
  • gaurav.bhorkar
    gaurav.bhorkar
    This may also be because of compiler optimization. Did you get a warning?

    PS: I didn't compile your code
  • ayusha patnaik
    ayusha patnaik
    i think its by default since u r using it for a single tym...if u use multiple operations thn the correct output would be avilable fr a single operation...means it is set as a default .

You are reading an archived discussion.

Related Posts

If anyone need an IEEE paper visit this site IEEE Xplore - Home and just post the title. I'll post back the link to download the paper..
he can anybody help me out with the project ideas for kvpy i am a student in vnit my areas of interest are solar energy technology plz do reply as...
Answer the following UNIX Questions. 1. In Unix inter process communication takes place using? 2. The Very first process created by kernel is?
[pop culture reference] Because Morgan Freeman said nobody responds to 'Help!' in Se7en. [/pop culture reference] Looking for a fourth year project. No source code. Suggestions, just. Mine got rejected...
Reading Comprehension aka RC seems to be one of the toughest sections in the revised GRE. We've received feedback from lot of CEans saying the RC is tougher than older...