Elements of C (Lesson)

Elements of C Programming

To write programs in C, we need to understand the fundamental building blocks, often referred to as tokens.

4.1 Character Set

The character set in C includes:

  • Alphabets: A-Z, a-z
  • Digits: 0-9
  • Special Characters: + - * / % ^ & ( ) [ ] { } = < > . , : ; ' " ! # _
  • White Spaces: Space, Tab, Newline, Form-feed.

4.2 C Tokens

The smallest individual units in a C program are called Tokens.

  • Keywords
  • Identifiers
  • Constants
  • Strings
  • Special Symbols
  • Operators

4.3 Keywords and Identifiers

Keywords

Keywords are reserved words whose meaning is already known to the compiler. There are 32 keywords in C: 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.

Identifiers

Identifiers are names given to variables, functions, and arrays. Rules for naming identifiers:

  1. Must begin with a letter or underscore (_).
  2. Can contain letters, digits, and underscores.
  3. No special characters allowed (except _).
  4. Keywords cannot be used as identifiers.
  5. C is case-sensitive (Age and age are different).

4.4 Data Types in C

Data types specify what kind of data a variable can store.

TypeDescriptionSize (typical)Format Specifier
intIntegers (whole numbers)4 bytes%d
charSingle character1 byte%c
floatSimple floating point (decimals)4 bytes%f
doubleDouble precision floating point8 bytes%lf
voidRepresents "empty" or "no type"--

4.5 Variables and Constants

Variables

A variable is a container (memory location) whose value can change during program execution.

int score = 100; // Initialization
score = 200; // Re-assignment

Constants (Literals)

Constants are fixed values that do not change. Ways to define constants in C:

  1. Using const keyword:
    const float PI = 3.14159;
    
  2. Using #define preprocessor:
    #define MAX_BUFFER 1024
    

4.6 Escape Sequences

Special character sequences used to represent certain whitespace or control characters.

  • \n: Newline
  • \t: Horizontal tab
  • \\: Backslash
  • \': Single quote
  • \": Double quote
  • \0: NULL character

See the Lecture Slides for a full table of Data Type ranges and modifiers (short, long, signed, unsigned).

PREVIOUS
Getting Started
NEXT
Operators in C