📜  否则,没有C中的IF和L值必填错误

📅  最后修改于: 2021-05-26 03:24:42             🧑  作者: Mango

没有IF的其他

如果我们在if和else子句之间编写任何内容,则会显示此错误。

例子:

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

输出:

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值

当我们将常量放在=运算符的左侧并将变量放在其右侧时,会发生此错误。

例子:

#include 
  
void main()
{
    int a;
    10 = a;
}

输出:

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

例2:在第12行,由于arr ++的意思是arr = arr + 1,它将显示一个错误的L值,这就是它们在普通变量和数组中的区别。如果我们编写a = a + 1(其中a是普通变量),则编译器将知道其工作,并且不会出错,但是当您编写arr = arr + 1(其中arr是数组的名称)时,编译器会认为arr包含地址以及如何更改地址。因此,它将arr作为地址,并且左侧将保持不变,因此将显示错误,因为它是所需的L值。

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

输出:

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

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。