C Programming

Unit 2: Elements of C

C tokens, escape sequences, variables, data types, constants, expressions, statements, and comments.

C tokens

A token is the smallest individual unit of a C program. The C language has six types of tokens:

  1. Keywords — reserved words with predefined meaning (e.g., int, return, if).
  2. Identifiers — names given to variables, functions, arrays, etc.
  3. Constants — fixed values that do not change during execution.
  4. Strings — sequences of characters enclosed in double quotes.
  5. Operators — symbols that perform operations (e.g., +, -, *).
  6. Special symbols — punctuation such as { }, ( ), ;, ,.

Example:

int sum = a + b;   /* int, sum, =, a, +, b, ; are tokens */

Escape sequences

Escape sequences are character combinations beginning with a backslash (\) that represent special characters inside strings.

EscapeMeaning
\nNew line
\tHorizontal tab
\bBackspace
\rCarriage return
\\Backslash
\'Single quote
\"Double quote
\0Null character
\aAlert (bell)

Example:

#include <stdio.h>

int main(void)
{
    printf("Name:\tRam\n");
    printf("Course:\t\"C Programming\"\n");
    return 0;
}

Variables

A variable is a named memory location used to store a value that can change during execution.

Rules for naming variables:

— The first character must be a letter or an underscore.
— The remaining characters may be letters, digits, or underscores. — Keywords cannot be used as variable names.
— C is case sensitive (Sum and sum are different).

Declaration and initialization:

int age;          /* declaration            */
age = 20;         /* assignment             */

float pi = 3.14;  /* declaration + init.    */
char grade = 'A';

Data types

C provides several built-in data types.

TypeTypical sizeExample values
char1 byte'A', '5'
int2 or 4 bytes10, -50
float4 bytes3.14, -0.5
double8 bytes3.141592653589
voidRepresents no value

Type modifiers: short, long, signed, unsigned.

Example:

#include <stdio.h>

int main(void)
{
    int age = 20;
    float height = 5.7f;
    char grade = 'A';

    printf("Age: %d, Height: %.1f, Grade: %c\n", age, height, grade);
    return 0;
}

Constants / Literals

A constant is a value that does not change during program execution.

Types of constants:

Integer constant10, -25, 0x1A (hex), 017 (octal).
Floating-point constant3.14, 1.2e3.
Character constant'A', '9'.
String constant"Hello".

Constants can be defined using #define or the const keyword.

#include <stdio.h>
#define PI 3.14

int main(void)
{
    const int MAX = 100;
    printf("PI = %f, MAX = %d\n", PI, MAX);
    return 0;
}

Expressions

An expression is a combination of variables, constants, and operators that evaluates to a single value.

int a = 5, b = 3, c;
c = a + b * 2;      /* expression: a + b * 2 */

Statements and comments

A statement is an instruction that tells the computer to perform an action. Every C statement ends with a semicolon (;).

int x = 10;          /* declaration statement */
x = x + 1;           /* assignment statement  */
printf("%d\n", x);   /* function-call stmt    */

Comments are text ignored by the compiler. They describe code for human readers.

/* This is a multi-line comment.
   It can span several lines. */

// This is a single-line comment.