Вы находитесь на странице: 1из 31

CEN 102

LECTURE 4
Data Types, Variables,
Assignment statements and Arithmetic
operators

Outline
Simple Data Types

Integral
Floating point
Char (and String)
Boolean

Variables, Constants
Declaration
Usage

Assignment statements
Arithmetic operators
Order of operators

Data Types, Variables, Assignment statements and Arithmetic operators

Data Types
The purpose of the programming languages is to
produce the desired outputs by processing and
manipulating the given data. Thus, programs manipulate
data.
There are four standard data types:

Integral
Floating point
Char
String
Boolean

Data Types, Variables, Assignment statements and Arithmetic operators

Variables
Previous lesson, we talked about variable declaration.
int a, b; //Declare two int variables
printf("Enter two numbers..:");
scanf("%d %d", &a, &b);
int sum = a + b; /*Calculate sum*/

Variable names such as a, b and sum actually


correspond to locations in the computers memory.
Every variable has a name, a type and a value.

Data Types, Variables, Assignment statements and Arithmetic operators

Variable Declaration
When we use the expression:

int

num;

The compiler:

:
:
0
:
:
:

reserves an empty memory location


With the name: num
It associates num with the data type int and
In some cases, it also puts the initial value 0 to this memory
location.

Data Types, Variables, Assignment statements and Arithmetic operators

Variable Initialization and Usage


When a variable is used in an input statement like:
scanf("%d", &num);
the value got is placed into the memory location of
the variable.
If the variable is used in a logical or arithmetic
expression its value is taken from its memory.

Data Types, Variables, Assignment statements and Arithmetic operators

Using Constants
Constants are similar to variables. They reserve a
memory location and store value in there.
But, their values cannot be changed throughout the
program run.
The keyword const is used at the declaration
#include<stdio.h>
int main(void) {
const int A=6, MAX = 30; // OK, initialized
printf("The MAX is %d\n", MAX); // OK
A=3;
// Error, A is constant
return 0;
}

Data Types, Variables, Assignment statements and Arithmetic operators

Assignment operator
The assignment operator '=' is also used to change the
value of a variable. Its form is:
A variable = (an expression) ;
Example:
sum = a + b;

On the left of assignment operator there can be only one


variable.
On the right of the assignment operator, there can be a
direct value, a variable or an expression.
The result of the right side is calculated and in the end
is assigned to the memory location of left hand side
variable.

Data Types, Variables, Assignment statements and Arithmetic operators

Assignment operator
Examples:
int a, b, sum;
a=5
// Not OK, ; is missing
b=7;
// is OK
a+b=sum
// Not OK, Wrong placement
sum==a+b;
// works but wrong meaning ==
sum=a+b;
// is OK

Data Types, Variables, Assignment statements and Arithmetic operators

Data Types

Integral
Data Types

char (1 byte)
short (2 bytes)
int (4 bytes)
long (4 bytes)
bool (1 byte)
unsigned char (1 byte)
unsigned short (2 bytes)
unsigned int (4 bytes)
unsigned long (4 bytes)

Data Types, Variables, Assignment statements and Arithmetic operators

Why so many different integral types are needed?


every data type has different sizes, limits and properties.
When only the numbers between 1 and 100 is needed to be

processed, you do not need to use a data type that can store
large numbers like one billion. You should better use a small sized
data type (char, short).

When the numbers needed to be processed are as large as one


billion , you cannot use a data type that can store only small
values. You must use a data type that can store large values (int,

long ).
Signed data types have negative and positive values.
Unsigned data types have zero and positive values only.

Why so many different integer types are needed?

char: 1 Byte = 8 bits 28= 256 possible values


signed char: -27 to +27-1 (-128 127)
unsigned char 028-10255
short: 2 Bytes = 16 bits 216= 65536 possible values
short: -215 to +215-1 -32788-1,0,1,232767
unsigned short: 0 to +216-1 0-65535
int, long: 4 Bytes = 32 bits 232 possible values

Signed: -231 to +231-1 -2,147,483,6482,147,483,647


unsigned: 0 to 232 04,294,967,295

Integer - limits

signed
unsigned
signed
unsigned
signed
unsigned

char
short
int, long

(-128 , 127)
(0 , 255)
(-32768 , 32767)
(0 , 65535)
(-2147483648 , 2147483647)
(0 , 4294967295)

Data Types, Variables, Assignment statements and Arithmetic operators

Data Type: int


Examples:
int x;
short y;
char z=65;
y= z;
//is OK.
x= 200000;
// is OK.
z= 2 * x + y;// As a statement it is
// OK but with some problems. z can
// store only the values up to 127

