C Syntax
Master the essential C syntax rules that form the foundation of every C program.
C Programming Syntax is the set of rules all C programs must follow to be written and structured correctly. Every C program includes header files, functions, and statements. Understanding syntax is essential for writing error-free code.
A simple C program demonstrates the fundamental syntax and structure.
c#include <stdio.h> int main() { printf("Hello World!"); return 0; }
textHello World!
Header files contain function declarations and macros used throughout programs. They are included at the top with the #include directive and typically have a .h extension.
Example:
c#include <stdio.h>
- stdio.h: Provides input/output functions like
printf()andscanf().
Standard Header Files
Provided by the C language.
c#include <stdio.h> #include <math.h> #include <string.h>
User-Defined Header Files
Created by the programmer.
c#include "myfile.h"
| Syntax | Purpose | Example |
|---|---|---|
<...> | System header files | #include <stdio.h> |
"..." | User-defined header | #include "myfile.h" |
Tips:
- Always include header files at the top of your program.
- Use
< >for standard libraries and" "for custom headers.
#include <stdio.h>: Includes the header for input/output functions.- Blank Line: Ignored by the compiler, it improves readability.
int main(): Entry point of the program.{ ... }: Denotes the code block for the function.printf("Hello World!");: Prints text to the console.return 0;: Signals successful end of program.}: Ends the function and the program block.
Important Rules:
- Every statement ends with a semicolon
;. - The C language is case-sensitive.
main()is mandatory and marks the starting point.- All code must be inside
{ }. - Proper indentation makes code easier to read.
C syntax allows writing entire programs in a single line, though this harms readability:
c#include<stdio.h> int main(){printf("Hello World!");return 0;}
textHello World!
| Concept | Meaning | Example |
|---|---|---|
#include | Add header file | #include <stdio.h> |
main() | Starting point | int main() |
{} | Code block | { printf("Hello"); } |
printf() | Output | printf("Hello World"); |
; | End statement | int a = 10; |
return 0 | Signal success | return 0; |
Remember: Include → Main → Print → Return → End.
A statement is an instruction that tells the computer to do something. Each C statement must end with a semicolon ;.
Example:
cprintf("Hello World!");
This tells the computer to print "Hello World!" on the screen.
- If you forget the semicolon, you'll get a compilation error.
Wrong Example:
cprintf("Hello World!") // Error: missing ;
Correct Example:
cprintf("Hello World!");
Programs typically use more than one statement, which are executed from top to bottom.
Example:
cprintf("Hello World!"); printf("Have a good day!"); return 0;
textHello World!Have a good day!
How This Works, Step-by-Step:
printf("Hello World!");prints the first line.printf("Have a good day!");prints the second message.return 0;ends the program successfully.- Statements are executed in order.
- Every C statement must end with a semicolon, or it will cause errors.
- The program starts with
main(), and includes header files at the top. - The C language is case-sensitive (
main≠Main). - All code logic must be enclosed within
{}code blocks. - Clear formatting and indentation enhance the readability and maintainability of code.
- Write a C program that prints your name on the screen.
- Identify the error in:
printf("Hello"); printf("World!") - Add a user-defined header file called
mydata.hin a program. - Write a C statement that adds two integers and prints their sum.
- Modify the main program to print three separate lines using
printf().
- Always use meaningful indentation and spacing to improve code clarity.
- Include all required header files at the beginning of your program.
- Double-check that every statement ends with a semicolon.
- Use comments to explain non-obvious sections in your program.
- Practice writing and reading C code to become comfortable with its syntax.
Quick recap quiz?
We'll generate 5 MCQs from this lesson and check your understanding instantly. Takes ~30 seconds.
Program
#include <stdio.h>
int main(void) {
printf("Hello World!\n");
printf("Welcome to C Syntax.\n");
printf("Happy Learning!\n");
return 0;
}