1.
Compilation and Execution of a C Program
What is Compilation?
● Compilation is the process of converting the C program written in human-readable form
(source code) into machine-readable form (object code or executable).
● The compiler checks your code for errors and translates it into machine language.
Steps to Compile and Execute:
Write the C program using a text editor (e.g., Notepad, VS Code).
Save the program with a .c extension (e.g., hello.c).
Compile the program using a C compiler (e.g., gcc hello.c -o hello).
○ If there are no errors, an executable file (hello) is created.
Run or execute the program (e.g., ./hello on Linux/Mac or [Link] on Windows).
[Write Source Code (.c file)]
[Compile using Compiler]
[Error?] → Yes → [Fix Errors] → Back to Compile
↓ No
[Generate Executable]
[Run Program]
[Program Output Displayed]
What happens during Compilation?
● Preprocessing: Handles directives like #include and #define.
● Compilation: Translates source code to assembly code.
● Assembly: Converts assembly code to machine code.
● Linking: Combines all machine code files and libraries into an executable.
2. Structure of a C Program
[Start]
↓
[Preprocessor Directives]
↓
[main() Function]
↓
[Statements inside {}]
↓
[End of main() with return 0]
#include <stdio.h> // Preprocessor directive
int main() // Main function - starting point
{
printf("Hello, World!\n"); // Output statement
return 0; // Return statement (exit status)
}
Breakdown:
● Preprocessor Directive: #include <stdio.h> tells the compiler to include the
Standard Input Output library.
● main() function: Entry point of any C program.
● Statements: Instructions inside { } executed sequentially.
● printf() prints output to the screen.
● return 0; indicates successful program termination.
3. Tokens in C
Tokens are the smallest units in a C program that have meaning. The compiler recognizes these
tokens.
Types of Tokens:
[Tokens]
|
----------------------------------
| | | | |
[Identifiers] [Keywords] [Constants] [Strings] [Special
Symbols]
a) Identifiers
● Names given to variables, functions, arrays, etc.
● Rules:
○ Must start with a letter (A-Z or a-z) or underscore _.
○ Followed by letters, digits (0-9), or underscores.
○ Case sensitive (Var and var are different).
○ Cannot be a keyword.
● Examples: age, total_sum, _counter
b) Keywords
● Reserved words that have special meaning in C.
● Cannot be used as identifiers.
● Examples:
○ int, return, if, else, while, for, void, char, float, etc.
c) Constants (Literals)
● Fixed values that do not change during execution.
● Types:
○ Integer constants: 100, -25
○ Floating-point constants: 3.14, -0.01
○ Character constants: 'A', 'z'
○ String constants: "Hello, World!"
d) Strings
● Sequence of characters enclosed in double quotes "".
● Used to represent text.
● Example: "Welcome to C programming"
e) Special Symbols (Operators and Punctuations)
● Symbols that perform operations or separate statements.
● Examples:
○ Arithmetic operators: +, -, *, /, %
○ Assignment operator: =
○ Comparison operators: ==, !=, <, >
○ Logical operators: &&, ||, !
○ Other symbols: ; (semicolon), { } (braces), ( ) (parentheses), , (comma)
Summary Table:
Token Type Description Example
Identifier Names of variables/functions count, sum,
_x
Keyword Reserved words int, return,
if
Constant Fixed values 100, 'A',
3.14
String Sequence of characters "Hello"
Special Operators & punctuation marks +, ;, {}, =
Symbols
Tips for Beginners:
● Always end statements with a semicolon ;.
● Use meaningful identifiers.
● Keywords should never be used as variable names.
● Practice writing simple programs to get comfortable with syntax.