0% found this document useful (0 votes)
2 views37 pages

L02 - Variables, Data Types

The lecture covers variables and data types in C programming, explaining variable declaration, initialization, and the rules for naming variables. It details the three basic data types: integers, floating points, and characters, along with case sensitivity in variable names. Additionally, it includes a simple program example to add two numbers and the use of format specifiers for input and output operations.

Uploaded by

Ameer Hamza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views37 pages

L02 - Variables, Data Types

The lecture covers variables and data types in C programming, explaining variable declaration, initialization, and the rules for naming variables. It details the three basic data types: integers, floating points, and characters, along with case sensitivity in variable names. Additionally, it includes a simple program example to add two numbers and the use of format specifiers for input and output operations.

Uploaded by

Ameer Hamza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Lecture: 2

Variables & Data Types


Dr. Abdul Hameed

Opverse - Tech
Variable
Variables are named memory locations
that have a type, such as integer, float and
character
Variable
Variable
Variable Declaration

data_type variable_name;

int age;
float height;
char grade;
Variable Declaration
Variable Declaration
Variable Declaration
Variable Declaration
Variable initialization

int age;
float height;
char grade;
age = 10;
height = 5.6;
grade = ‘A’;
Variable Initialization
Variable declaration
and initialization
data_type variable_name = value;

int age = 10;


float height = 5.6;
char grade = ‘A’;
Variable in Memory
Rules for Naming Variables
Rules for Naming Variables
Rules for Naming Variables
Rules for Naming Variables
Rules for Naming Variables
Rules for Naming Variables
Rules for Naming Variables
Rules for Naming Variables
Data Types
C has three basic predefined data types:
• Integers (whole numbers)
– int, long int, short int, unsigned int
• Floating point (real numbers)
– float, double
• Characters
– char
Case Sensitivity
• C is case sensitive
– It matters whether an identifier, such as a
variable name, is uppercase or lowercase.
– Example:
area
Area
AREA
ArEa
are all seen as different variables by the
compiler.
Program - 2

Write a C Program to
add two numbers
Program to add two
numbers
#include <stdio.h>
Link Section
Header Section
Preprocessor Directive
Program to add two
numbers
#include <stdio.h>
Main function
int main()
Program to add two
numbers
#include <stdio.h>
int main()
{ Body of Main
function

}
Program to add two
numbers
inches

#include <stdio.h>
int main( )
{
int a, b, c ;
}
Program to add two
numbers
inches

#include <stdio.h>
a
int main( ) garbage
{ b
int a, b, c ; garbage
c
garbage
}
Program to add two
numbers
inches

#include <stdio.h>
a
int main( ) garbage
{ b
int a, b, c ; garbage
a = 7; c
garbage
}
Program to add two
numbers
inches

#include <stdio.h>
a a
int main( )
garbage 7
{
b
int a, b, c ;
garbage
a = 7; Variable c
initialization
garbage
}
Program to add two
numbers
inches

#include <stdio.h>
a a
int main( )
garbage 7
{
b b
int a, b, c ;
garbage 6
a = 7;
b = 6; c
garbage

}
Program to add two
numbers
inches

#include <stdio.h>
a a
int main( )
garbage 7
{
b b
int a, b, c ;
garbage 6
a = 7;
c c
b = 6;
c = a + b; garbage 13
a+b
} and assigned to c
Program to add two
numbers
inches

#include <stdio.h>
Output:
int main( ) 13
{
int a, b, c ; a
a=7;
7
b=6;
b
c = a + b; 6
printf (“%d”, c) ; c
13
}
Program to add two
numbers
#include <stdio.h>
int main( )
{
Output:
int a, b, c ; 13
a=7;
b=6;
c = a + b;
printf (“%d”, c) ;
return 0 ;
}
Format specifiers start with a percentage % operator

Format Specifier

• Format specifiers in C are used for input and


output purposes.
• Format specifier starts with a percentage % sign.
• Using format specifier the compiler can
understand that what type of data is in input and
output operation.
Format Specifier

Format
Description
Specifier
%d Integer Format Specifier
%f Float Format Specifier
%c Character Format Specifier
%s String Format Specifier
%u Unsigned Integer Format Specifier
%ld Long Int Format Specifier

You might also like