Chapter 2- Constants, Variables and Data Types (1)

2.1 Introduction

  • A program is an ordered sequence of precise instructions that process data and deliver information.
  • Programs are written in a specific programming language that has its own vocabulary and grammar (syntax rules).
  • Chapter focus: constants, variables, and data types in the C programming language.

2.2 Character Set

  • Characters legal in C programs are grouped into four categories:
    • Letters: A – Z, a – z
    • Digits: 0 – 9
    • Special characters: e.g.
    • Punctuation: , . ; : ? ! ' "
    • Operators / delimiters: + - * / % & | ^ ~ < > ( ) [ ] { } #
    • Others: _ \ $ (underscore, back-slash, dollar)
    • White spaces: blank, horizontal tab, new line, carriage return, form feed
  • The compiler ignores white spaces except inside string literals.
Trigraphs (ANSI C)
  • Provide alternate spellings for nine characters that may be missing on some keyboards.
  • Syntax: two question marks followed by a third char: ??x.
  • Translation table:
    • ??=#, ??([, ??)], ??<{, ??>}, ??!|, ??'^, ??/\, ??-~.
  • Example: ??( can be typed instead of [ on a restricted keyboard.

2.3 C Tokens

  • The smallest individual units in a C program.
  • Six kinds:
    1. Keywords (e.g. if, while, float)
    2. Identifiers (e.g. main, amount)
    3. Constants (100, -15.5)
    4. String literals ("ABC", "year")
    5. Operators (+ - * / < == etc.)
    6. Special symbols / punctuators ({ } ( ) [ ] ; , etc.)

2.4 Keywords & Identifiers

Keywords
  • Reserved words with fixed meaning; cannot be reused as identifiers.
  • ANSI C has 32 (C99 adds more). Sample list:
    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
  • Must be written in lowercase.
Identifiers
  • User-defined names for variables, functions, arrays, etc.
  • Composition rules:
    1. First char must be a letter or _.
    2. Remaining chars may be letters, digits, or _.
    3. Up to 31 significant chars (C99: 63+).
    4. Cannot match a keyword.
    5. No embedded whitespace.
  • Case-sensitive: Total, total, and TOTAL are three distinct identifiers.
  • Recommended style: use lowercase + _ to separate words for readability (e.g. class_strength).

2.5 Constants

Taxonomy
CONSTANTS
 ├─ Numeric
 │   ├─ Integer (decimal | octal | hexadecimal)
 │   └─ Real (floating-point)
 └─ Character
     ├─ Single-character constant
     ├─ String constant
     └─ Back-slash escape sequence
2.5.1 Integer Constants
  • Decimal: sequence of digits 0-9, optional +/- (e.g. 123, `-321