Find z, x and y after int z, x = 5, y = -10, a = 4, b = 2; z = x++ - --y * b / a;
Data types decide how many bytes a C value occupies and what range it can hold; operators combine values into expressions. Precedence, type conversions and sequence points decide what an expression really computes — the heart of most PSC output questions.
i = i++ + ++i), the cook may do anything: undefined behaviour.017 is octal (15), 0x1F is hex (31); suffixes U, L, LL, F change the type. 'A' has type int in C.a % b takes the sign of a: −7 / 2 = −3 and −7 % 2 = −1 (C99).* / %, + -, << >>, relational, == !=, &, ^, |, &&, ||, ?:, assignment, comma.?: and assignment group right-to-left. && and || short-circuit and yield 1 or 0.sizeof is a compile-time operator that does not evaluate its operand; changing a variable twice between sequence points (i = i++ + ++i) is undefined.C was created by Dennis Ritchie at AT&T Bell Labs around 1972, so that the UNIX operating system could be rewritten in a high-level language. It evolved from Ken Thompson's language B, which itself came from Martin Richards's BCPL. Kernighan and Ritchie's book (1978) described 'K&R C'; the first official standard was ANSI C (C89), adopted by ISO as C90, followed by C99, C11, C17 and C23.
#include <stdio.h> /* preprocessor: brings in the declaration of printf */
int main(void) /* every program starts executing at main */
{
int marks = 72; /* declaration with initialisation */
printf("Marks = %d\n", marks);
return 0; /* 0 reports success to the operating system */
}The compiler reads a source file as a stream of tokens — the smallest meaningful units. There are six kinds: keywords, identifiers, constants, string literals, operators and punctuators (special symbols such as { } ; , ( )). Spaces, tabs, newlines and comments only separate tokens; they never change meaning on their own.
_, and nothing else — no spaces, hyphens, #, @ or $ (some compilers accept $ as an extension)._count and total2 are valid, 2total is not.sum, Sum and SUM are three different names.main, printf, getchar and Int are not keywords and are legal names.| Group | Keywords | Count |
|---|---|---|
| Data types | char int float double short long signed unsigned void struct union enum | 12 |
| Storage classes & qualifiers | auto register static extern typedef const volatile | 7 |
| Control flow | if else switch case default for while do break continue goto return | 12 |
| Operator | sizeof | 1 |
inline, restrict, _Bool, _Complex, _Imaginary) → 37, and C11 added 7 more (_Alignas, _Alignof, _Atomic, _Generic, _Noreturn, _Static_assert, _Thread_local) → 44. All keywords are lowercase, so Void, Sizeof and Getchar are not keywords — a favourite trick option.Every variable has a type, which fixes three things: how many bytes it occupies, how its bits are interpreted, and which operations are legal on it. C's primary types are char, int, float and double, plus void meaning 'no value'. The modifiers short, long, signed and unsigned adjust the size and the sign of the integer types.
| Type | Bytes | Range / precision | printf |
|---|---|---|---|
char, signed char | 1 | −128 to 127 | %c or %d |
unsigned char | 1 | 0 to 255 | %c or %u |
short | 2 | −32,768 to 32,767 | %hd |
unsigned short | 2 | 0 to 65,535 | %hu |
int | 4 (2 in 16-bit Turbo C) | −2,147,483,648 to 2,147,483,647 (16-bit: −32,768 to 32,767) | %d or %i |
unsigned int | 4 | 0 to 4,294,967,295 | %u |
long | 8 (4 on Windows and 32-bit systems) | about ±9.22 × 10¹⁸ when 8 bytes | %ld |
long long (C99) | 8 | −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | %lld |
float | 4 | about ±3.4 × 10³⁸, 6–7 significant digits | %f %e %g |
double | 8 | about ±1.7 × 10³⁰⁸, 15–16 significant digits | %f (%lf in scanf) |
long double | 8, 12 or 16 | at least as precise as double | %Lf |
The C standard does not fix exact sizes; it fixes minimums and an order. sizeof(char) is always exactly 1, and sizeof(char) ≤ sizeof(short) ≤ sizeof(int) ≤ sizeof(long) ≤ sizeof(long long), with short and int at least 16 bits, long at least 32 and long long at least 64. Whether a plain char is signed or unsigned is implementation-defined — signed on most x86 compilers, unsigned on ARM Linux.
Your compiler's exact limits are macros in <limits.h> — CHAR_BIT (8), SCHAR_MIN, SCHAR_MAX, UCHAR_MAX, SHRT_MAX, INT_MIN, INT_MAX, UINT_MAX, LONG_MAX, LLONG_MAX — and, for floating types, in <float.h> (FLT_MAX, DBL_MAX, FLT_DIG = 6, DBL_DIG = 15, DBL_EPSILON). Print a size with printf("%zu", sizeof(long));.
unsigned char c = 255; c++; leaves 0. Signed overflow such as INT_MAX + 1 is undefined behaviour — you may see a negative number, or the optimiser may assume it never happens. Converting an out-of-range value into a signed type (short s = 40000;) is implementation-defined; most compilers keep the low-order bits (−25,536 here).A constant (literal) is a fixed value written directly in the source. Integer constants may be written in decimal, octal or hexadecimal, and a suffix can change their type. Floating constants are double unless suffixed. You can also give a constant a name, either with the preprocessor (#define PI 3.14159) or as a read-only variable (const double pi = 3.14159;).
| Literal | Meaning | Value / type |
|---|---|---|
25 | decimal | 25, int |
031 | octal — a leading 0 | 25 |
0x19 or 0X19 | hexadecimal | 25 |
25u 25L 25UL 25LL | integer suffixes | unsigned int, long, unsigned long, long long |
3.14, 2.5e-3 | floating constants | double (the second is 0.0025) |
3.14f, 3.14L | floating suffixes | float, long double |
'A', '\101', '\x41' | character constants | all equal 65; type int in C |
"KPSC" | string literal | array of 5 chars, including the hidden '\0' |
printf("%d", 010); prints 8, and int x = 08; does not even compile because 8 is not an octal digit. For output, %o prints octal, %x/%X hexadecimal and %#x adds the 0x prefix: printf("%o %x %#x", 8, 255, 255); prints 10 ff 0xff. In scanf, %x reads hexadecimal input.Escape sequences stand for characters you cannot type directly inside quotes: \n newline, \t tab, \0 the null character (value 0), \\ backslash, \' and \" quotes, \a alert (bell), \b backspace, \r carriage return, \f form feed, \v vertical tab, \ooo up to three octal digits and \xhh hexadecimal digits.
Characters are just small integers. In ASCII, '0'–'9' are 48–57, 'A'–'Z' are 65–90, 'a'–'z' are 97–122, the space is 32 and '\0' is 0. So 'a' - 'A' is 32, c - '0' turns a digit character into its numeric value, and 'A' + 2 is 'C'. The format decides how a value is shown: printf("%c", 66) prints B, while printf("%d", 'B') prints 66.
#include <stdio.h>
int main(void)
{
char ch = 'g', digit = '7';
printf("%c %d\n", ch, ch); /* g 103 */
printf("%c\n", ch - 32); /* G (lower -> upper case) */
printf("%d\n", digit - '0'); /* 7 (character -> value) */
printf("%c\n", 'A' + 25); /* Z */
printf("%zu %zu\n", sizeof(char), sizeof('A')); /* 1 4 : 'A' is an int in C */
return 0;
}An expression combines operands (variables, constants, function calls) with operators to produce a value. Operators are classified by how many operands they take: unary (one operand: -x, !x, ++x), binary (two: a + b) and ternary (three — C has only ?:). The arithmetic operators are + - * / %; division and remainder hold most of the surprises.
| Expression | Result | Why |
|---|---|---|
7 / 2 | 3 | int ÷ int discards the fraction |
-7 / 2 | −3 | truncation is toward zero, not down to −4 |
7 % 2 | 1 | remainder |
-7 % 2 | −1 | the sign of % follows the dividend (left operand) |
7 % -2 | 1 | the dividend 7 is positive |
7.0 / 2 | 3.5 | one double operand makes it real division |
7 % 2.0 | compile error | % needs integers; use fmod() from <math.h> |
1 / 2 * 4 | 0 | left to right: 1 / 2 is 0 first |
Relational operators (<, <=, >, >=) and equality operators (==, !=) compare two values and yield the int 1 for true or 0 for false. C89 has no boolean type (C99 added _Bool), and in any condition every non-zero value counts as true. Hence printf("%d", 5 > 3) prints 1, and a < b < c really means (a < b) < c — comparing 0 or 1 with c.
Logical operators are && (AND), || (OR) and ! (NOT); they also return only 1 or 0. !0 is 1, !7 is 0 and 5 && -3 is 1. Watch the precedence of !: it is unary, so in !a >= 400 it applies to a first. With a = 500 the test becomes 0 >= 400, which is false — a trick used in a KPSC paper.
&& evaluates its right operand only if the left is true; || only if the left is false. With int a = 0, b = 5;, the statement c = a && b++; leaves b at 5, and so does c = !a || b++;. This guarantee makes if (p != NULL && p->count > 0) safe. The bitwise & and | never short-circuit — both sides always run.#include <stdio.h>
int main(void)
{
int i = -3, j = 2, k = 0, m;
m = ++i && ++j || ++k; /* grouped as (++i && ++j) || ++k */
printf("%d %d %d %d\n", i, j, k, m); /* -2 3 0 1 : ++k never runs */
return 0;
}The assignment x = expr stores a value and is itself an expression whose value is the value stored. Because assignment groups right-to-left, a = b = c = 0; means a = (b = (c = 0)). Its left side must be a modifiable lvalue — something with a storage location — so 5 = x;, a + b = c; and ++5 are compile errors ('lvalue required').
| Form | Equivalent to | Value of the whole expression |
|---|---|---|
x += 5 | x = x + 5, but x is evaluated only once | the new x |
x *= y + 1 | x = x * (y + 1) — the whole right side first | the new x |
-= /= %= &= |= ^= <<= >>= | the same pattern for each operator | the new x |
++x (pre-increment) | add 1, then use the value | the new x |
x++ (post-increment) | use the value, then add 1 | the old x |
--x / x-- | the same rules with subtract 1 | new x / old x |
int a = 5, b, c;
b = ++a; /* a becomes 6 first, then b = 6 */
c = a++; /* c gets the old value 6, then a becomes 7 */
/* now a = 7, b = 6, c = 6 */
int x = 10;
x *= 2 + 3; /* x = 10 * (2 + 3) = 50, not 10 * 2 + 3 = 23 */The conditional (ternary) operator cond ? e1 : e2 evaluates cond; if it is non-zero the result is e1, otherwise e2 — and only the chosen branch is evaluated. max = (a > b) ? a : b; is the classic use. It groups right-to-left, so a > b ? x : c > d ? y : z means a > b ? x : (c > d ? y : z).
The comma operator e1, e2 evaluates e1, throws its value away, then evaluates e2; the result is e2. It has the lowest precedence of all: x = (a = 2, a + 3); sets x to 5, but without the brackets x = a = 2, a + 3; parses as (x = a = 2), a + 3; and sets x to 2. The commas between function arguments and in declarations are separators, not this operator.
sizeof yields a size in bytes of type size_t (an unsigned integer; print it with %zu). It is normally worked out at compile time and it does not evaluate its operand: after int i = 5; size_t n = sizeof(i++); i is still 5. Brackets are required only around a type name — sizeof x and sizeof(int) are both valid. Variable-length arrays are the one case measured at run time.Bitwise operators act on the individual bits of integer operands (never on float or double). Take a = 12 (binary 1100) and b = 10 (1010):
| Operator | Name | Rule for each bit | Result with 12 and 10 |
|---|---|---|---|
& | AND | 1 only if both bits are 1 | 12 & 10 = 1000 = 8 |
| | OR | 1 if either bit is 1 | 12 | 10 = 1110 = 14 |
^ | XOR (exclusive OR) | 1 if the bits differ | 12 ^ 10 = 0110 = 6 |
~ | NOT, one's complement (unary) | flips every bit | ~12 = −13, since ~x = −x − 1 |
<< | left shift | moves bits left, fills with 0; ×2 per place | 12 << 2 = 48 |
>> | right shift | moves bits right; ÷2 per place | 12 >> 2 = 3 |
x << n equals x × 2ⁿ as long as no 1-bits fall off the top, and x >> n equals x ÷ 2ⁿ rounded down for non-negative x. Right-shifting a negative signed value is implementation-defined (usually an arithmetic shift that copies the sign bit). Shifting by a negative amount, or by at least the width of the type (e.g. 1 << 32 for a 32-bit int), is undefined.
| Task | Expression |
|---|---|
| Test bit k | (x >> k) & 1, or x & (1 << k) (non-zero if set) |
| Set bit k | x | (1 << k) |
| Clear bit k | x & ~(1 << k) |
| Toggle bit k | x ^ (1 << k) |
| Is x odd? | x & 1 gives 1 for odd, 0 for even |
| Is x (> 0) a power of 2? | (x & (x - 1)) == 0 |
| Clear the lowest 1-bit | x & (x - 1) |
| Isolate the lowest 1-bit | x & -x |
| Multiply / divide by 8 | x << 3 / x >> 3 (non-negative x) |
#include <stdio.h>
int count_bits(unsigned int x)
{
int count = 0;
while (x) {
x &= x - 1; /* removes the lowest 1-bit on each pass */
count++;
}
return count;
}
int main(void)
{
unsigned int flags = 0;
flags |= 1u << 3; /* set bit 3 -> 8 (1000) */
flags |= 1u << 0; /* set bit 0 -> 9 (1001) */
flags &= ~(1u << 3); /* clear bit 3 -> 1 (0001) */
printf("%u %d\n", flags, count_bits(45)); /* 1 4 (45 = 101101) */
int a = 6, b = 9;
a ^= b; b ^= a; a ^= b; /* swap without a temporary */
printf("%d %d\n", a, b); /* 9 6 */
return 0;
}2 & 1 is 0 (the two numbers share no 1-bits) but 2 && 1 is 1 (both are non-zero). Worse, the bitwise operators rank below ==, so if (x & 1 == 0) means if (x & (1 == 0)), i.e. x & 0 — always false. Write if ((x & 1) == 0).When an expression mixes operators without brackets, precedence decides which operator takes its operands first, and associativity decides how operators of the same level group: left-to-right (a - b - c is (a - b) - c) or right-to-left (a = b = c is a = (b = c)). C has 15 levels:
| Level | Operators | Associativity |
|---|---|---|
| 1 (highest) | () call, [] subscript, . and -> member access, postfix ++ -- | left → right |
| 2 | prefix ++ --, unary + -, ! ~, (type) cast, * indirection, & address-of, sizeof | right → left |
| 3 | * / % | left → right |
| 4 | + - | left → right |
| 5 | << >> | left → right |
| 6 | < <= > >= | left → right |
| 7 | == != | left → right |
| 8 | & bitwise AND | left → right |
| 9 | ^ bitwise XOR | left → right |
| 10 | | bitwise OR | left → right |
| 11 | && logical AND | left → right |
| 12 | || logical OR | left → right |
| 13 | ?: conditional | right → left |
| 14 | = += -= *= /= %= &= ^= |= <<= >>= | right → left |
| 15 (lowest) | , comma | left → right |
a + b * c → a + (b * c), while a - b + c → (a - b) + c (same level, left to right).*p++ → *(p++): postfix outranks unary *, so the pointer moves, not the value it points to.-x * y → (-x) * y, and !x == y → (!x) == y.1 << 3 + 1 → 1 << (3 + 1) = 16, because + outranks <<.a = b == c → a = (b == c); x & mask == 0 → x & (mask == 0).a > b ? a : b + 1 → a > b ? a : (b + 1).5 & 3 | 2 ^ 1 << 2 → (5 & 3) | (2 ^ (1 << 2)) = 1 | 6 = 7.f() + g() * h() the multiplication groups first, yet the compiler may call f, g and h in any order. Except for &&, ||, ?: and the comma operator, C does not say which operand is evaluated first — nor in what order a function's arguments are evaluated.Before doing arithmetic, C applies integer promotion: char, short, _Bool and bit-fields are converted to int (or to unsigned int if int cannot hold all their values). That is why char a = 120, b = 10; printf("%d", a + b); prints 130 even with a signed 8-bit char — the addition is done in int, and nothing overflows.
When a binary operator receives two different arithmetic types, the usual arithmetic conversions convert the 'lower' operand to the 'higher' type: long double > double > float > unsigned long long > long long > unsigned long > long > unsigned int > int. So 5 / 2.0 is 2.5 (5 becomes double) and 3.0f * 2 is a float. Assignment then converts the result to the type of the left side.
int i = -1; unsigned u = 1; the test i < u is false, because −1 becomes 4,294,967,295. For the same reason sizeof(int) > -1 is false: sizeof yields an unsigned size_t.| Expression | Result | Reason |
|---|---|---|
(float)7 / 2 | 3.5 | the cast applies to 7 before the division |
(float)(7 / 2) | 3.0 | 7 / 2 has already become 3 |
int n = 3.99; | 3 | conversion to an integer type truncates toward zero |
int n = -3.99; | −3 | truncation, not rounding down |
char c = 300; | 44 (typically) | only the low 8 bits survive: 300 − 256 |
7 / 2 * 2.0 | 6.0 | 7 / 2 = 3 first, then 3 × 2.0 |
float f = 0.1; then f == 0.1 | 0 (false) | the float and the double approximations of 0.1 differ |
A sequence point is a moment at which every earlier side effect (assignment, ++, --, a write by a function) is complete. C has one at the end of each full expression (the ;), after the first operand of &&, ||, ?: and the comma operator, and just before a function is called (after its arguments are evaluated). C11 describes the same rules as 'sequenced before'.
i = i++ + ++i;, x = x++;, a[i] = i++;, printf("%d %d", i, i++);. There is no correct output; one compiler printed 12 for i = 5; i = i++ + ++i; while warning about 'multiple unsequenced modifications'. In an MCQ, choose 'undefined / compiler dependent' whenever it is offered.int i = 5, j, x;
j = i++ + 1; /* fine: i changes once -> j = 6, i = 6 */
x = (i++, i++); /* fine: the comma is a sequence point -> x = 7, i = 8 */
j = i++ && i; /* fine: && is a sequence point -> j = 1, i = 9 */
/* i = i++ + ++i; undefined: i modified twice */
/* x = i++ * i; undefined: i modified and read */Try the problem first, then reveal the working one step at a time.
Find z, x and y after int z, x = 5, y = -10, a = 4, b = 2; z = x++ - --y * b / a;
What does this print? int a = 0, b = 1, c = 5, r1, r2; r1 = a++ && ++b; r2 = a++ || c++; printf("%d %d %d %d %d", a, b, c, r1, r2);
Assume an 8-bit signed char and a 4-byte int. Predict: (1) printf("%d", (signed char)200); (2) unsigned char u = 250; u += 10; printf("%d", u); (3) int i = -1; unsigned int n = 1; printf("%s", i < n ? "less" : "not less"); (4) printf("%.2f", (double)(9 / 2)); (5) printf("%.2f", 9 / 2.0f);
Evaluate int r = 5 & 3 | 2 ^ 1 << 2; and int s = ~5 & 0x0F;
Predict the output: int a = 17, b = 5; printf("%d %d %d %d %d", a / b, a % b, -a / b, -a % b, a % -b);
Starting each time from int i = 3, j; int a[10];, say which statements are well-defined and give the resulting values: (a) j = i++ * 2; (b) i = i++; (c) j = (i++, i + 10); (d) a[i] = i++; (e) j = i++ > 3 || i++ > 3;
A 16-bit compiler stores int in 2 bytes. (a) Give the ranges of int and unsigned int. (b) What does unsigned int u = 0; u--; leave in u? (c) Why is int big = 40000; a problem there but not with GCC on a PC?
Write single expressions (no loops, no if) for an unsigned int x: (a) is x odd? (b) set bit 4; (c) clear bit 0; (d) toggle bit 2; (e) x × 10 using only shifts and +; (f) is x a power of two?
Fully bracket and evaluate r = a + b * c > 20 && c % a == 2 || !b; for a = 3, b = 4, c = 5. Which operand is never evaluated?
f() + g() * h() the three calls may happen in any order, and printf("%d %d", i, i++) is undefined.x & 1 == 0 means x & (1 == 0) because bitwise operators rank below == — always bracket bit tests.-1 < 1u is false and sizeof(int) > -1 is false: the signed value is converted to unsigned before comparing.5 / 2 is 2 and 1 / 2 * 4.0 is 0.0. Cast an operand before dividing: (double)5 / 2 is 2.5, but (double)(5 / 2) is 2.0.010 is 8; 08 does not compile), and 'A' has type int in C — sizeof('A') is 4, unlike C++ where it is 1.12 questions. 70 % passes the topic quiz (+30 XP, +20 more for 100 %). Exam scoring: +1 correct, −⅓ wrong.
Modelled on real KPSC questions (reworded) with the same concept and answer; each links to its original paper.
sizeof operator yield?