This question already has answers here:
What does the comma operator , do?
(8 answers)
How does the Comma Operator work
(9 answers)
Closed 6 years ago.
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);
Related
This question already has answers here:
How does concatenation of two string literals work?
(4 answers)
Closed last year.
I have seen this way of using combination of strings in printf and scanf statements.
int a;
printf("Printing" "using" "multiple" "strings" "%d", a);
// The above is just an example, some usage that I saw was for printing specific integer types like int_32
// uint32_t var; printf("Value is %" PRTu32, var);
I always thought that we could only use a single string as a format specifier. Like as written in the definition of printf function it seems format can point to only one string.
int printf ( const char * format, ... );
So out of curiosity I tried the following code and it ran successfully!
char arr[] = "Hello " "World";
printf("%s",arr); // Output - Hello World
Could anyone explain how this concatenation thing works and what is the correct way of doing it. Any help is appreciated.
If you give a space or no space in between two string literals it concatenates the string literals.
That's one of the C feature: This is defined by the ISO C standard, adjacent string literals are automatically combined/concatinated into a single one.
This question already has answers here:
function parameter evaluation order
(5 answers)
Closed 5 years ago.
If you have a string as below
str="insert 111,name,123456789"
when you pass it to strtok and try to print the values, they are output in reverse.
For example:
char* token=strtok(str," ");
printf("%s %s %s %s\n",token,strtok(NULL,","),strtok(NULL,","),strtok(NULL,","));
output: insert 1234567 name 1111
instead of:insert 111 name 123456789
Why is this happening? How can this be fixed?
parameters are pushed to the stack in the Calling Convention order, which in your case, reverse... therefore parameter 5 is first to evaluate and pushed in the stack, then 4,3,2 and the format string.
as many comments before suggested, your call style is very discouraged,
and should be avoided.
This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 6 years ago.
I am checking a student's homework.
The assignment is to print the amount of English letters to the console.
For some reason, what he did works (7th line):
int main(void)
{
char first = 'A';
char last = 'Z';
int amount = 0;
amount = ("%d - %d", last - first + 1);
printf("The amount of letters in the English alphabet is %d\n", amount);
return(0);
}
After seeing it, I tried putting other things in the brackets instead of "%d - %d". No matter what I put there and how many commas were there, it'd only take what's after the last comma (which is the correct sentence).
What is actually happening there?
This is one of the examples of usage of comma operator. In case of
("%d - %d", last - first + 1);
the LHS operand of the comma operator ("%d - %d") is evaluated, result is discarded, then RHS (last - first + 1) is evaluated and returned as the result. The result, is then assigned to amount and thus, you have the amount holding the result of the operation last - first + 1.
Quoting C11, chapter ยง6.5.17, comma operator
The left operand of a comma operator is evaluated as a void expression; there is a
sequence point between its evaluation and that of the right operand. Then the right
operand is evaluated; the result has its type and value.
FWIW, in this case, "%d - %d" is just another string literal, it does not carry any special meaning.
This question already has answers here:
Formatted and unformatted input and output and streams
(3 answers)
Closed 9 years ago.
I want to know what is meant by formatted in the printf and scanf functions of the C language. I am new to C programming and did not understand what is meant by formatted. Can anyone please give me the appropriate explanation?
printf("%s %d", "abc", 123);
%s %d is the format string and contains instructions for what to do with the arguments. In this case, it says print string, print a space, then print an integer.
As in formatted output
So you can 'print' say a double called myMoney of 11003.145 as $11,003.15.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
comma separated expression in while loop in C
Hi All,
Has anybody ever encountered this format of while loop in C?
If yes, what is the syntax? I am not able to understand this. Please help.
Regards
kingsmasher1
while(printf("> "), fgets(str, 100, stdin), !feof(stdin))
{
}
This is an excellent example of evil code. Do not write code like this. It is hard to read and debug. It is useful only in obfuscated C contests or to otherwise demonstrate how clever you are. In some jurisdictions, it may render you liable to charges of compiler abuse. Using code like this may cause subsequent maintainers to hunt you down and LART you with extreme prejudice. In extreme cases, Randall Munroe may make fun of you.
Your loop is equivalent to:
do {
printf("> ");
fgets(str, 100, stdin);
} while(!feof(stdin));
This is an instance of the Comma Operator: http://en.wikipedia.org/wiki/Comma_operator Two instances, actually.
The comma operator evaluates the expression on the left hand side of the comma first, then the one on the right, returning the latter as the value of the entire expression.
So in this case, it's equivalent to
do
{
printf("> ");
fgets(str, 100, stdin);
} while (!feof(stdin));
I don't recommend writing obtuse code like this. The comma operator is rarely used - typically in macros which should act like an expression but actually execute a sequence of operations.
That's an abuse of the comma operator.
What it does is
while(printf("> "), fgets(str, 100, stdin), !feof(stdin))
it evaluates each of the expressions separated by the comma operator and discards the values of all but the rightmost one. It uses the value of the right most one as the value of the whole expression. So, you have
while (expression) /*...*/;
and, your expression is
printf("> "), fgets(str, 100, stdin), !feof(stdin)
That expression:
prints a greater than sign and a space evaluating to 2; it discards the 2
reads at most 100 chars from stdin into str and evalutes to either str or NULL if there was an error. That value (str or NULL) is discarded
it sees if the stream stdin is in an end-of-file condition and the value is the value of the whole expression (1 if stdin is in end-of-file condition; 0 otherwise)