← Back to C Course | Chapter 1: Introduction & Basics | Lesson 4 of 21

C First Program

Your first C program is like saying hello for the first time. You write a main function that shows a message on the screen and then finishes.
Syntax
c
#include <stdio.h>

int main() {
  printf("text");
  return 0;
}

The Main Function

Execution of every C program begins inside the function named main -- the operating system looks specifically for this function and calls it first, regardless of how many other functions your file defines.

Example: The Main Function

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("Execution starts here, inside main.");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Printing Output

printf writes formatted text to the screen and is how a C program produces any visible output; without it (or a similar output call), your program could run perfectly and you'd never know, since nothing would appear on screen.

Example: Printing Output

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("Hello, World!");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Returning Zero

Returning 0 from main is a signal to the operating system (and any script or tool that launched your program) that execution completed without errors -- returning a non-zero value is the conventional way to report failure.

Example: Returning Zero

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("Program completed successfully.");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Header Files

#include <stdio.h> pulls in the declarations for standard input/output functions like printf and scanf from the C standard library; without it, the compiler wouldn't know these functions exist and would refuse to compile your call to them.

Example: Header Files

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("stdio.h provides printf and scanf declarations.");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Executing the Program

Compiling and actually running your program -- not just getting it to compile cleanly -- is the only way to confirm it behaves the way you expect; a program with zero compiler errors can still produce completely wrong output.

Example: Executing the Program

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare result and set it to 5 / 2
	int result = 5 / 2;
	// Print formatted text to the screen
	printf("Result: %d", result);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting #include <stdio.h>, so printf is undeclared and the compiler warns or fails.
  2. Leaving out the \n in printf("Hello"), so the shell prompt appears right after the text.
  3. Using void main() instead of int main(), which is not portable standard C.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.