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:
- Keywords — reserved words with predefined meaning (e.g.,
int,return,if). - Identifiers — names given to variables, functions, arrays, etc.
- Constants — fixed values that do not change during execution.
- Strings — sequences of characters enclosed in double quotes.
- Operators — symbols that perform operations (e.g.,
+,-,*). - 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.
| Escape | Meaning |
|---|---|
\n | New line |
\t | Horizontal tab |
\b | Backspace |
\r | Carriage return |
\\ | Backslash |
\' | Single quote |
\" | Double quote |
\0 | Null character |
\a | Alert (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.
| Type | Typical size | Example values |
|---|---|---|
char | 1 byte | 'A', '5' |
int | 2 or 4 bytes | 10, -50 |
float | 4 bytes | 3.14, -0.5 |
double | 8 bytes | 3.141592653589 |
void | — | Represents 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 constant — 10, -25, 0x1A (hex), 017 (octal).
— Floating-point constant — 3.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.