Skip to content
Related Articles

Related Articles

Else without IF and L-Value Required Error in C
  • Difficulty Level : Hard
  • Last Updated : 08 Jan, 2019

Else without IF

This error is shown if we write anything in between if and else clause.

Example:




#include <stdio.h>
  
void main()
{
    int a;
  
    if (a == 2)
        a++;
  
    // due to this line, we will
    // get error as misplaced else.
    printf("value of a is", a);
  
    else printf("value of a is not equal to 2 ");
}


Output:

prog.c: In function 'main':
prog.c:15:5: error: 'else' without a previous 'if'
     else printf("value of a is not equal to 2 ");
     ^

L-value required

This error occurs when we put constants on left hand side of = operator and variables on right hand side of it.

Example:






#include <stdio.h>
  
void main()
{
    int a;
    10 = a;
}


Output:

prog.c: In function 'main':
prog.c:6:5: error: lvalue required as left operand of assignment
  10 = a;
     ^

Example 2: At line number 12, it will show an error L-value because arr++ means arr=arr+1.Now that is what their is difference in normal variable and array. If we write a=a+1 (where a is normal variable), compiler will know its job and their will be no error but when you write arr=arr+1 (where arr is name of an array) then, compiler will think arr contain address and how we can change address. Therefore it will take arr as address and left side will be constant, hence it will show error as L-value required.




#include <stdio.h>
  
void main()
{
    int arr[5];
    int i;
    for (i = 0; i < 5; i++) {
        printf("Enter number: ");
        scanf("%d", arr);
        arr++;
    }
}


Output:

prog.c: In function 'main':
prog.c:10:6: error: lvalue required as increment operand
   arr++;
      ^

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :