A Comprehensive Guide to the Building Blocks of C Programs
In C programming, tokens are the smallest individual units that make up a program. When a C program is compiled, it is first broken down into tokens by the preprocessor. These tokens are the basic building blocks that the compiler understands and processes.
Definition: A token is the smallest element of a C program that is meaningful to the compiler.
int, if, return)"Hello, World!")+, -, *, /);, ,, {, })
#include <stdio.h> // Preprocessor directive
int main() { // Keyword, Identifier, Punctuators
int number = 10; // Keyword, Identifier, Operator, Constant, Punctuator
printf("Value: %d", number); // Identifier, String, Punctuators, Identifier
return 0; // Keyword, Constant, Punctuator
} // Punctuator
Keywords are reserved words that have special meaning in the C language. They cannot be used as identifiers (variable names, function names, etc.) because they are part of the language syntax.
Important: All keywords in C are written in lowercase. C is case-sensitive, so int is a keyword but INT is not.
| Category | Keywords |
|---|---|
| Data types | char, double, enum, float, int, long, short, signed, struct, union, unsigned, void |
| Storage classes | auto, extern, register, static |
| Control flow | break, case, continue, default, do, else, for, goto, if, return, switch, while |
| Other | const, sizeof, typedef, volatile |
| C99 additions | _Bool, _Complex, _Imaginary, inline, restrict |
#include <stdio.h>
int main() {
int number = 10; // 'int' is a keyword
if (number > 5) { // 'if' is a keyword
printf("Greater than 5\n");
} else { // 'else' is a keyword
printf("Less than or equal to 5\n");
}
return 0; // 'return' is a keyword
}
Error Example: Using a keyword as an identifier will cause a compilation error.
int int = 10; // Error: 'int' is a keyword and cannot be used as a variable name
Identifiers are names given to various program elements such as variables, functions, arrays, structures, etc. They are user-defined names that represent memory locations.
age, Age, and AGE are different identifiers)| Valid Identifiers | Invalid Identifiers |
|---|---|
age |
int (keyword) |
_count |
5number (starts with digit) |
student_name |
first-name (contains hyphen) |
MAX_SIZE |
first name (contains space) |
calculateAverage |
price$ (contains special character) |
Constants are fixed values that do not change during program execution. They are also called literals.
Whole numbers without fractional parts. Can be decimal, octal, or hexadecimal.
123 // Decimal integer constant
045 // Octal integer constant (starts with 0)
0x2F // Hexadecimal integer constant (starts with 0x or 0X)
123L // Long integer constant
123UL // Unsigned long integer constant
Numbers with fractional parts. Also called real constants.
3.14 // Simple floating-point constant
2.0 // Floating-point constant
0.5 // Floating-point constant
3.14e2 // Scientific notation (3.14 × 10² = 314.0)
3.14E-2 // Scientific notation (3.14 × 10⁻² = 0.0314)
3.14f // Float constant (single precision)
3.14L // Long double constant
Single characters enclosed in single quotes.
'A' // Character constant
'5' // Character constant (not the number 5)
'\n' // Escape sequence for newline
'\t' // Escape sequence for tab
'\\' // Escape sequence for backslash
'\0' // Escape sequence for null character
Special character combinations that represent non-printable characters.
| Escape Sequence | Meaning |
|---|---|
\a |
Alert (bell) |
\b |
Backspace |
\f |
Form feed |
\n |
Newline |
\r |
Carriage return |
\t |
Horizontal tab |
\v |
Vertical tab |
\\ |
Backslash |
\' |
Single quote |
\" |
Double quote |
\0 |
Null character |
\xhh |
Hexadecimal character code |
\ooo |
Octal character code |
String literals are sequences of characters enclosed in double quotes. They are stored as arrays of characters terminated by a null character (\0).
"Hello, World!" // String literal
"Hello" "World" // Concatenated string literals (becomes "HelloWorld")
"Line 1\nLine 2" // String with escape sequence
"Path: C:\\Program Files" // String with escaped backslash
"" // Empty string
Note: String literals are stored in read-only memory. Attempting to modify them can lead to undefined behavior.
#include <stdio.h>
int main() {
char greeting[] = "Hello, World!"; // String literal used to initialize array
printf("%s\n", greeting); // String literal as format string
printf("The answer is %d\n", 42); // String literal with format specifier
return 0;
}
Warning: Don't confuse character constants with string literals. 'A' is a character constant (type char), while "A" is a string literal (type char[2] - 'A' plus '\0').
Operators are symbols that perform operations on variables and values. C provides a rich set of operators.
| Operator | Description | Example |
|---|---|---|
+ |
Addition | a + b |
- |
Subtraction | a - b |
* |
Multiplication | a * b |
/ |
Division | a / b |
% |
Modulus (remainder) | a % b |
++ |
Increment | a++ or ++a |
-- |
Decrement | a-- or --a |
| Operator | Description | Example |
|---|---|---|
== |
Equal to | a == b |
!= |
Not equal to | a != b |
> |
Greater than | a > b |
< |
Less than | a < b |
>= |
Greater than or equal to | a >= b |
<= |
Less than or equal to | a <= b |
| Operator | Description | Example |
|---|---|---|
&& |
Logical AND | a && b |
|| |
Logical OR | a || b |
! |
Logical NOT | !a |
| Operator | Description | Example |
|---|---|---|
= |
Assignment | a = 5 |
+=, -=, etc. |
Compound assignment | a += 5 (equivalent to a = a + 5) |
?: |
Conditional (ternary) | a > b ? a : b |
&, * |
Pointer operators | &a (address), *ptr (dereference) |
sizeof |
Size of type or variable | sizeof(int) |
, |
Comma operator | a = 5, b = 10 |
Punctuators are symbols that have syntactic and semantic meaning in the C language. They are used to organize code structure and separate tokens.
| Punctuator | Description | Example |
|---|---|---|
; |
Statement terminator | int a = 10; |
, |
Separator in declarations, parameters, etc. | int a, b, c; |
: |
Label terminator, ternary operator | case 1: printf("One"); |
() |
Function calls, expressions, parameters | printf("Hello"); |
{} |
Code blocks, compound statements | { int a = 10; } |
[] |
Array declarations and access | int arr[10]; arr[0] = 5; |
. |
Member access for structures | student.age = 20; |
-> |
Member access through pointer | ptr->age = 20; |
# |
Preprocessor directives | #include <stdio.h> |
#include <stdio.h> // # and <> are punctuators
int main() { // () and {} are punctuators
int a, b, c; // , and ; are punctuators
a = 10; // = is an operator, ; is a punctuator
b = 20;
c = a + b; // + is an operator
if (a > b) { // () and {} are punctuators
printf("a is greater\n"); // () and ; are punctuators
} else {
printf("b is greater or equal\n");
}
return 0; // ; is a punctuator
} // {} are punctuators
Whitespace refers to blank spaces, tabs, newline characters, and comments. The compiler generally ignores whitespace, but it is important for code readability and to separate tokens.
)\t)\n)\r)\f)\v)Note: Whitespace is required to separate tokens that would otherwise be combined. For example, inta is different from int a.
// Valid use of whitespace
int main ( ) {
int a = 10 ;
return 0 ;
}
// Also valid (but less readable)
int main(){int a=10;return 0;}
Important: While the compiler ignores most whitespace, it is crucial for code readability. Consistently formatted code with proper indentation is much easier to understand and maintain.
a = b + c; instead of a=b+c;printf("%d", a); instead of printf("%d",a);Preprocessor directives are lines in your code that begin with #. They are processed by the preprocessor before the actual compilation begins.
| Directive | Description | Example |
|---|---|---|
#include |
Includes header files | #include <stdio.h> |
#define |
Defines macros | #define PI 3.14159 |
#undef |
Undefines a macro | #undef PI |
#ifdef |
Conditional compilation if macro is defined | #ifdef DEBUG |
#ifndef |
Conditional compilation if macro is not defined | #ifndef DEBUG |
#if, #elif, #else, #endif |
Conditional compilation | #if VERSION > 2 |
#pragma |
Implementation-specific directives | #pragma once |
#error |
Prints an error message | #error "Not implemented" |
#include <stdio.h> // Include standard I/O header
#include "myheader.h" // Include user-defined header
#define PI 3.14159 // Define a constant
#define SQUARE(x) ((x) * (x)) // Define a macro
#ifdef DEBUG // Conditional compilation
#define DEBUG_PRINT(msg) printf("DEBUG: %s\n", msg)
#else
#define DEBUG_PRINT(msg)
#endif
int main() {
double radius = 5.0;
double area = PI * SQUARE(radius); // Use macros
DEBUG_PRINT("Calculating area"); // Debug message
printf("Area: %.2f\n", area);
return 0;
}
Note: Preprocessor directives are not C statements and do not end with a semicolon. They are processed before compilation and can significantly alter the code that gets compiled.
#include <stdio.h> // Preprocessor directive
// Function to calculate square
int square(int num) { // Keyword, Identifier, Punctuators
return num * num; // Keyword, Identifier, Operator, Identifier, Punctuator
}
int main() { // Keyword, Identifier, Punctuators
int number = 5; // Keyword, Identifier, Operator, Constant, Punctuator
int result; // Keyword, Identifier, Punctuator
result = square(number); // Identifier, Operator, Identifier, Punctuators, Identifier, Punctuator
printf("Square of %d is %d\n", number, result); // Identifier, String, Punctuators, Identifiers, Punctuator
return 0; // Keyword, Constant, Punctuator
} // Punctuator
#include <stdio.h> // Preprocessor directive
#include <math.h> // Preprocessor directive
#define PI 3.14159 // Preprocessor directive
// Function declaration
double calculate_circle_area(double radius);
int main() {
double radius; // Keyword, Identifier, Punctuator
double area; // Keyword, Identifier, Punctuator
printf("Enter the radius of the circle: "); // Identifier, String, Punctuator
scanf("%lf", &radius); // Identifier, String, Punctuator, Operator, Identifier, Punctuator
// Calculate area
area = calculate_circle_area(radius); // Identifier, Operator, Identifier, Punctuators, Identifier, Punctuator
printf("Area of circle with radius %.2f is %.2f\n", radius, area); // Identifier, String, Punctuators, Identifiers, Punctuator
return 0; // Keyword, Constant, Punctuator
}
// Function definition
double calculate_circle_area(double radius) { // Keyword, Identifier, Punctuators, Keyword, Identifier, Punctuator
return PI * pow(radius, 2); // Keyword, Identifier, Operator, Identifier, Punctuators, Constant, Punctuator
}
Programming Tip: Understanding tokens is fundamental to learning C programming. As you practice, you'll develop an intuition for how these building blocks fit together to create working programs.
9. Comments
Comments are explanatory notes added to source code that are ignored by the compiler. They are used to document code and improve readability.
Types of Comments:
Single-line Comments
Start with
//and continue to the end of the line.// This is a single-line commentint age = 25; // This comment explains the variable
Multi-line Comments
Start with
/*and end with*/. Can span multiple lines./* This is a multi-line comment
that spans multiple lines */
/* This comment explains the function:
- Parameters: a and b
- Returns: sum of a and b */
int add(int a, int b) {
return a + b;
}
Note: Comments are removed during preprocessing and do not affect the compiled program.
Best Practices for Comments:
Warning: Don't nest multi-line comments.
/* This is /* nested */ comment */will cause a compilation error because the first*/ends the comment.