Search code examples
cmingw32

meaning of Immediate Operand of Cast


I was looking at this SO post. Wanted to know two things

  1. C99 standard says

An integer constant expression shall have integer type

But not sure if long long and long are also treated in the same way. I tried with below example and did not get any compiler warnings or errors. So I guess integer means enum, char, int, long and long long.

int main(void)
{
    unsigned long long a=4294967296LL; // no need of LL
    switch (a)
    {
    case 4294967296:
        printf("Hello");
        break;
    }
return(0);
}
  1. Can anyone explain the meaning of 'immediate operands of casts' in the statement "An integer constant expression shall have integer type and shall only have operands that are integer constants, ..... whose results are floating constants that are the immediate operands of casts"

(there is one unanswered comment by @user963241 in the same SO post).

Appreciate one switch case example to substantiate the use of floating constants that are immediate operands of casts.

I use MinGW 32 bit compiler.


Solution

  • As per the section 6.2.5 on Types in the C standard draft (N1570):

    There are five standard integer types, designated as char, short int, int, long int, long long int.

    These have signed and unsigned counterparts.

    The meaning of "floating constants that are the immediate operands of casts" means that the operand of the cast is by itself (not after some arithmetic computation) is a floating constant.

    For example:

    (int)(3.14f) //1. Here the operand is an floating constant that is an immediate operand 
    
    (int)(22.0/7.0f) //2. Here the operand is NOT an floating constant that is an immediate operand. 
    

    You can use 1 in the switch case statement like this:

    switch(op) {
      case (int)(3.14f):
      break;
    }