Variables and Data Types: Session 2
Variables and Data Types: Session 2
Session 2
Objectives
Discuss variables Differentiate between variables and constants List the different data types and make use of them in C programs Discuss arithmetic operators
Variables
Memory
Data 15
15
Data in memory
Example
BEGIN DISPlAY Enter 2 numbers
INPUT A, B C =A+ B
DISPLAY C
END
A, B and C are variables in the pseudocode Variable names takes away the need for a programmer to access memory locations using their address The operating system takes care of allocating space for the variables To refer to the value in the memory space, we need to only use the variable name
Elementary Programming with C/Session 2/ 4 of 22
Constants
A constant is a value whose worth never changes Examples 5 5.3 Black C
numeric / integer constant numeric / float constant string constant Character constant
Identifier Names
The names of variables, functions, labels, and various other user defined objects are called identifiers Some correct identifier names
! is invalid
Identifiers can be of any convenient length, but the number of characters in a variable that are recognized by a compiler varies from compiler to compiler Identifiers in C are case sensitive
Elementary Programming with C/Session 2/ 6 of 22
Keywords
Keywords : All languages reserve certain words for their internal use Keywords hold a special meaning within the context of the particular language No problem of conflict as long as the keyword and the variable name can be distinguished. For example, having integer as a variable name is perfectly valid even though it contains the keyword int
Elementary Programming with C/Session 2/ 8 of 22
Data Types-1
Different types of data are stored in variables. Some examples are: Numbers Whole numbers. For example, 10 or 178993455 Real numbers. For example, 15.22 or 15463452.25 Positive numbers Negative numbers Names. For example, John Logical values. For example, Y or N
Data Types-2
A data type describes the kind of data that will fit into a variable The name of the variable is preceded with the data type For example, the data type int would precede the name varName
Datatype variableName
int varName
int
float
double
char
void
Type int
Stores numeric data int num; Cannot then store any other type of data like Alan or abc 16 bits (2 bytes) Integers in the range -32768 to 32767 Examples: 12322, 0, -232
Type float
Stores values containing decimal places float num;
Type double
Stores values containing decimal places
double num;
Type char
Stores a single character of information
char gender; gender='M';
Type void
Stores nothing Indicates the compiler that there is nothing to expect
Derived data type unsigned int (Permits only positive numbers) short int (Occupies less memory space than int)
Long int /longdouble (Occupies more space than int/double)
Elementary Programming with C/Session 2/ 17 of 22
unsigned short
int int
long
int/double
varNum is allocated 2 bytes modifier may be used with the int and float data types unsigned int supports range from 0 to 65535
Elementary Programming with C/Session 2/ 18 of 22
Sample Declaration
main () { char abc; /*abc of type character */ int xyz; /*xyz of type integer */ float length; /*length of type float */ double area; /*area of type double */ long liteyrs; /*liteyrs of type long int */ short arm; /*arm of type short integer*/ }
Elementary Programming with C/Session 2/ 22 of 22