CrazyEngineers
  • Pensu
    Pensu

    MemberAug 9, 2011

    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
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Reya

    MemberAug 9, 2011

    If i run the program using online compiler it returns the output as 0.Whereas in devcpp it returns 7.
    Are you sure? This action cannot be undone.
    Cancel
  • Pensu

    MemberAug 9, 2011

    @praveena: exactly my point....how it can print 7 while i am not returning anything.
    Are you sure? This action cannot be undone.
    Cancel
  • Bharath51

    MemberAug 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.....
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberAug 15, 2011

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

    PS: I didn't compile your code
    Are you sure? This action cannot be undone.
    Cancel
  • ayusha patnaik

    MemberAug 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 .
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register