A simple C problem.....

Pensu
@pensu-8tNeGU โ€ข Oct 25, 2024

Hey ppl,

I have a problem related to a C program.

#include<stdio.h>
#include<conio.h>


 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

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Reya

    @reya-SMihdC • Aug 9, 2011

    If i run the program using online compiler it returns the output as 0.Whereas in devcpp it returns 7.

  • Pensu

    @pensu-8tNeGU • Aug 9, 2011

    @praveena: exactly my point....how it can print 7 while i am not returning anything.

  • Bharath51

    @bharath51-UnF02G • Aug 13, 2011

    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

    @gauravbhorkar-Pf9kZD • Aug 15, 2011

    This may also be because of compiler optimization. Did you get a warning?

    PS: I didn't compile your code

  • ayusha patnaik

    @ayusha-patnaik-UIGhHT • Aug 15, 2011

    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 .