Home » C programs » Common C program Errors

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?
Submitted by IncludeHelp, on September 07, 2018

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

Consider the code:

#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!





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.