简体   繁体   中英

C - printf shows unexpected output

I am kind of new to programming and wanted to write a program to read 5 numbers and add them. this is how it looked like.

#include <stdio.h>

int main(int argc, char *argv[])
{
    int a,b,c,d,e,sum;

    printf("Enter the 5 numbers\n");
    scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);

    sum=a+b+c+d+e;
    printf("Sum of entered integers is %d\n,sum");

    return 0;
}

I haven't reached loops till now. So, why isn't my program not giving the correct results? It showed no compiling error to me. But when I input the numbers 1,2,3,4,5, it gave some gibberish result as 2752264!!!

Your printf statement syntax is wrong.

Use printf("Sum of entered integers is %d\\n", sum);

instead of printf("Sum of entered integers is %d\\n,sum");

You need to specify what variable value you have to print for %d inside the printf statement and it has to be outside the quotes. Right now in your case you are not specifying any variable as everything is inside the quotes. So it is taking garbage value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM