Like Tree1Likes
  • 1 Post By jimblumberg

Need help with the following code....

This is a discussion on Need help with the following code.... within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> int calsum(int x,int y,int z); void main() { int a,b,c,sum; printf("\nENter any three numbers\n"); scanf("%d\n%d\n%d",&a,&b,&c) ; sum=calsum(a,b,c); printf("\nSum=%d\n",sum); ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    6

    Question Need help with the following code....

    Code:
    #include<stdio.h>
    int calsum(int x,int y,int z);
    void main()
    {
        int a,b,c,sum;
        printf("\nENter any three numbers\n");
        scanf("%d\n%d\n%d",&a,&b,&c)  ;
        sum=calsum(a,b,c);
        printf("\nSum=%d\n",sum);
    }
    int calsum(int x,int y,int z)
    {
        int d;
        d=x+y+z;
        return;
    }
    I thought this program will print garbage value...
    but in a compiler C-free : it outputs the value x+y each time and in gcc compiler it gives the value of x+y+z
    Can you explain the actual behaviour of the program please.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    593
    should be int main, and it should return d correct?

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,717
    You need to return a value from your function. Try
    Code:
    return d;
    . Otherwise the compiler will return some "random" value.

    Jim

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    26
    Considering you're not returning anything (and you're using void main ), it should be getting garbage values/runtime error/etc. I don't know about your compiler(s), but it may just be "correcting" you because it thinks you forgot to add a "d" to the return value. And for reference, you could change it to:

    Code:
    int calsum(int x,int y,int z)
    {
        return (x + y + z);
    }
    It's not any better, just a little cleaner-looking.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    Actually I was trying to find out what does it return when I dont specify what to return.
    I expected garbage but it gave the correct answer(in gcc, ubuntu. I am not much concerned about the C-free output as I dont know how good compiler it is.)

    Can anyone plz explain how the Stack is used to store and clear values between function calls in C. Is it somthing like, the returned value is already at the location pointed to by the return statement by default?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,717
    It is undefined behavior, it may work now, but break tomorrow. The compiler will return something, but you can never rely on the value being correct. The value you are getting may be affected by a different compiler, compiler optimizations, and many other things. If you have a function that returns a value, you should always return the correct type of variable. If you don't want a value returned then use a void function.

    Jim
    beginningC likes this.

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    Thanlk u all.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2010, 10:28 AM
  2. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  3. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 06:09 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21