problem in c programing to find sum of 2 values ?

joyson1988 101 Aug 16, 2013 at 11:11

hye dear all actually i am trying to get the sum of 2 nos. but i don’t know it is showing some error and it is not compiling so please help to tell me what is wrong ?

program i was trying to run it was like that.


void main ()
    int x,y,sum;
printf("enter 2 nos.");



can any buddy tell me where is the error in this program ?

2 Replies

Please log in or register to post a reply.

Vilem_Otte 117 Aug 16, 2013 at 11:51

With a bit of explanation:

#include <stdio.h>
// Always use int main, never void (there is just one exception and that is operating
// system kernel programming, because that doesn't ever return value, but calls HALT
// causing PC to shutdown.
int main()
     // Declare variables where to store first number, second number and their sum
    int x, y, sum;
    // Print out to user, that he needs to enter two numbers
    printf("Enter 2 numbers.\n");
    // Now scan the next input line, it should contain 2 numbers separated by space
    // You might wonder, why there is '&' symbol before each variable. Read something
    // on memory and pointers-to-memory -> that should explain why '&' is there.
    // In short, scanf stores read values at some place in a memory and it needs you
    // to provide memory-address of that place in memory (with a so called pointer
    // variable). Getting memory-address of some variable 'x' is done by using
    // dereference operator - '&x', getting value of memory address is opposite process
    // and is done using reference operator - '*x'.
    scanf("%d %d", &x, &y);

    // After scanf, there will always be new char pending (newline), getchar now

    // Compute sum of the values
    sum = x + y;

    // Print it out
    printf("sum = %d\n", sum);

    // Wait for return, then exit the program

    // Success means return value of 0
    return 0;
Reedbeta 167 Aug 16, 2013 at 16:24

joyson1988, in the future when posting code on the forum, please put [ code ] … [ /code ] tags (remove the spaces) around the code, to preserve the code’s formatting. I added them to your post but unfortunately most of the formatting was already messed up.

Also, when asking help about a compiler error, please post the error message in its entirety and indicate which line of code the error refers to.