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 ?

Recommended Answers

All 3 Replies

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.

commented: AHhhhhhh the number of characters returned ... I seee +3

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

How do I mark as solved. rproffitt solved this one for me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.