Turning Learners Into Developers
Codekilla
CODEKILLA
back to course
Lesson 03 / 398%· free preview
Getting Started3/5

C Syntax

Master the essential C syntax rules that form the foundation of every C program.

Definition

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.

Basic Structure of a C Program

A simple C program demonstrates the fundamental syntax and structure.

c
#include <stdio.h>

int main() {
  printf("Hello World!");
  return 0;
}
Output
text
Hello World!
Header Files in C

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() and scanf().
Types of Header Files

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"
SyntaxPurposeExample
<...>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.
Line-by-Line Explanation of C Program Syntax
  1. #include <stdio.h>: Includes the header for input/output functions.
  2. Blank Line: Ignored by the compiler, it improves readability.
  3. int main(): Entry point of the program.
  4. { ... }: Denotes the code block for the function.
  5. printf("Hello World!");: Prints text to the console.
  6. return 0;: Signals successful end of program.
  7. }: 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.
One-Line Program Example

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;}
Output
text
Hello World!
Key Syntax Concepts Table
ConceptMeaningExample
#includeAdd header file#include <stdio.h>
main()Starting pointint main()
{}Code block{ printf("Hello"); }
printf()Outputprintf("Hello World");
;End statementint a = 10;
return 0Signal successreturn 0;
A Memory Trick

Remember: IncludeMainPrintReturnEnd.

What is a Statement?

A statement is an instruction that tells the computer to do something. Each C statement must end with a semicolon ;.

Example:

c
printf("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:

c
printf("Hello World!") // Error: missing ;

Correct Example:

c
printf("Hello World!");
Multiple Statements

Programs typically use more than one statement, which are executed from top to bottom.

Example:

c
printf("Hello World!");
printf("Have a good day!");
return 0;
Output
text
Hello 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.
Syntax Flow Diagram
#include main() { ... } return 0; Program Flow: Top (Start) → Bottom (End)
Key Takeaways
  1. Every C statement must end with a semicolon, or it will cause errors.
  2. The program starts with main(), and includes header files at the top.
  3. The C language is case-sensitive (mainMain).
  4. All code logic must be enclosed within {} code blocks.
  5. Clear formatting and indentation enhance the readability and maintainability of code.
Interview Questions

Practice Questions
  1. Write a C program that prints your name on the screen.
  2. Identify the error in: printf("Hello"); printf("World!")
  3. Add a user-defined header file called mydata.h in a program.
  4. Write a C statement that adds two integers and prints their sum.
  5. Modify the main program to print three separate lines using printf().
Pro Tips
  1. Always use meaningful indentation and spacing to improve code clarity.
  2. Include all required header files at the beginning of your program.
  3. Double-check that every statement ends with a semicolon.
  4. Use comments to explain non-obvious sections in your program.
  5. Practice writing and reading C code to become comfortable with its syntax.
AI-powered recap

Quick recap quiz?

We'll generate 5 MCQs from this lesson and check your understanding instantly. Takes ~30 seconds.

# program

Program

C
#include <stdio.h>

int main(void) {
    printf("Hello World!\n");
    printf("Welcome to C Syntax.\n");
    printf("Happy Learning!\n");
    return 0;
}
Ready to move on?
// example library
Practice C Syntax with runnable code
Browse 360 runnable examples · across 36 chapters · short, copy-paste-friendly · grouped by topic
Open chapter
// suggested companion course
Data Structures with C
Comfortable with C? Level up — arrays · linked lists · stacks · queues · trees · graphs · sorting algorithms.
Open course
// glossary lookup
Every term you just saw, explained
Quick definitions for variables, pointers, loops, functions and every concept in one searchable page.
Open Terminology
// feedback.matters()
Did this lesson help you?