Else without IF and L-Value Required Error in C

Else without IF

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

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#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 ");
}

chevron_right


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:



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Hello friends, I have a deep knowledge on programming language like C,C++,Java,especially in C and Java I like to do comparative study and likes to ask question ie if , why and why not we In future, i want to be an entrepreneur and really focus on how to improve Education System

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.