Search

3.4 — Sizeof, comma, and conditional operators

Sizeof operator

Operator Symbol Form Operation
Sizeof sizeof sizeof(type)
sizeof(variable)
Returns size of type or variable in bytes

We covered the sizeof(type) form of the operator in lesson 2.3 -- Variable sizes and the sizeof operator. Refer to that lesson for more information on the sizeof(type) form of the operator.

Note that sizeof can also be used on a variable, and will return the size of that variable:

Comma operator

Operator Symbol Form Operation
Comma , x, y Evaluate x then y, returns value of y

The comma operator allows you to evaluate multiple expressions wherever a single expression is allowed. The comma operator evaluates to its rightmost operand.

For example:

z would be assigned the result of evaluating ++y, which equals 3.

In almost every case, a statement written using a comma would be better written as separate statements. For example, the above code should be written as:

Note that comma has the lowest precedence of all the operators, even lower than assignment. Because of this, the following two lines of code do different things:

Most programmers do not use the comma operator at all, with the single exception of inside for loops, where its use is fairly common. We discuss for loops in a future lesson.

Note that although a comma is used to separate the parameters in a function call, this is not using the comma operator.

Similarly, when declaring multiple variables on a single line, the comma there is just a normal separator, not the comma operator

Rule: Avoid using the comma operator, except within for loops.

Conditional operator

Operator Symbol Form Operation
Conditional ?: c ? x : y If c is nonzero (true) then evaluate x, otherwise evaluate y

The conditional operator (?:) (also known as the “arithmetic if” operator) is C++’s only ternary operator (it takes 3 operands). Because of this, it’s also sometimes referred to as the “ternary operator” (we suggest you avoid calling it this in case C++ adds another ternary operator in the future).

The ?: operator provides a shorthand method for doing a particular type of if/else statement.

If/else statements in the following form:

if (condition)
    expression;
else
    other_expression;

can be rewritten as:

(condition) ? expression : other_expression;

Note that the operands of the conditional operator must be expressions themselves (not statements).

For example, an if/else statement that looks like this:

if (condition)
    x = some_value
else
    x = some_other_value

can be rewritten as:

x = (condition) ? some_value : some_other_value;

Many people find this more compact form easier to read.

As another example, to put the larger of values x and y in variable larger, we could write this:

Or this:

It is common to put the conditional part of the expression inside of parenthesis, both to make it easier to read, and also to make sure the precedence is correct.

It is also worth noting that the expression between the ? and : is evaluated as if it were parenthesized.

Keep in mind that the ?: operator has a very low precedence. If doing anything other than assigning the result to a variable, the ?: statement needs to be wrapped in parenthesis.

For example to print the larger of values x and y to the screen, we could do this:

Or we could do this:

Because the << operator has higher precedence than the ?: operator, the statement:

would evaluate as:

That would print 1 (true) if x > y, or 0 (false) otherwise!

The conditional operator gives us a convenient way to simplify simple if/else statements, particularly when assigning the result to a variable or returning the result as part of a function return. It should not be used for complex if/else statements, as it quickly becomes both unreadable and error prone.

Rule: Only use the conditional operator for simple conditionals where it enhances readability.

The conditional operator evaluates as an expression

It’s worth noting that the conditional operator evaluates as an expression, whereas if/else evaluates as statements. This means the conditional operator can be used in some places where if/else can not.

For example, when initializing a const variable:

There’s no satisfactory if/else statement for this, since const variables must be initialized when defined, and the initializer can’t be a statement.

3.5 -- Relational operators (comparisons)
Index
3.3 -- Increment/decrement operators, and side effects

