1.data Types
1.data Types
Data Types
Basic Data Types
Data Type Memory (bytes) Range Format Code
float 4 %f
1.2E-38 to 3.4E+38
double 8 %lf
1.7E-308 to 1.7E+308
Memory Concepts
• Variables
– Variable names correspond to locations in the computer's memory.
– Every variable has a name, a type, a size and a value.
– Whenever a new value is placed into a variable (through scanf, for
example), it replaces (and destroys) previous value
– Reading variables from memory does not change them
• A visual representation
Using Variables: Output
Format specifier Variable
int a, b, c;
a = 3;
b = 8;
c = a + b;
printf(“%d” , c);
We can do……
Output: The value of c is 11
int a, b, c;
a = 3;
b = 8;
c = a + b;
printf(“The value of c is %d” , c);
Using Variables: Output
float a = 8.958;
printf("%f ", a);
int a, b, c;
a = 3; double a = -9.8;
b = 8; printf("%lf ", a);
c = a + b;
printf(“%d” , c);
char a = ‘Z’;
printf("%c", a);
char x = 65;
printf("%c", x);
Data input
Format specifier Variable float b;
scanf("%f", &b);
scanf(type, where);
double c;
int a; scanf("%lf", &c);
scanf(“%d” , &a);
char d;
scanf("%c", &d);
What is the output if user enters 0
int a; char a;
scanf(“%i”, &a); scanf(“%c”, &a);
printf(“%d” , a); printf(“%d” , a);
Naming convention of a variable
• Capital letters A-Z, lowercase letters a-z, digits 0-9, and the
underscore character
• First character must be a letter or underscore
• Usually only the first 31 characters are significant
• There can be no embedded blanks
• Naming convention of a variable
• Keywords cannot be used as identifiers
• Identifiers are case sensitive
Key words in C
#include<stdio.h>
int main(){
int a,b;
scanf(“%d%d”,&a,&b);
printf(“Addition: %d\n”,a+b);
printf(“Subtraction: %d\n”,a-b);
printf(“Multiplication: %d\n”,a*b);
printf(“Division: %d\n”,a/b);
return 0;
}
Working with int…examples
• Convert height of a person:
▫ Given height in feet inch as input → convert it to inch
▫ Given height in inch as input → convert it to feet inch
• Time difference of two cities.
▫ Think time as two separate integers: one quantity for hour and
the other quantity for minute as input
– Dhaka: 11:20
– India: 10:50
• To extract digits of a number:
– Given a 4-digit number as input, can you reverse it?
– Modulus (%) and division (/) operators are good enough.
Working with fractions: Example 1
Sonali Bank annually provides interests at a certain rate to all its
clients having a savings account with the bank. Write down a program
that will take initial balance, and annual interest rates and will
determine and print: b = 1,00,000, r = 10%
(1) Balance after one year interest1 = 100000*0.01
(2) Balance after two years = 10,000
(3) Balance after n years where b1 = 1,00,000 + 10,000
= 1,10,000
n will also be input to your
program.
b1 = 1,10,000, r = 10%
interest2 = 110000*0.01
= 11,000
b2 = 1,10,000 + 11,000
= 1,21,000
Type conversion
• Lower to higher auto-conversion (called auto-casting)
int x = 9;
float y = x; //OK no warning no error
c = 5; If c = 5, then
++c; printf( "%d", ++c);
printf(“%d”,c);
and Prints 6
c++; printf( "%d", c++);
printf(“%d”,c);
Prints 5