Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 19

CSE-

1101
Structured Programming

Rafid Mostafiz
IIT-NSTU
Lecture Contents
2

 Basic terms
 Data type
 Variables
Basic term
3

 Tokens
 In C programs, the smallest individual units are known
as tokens.
Basic term
4

 Every C word is classified as either a keyword or an identifier.


 All keywords have fixed meanings and these meanings cannot be
changed.
Basic term
5

 Identifier Identifiers refer to the names of variables,


functions and arrays.
 They are user-defined names and consist of a
sequence of letters and digits, with a letter as a first
character.
 Both uppercase and lowercase letters are permitted.
 The underscore character is also permitted in
identifiers.
Data types
6

 A data type is
 A set of values
 A set of operations on those values
 A data type is used to
 Identify the type of a variable when the variable is
declared
 Identify the type of the return value of a function
 Identify the type of a parameter expected by a function
Data types
7

 C has a small family of datatypes.


 Numeric (int, float,double)
 Character (char)
 User defined (struct,union)
 Fundamental data types:
 void – used to denote the type with no values
 int – used to denote an integer type
 char – used to denote a character type
 float, double – used to denote a floating point type
 int *, float *, char * – used to denote a pointer type, which is a memory
address type
Size and range
8

 Range of the basic data type is as follows:

Type Size (bits) Range


Char 8 -128 to 127
int 16 -32,768 to 32767
float 32 3.4E-38 to 3.4E+38
Double 64 1.7E-308 to 1.7E+308
Integer Types
9

 Values of an integer type are whole numbers.


 The integer types, in turn, are divided into two
categories: signed and unsigned.
 signed and unsigned integer may categorized into
three forms: short int, int, long int
 By default, integer variables are signed in C
 To tell the compiler that a variable has no sign bit,
declare it to be unsigned.
Size and range of integer
10

 Range of the basic data type is as follows:


Type Size Range
(bits)
Short int 8 -128 to 127
Unsigned shor int 8 0 to 255
Int 16 –32,768 to 32,767
Signed int 16 0 to 65535
Long intg 32 –2,147,483,648 to 2,147,483,647
unsigned long int 32 0 to 4,294,967,295
Size and range of integer
11

 Float and double has only signed form


Type Size Range
(bits)
Float 32 3.4E-38 to 3.4E+38

double 64 1.7E-308 to 1.7E+308

long double 80 80 3.4E-4932to 1.1E+4932


Variable
12

 Most programs need to a way to store data


temporarily during program execution.
 These storage locations are called variables.
 A variable is a data name that may be used to store
a data value.
 A variable may take different values at different
times of execution and may be chosen by the
programmer in a meaningful way.
 Example: Average, height
Variable
13

 Consider: int x=0,y=0; y=x+2;


 x, y are variables
 Naming Rules
 Variable names can contain letter, digits and _
 Variable names should start with letters.
 Keywords (e.g., for, while etc.) cannot be used as
variable names
 White space is not allowed.
 Variable names are case sensitive
 int x; int X declares two different variables.
Variable
14

 Pop quize
 money$owed; (incorrect: cannot contain $)
 total_count (correct)
 score2 (correct)
 2ndscore (incorrect: must start with a letter)
 long (incorrect: cannot use keyword)
 char (incorrect: cannot use keyword)
Variable Type
15

 Every variable must have a type


 C has a wide variety of types, including int and
float.
 A variable of type int (short for integer) can store a
whole number such as 0, 1, 392, or –2553.
 A variable of type float (short for floating-point)
can store much larger numbers than an int variable.
 Also, a float variable can store numbers with digits
after the decimal point, like 379.125.
Variable Declarations
16

 Variables must be declared before they are used.


 The general format for a declaration is
 type variable-name [=value];
 type v1,v2…..vn;
 Variables can be declared one at a time:
int height;
float profit;
 Alternatively, several can be declared at the same time:
int height, length, width, volume;
float profit, loss;
Variable Assignment
17

 A variable can be given a value by means of


assignment:
 height = 8;
 Before a variable can be assigned a value—or used
in any other way—it must first be declared.
 An int variable is normally assigned a value of type
int, and a float variable is normally assigned a
value of type float.
Variable Assignment
18

 Example
int x; x=10;
int a,b; a=5;
float mmm; b=45;
mmm=3.50
 It is also possible to assigned value to a variable using assigned (=) operator.
 Example: int x=10;
19

Any Question?

You might also like