Variables
A variable is like a container that holds data which can change during program execution.
Syntax:
data_type variable_name = value;
Example:
int age = 18;
char grade = ‘A’;
- Rules for Naming Variables:
- Must start with a letter or underscore
- Cannot use C keywords
- Case-sensitive (Total ≠ total)
- Data Types
|
Category |
Data Type |
Description |
Example |
|
Integer |
int |
Whole numbers |
int age = 20; |
|
Floating Point |
float, double |
Decimal numbers |
float pi = 3.14; |
|
Character |
char |
Single character |
char grade = ‘A’; |
|
Void |
void |
No return value (used in functions) |
void greet (); |
Modifiers:
Used to extend the properties of base data types.
|
Modifier |
Use |
Example |
|
short |
Small integer size |
short a = 10; |
|
long |
Large range of int |
long b = 10000; |
|
signed |
Can store +ve and -ve |
signed int x; |
|
unsigned |
Only +ve values |
unsigned int y; |
Constants in Detail:
- #define: Replaces value before compilation.
- const: Cannot change during execution.
#define PI 3.14
const int MAX = 100;
Input and Output:
- scanf () reads input.
- printf () displays output.
int a;
printf (“Enter a number: “);
scanf(“%d”, &a);
printf(“You entered %d”, a);
Format Specifiers
|
Format |
Data Type |
Example Input |
|
%d |
int |
10 |
|
%f |
float |
5.3 |
|
%lf |
double |
8.12345 |
|
%c |
char |
‘A’ |
|
%s |
string |
“hello” |