Elements of C (Lesson)
Elements of C Programming
To write programs in C, we need to understand the fundamental building blocks, often referred to as tokens.
4.1 Character Set
The character set in C includes:
- Alphabets: A-Z, a-z
- Digits: 0-9
- Special Characters:
+ - * / % ^ & ( ) [ ] { } = < > . , : ; ' " ! # _ - White Spaces: Space, Tab, Newline, Form-feed.
4.2 C Tokens
The smallest individual units in a C program are called Tokens.
- Keywords
- Identifiers
- Constants
- Strings
- Special Symbols
- Operators
4.3 Keywords and Identifiers
Keywords
Keywords are reserved words whose meaning is already known to the compiler. There are 32 keywords in C:
auto, break, case, char, const, continue, default, do, double, else, enum, extern, float, for, goto, if, int, long, register, return, short, signed, sizeof, static, struct, switch, typedef, union, unsigned, void, volatile, while.
Identifiers
Identifiers are names given to variables, functions, and arrays. Rules for naming identifiers:
- Must begin with a letter or underscore (
_). - Can contain letters, digits, and underscores.
- No special characters allowed (except
_). - Keywords cannot be used as identifiers.
- C is case-sensitive (
Ageandageare different).
4.4 Data Types in C
Data types specify what kind of data a variable can store.
| Type | Description | Size (typical) | Format Specifier |
|---|---|---|---|
int | Integers (whole numbers) | 4 bytes | %d |
char | Single character | 1 byte | %c |
float | Simple floating point (decimals) | 4 bytes | %f |
double | Double precision floating point | 8 bytes | %lf |
void | Represents "empty" or "no type" | - | - |
4.5 Variables and Constants
Variables
A variable is a container (memory location) whose value can change during program execution.
int score = 100; // Initialization
score = 200; // Re-assignment
Constants (Literals)
Constants are fixed values that do not change. Ways to define constants in C:
- Using
constkeyword:const float PI = 3.14159; - Using
#definepreprocessor:#define MAX_BUFFER 1024
4.6 Escape Sequences
Special character sequences used to represent certain whitespace or control characters.
\n: Newline\t: Horizontal tab\\: Backslash\': Single quote\": Double quote\0: NULL character
See the Lecture Slides for a full table of Data Type ranges and modifiers (short, long, signed, unsigned).