Programming Languages · Programming in C
Tokens Data Types and Operators
Overview
A C program is a sequence of characters that the compiler first groups into tokens, then checks for grammar, and finally translates into machine code.
Understanding tokens, data types and operators is therefore the foundation of every C question on the UGC NET paper: expression evaluation, integer division, bitwise logic, increment order, precedence and type conversion all depend on it.
The topic sits inside the Programming in C chapter of Programming Languages. Before studying it, you should know basic computer arithmetic (binary, two's complement, hexadecimal) and the idea of a compiler.
Key concepts
- A token is the smallest lexical unit of a program.
C has six token types: keywords, identifiers, constants, string literals, operators, and punctuators (also called separators, e.g.,;{}()). - Keywords: C90 has 32 keywords (
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). C99 adds five (_Bool, _Complex, _Imaginary, inline, restrict), giving 37. C11 adds seven more (_Alignas, _Alignof, _Atomic, _Generic, _Noreturn, _Static_assert, _Thread_local), giving 44. - Identifier rules: first character is a letter (
A–Z,a–z) or underscore_; remaining are letters, digits or underscore; no keyword; case-sensitive. So_x1andSumare valid;2x,#a,floatare invalid. - Data types split into primary (
char, int, float, double, void), derived (array, pointer, function), user-defined (struct, union, enum, typedef), and qualified (const, volatile, restrict). - Size guarantees from the C standard (minimum bits):
char= 1 byte = CHAR_BIT (usually 8);short≥ 16;int≥ 16;long≥ 32;long long≥ 64.
In order:sizeof(char) ≤ sizeof(short) ≤ sizeof(int) ≤ sizeof(long) ≤ sizeof(long long). Typical 64-bit: char 1, short 2, int 4, long 8, long long 8, float 4, double 8, long double 16. sizeof(char)is 1 by definition — the size of every other type is measured in multiples ofchar.- Signed/unsigned:
signed charrange −128…127 (two's complement);unsigned char0…255;int−2³¹…2³¹−1;unsigned int0…2³²−1. - Operators are grouped as: arithmetic
+ − * / %; relational< > <= >=; equality== !=; logical&& || !; bitwise& | ^ ~ << >>; assignment= += −= *= /= %= &= |= ^= <<= >>=; increment/decrement++ --; conditional?:; comma,;sizeof; address-of&; dereference*. - Integer division and
%: operands are both integers, so the result is integer (fraction discarded). In C99 the sign of/truncates toward zero, and the sign of%is the sign of the dividend:7/2 = 3,-7/2 = -3,7%-2 = 1,-7%2 = -1. - Short-circuit:
&&evaluates the right operand only if the left is non-zero;||evaluates the right only if the left is zero. - Integer promotions and usual arithmetic conversions:
charandshortare promoted tointin any arithmetic expression; mixed types are converted to the wider/common type before the operation. - Bitwise shift:
<<fills vacated low bits with 0;>>fills vacated high bits with 0 for unsigned operands, and for signed negative operands the behaviour is implementation-defined (in practice arithmetic shift, i.e. sign bit replicated).
How it works
Step 1 — Lexical analysis (tokenizer). The compiler scans source characters and produces tokens.
Step 2 — Expression evaluation happens in a fixed order:
- Lex the expression into tokens.
- Apply precedence: higher-precedence operators bind first.
- For equal precedence, apply associativity (left-to-right generally; right-to-left for unary, assignment and
?:). - Perform integer promotions and usual arithmetic conversions to unify operand types.
- Apply short-circuit rules for
&&,||, and evaluate?:left-to-right. - Where
++/--acts, remember: prefix modifies first and returns the new value; postfix returns the old value and then modifies.
Step 3 — Precedence table (high to low):
| # | Operators | Associativity |
|---|---|---|
| 1 | () [] . -> postfix ++ -- | left |
| 2 | prefix ++ -- ! ~ unary + - * & sizeof (type) | right |
| 3 | * / % | left |
| 4 | + - | left |
| 5 | << >> | left |
| 6 | < <= > >= | left |
| 7 | == != | left |
| 8 | & | left |
| 9 | ^ | left |
| 10 | | | left |
| 11 | && | left |
| 12 | || | left |
| 13 | ?: | right |
| 14 | = += -= *= /= %= &= |= ^= <<= >>= | right |
| 15 | , | left |
Step 4 — Overflow of a unsigned char. Because char operands are first promoted to int, c + 1 becomes an int value; the result is then truncated modulo 256 when stored back into unsigned char.
Step 5 — Compound assignment. X op= Y is equivalent to X = X op Y, where the right side is taken as a whole. Hence X -= Y + 1 means X = X − (Y + 1).
Drawing the diagram…
Formulas
- Integer division (C99):
a / b = trunc(a / b)for integers, withb ≠ 0. Remainder satisfiesa = (a/b)*b + a%b. - Remainder sign:
sign(a % b) = sign(a). - Bitwise AND:
x & y— result bit is 1 only when both bits are 1. - Bitwise OR:
x | y— result bit is 1 when either bit is 1. - Bitwise XOR:
x ^ y— result bit is 1 when bits differ. - Left shift:
x << n = x × 2ⁿ(forn ≥ 0, no overflow). - Right shift:
x >> n = ⌊x / 2ⁿ⌋for unsigned non-negativex. - Sizeof:
sizeof(type)returns size in bytes (multiples ofsizeof(char) = 1). - Signed range:
[−2ⁿ⁻¹, 2ⁿ⁻¹ − 1]; unsigned range:[0, 2ⁿ − 1]wherenis the bit-width. - Modulo wrap for unsigned char: stored value =
(computed value) mod 256(assuming 8-bit char).
Worked examples
Example 1 (medium). Evaluate 1 + 2 * 3 - 4 / 2 in C.
2 * 3 = 6;4 / 2 = 2(left to right, same precedence as*).- Expression becomes
1 + 6 - 2 = 5. Answer: 5.
Example 2 (hard, two-step). What does the following print?
#include <stdio.h>
int main(void) {
int a = 5, b = 2, c;
c = a++ * --b;
printf("%d %d %d", a, b, c);
return 0;
}
a++returns 5 and makesa = 6;--bmakesb = 1and returns 1.c = 5 * 1 = 5. Output:6 1 5.
Example 3 (hard, precedence + short-circuit). Evaluate 5 + 3 * 2 > 10 && 4 != 3 || !0.
3*2 = 6;5 + 6 = 11(multiplication before addition).11 > 10gives1;4 != 3gives1;!0 = 1.1 && 1 = 1;1 || 1 = 1. Answer: 1.
Example 4 (medium, data types). Consider unsigned char c = 255; c = c + 1; printf("%d", c);. Output?
c + 1promotes tointvalue 256.- 256 assigned to 8-bit
unsigned charwraps modulo 256 to 0. Output:0.
Example 5 (statement-type). Evaluate the three statements.
- I.
sizeof(char) == 1— true by the C standard. - II.
sizeof(int) >= 2(≥16 bits) — true. - III.
sizeof(long) >= sizeof(int)— true.
All three are true. Only statements that contradict these minimums would be false.
Comparison
| Concept | = | == |
|---|---|---|
| Meaning | assignment | equality test |
| Type | statement/expression value = assigned value | Boolean (0 or 1) |
| Trap | if (x = 3) assigns and returns 3 (true) | if (x == 3) compares |
| Concept | & | && | | | || |
|---|---|---|---|---|
| Type | bitwise | logical | bitwise | logical |
| Operands | bit-by-bit integer | treat non-zero as true | bit-by-bit | treat non-zero as true |
| Short-circuit | no | yes | no | yes |
| Example | 6 & 3 = 2 | 6 && 3 = 1 | 6 | 3 = 7 | 6 || 3 = 1 |
| Concept | Prefix ++x | Postfix x++ |
|---|---|---|
| Value used in expression | new value | old value |
| Side effect | increment first | increment after |
If int x=5; | y = 6, x = 6 | y = 5, x = 6 |
| Concept | signed char | unsigned char |
|---|---|---|
| Range | −128…127 | 0…255 |
| Wrap from 255 → | 127 then −128 | 0 |
>> behaviour | implementation-defined for negatives | logical shift (fills 0) |
| Concept | float | double | long double |
|---|---|---|---|
| Typical size | 4 bytes | 8 bytes | 10–16 bytes |
| Precision | ~7 digits | ~15 digits | ~19 digits |
Revision summary
- Identifiers start with letter or
_; digits allowed only after; keywords cannot be identifiers. - C90 keywords = 32; C99 = 37; C11 = 44.
sizeof(char) = 1always;sizeof(int) ≥ 2;sizeof(short) ≤ sizeof(int) ≤ sizeof(long) ≤ sizeof(long long).- C99:
/truncates toward zero,%takes the sign of the dividend. a++uses old value;++auses new value.- Bitwise binds looser than arithmetic;
<< >>bind tighter than relationals but looser than+ −. &&binds tighter than||; both short-circuit.- Compound
X op= Y≡X = X op (Y). unsigned char x = 255; x = x + 1;gives 0 (mod-256 wrap).- Prefix unary,
?:, and assignment associate right to left; everything else is left to right. sizeofis an operator, not a function; result issize_t.- For signed negative right-shift, declare it implementation-defined (in practice sign-extended).
Previous-year exam insights
- Expression evaluation is the most common pattern: a single line like
x*3 && 3 || j | kor(1&2)+(3&4).
Always write out the precedence order first, then substitute values. - Bitwise with hex or decimal operands: convert both to binary, apply
& | ^, and re-express in the asked base.35 | 7 = 39;0x5AB6 & 0x61CD = 0x4084. - Increment/decrement traps:
1 + x++gives1 + old;c = ++*pmeans++(*p)not(++p). =versus==inifconditions: option looks correct but assigns; the correct answer uses==.- Integer division vs real division:
b/awitha=5, b=9gives1, not1.8;1/2is 0, never 0.5. - Assignment operator traps:
X -= Y + 1isX = X − (Y + 1), notX = X − Y + 1. - Right-shift sign is the classic trick: for
-1 >> 4the standard says implementation-defined; typical answer isffff(arithmetic shift on 16-bit). - Pattern
5 > 3 > 1exploits left-to-right associativity of relational operators: it evaluates to(5>3)>1 = 1>1 = 0, not5>(3>1).
Practice at PARIKSHA
You are ready for the PARIKSHA tests of this sub-topic when you can:
- Classify any token as keyword / identifier / constant / operator / punctuator and state which identifiers are illegal and why.
- State the C90 keyword count, name the four primary data types, and list the size guarantees
sizeof(char) ≤ sizeof(short) ≤ sizeof(int) ≤ sizeof(long) ≤ sizeof(long long). - Evaluate expressions using the full precedence table, including mixed bitwise, logical and relational operators.
- Trace prefix vs postfix
++and--inside a compound expression and state the final value of every variable. - Compute
a/banda%bfor all four sign combinations and justify C99 truncation-toward-zero. - Convert an integer to binary, apply
&,|,^, and write the decimal/hex result — including one negative hex value. - Handle
unsigned charoverflow andstatic_cast<int>promotion correctly, and explain why the answer wraps mod 256. - Distinguish short-circuited
&&/||from bitwise&/|on identical operands. - Answer a Statement-I/II/III question by checking each type-size claim against the C standard's minimums.
- Interpret a
sizeofexpression together with*p++,++*p, and(*p)++correctly.
Attempt the sub-topic test first, then the subject test.