Every C program is built using small building blocks known as tokens. Tokens are the smallest meaningful elements recognized by the C compiler and form the foundation of every program.
Understanding variables, constants, data types, operators, and variable scope is essential before writing logical programs. These concepts help programmers store, manipulate, and process data efficiently.
Learning Objectives
Understand C tokens
Declare and initialize variables
Work with different data types
Use constants
Apply arithmetic, relational, logical, and assignment operators
Understand operator precedence
Learn variable scope and lifetime
What are C Tokens?
Tokens are the smallest individual units in a C program.
The compiler recognizes the following types of tokens:
Keywords
Identifiers
Constants
Variables
Operators
Special symbols
String literals
Example:
int age = 25;
Here:
int → Keyword
age → Identifier
= → Operator
25 → Constant
; → Special symbol
Keywords
Keywords are reserved words that have predefined meanings.
Common keywords include:
int
float
char
double
if
else
while
for
return
void
Keywords cannot be used as variable names.
Identifiers
Identifiers are names given to variables, functions, arrays, and other program elements.
Example:
int studentAge;
float salary;
char grade;
Good identifier names:
studentName
totalMarks
employeeSalary
Avoid names like:
a
x1
temp123 (unless meaningful)
Variables
A variable stores data that can change while the program is running. Example:
int age = 24;
float salary = 45000.50;
char grade = 'A';
Variables must be declared before they are used.
Data Types
Data types specify the type of data that a variable can store.
Data Type | Description |
int | Integer values |
float | Decimal values |
double | Large decimal values |
char | Single character |
void | No return value |
Example:
int age = 25;
float percentage = 89.5;
char grade = 'A';
Constants
Constants store fixed values that cannot be changed during program execution.
Example:
const float PI = 3.14159;
Or
#define MAX_SIZE 100
Constants improve readability and make programs easier to maintain.
Operators
Operators perform calculations and comparisons.
Arithmetic Operators
+
-
*
/
%
Example:
int a = 10;
int b = 5;
printf("%d", a + b);
Output:
15
Relational Operators
==
!=
>
<
>=
<=
Used for comparisons.
Example:
if(age >= 18)
Logical Operators
&&
||
!
Example:
if(age >=18 && marks >50)
Assignment Operators
=
+=
-=
*=
/=
Example:
total += marks;
Operator Precedence
Some operators execute before others.
Example:
int result = 5 + 10 * 2;
Output:
25
because multiplication executes before addition.
Use parentheses to improve readability.
int result = (5 + 10) * 2;
Output: 30
Scope of Variables
Scope determines where a variable can be accessed.
Local Variable
int main()
{
int age = 20;
}
The variable exists only inside the function.
Global Variable
int age = 20;
int main()
{
printf("%d", age);
}
Global variables are accessible throughout the program.
Lifetime of Variables
Variable lifetime refers to how long a variable exists during program execution.
Local variables exist only while the function is executing.
Global variables exist throughout the execution of the program.
Complete Example
#include <stdio.h>
const float PI = 3.14;
int totalStudents = 50;
int main()
{
int age = 20;
float marks = 85.5;
printf("Age: %d\n", age);
printf("Marks: %.1f\n", marks);
printf("PI: %.2f\n", PI);
return 0;
}
Output
Age: 20
Marks: 85.5
PI: 3.14
Best Practices
Use meaningful variable names.
Declare variables close to where they are used.
Use constants for fixed values.
Avoid unnecessary global variables.
Follow consistent naming conventions.
Use parentheses in complex expressions.
Common Mistakes
Using keywords as variable names.
Declaring variables without initialization when values are required.
Confusing = with ==.
Ignoring operator precedence.
Accessing local variables outside their scope.
Still need help?
Contact us