Home » C programs » Common C program Errors

Error: expected declaration specifies before printf in C

Here, we are going to learn why an error expected declaration specifies before printf() in C occurred and how to fix it in C programming language?
Submitted by IncludeHelp, on September 09, 2018

The main cause of this error is - missing opening curly brace ({), before the printf() function.

Example:

#include <stdio.h>

int main(void)
	printf("Hello world");
	return 0;
}

Output

prog.c: In function ‘main’:
prog.c:4:2: error: expected declaration specifiers before ‘printf’
  printf("Hello world");
  ^~~~~~
prog.c:5:2: error: expected declaration specifiers before ‘return’
  return 0;
  ^~~~~~
prog.c:6:1: error: expected declaration specifiers before ‘}’ token
 }
 ^
prog.c:6:1: error: expected ‘{’ at end of input

In this program, opening brace of the main() block is missing

How to fix?

To fix this and such errors, please take care of curly braces, they are properly opened and closed.

Correct code:

#include <stdio.h>

int main(void){
	printf("Hello world");
	return 0;
}

Output

Hello world




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.