Error: Expected '}' before 'else' in C

Here, we will learn why an error Expected '}' before 'else' and how to fix it in C programming language?
By IncludeHelp Last updated : March 10, 2024

Error: Expected '}' before 'else'

Error: Expected '}' before 'else' occurs, if closing scope curly brace of if statement is missing.

Consider the code:

Example

#include <stdio.h>

int main() 
{
	int a = 10;
	
	if(a == 10)
	{
		printf("Yes!\n");
	else
	{
		printf("No!\n");
	}
	
	return 0;
}

Output

prog.cpp: In function ‘int main()’:
prog.cpp:10:2: error: expected ‘}’ before ‘else’
  else
  ^~~~

How to fix?

See the code, closing curly brace } is missing before else statement. To fix this error - close the if statement's scope properly.

Correct Code

#include <stdio.h>

int main() 
{
	int a = 10;
	
	if(a == 10)
	{
		printf("Yes!\n");
	}		
	else
	{
		printf("No!\n");
	}
	
	return 0;
}

Output

Yes!

C Common Errors Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.