Académique Documents
Professionnel Documents
Culture Documents
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
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
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 Declaration
When we use the expression:
int
num;
The compiler:
:
:
0
:
:
:
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;
}
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;
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
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)
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).
long ).
Signed data types have negative and positive values.
Unsigned data types have zero and positive values only.
Integer - limits
signed
unsigned
signed
unsigned
signed
unsigned
char
short
int, long
(-128 , 127)
(0 , 255)
(-32768 , 32767)
(0 , 65535)
(-2147483648 , 2147483647)
(0 , 4294967295)
Example:
The number: 0.00123457
Scientific Format: 1.234567e-003
Fixed format: 0.001235
a = 1000;
f = a;
A
Logical
Expr.
True
B False
23
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
78
'78'
Char
Meaning
Have no meaning
Arithmetic Operators
There are 5 arithmetic operators, there.
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);
Order of 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.