1. Gyanastra
  2. Programming Languages
  3. Programming in C
  4. Tokens Data Types and Operators
Free lesson, no account needed 11 min read 10 practice questions after it
Asked in past papers18 times
  • GATE CSE2014200820041995
  • ISRO201720152014
  • UGC NET20192018201620142013201020082007200620052004

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 _x1 and Sum are valid; 2x, #a, float are 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 of char.
  • Signed/unsigned: signed char range −128…127 (two's complement); unsigned char 0…255; int −2³¹…2³¹−1; unsigned int 0…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: char and short are promoted to int in 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:

  1. Lex the expression into tokens.
  2. Apply precedence: higher-precedence operators bind first.
  3. For equal precedence, apply associativity (left-to-right generally; right-to-left for unary, assignment and ?:).
  4. Perform integer promotions and usual arithmetic conversions to unify operand types.
  5. Apply short-circuit rules for &&, ||, and evaluate ?: left-to-right.
  6. 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):

#OperatorsAssociativity
1() [] . -> postfix ++ --left
2prefix ++ -- ! ~ 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).

Figure. The lexer separates source characters into three token groups, which all feed the parser. The parser then produces an expression tree and machine code.

Formulas

  • Integer division (C99): a / b = trunc(a / b) for integers, with b ≠ 0. Remainder satisfies a = (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ⁿ (for n ≥ 0, no overflow).
  • Right shift: x >> n = ⌊x / 2ⁿ⌋ for unsigned non-negative x.
  • Sizeof: sizeof(type) returns size in bytes (multiples of sizeof(char) = 1).
  • Signed range: [−2ⁿ⁻¹, 2ⁿ⁻¹ − 1]; unsigned range: [0, 2ⁿ − 1] where n is 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 makes a = 6; --b makes b = 1 and 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 > 10 gives 1; 4 != 3 gives 1; !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 + 1 promotes to int value 256.
  • 256 assigned to 8-bit unsigned char wraps 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===
Meaningassignmentequality test
Typestatement/expression value = assigned valueBoolean (0 or 1)
Trapif (x = 3) assigns and returns 3 (true)if (x == 3) compares
Concept&&&|||
Typebitwiselogicalbitwiselogical
Operandsbit-by-bit integertreat non-zero as truebit-by-bittreat non-zero as true
Short-circuitnoyesnoyes
Example6 & 3 = 26 && 3 = 16 | 3 = 76 || 3 = 1
ConceptPrefix ++xPostfix x++
Value used in expressionnew valueold value
Side effectincrement firstincrement after
If int x=5;y = 6, x = 6y = 5, x = 6
Conceptsigned charunsigned char
Range−128…1270…255
Wrap from 255 →127 then −1280
>> behaviourimplementation-defined for negativeslogical shift (fills 0)
Conceptfloatdoublelong double
Typical size4 bytes8 bytes10–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) = 1 always; 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; ++a uses 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.
  • sizeof is an operator, not a function; result is size_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 | k or (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++ gives 1 + old; c = ++*p means ++(*p) not (++p).
  • = versus == in if conditions: option looks correct but assigns; the correct answer uses ==.
  • Integer division vs real division: b/a with a=5, b=9 gives 1, not 1.8; 1/2 is 0, never 0.5.
  • Assignment operator traps: X -= Y + 1 is X = X − (Y + 1), not X = X − Y + 1.
  • Right-shift sign is the classic trick: for -1 >> 4 the standard says implementation-defined; typical answer is ffff (arithmetic shift on 16-bit).
  • Pattern 5 > 3 > 1 exploits left-to-right associativity of relational operators: it evaluates to (5>3)>1 = 1>1 = 0, not 5>(3>1).

Practice at PARIKSHA

You are ready for the PARIKSHA tests of this sub-topic when you can:

  1. Classify any token as keyword / identifier / constant / operator / punctuator and state which identifiers are illegal and why.
  2. 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).
  3. Evaluate expressions using the full precedence table, including mixed bitwise, logical and relational operators.
  4. Trace prefix vs postfix ++ and -- inside a compound expression and state the final value of every variable.
  5. Compute a/b and a%b for all four sign combinations and justify C99 truncation-toward-zero.
  6. Convert an integer to binary, apply &, |, ^, and write the decimal/hex result — including one negative hex value.
  7. Handle unsigned char overflow and static_cast<int> promotion correctly, and explain why the answer wraps mod 256.
  8. Distinguish short-circuited &&/|| from bitwise &/| on identical operands.
  9. Answer a Statement-I/II/III question by checking each type-size claim against the C standard's minimums.
  10. Interpret a sizeof expression together with *p++, ++*p, and (*p)++ correctly.

Attempt the sub-topic test first, then the subject test.

Pages 1–2 of 2