Variables in C
- Understand what a variable is.
- Declare variables in C.
- Store values in variables.
- Display variable values using
printf(). - Use common format specifiers.
- Perform calculations using variables.
1. What is a Variable?
A variable is a named memory location used to store a value of a particular data type.
The value stored in a variable can change during program execution. This is why it is called a variable.
A variable is referred to using an identifier throughout the program.
x
25
int age = 20;, the variable
age is used to refer to a memory location containing
the integer value 20.
2. Creating a Variable – Declaration
A variable is created using a declaration statement.
A declaration introduces a variable and specifies the type of data that it can store.
Syntax
Example
| Part | Meaning |
|---|---|
int |
Data type |
x |
Identifier / variable name |
; |
Terminates the statement |
3. Basic Data Types in C
A data type specifies what kind of data a variable can store.
| Data Type | Used For | Example | Format Specifier |
|---|---|---|---|
int |
Integer numbers | 25 |
%d |
float |
Decimal numbers | 2.34 |
%f |
double |
Decimal numbers with greater precision | 25.6789 |
%f with printf() |
char |
A single character | 'A' |
%c |
printf(), %f is normally used to print
floating-point values, including values passed as double.
The %lf specifier is primarily used with
scanf() for reading a double.
4. Storing Data into a Variable
A value can be stored in a variable using an
assignment statement. A value can also be obtained
from the user using input functions such as scanf().
Assignment Operator
The symbol = is called the
assignment operator in C.
x = 10;
This means: store the value 10 in the variable x.
5. General Forms of Assignment Statement
| Form | Example | Meaning |
|---|---|---|
var = val; |
x = 1; |
Assign a value to a variable. |
var = var; |
y = x; |
Copy the value of one variable into another. |
var = expr; |
z = x + y; |
Assign the result of an expression. |
var = func(); |
a = test(); |
Assign a function’s return value. |
= does not mean “is equal to”
in the mathematical sense. It means “assign/store the value on the
right into the variable on the left.”
6. Displaying the Value of a Variable
The printf() function is used to display values on
the monitor.
General Syntax
printf(“%fsp”, var);
printf(“%fsp”, expr);
Here %fsp represents an appropriate
format specifier.
7. Format Specifiers
A format specifier is a special sequence beginning with
% that tells printf() what type of value
should be displayed.
| Format Specifier | Data Type | Example |
|---|---|---|
%d |
int | printf("%d", x); |
%f |
float / floating-point output | printf("%f", temp); |
%c |
char | printf("%c", ch); |
%s |
String | printf("%s", name); |
8. Example 1 – Integer Variables and Expressions
#include <stdio.h>
int main()
{
printf("%d", 5);
int x;
x = 15;
printf("\nx = %d", x);
printf("\n%d", x + 5);
return 0;
}
Output
%dis used to display an integer.x = 15;stores 15 inx.x + 5produces the value 20.-
\nmoves the cursor to the next line.
9. Example 2 – Displaying Two Variables
#include <stdio.h>
int main()
{
int x, y;
x = 12;
y = 24;
printf("x = %d, y = %d", x, y);
return 0;
}
Output
10. Example 3 – Float Variable and Precision
#include <stdio.h>
int main()
{
float temp;
temp = 2.34;
printf("temp = %.2f", temp);
return 0;
}
Output
%.2f mean?
The .2 specifies the number of digits to display
after the decimal point.
For example:
%.2f→ 2 digits after decimal point%.3f→ 3 digits after decimal point%.1f→ 1 digit after decimal point
11. Example 4 – Character Variable
#include <stdio.h>
int main()
{
char choice;
choice = '#';
printf("choice = %c", choice);
return 0;
}
Output
'A', '#', and '9'.
12. Example 5 – Changing the Value of a Variable
A variable can hold different values at different times during program execution.
#include <stdio.h>
int main()
{
char ch;
ch = 'A';
printf("ch = %c\n", ch);
ch = '#';
printf("ch = %c\n", ch);
ch = '9';
printf("ch = %c", ch);
return 0;
}
Output
ch is the same variable throughout the
program, but its stored value changes from 'A' to
'#' and finally to '9'.
13. Declaration vs Assignment
| Statement | Purpose |
|---|---|
int x; |
Declares an integer variable. |
x = 25; |
Assigns 25 to the variable. |
int x = 25; |
Declares and initializes the variable. |
Example:
int age = 18;
14. Rules for Naming Variables
- A variable name can contain letters, digits, and underscore.
- A variable name must not begin with a digit.
- Spaces are not allowed in variable names.
- C keywords cannot be used as variable names.
- C is case-sensitive.
| Valid | Invalid |
|---|---|
age |
2age |
studentName |
student name |
total_marks |
total-marks |
number1 |
int |
15. Practice Questions
Basic Practice
first, store the value 25 in it, and
display it on the monitor.
Formula: Area = length × breadth
Formula: Area = π × radius × radius
Formula: Average = (Number1 + Number2 + Number3) / 3
Additional Practice
float variable called price,
assign a value to it, and display it with exactly two digits
after the decimal point.
grade, store
the character 'A', and display it.
Formula: Perimeter = 2 × (length + breadth)
Formula: Fahrenheit = (Celsius × 9 / 5) + 32
16. Think and Answer
x = 10; do?
int x; and
int x = 10;?
%d in printf()?
%.2f mean?
'A' written using single quotes?
17. Quick Revision
int x;
x = 25;
x + 5
printf()
- A variable is a named memory location used to store data.
- A declaration specifies the variable’s data type and name.
-
The
=operator is used for assignment. -
printf()is used to display values. -
Format specifiers tell
printf()what type of value is being displayed. - A variable’s value can change during program execution.