-9

Came across a code of which I couldn't understand what these two lines are doing. Can't even get the syntax. Please specify what type of functionalities have been used like for eg I am guessing that there has been a use of conditional operators in the first line.

unsigned long long base_size = b >= 2 ? (b-2)/2:0;
printf("%llu\n",(base_size*(base_size+1))/2);
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Ankit Gupta
  • 99
  • 2
  • 5

3 Answers3

3

As per the operator priority is c, the first line is effectively

unsigned long long base_size = ( b >= 2 ? (b-2)/2:0 );

Now, the expression inside ( ) uses a ternary operator or Conditional operator.

As per the C11 standard, chapter 6.5.15, the syntax is

conditional-expression:
                    logical-OR-expression
                    logical-OR-expression ? expression : conditional-expression

and the semantics is [follow my emphasis]

The first operand is evaluated; there is a sequence point between its evaluation and the evaluation of the second or third operand (whichever is evaluated). The second operand is evaluated only if the first compares unequal to 0; the third operand is evaluated only if the first compares equal to 0; the result is the value of the second or third operand (whichever is evaluated) converted to the type described belowNOTE.

NOTE: A conditional expression does not yield an lvalue.

So, following that, your case,

b >= 2 ? (b-2)/2:0 

first, the first expression, b >= 2 is evaluated. if b has value greater than or equal to 2, it returns 1, otherwise, 0.

  • if it evaluates to 1, the second operand, (b-2)/2 is executed and the final result of the conditional operator is the value of the expression.

  • similarly, it evaluates to 0, the third operand 0 is returned as the result of the conditional operator.

Finally, the return value of the ternary operator is used to initialize the unsigned long long base_size variable.

About the second line, printf() is a standard C library function, prototyped in stdio.h. You can find more on this here.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
3

What is used here is the conditional operator ( Also called ternary operator because it uses 3 expressions) The format of the conditional operator is

expression1 ? expression2 : expression3.

Now let me explain this.

If expression1 evaluates to true, then the value of the whole expression is the value of expression2, otherwise, the value of the whole expression is expression3.

Now take this simple example

result = marks >= 50 ? 'P' : 'F' ;

result will have the value 'P' if the expression marks >= 50 evaluates to true, otherwise, result will get 'F'.

Now let us move on to your case

unsigned long long base_size = b >= 2 ? (b-2)/2:0; 
printf("%llu\n",(base_size*(base_size+1))/2);

It checks if b >= 2, if it is, then assigns base_size the value (b-2)/2 else it assigns base_size the value 0.

It is also the equivalent of

if( b >= 2 )
   base_size = ( b - 2 ) / 2;
else
   base_size = 0;

Now, just in case you don't know

printf("%llu\n",(base_size*(base_size+1))/2);

What this does is outputs the value of base_size * ( base_size + 1 ) / 2 onto your output screen.

Arun A S
  • 6,421
  • 4
  • 29
  • 43
2

This is called the ternary operator

x = condition ? value : another_value;

means

if (condition)
    x = value;
else
    x = another_value;

it's evident that value and another_value should have the same type as x and that condition must be an expression which can evaluate to a truth value.

The code you posted though doesn't look good because it's hard to read, it would be way better this way

unsigned long long base_size;

base_size = (b >= 2) ? (b - 2) / 2 : 0;
printf("%llu\n", (base_size * (base_size + 1)) / 2);

and this would be equivalent to

if (b >= 2)
    printf("%llu\n", (((b - 2) / 2) * (((b - 2) / 2) + 1)) / 2);
else
    printf("0\n");
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97