print variables inside printf

1
2
3
4
5
double a = 10;
double b = 20;
double c = 30;
printf("\sum %d", a, " and %d", b, "  is = %d", c);
printf("stop");


No compilation errors.
Run time output is:
sum 0
I predicted output: sum 10 and 20 is 30.
What am I doing wrong?

Thx
Last edited on
You're doing a few things wrong. And if you increase the warning level of your compiler it should've at least given you some warnings.

1
2
double a = 10, b = 20, c = 30;
printf("sum %f and %f is %f\n", a, b, c);


So don't put a backslash before the s in sum.
Use %f instead of %d to print floats or doubles.
The first argument to printf is the "format string" and contains all of the output with the "format specifiers" (starting with %) showing where subsequent arguments are to be printed.
Topic archived. No new replies allowed.