c:if 没有 else_错误:C中没有其他'if'的'else'

c:if 没有 else

This error: 'else' without a previous 'if' is occurred when you use else statement after terminating if statement i.e. if statement is terminated by semicolon.

错误:如果在终止if语句后使用else语句(即if语句以分号终止),则会出现“ else”,而没有先前的“ if”

if...else statements have their own block and thus these statement do not terminate.

if ... else语句具有自己的块,因此这些语句不会终止。

Consider the given code:

考虑给定的代码:

#include <stdio.h>

int main()
{
	int a = 10;
	if(a==10);
	{
		printf("True\n");
	}
	else
	{
		printf("False\n");
	}

	return 0;
}

Output

输出量

prog.c: In function 'main':
prog.c:8:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
     if(a==10);
     ^~
prog.c:9:5: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the 'if'
     {
     ^
prog.c:12:5: error: 'else' without a previous 'if'
     else
     ^~~~


How to fix?

怎么修?

See the statement, if(a==10);

参见语句if(a == 10);

Here, if statement is terminated by semicolon (;). Thus, Error: 'else' without a previous 'if' in C is occurred.

在此,if语句以分号( ; )终止。 因此,发生错误:C中没有先前的“ if”的“ else”

To fix the error remove the semicolon (;) after the if statement.

要解决该错误,请在if语句后删除分号( ; )。

Correct code:

正确的代码:

#include <stdio.h>

int main()
{
	int a = 10;
	if(a==10)
	{
		printf("True\n");
	}
	else
	{
		printf("False\n");
	}

	return 0;
}

Output

输出量

True

翻译自: https://www.includehelp.com/c-programs/else-without-a-previous-if-error-in-c.aspx

c:if 没有 else

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值