48 comments to 3.4 — Sizeof, comma, and conditional operators

  • Steve

    Hey, whats the point of the comma operator if all it does is evaluate to the rightmost operand?

    • The only place I see the comma operator commonly used is inside loops, to increment multiple variables, especially for loops. We cover for loops in more detail in Chapter 5.7 -- For statements.

      Using the comma operator is a useful way to increment or decrement multiple variables in a single expression. For example:

      Due to the particular syntax constraints on for loops, all your variables have to be modified in a single expression -- this allows you to do that.

  • manju23reddy

    hi,
    using sizeof function
    will resolve during
    1. runtime
    2. compile time 3. pre-compile time

    plz answer me

  • jeremy

    Is it bad form to place the assignment inside the arithmetric if? eg.:

    It compiles fine with Codeblocks and GCC.
    This seems more elegant IMHO.

    • Alex

      I think

      is the preferred form for several reasons.

      1) It’s not as redundant (there’s only one assignment to z instead of two).
      2) It makes it clearer that something is being assigned to z.

  • bla

    It’s a bit confusing that a lower precedence corresponds to a higher precedence level here. Is that the standard way of saying it?

  • bacia

    return x, y;

    would this only return y?

  • Catreece

    The arithmetic if is handy to know it exists, but I don't think I'll ever use it myself. It's far more concise, but also far less clear what's going on and it took a few re-reads of the example code to figure out what was being said even after understanding how it works because of the odd syntax.

    I figure it'll be handy when reading over the code from other people, but I'm a stickler for keeping things as easy to read and understand as possible since my past experience has been that, in a week, you won't remember why you did something a certain way and if you have to change it, you'll definitely save time by it being easier to read than you would've saved by typing out a few more lines.

    I suppose if you used the arithmetic if constantly, all the time, you'd get used to the format eventually, but even then it's still a matter of consistency: if you're going to do ten of the same kind of action, do it the same way each time so that you don't have to mentally juggle where the variables go when trying to adjust something, especially if you have to adjust all ten of those functions in a similar manner that's just barely off enough that you can't just do a global fix. At that point it's nice to have all ten of those functions looking nearly identical so that it's blatantly obvious what needs to be changed where without having to think about what needs to be altered.

    I guess that's just my personal preference, but I'd always favour easier to read code over faster to write code if given the choice. =P

  • techsavvy....aye

    how would you use the above snippets for programming ? can you give an example ?

    • Darren

      Finding the minimum or maximum between two values e.g.

      Templating is an advanced topic but essentially means you can call this function with any type you like, so long as the type has the greater-than (>) and less-than (<) operators defined.

  • mike

    Why should we avoid calling it the ternary operator?

    • Alex

      Because that’s not its name. It’s the conditional operator. 🙂

      Also, there aren’t any guarantees there won’t ever be another ternary operator.

  • Mr D

    ok, so:

    means that the compiler only asks “is x bigger then y” (if so, output 1, otherwise 0) and the

    part is just ignored?

    (By the way, just so you know, when one posts a comment, then later edits it, after the edit the post no longer formats correctly using

    )

    • Alex

      Yes, I believe your analysis is correct.

      I noticed that the code formatter, the AJAX comment editor, and the “literal comments” plugin (that ensures code outside of code tags doesn’t get treated as HTML) don’t play together correctly. It’s something we’ll have to live with for now.

  • Aryan

    How does the compiler know that I am using the normal comma and not the comma operator? Is it that the comma operator gets activated or something when its around some particular type of syntax like the loop that you mentioned.
    Also if I want to use comma operator how does the compiler know that I am using the operator and not the normal comma?
    (I hope I didn’t gave you a headache with so many commas) 😉

    • Alex

      When the compiler compiles your program, it breaks down each line into components and checks them against its list of valid syntax to ensure each line is valid.

      In some of those rules, the comma symbol is used as a separator (e.g. to separate multiple arguments or function parameters).
      In other rules, the comma is allowed as an operator (e.g. in an expression).

      The language has been defined in such a way that there is no ambiguity about which use is intended (otherwise, the compiler wouldn’t be able to compile your program without additional input from you).

  • Aryan

    How do you avoid using a comma operator? I mean a comma is a comma. And the syntax of comma operator and normal comma is astoundingly similar!
    Also how does the compiler know that I am using a comma operator and not a comma or using a comma and not a comma operator? (Sorry for so many commas)  😉

    • Alex

      I’m not sure what you mean. The C++ compiler is smart enough to infer when you’re trying to use the comma operator and when you’re not.

      Typically in C++ the comma operator is used with for loops and that’s about it. The other uses of comma are just to fulfill the expected syntax (e.g. to separate function arguments or parameters) -- they’re not a use of the comma operator.

  • afshin javani

    is it possible ,to make a program with c++ for android devices ?

    int my knowledge=little,why=many;

    while((my knowledge==little)||(why==many))
    {
       If(your answer==yes)
           how?;

       my knowledge++;
       why-;
    }

    • Devashish

      Yes, it’s possible. You can use the NDK plugin with eclipse for writing in C++. But, because C++ is not the primary language used to write for android, I think you should learn java. If you are familiar with C++, it won’t take more than a month to write codes in java. It’s a truth that Java is the favorite language chosen by thousands of programmers, to write apps for android.

  • Pravasi Meet

    Consider following program (See live demo http://ideone.com/Nj3W4z & http://cpp.sh/8snm3)

    The above program prints 4 as an output in C but prints 20(sizeof array) as an output in C++. The comma operator in C always results in an r-value even if its right operand is an l-value, while in C++ the comma operator will result in an l-value if its right operand is an l-value. This means that certain expressions are valid in C++ but not in C. That’s why in above C program there is lvalue to rvalue conversion occurs but in C++ it always results in locator value.
    Following is simple example to demonstrate it.

    When you compile above program in C the C compiler will give following error:
    [Error] lvalue required as left operand of assignment

    It would be better If you add this information.

  • bram jorna

    what is the usefull use of the conditional operator?
    i get that it makes the code smaller. but doing so it might confuse you and give errors.
    does it have any other advantages over a normal if/else statement apart from reducing the size of the typed code??

    • Alex

      Yes. The conditional operator evaluates as an expression, whereas if/else evaluates as a set of statements. This means the conditional operator can be used in places where if/else can not.

      For example, when initializing a const variable:

      There’s no equivalent if/else statement for this, since const variables must be initialized when defined, and the initializer can’t be a statement.

      • Chris

        I don’t understand your sentence "There’s no equivalent if/else statement for this, since const variables must be initialized when defined, and the initializer can’t be a statement.". what is the point?

        I can use if/else statement like this and work well :

        • Alex

          I think we’re quibbling about the meaning of “equivalent”. You’re using it in the sense of “produces the same result”, I’m using in the sense of “as good as”.

          While your suggestion does work (in this case), it has some serious downsides:
          * It’s a lot more complicated (7 lines instead of 2, and introduces a new variable into the scope)
          * It’s may be a lot slower, because you’re doing an assignment and an initialization, whereas the conditional version does only the initialization. Integers are cheap to copy, so in this case it’s probably not a big deal. But if this were a large array, you wouldn’t want to take the performance hit of making an extra copy.

          Therefore, I think it’s safe to say it’s not as good as using a conditional.

          I’ll change the word “equivalent” to “satisfactory” to indicate the statement is a comprehensive judgment, not just based on whether it works.

  • Jim

    Alex,
    You’ve used z = ( x, y ) as a comma separator.  Does the compiler first assign z = x , then assign z = y, so this is the reason the comma uses the right most operand?

    Is it true that if the code were z = ( u,v,w,x,y ) that  all of these variables would be assigned one at a time to z and end up with z = y?  Is it also possible to use integers
    z= ( 1, 2 )?

    • Alex

      No, (x,y) is evaluated first to produce the value y, and then y is assigned to z. At no point will x be assigned to z.

      For z = (1, 2), (1, 2) should evaluate to 2, and then the value 2 will be assigned to z.

  • Domenic D

    I believe you said that the parts after the condition must be expressions not statements. Kinda wondering if there is a reason for that.

    eg)

    After the condition the two parts above are statements. Right? This works in CB but why should I not do this. Confused???

    • Alex

      cout << “whatever” is actually an expression, not a statement (remember, statements end in a semicolon).

      Note that you can make any expression into a statement by putting a semicolon on the end of it (these are called expression statements). So when we write cout << “whatever”; this is an expression statement.

      As for the reason why statements are disallowed inside the conditional operator, the conditional operator itself is an expression, sso what is inside it must also be an expression so the whole thing can evaluate to a single value. Otherwise you could do some weird stuff like this:

      What does this even mean?

  • Karl

    how to replace if else with conditional operator here??

    • Alex

      Replace:

      with:

  • Peng

    Consider the above example, the GCC complain:
    F:\LearnCpp\main.cpp|12|error: ‘classSize’ was not declared in this scope|
    "It’s worth noting that the conditional operator evaluates as an expression, whereas if/else evaluates as statements. ", what’s the exact meaning of this? Could you give the more detailed about it?
    Nothing could be better for your help, Thanks Alex 🙂

    • Alex

      The problem here is that the lines that execute as part of the if/else statement are considered to have block scope, so the above actually compiles as if you’d typed:

      In this context, it’s a little easier to see that classSize is local to the block, and thus not accessible to be printed using std::cout.

      Remember: an expression is a sequence of operators and operands that evaluates to a value. A statement is the C++ equivalent of a sentence, and ends in a semicolon. You can define a named variable in a statement, but not an expression.

  • Nyap

    so basically;
    1) sizeof operators will also evaluate to the size of a variable if you give it’s name/identifier
    2) comma operators are a way of combining multiple expressions. They evaluate to the value that is on the very right
    3) conditional operators:
           (expression) ? (value to evaluate to if expression is non-zero/true) : (value to evaluate to if expression is zero/false)

  • subh

    I used code::blocks IDE and executed below statements. Outcome is 0;  Contrary to the given statement about comma operator (,).

    int x = 0;
    int y = 2;
    int z = (++x, ++y)

  • Grace

    Could you please evaluate this and tell me why 1 is displayed?
    int x=10,y=5;
    cout<<(x>y)&&((x-2<2) || (!y>5));

    Thanks!

  • Matthieu B.

    There’s an empty code section of one line right at the end of this lesson.

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter