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

1.data Types

The document discusses various C programming language concepts including data types, variables, operators, and assignment operators. It provides details on basic data types like int, char, float, and double along with their memory sizes and ranges. It also explains concepts like variable naming conventions, reading and writing variables, arithmetic operators, operator precedence, and shorthand notations for assignment operators.
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)
36 views

1.data Types

The document discusses various C programming language concepts including data types, variables, operators, and assignment operators. It provides details on basic data types like int, char, float, and double along with their memory sizes and ranges. It also explains concepts like variable naming conventions, reading and writing variables, arithmetic operators, operator precedence, and shorthand notations for assignment operators.
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/ 24

Programming Language

Data Types
Basic Data Types
Data Type Memory (bytes) Range Format Code

short int 2 -32,768 to 32,767 %hd

int 4 -2,147,483,648 to 2,147,483,647 %d , %i

char 1 -128 to 127 %c

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; printf(type, what);


a = 3;
b = 8; Output: 11
c = a + b;
printf(“%d” , c);
Suppose we want……
Output: The value of c is 11

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

auto break case char const continue default do

double else enum extern float for goto if

int long register return short signed sizeof static

struct switch typedef union unsigned void volatile while


Same valid/invalid variable
names
Arithmetic Operators for int
Name of the
Operator Arithmetic Operation Syntax
Operator

+ Addition Add two operands. x+y

Subtract the second operand from


– Subtraction x–y
the first operand.

* Multiplication Multiply two operands. x*y

Divide the first operand by the


/ Division x/y
second operand.

Calculate the remainder when the


% Modulus first operand is divided by the x%y
second operand.
Simple Example: Use of operators
• Write down a program that will take two integers as input and will
print the results of their addition, subtraction, multiplication and
division.

#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

• Higher to lower still auto-casting but generates warning


float x = 9.5; 44
int y = x; //OK but generates warning but no error
int y = (int) x; // warning is gone

• Work out the followings:


float x = 5/3; float x = 5.0/3;
int y = 5/3; int y = 5.0/3;

x = 1.0 y=1 x = 1.6667 y=1


Working with characters
• Write down a program that prints ASCII value of a character given
as input. (Hint: characters could be thought of as numbers)
• Write down a program that will take a small letter as input and will
print its previous letter. (Hint: as characters could be thought of as
numbers, you are allowed to subtract/add numbers with it)
• Write down a program that will take an uppercase letter as input and
will convert it to lower case letter.
Operator precedence
Category Operator Associativity Category Operator Associativity

Postfix () [] -> . ++ - - Left to right Bitwise AND & Left to right

Unary + - ! ~ ++ - - Right to left Bitwise XOR ^ Left to right


(type)* & Bitwise OR | Left to right
sizeof
Logical AND && Left to right
Multiplicative * / % Left to right Logical OR || Left to right

Additive +- Left to right Conditional ?: Right to left


Assignment = += -= *= /= Right to left
Shift << >> Left to right
%=>>= <<=
Relational < <= > >= Left to right &= ^= |=

Equality == != Left to right Comma , Left to right


Operator Precedence Examples
• Find the values of the followings:
Increment and Decrement Operators
• Increment operator (++) can be used instead of c=c+1
• Decrement operator (--) can be used instead of c=c-1.
• Preincrement
– Operator is used before the variable (++c or --c)
– Variable is changed, then the expression it is in is evaluated
• Postincrement
– Operator is used after the variable ( Operator is used after the variable
(c++ or c--)
– Expression executes, then the variable is changed
Increment and Decrement Operators
• When variable not in an expression
– Preincrementing and postincrementing have the same effect.

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

Prints 6 in both cases.


In either case, c now has the value of 6
Assignment Operators (shorthand
notations)
• Assignment operators abbreviate assignment expressions
c = c + 3;
can be abbreviated as c += 3; using the addition assignment operator.
• Examples of other assignment operators:
d -= 4 (d = d - 4)
e *= 5 (e = e * 5)
f /= 3 (f = f / 3)
g %= 9 (g = g % 9)
Assignment Operators (shorthand
notations)
• Assignment operators abbreviate assignment expressions
c = c + 3;
can be abbreviated as c += 3; using the addition assignment operator.
• Examples of other assignment operators:
d -= 4 (d = d - 4)
e *= 5 (e = e * 5)
f /= 3 (f = f / 3)
g %= 9 (g = g % 9)

You might also like