in Programming
1,373 views
10 votes
10 votes

Which of the following statements produce a compile time error in C?

  1. int a = sizeof 3;
  2. *(1000) = 5;
  3. int a = 5; ((int)a)++;
  4. int b = 5, *a = &b; ((int*)a)++;
  1. 1, 2 and 3
  2. 2 only
  3. 2, 3 and 4
  4. All 4
in Programming
by
1.4k views

3 Comments

@Arjun sir why error in 3 and 4 ?
0
0
sizeof(3) or sizeof 3?
1
1

yes .there are two forms for sizeof

sizeof unary- expression

sizeof(type)

4
4

4 Answers

26 votes
26 votes
Best answer

Before coming to this question, let me give some C statements:

int a, b, c;
a = b = c = 9;//= is right associative 
//and returns the right value and hence
//c is assigned 9 followed by b and then a.
//Now, I do
c = 1;
c = b;

So, nothing abnormal here rt? But what if I do

1 = c;

Most people should catch the problem here. It should be an error. And this error is called "lvalue required" error. If we see the above 2 statements, "=" allows both constants and variables/expressions as RHS. But its LHS must always be a variable. The term variable is important here- it denotes that the content of that particular memory location can be modified.  So, whwnever left side of "=" is not a variable we get an "lvalue required" error. Now lets come to the given question.

  1. int a = sizeof 3;
  2. *(1000) = 5;
  3. int a = 5; ((int)a)++;
  4. int b = 5, *a = &b; ((int*)a)++;

Here, 1 is fine. sizeof is an operator and not a function in C. We do not need to give a () unless we are using a type and not an expression.

All 2, 3 and 4 are errors.

  1. For 2, * operation requires that its operand must be a pointer but here it is a constant. Even normal variables are not allowed here. i.e.,
    int a;
    *a = 5;//invalid
    int *p = &a;
    *p = 5;//valid
    *((int*)1000)= 5;
    //syntactically correct but works only if 1000 is a valid memory location
    
  2. int a = 5; ((int)a)++;

    This will be more confusing. Without (int) it is fine. But what happens with (int) - typecasting is that it always returns the value. i.e., (int)a - returns the integer value of memory location a. This is fine if we use it as RHS of "=" as both "a=b" and "a=5" does the same operation if b is having value 5. But not when we use at LHS. This is same as 5++ which is not valid. Like "=", ++ also requires a variable - as it takes the value from the memory location increments it and stores back.

  3. int b = 5, *a = &b; ((int*)a)++;
    This is same as above- even for pointers type cast operator returns an rvalue- the value of the pointer. And this cannot be used for ++ or =. But we can do
     

    int b = 5, *a = &b; (*a)++;//increments b
    int b = 5, *a = &b; ((*)a)++;//syntax eror
selected by
by

4 Comments

int b=5,*a=&b; (*)a++       //increments b But I think (*a)++ increments the value of b;

int b=5, *a=&b; ((*)a)++    //syntax error

How??

why (*)a++ is not same as ((*)a)++?
0
0
best explanation
0
0

Another possibly relevant thing to add here, 

sizeof('a') == sizeof(int)//is true
sizeof('a') == sizeof(char) //is false

Character literal is always promoted to an integer in C.

1
1
2 votes
2 votes

2,3,4 have lvalue problems.

Lvalue must always be an operand . That operand should not be constant, it should be like that we can increment or decrement it .

Option C is correct .

by

4 Comments

yes, but it has an error before that.
0
0
which error before that @ arjun sir??
0
0
invalid type argument of unary '*' (have 'int')
     *(1000) = 5;
0
0
0 votes
0 votes
option b

1. sizeof is an operator

2. compile time error ;- lvalue required

3. increament value of a

4. increament value of a ,value of b will remain same
0 votes
0 votes
  1. is all right. You need to put parentheses after sizeof, only if you're dealing with a data type.
    If you're dealing with an expression, you may or may not enclose it inside parentheses.

     
  2. * wants a pointer. A pointer is a variable as in, it can point to different addresses if we assign them to it.
    But "1000" being a constant can't point to any other address other than 1000. Hence, it can't qualify as a pointer.
    error

     
  3. ((int)a)++;
    Typecasting returns the value of the variable in the typecasted type. So, this statement turns into
    (5)++;
    => 5++
    Now "++" or "--" want a variable to work on. 5 is a constant.
    error

     
  4. ((int*)a)++​​​​​​​
    Again, typecasting would return some value. It'd be constant. Let's say 39.
    So, (39)++.
    error

Option C

Answer:

Related questions