The ternary operator inside printf

After reading this , I started thinking that I had learned about printf() about the bast. Suddenly, I found the following code snippet from this book:

 int main() { char str[]="Hello"; int i=5,j=10; printf(i>j?"%50s":"%s",str); //unable to understand this return 0; } 

Surprisingly, the above code works without errors, and it prints Hello. According to my knowledge, the following printf() syntax is:

 int printf(const char *format,argument_list); 

So, according to this syntax, printf() should start with a format string. But, as you can see above, printf() code starts with i>j . Does this mean that I am mistaken in interpreting printf () syntax? Does the ternary operator inside printf () set a special case?

EDIT

I know about the ternary operator. I am asking about the first argument to printf() , which should be const char *, which, it seems to me, is not given in my example.

+6
source share
9 answers

Conditional operator:

 i>j?"%50s":"%s" 

is an expression, and it must be evaluated before the function call itself can be evaluated. We can see this by going to the section of the standard section C99 6.5.2.2 Functional calls, which says:

An argument can be an expression of any type of object. In preparation for calling the function, the arguments are evaluated and each parameter is assigned a value for the corresponding argument. 81)

So, what is the result of evaluating the conditional operator? If we go to section 6.5.15 Conditional operator, then it says (my attention):

The first operand is evaluated; there is a point in the sequence after its evaluation. The second operand is evaluated only if the first comparison is not equal to 0; the third operand is evaluated only if the first is compared to 0; the result is the value of the second or third operand (depending on what is being evaluated) , converted to the type described below. 95

therefore, in any case, the result will be a string literal that will decay to a pointer to a char that satisfies the requirement for the first argument to printf .

+6
source

This code is normal and not a special case. The requirement for printf is that the first argument must be of type const char* , but this does not necessarily mean that it must be a string literal, such as "%s" . All this means that you need to pass an expression of type const char* as the first argument. And i>j?"%50s":"%s" fulfills this requirement.

+3
source

This is a ternary operator, and in this case the condition i> j is false, so% s will be passed as the printf parameter, which will print the value of the character array that it welcomes.

+2
source

Does this mean that I am mistaken in interpreting printf () syntax?

No, you do not interpret this incorrectly.

Is placing a ternary operator inside printf () a special case?

In C, you can say that this is an expression instead of an expression

Your code is equivalent to:

 if (i > j) printf("%50s", str); else printf("%s", str); 
+1
source

A ternary operator is just an inline if that is used as an expression (while a regular if used to create a block). Your line is equal to this:

 if (i > j) printf("%50s", str); else printf("%s", str); 
+1
source
 if(i>j) printf("%50s",str); else printf("%s",str); 

Therefore, Hello prints in both situations.

+1
source

I think you understand the printf syntax well, but I think you are missing something in the C syntax.

It is a form of the "compact IF like" statement, formatted as follows: (condition? True: false)

For example, you can:

 int a=5; int b=(a==5 ? 128 : 256); int c=(a!=5 ? 8 : 9); 

In this case, b = 128 and c = 9.

Another example:

 int flag=1; printf("The value is: %s", (flag!=0 ? "true" : "false) ); 

In this case, you can see: true

In your example:

 printf(i>j?"%50s":"%s",str); 

if I am the top j, you use the format "% 50s", and if I am lower, you use the format "% s"

It could be a view:

 if (i>j) printf("%50s",str); else printf("%s",str); 

You can see the advantage of compact test.

+1
source

Q: Does this mean that I am mistaken in interpreting printf () syntax?
A: No, you just need to expand what is permissible.

Q: Is placing a ternary operator inside printf () a special case?
A: No ?: not special, but is sometimes confused at first sight.

The format provided by printf() does not have to be a literal. It can be any string variable.

 int main() { char str[]="Hello"; char *format; int i,j; i = 5; j = 10; format = i > j ? "%50s" : "%s"; printf(format, str); i = 10; j = 5; format = i > j ? "%50s" : "%s"; printf(format, str); return 0; } 
+1
source

There is a statement of the form: condition? followent: alternative.

The condition is checked, and if it is true, you will receive the following. otherwise you will get an alternative.

For instance:

 5>3 ? 10 : 5 

5> 3 is true, so you get 10.

0
source

Source: https://habr.com/ru/post/971654/


All Articles