ACtE0301 Introduction to C programming¶
C tokens and program building blocks¶
A token is the smallest meaningful unit in a C program.
| Token class | Examples |
|---|---|
| Keywords | if, int, return, while |
| Identifiers | variable/function names chosen by programmer |
| Constants | 10, 3.14, 'A' |
| String literals | "hello" |
| Operators | +, &&, =, ++ |
| Punctuators / separators | ;, ,, (), {} |
Identifier rules:
- start with letter or underscore;
- may contain digits after the first character;
- case-sensitive;
- keywords cannot be reused as identifiers.
Data and operator recognition¶
Common operator families:
| Family | Examples | MCQ cue |
|---|---|---|
| Arithmetic | + - * / % |
% only for integer remainder |
| Relational | < <= > >= == != |
result is zero or nonzero in C truth sense |
| Logical | logical AND, logical OR, logical NOT | short-circuit evaluation |
| Bitwise | bitwise AND, bitwise OR, XOR, complement, left shift, right shift | acts on bits, not Boolean truth values |
| Assignment | = += -= *= |
assignment is an expression |
| Conditional | ?: |
compact if-else expression |
| Increment/decrement | ++ -- |
prefix vs postfix difference |
Important distinctions:
=assigns;==compares.&is bitwise/ address-of depending on context;&&is logical AND.|is bitwise OR;||is logical OR.- postfix
i++yields old value then increments; prefix++iincrements then yields new value.
Formatted and unformatted I/O¶
| Kind | Typical function | Recognition |
|---|---|---|
| Formatted output | printf |
conversion specifiers such as %d, %f, %c |
| Formatted input | scanf |
requires addresses for non-array objects |
| Character I/O | getchar, putchar |
single character stream operation |
| String/line I/O | fgets, puts |
line-oriented or string output |
Common specifiers:
| Type | Specifier |
|---|---|
int |
%d or %i |
unsigned |
%u |
float / double in printf |
%f |
char |
%c |
| string | %s |
| hexadecimal integer | %x |
Trap: in scanf, float uses %f, double uses %lf.
Control statements and loops¶
| Construct | Recognition cue |
|---|---|
if, if-else |
conditional branch |
switch |
multi-way selection on integral-like expression |
for |
counted or compact loop |
while |
entry-controlled repetition |
do-while |
exit-controlled; body runs at least once |
break |
exits nearest loop or switch |
continue |
skips to next iteration |
In C, do and while are separate lowercase keywords; “do-while” is the name of a construct, not one keyword. C is case-sensitive, so keyword spelling and case must match exactly.
switch cues:
caselabels are compile-time integral constant expressions;- fall-through occurs unless
breakor another transfer statement stops it; defaultis optional.
Functions and recursion¶
Function roles:
- modularize code;
- support reuse and abstraction;
- can return a value or
void.
Recursion recognition:
- a function directly or indirectly calls itself;
- requires a base case to terminate;
- uses stack frames for each active call.
Example:
Trap: recursion is elegant for trees/divide-and-conquer, but iterative solutions may use less stack.
Arrays and strings¶
| Concept | Key fact |
|---|---|
| 1-D array | contiguous elements of same type |
| 2-D array | array of arrays; row-major layout in C |
| Multi-dimensional array | rightmost index varies fastest in memory |
| String literal in C | character array with terminating \0 |
String functions often tested:
| Function | Purpose |
|---|---|
strlen |
length excluding terminating null |
strcpy / strncpy |
copy string |
strcat / strncat |
concatenate |
strcmp / strncmp |
lexicographic comparison |
Trap: an array name usually decays to pointer to first element in expressions, but it is not itself a modifiable pointer object.
Common traps¶
scanf("%d", x);is wrong ifxis anint; use&x.%requires integer operands; using floating operands violates a language constraint and requires a diagnostic.do-whileexecutes at least once.- Strings in C need room for the terminating null byte.
getsis unsafe and removed from the language library; usefgets.
C-introduction revision box¶
- Token classes: keyword, identifier, constant, string, operator, punctuator.
=assigns;==compares.- Prefix and postfix increment produce different expression values.
switchfalls through unless stopped.- Recursion needs a base case.
- C strings are null-terminated character arrays.