STRUCTURE PROGRAMMING
BASICS OF C LANGUAGE
Abdul Wahab
Lecturer in IT
IECS, UST Bannu
[email protected]
Abdul Wahab (IECS UST Bannu) 1
Constant
• A quantity whose value cannot be changed during the
execution of the program is called constant.
• It may be a numeric or a non-numeric quantity
• For example:
const int a=20;
const char ch=‘M’;
2
Variable
• A quantity whose value may change during execution of
the program is called variable
• It may be numeric or a non-numeric quantity
• A variable represents a storage or memory location in the
computer memory in which data is stored.
• The name of the variable remain fixed but data stored in
that location may change from time to time.
3
Rules for Writing Variable Names
• The first character of the variable name may be an
alphabetic character or an underscore ( _ )
• The first character of variable name cannot be a digit
• Blank spaces are not allowed in a variable name
• Special characters (#, ^, $) cannot be used in variable
name
4
Rules for Writing Variable Names contd…
• Reserved C words cannot be used as variable name
• Some of compiler limits the number of characters in a
variable name up to a specific length
• A variable name declared for one data-type cannot be
used to declare another data-type
5
Note
• C/C++ is a case sensitive language.
• Variables declared with same spellings but different cases
are treated as different variable names.
• For example:
‘abc’ , ‘aBc’ , ‘ABC’ and “Abc” are all different variables
6
Examples of Valid and Invalid Variables
Variable Name Valid/ Invalid Remarks
Ahmad Valid
4percent Invalid Start with a digit
double Invalid C reserved word
Double Valid Start with a capital D
Foxpro Valid
_small Valid
x-y Invalid Special characters are not allowed
Father Name Invalid Space is not allowed
Father_Name Valid
$xyz Invalid
for Invalid C reserved word
7
Data Types used in C/C++
• The type of data is called a data type
• Four basic data types used in C/C++ are:
– int Integer
– float and double Real values
– char Characters
– Boolean True or False values
8
Integer Data Type
• An integer represents a whole number, that may be
positive or negative
• Depending upon the maximum values there are four
types of integer data type
Type Size (Bytes) Range
int 2 -32768 to 32767
long int 4 -2147483648 to 2147483647
Unsigned: 0 to 4294967295
short int 2 -32768 to 32767
unsigned int 2 0 to 65535
9
Float and Double Data Types
• The real/ floating point numbers consist of an integer
and a fractional parts
• Depending on the maximum value, there two types of
real:
Type Size (Bytes) Range
float 4 1.1 x 10-38 to 3.4 x 10+38
double 8 2.2 x 10-308 to 1.7 x 10+308
10
Char Data Type
• The char data type consists of alphabetic characters,
numeric digits and special characters
• Char data type takes only 1 byte to store the value
• In case of string, storage capacity is from 1 byte to 65535
bytes (64 MB)
11
Boolean Data Type
• Boolean data type is named after a famous mathematician
Jorge Boole who invented boolean algebra
• It is declared with keyword bool
• Its size is only 1 byte and takes either True or False values
12
Declaration of Variables
• Creating variables in a program is called declaration of
variable
• When a variable is declared, a specific size of memory is
allocated to that variable
• The memory is allocated according to the type of variable
13
Declaration of Variables Contd…
• General syntax of declaration of variable:
data_type name_of_variable(s);
• For example:
int ab;
float sqr, div, fy;
char ch, z;
14
Initialization of Variable
• Assigning value to a variable is known as initialization of
variable. For example:
sum=100;
• Variables can initialized both during declaration and after
declaration
• Initializing a variable at the time of declaration is called
inline initialization. For example:
int a=10, b=20, c=30;
15
Have a Good Day !
Abdul Wahab (IECS UST Bannu) 16