0

I wrote this code :

#include <stdio.h>

int main()
{
   int a,b = 10,c,d;
a = printf("b is %d\n",b);
c = printf("a is %d\n",a);
d = printf("c is %d\n",c);
    printf("d is %d\n",d);  // lose a null terminator in the previous strings ?? c and d
                            // are 7, but a is 8, and when you count characters in a's string theres 8, 
                            // and also 8  in  c, and d,  yet c and d only retain a 7 value count. WHY?

a = a + b + c + d;   // now lets add them together 
printf("Added together %d\n",a); // print the result. Confused ??? 
return 0;
}

Why is a 8 characters long and the a integer equal to 8, but integer c and integer d are both equal to 7, but they too are 8 characters in length ?

Edited by CodyOebel

2
Contributors
3
Replies
15
Views
1 Year
Discussion Span
Last Post by CodyOebel
2

OK, line 6's printf output is "b is 10" and has a trailing \n os that's 8.
Line 7 printf output is "a is 8" with a trailing \n so that's 7.

printf() returns the number of characters it printed so all looks right here.

Edited by rproffitt: clarfying

Votes + Comments
AHhhhhhh the number of characters returned ... I seee
0

So I see now .. if b was set to 101 and not 10.
This would mean that a = ("b is %d\n");

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.