0% found this document useful (0 votes)
13 views

C Variables

Uploaded by

mahedi hasan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

C Variables

Uploaded by

mahedi hasan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Presented By Supervised By

Md. Yeamin Hosen Mr. Mahedi Hasan


IT-22028 Lecturer
Session:2021-22 Department of ICT,MBSTU
C Variables

Variables are containers for storing data values, like numbers and characters.
In C, there are different types of variables (defined with different keywords),
for example:
int : stores integers such as 25,50;
float :stores floating point numbers such as 500.00;
char :Stores single character such as 'a' or ’B’
Format Specifiers

Format specifiers are used together with the printf() function to tell the compiler what type of
data the variable is storing.
It is basically a placeholder for the variable value.A format specifier starts with a percentage
sign %, followed by a character.
Example:
int Num = 50;
printf("%d", Num);
Data Types
As explained in the Variables chapter, a variable in C must be a specified data
type, and you must use a format specifier inside the printf() function to display it:

Example:
// Create variables
int myNum = 5;
float myFloatNum = 5.99;
char myLetter = 'D';

// Print variables
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
Basic Data Types
The data type specifies the size and type of information the variable will store.
In this tutorial, we will focus on the most basic ones:
Data Type Size Description Example

int 2 or 4 bytes Stores whole numbers, without decimals 1

float 4 bytes Stores fractional numbers, containing one or more 1.99


decimals. Sufficient for storing 6-7 decimal digits

double 8 bytes Stores fractional numbers, containing one or more 1.99


decimals. Sufficient for storing 15 decimal digits

char 1 byte Stores a single character/letter/number, or ASCII values 'A'


Operators
Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two values:

Example
int myNum = 100 + 50;
Operator Name Description Example

+ Addition Adds together two values x+y


- Subtraction Subtracts one value from another x-y

* Multiplication Multiplies two values x*y

/ Division Divides one value by another x/y

% Modulus Returns the division remainder x%y

++ Increment Increases the value of a variable ++x


by 1
-- Decrement Decreases the value of a variable --x
by 1
If else
Conditions and If Statements
You have already learned that C supports the usual logical conditions from mathematics:
• Less than: a < b
• Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to: a >= b
• Equal to a == b
• Not Equal to: a != b
You can use these conditions to perform different actions for different decisions.
+ Example
+ if (20 > 18) {
printf("20 is greater than 18");
}
Switch
Instead of writing many if..else statements, you can use the switch statement.
The switch statement selects one of many code blocks to be executed:

Syntax
switch (expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
Switch Example
int day = 4;
switch (day) {
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
}

// Outputs "Thursday" (day 4)


While Loop
The while loop loops through a block of code as long as a specified condition is true

Syntax
while (condition) {
// code block to be executed
}

Example:
int i = 0;

while (i < 5) {
printf("%d\n", i);
i++;
}
Do While Loop
The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the
condition is true, then it will repeat the loop as long as the condition is true.

Syntax

do {
// code block to be executed
}
while (condition);
Example
int i = 0;

do {
printf("%d\n", i);
i++;
}
while (i < 5);
For Loop

When you know exactly how many times you want to loop through a block of code, use
the for loop instead of a while loop
Syntax
for (expression 1; expression 2; expression 3) {
// code block to be executed
}
Example
int i;

for (i = 0; i < 5; i++) {


printf("%d\n", i);
}
C Break and Continue

Break
You have already seen the break statement used in an earlier chapter of this tutorial. It was used to "jump out" of
a switch statement.
The break statement can also be used to jump out of a loop.
This example jumps out of the for loop when i is equal to 4:
Example
int i;

for (i = 0; i < 10; i++) {


if (i == 4) {
break;
}
printf("%d\n", i);
}
C Break and Continue

Continue

The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the
next iteration in the loop.
This example skips the value of 4:

Example
int i;

for (i = 0; i < 10; i++) {


if (i == 4) {
continue;
}
printf("%d\n", i);
}

You might also like