Data Types, Variables, Assignment statements and Arithmetic operators

Data Types: float


Float data type is used to hold and process floating point
values/numbers in the memory.
Float numbers have the floating point in them like 3.280
The number 7 is integer. But the number 7.0 is float.
There are double and single precision
Single precision is named as float
Double precision is named as double

Data Types, Variables, Assignment statements and Arithmetic operators

Data Types: float


There are two different display formats
Scientific
Fixed

Example:
The number: 0.00123457
Scientific Format: 1.234567e-003
Fixed format: 0.001235

Data Types, Variables, Assignment statements and Arithmetic operators

Data Types: float


#include<stdio.h>
int main( void)
{
int
a = 1000;
float f = 1000;
double lf= 1000;
printf("The integer is %9d\n", a);
printf("The float is
%9.2e\n", f);
printf("The double is %9.1lf\n", lf);
return 0;
}

Data Types, Variables, Assignment statements and Arithmetic operators

Data Types: float


int can be assigned to float variables with no data loss:
int
float

a = 1000;
f = a;

but when float is assigned to int, there will be data loss:


float f = 3.12;
int
a = f;
printf("The integer is %d\n", a);
printf("The float is
%f\n", f);

Data Types, Variables, Assignment statements and Arithmetic operators

Data Types: bool


bool is used for decisions
For jumping or branching, the decision
statements are used.
Logical operators
<, <=, >, >=, ==, !=

A
Logical
Expr.

True

B False

Program flow comes from A, and according


to the decision given in logical expression,
it continues either from C or from B.

Data Types, Variables, Assignment statements and Arithmetic operators

Data Types: bool


bool in C is not Standard Data Type, but in C++ its like
Standard. In C, zero means False, one represents True.
E.g. if x is 5 and y is 6 then the following expression
produce the below stated results:
bool res; //in C: int res; can be used
res = x<=y;
// res will be True
res = x>=y;
// res will be False
res = x==y;
// res will be False
res = x!=y;
// res will be True

bool is used to store the result of a logical expression


in memory for later use.

Data Types, Variables, Assignment statements and Arithmetic operators

23

Data Types: char ASCII table

Data Types, Variables, Assignment statements and Arithmetic operators

Data Types: char


ASCII table contains the standard characters from 0 until 255.
Its used to keep one character in the memory.
Any character between single apostrophe is said to be a char

With apostrophe

Without apostrophe

Char

Meaning

'a'

is char a

can be a variable

'='

is char =

Assignment operator

' '

is space char

'7'

is char 7

Integer number (7)

Two chars. Error!

78

Integer number (78)

'78'

Char

Meaning

Have no meaning

Data Types, Variables, Assignment statements and Arithmetic operators

Data Types: char


char c;
c = 'a'; // OK
c = a; // a must be the same type var
// or there will be an error
c = 97; // OK, it means ASCII 97th char
c = ' '; // OK, space char
c = 13; // OK, ASCII Enter char
c = '7'; // OK, ASCII 55th char.
c = 'ab';// Error, only one char
c = '+'; // OK, ASCII char.
c = +;
// Error

Data Types, Variables, Assignment statements and Arithmetic operators

Data Types: string


Actually, string isnt a standard data type. But today,
it became like a standard and commonly used.
String means Character array.
In C language, there is no special type for string and
it is used as a char array.
char *str="This is a string";
printf("The variable str contains: %s\n", str);

In C++ there is a special type for string


string str="This is a string";

In C, there are special functions to process strings.

Data Types, Variables, Assignment statements and Arithmetic operators

Arithmetic Operators
There are 5 arithmetic operators, there.

Data Types, Variables, Assignment statements and Arithmetic operators

Arithmetic Operators
The arithmetic operators are binary operators, they need two
operands on either sides of it.
% (modulus) operator works only with integers.
The other operators produce int result if both sides of the
operator are ints.
int a=7, b=2; float c=2.0, res;
res=a / b; // 3.0
printf("The result is %f\n", res);
res=a / c;
// 3.5
printf("The result is %f\n", res);

Data Types, Variables, Assignment statements and Arithmetic operators

Order of Operators

Data Types, Variables, Assignment statements and Arithmetic operators

Order of Operators
Example:
Suppose variables a, b, c and x in the preceding
second-degree polynomial are initialized as follows:
a = 2, b = 3, c = 7 and x = 5.

What is the result of the following expression?


y = a * x % x + b * x + c;

Data Types, Variables, Assignment statements and Arithmetic operators

Вам также может понравиться