I have these lines in my program:
printf("%-29s\n",("%s, Capital", CO));
printf("%-29s\n",("%s, Drawing",CO));
and when I run the program, it only shows the %s
equivalent (CO
) instead of "%s, Drawing"
Please help?
I have these lines in my program:
printf("%-29s\n",("%s, Capital", CO));
printf("%-29s\n",("%s, Drawing",CO));
and when I run the program, it only shows the %s
equivalent (CO
) instead of "%s, Drawing"
Please help?
You used the comma operator and the behavior is normal.
An expression with comma operator A, B
means that first evaluate A
, ignore its result, then evaluate B
and the result of comma operator will be its value.
If you want to show "%s, Drawing", print it.
printf("%-29s\n","%s, Drawing");
As MikeCAT has already stated, you have used the comma operator. If you want to left-align the compound string, you can achieve it by first printing to a temporary buffer with snprintf
:
char tmp[30];
snprintf(tmp, sizeof(tmp), "%s, Capital", CO);
printf("|%-29s|", tmp);
snprintf(tmp, sizeof(tmp), "%s, Drawing", CO);
printf("|%-29s|", tmp);
Of course, given that you print a newline directly after the string (and the justification is therefore somewhat pointless), you might as well just print the desired format directly:
printf("%s, Capital\n", CO);
printf("%s, Drawing\n",CO);