Lecture 3 CP - II
Lecture 3 CP - II
Datatypes
Data types indicate the type of data that a variable can hold It may be numeric or non numeric ANSI C supports three classes of data types:
1. Primary (or fundamental) data types 2. Derived data types (arrays, functions, structures and functions) 3. User-Defined data types
PRIMARY DATA TYPES: All C compilers support five fundamental data types namely: 1. int 2. float 3. char 4. double 5. void
Integer
Signed Int short int long int unsigned type unsigned int unsigned short int unsigned long int
Character
char signed char unsigned char
void
PRIMARY DATATYPES IN C
A look on data type, keyword and their sizes in bytes
Types Integer Real Double Character non-specific Keyword int float double char void Size (in bytes) 2 4 8 1
int:
- It is reserved word that indicates an integer number
- It is a sequence of digits without decimal point - The range of int depends on the word length of the computer Ex: For 8-bit computer it is -128 to 127 For 16-bit computer it is -32,768 to +32,767
Following are some valid and invalid integers:
Float:
It is a keyword to indicate a floating point number They can expressed either in decimal form or scientific notation Scientific notation is chosen when the given data is too big or too small This scientific notation is also called as a MANTISSA-EXPONENT notation Following are valid and invalid floating point numbers:
Char:
It is a keyword to indicate a floating point number The data may be a character constant or a string constant A character constant can be defined as any single character enclosed within a pair of apostrophes Ex : a, p, $, 2 etc., Each character constant has an integer value and is given by the ASCII character set
Character constant ASCII value
A B Z a z 0 9 &
65 66 90 97
122 48 57 38
Char:
A string constant is defined as a sequence of printable ASCII characters placed between the double quotes. Ex: computer , Bangalkot , Hello , Rupees , Y2K etc.,
PRIMARY DATA TYPES IN C Double: It is a keyword to indicate a double precision floating point number It is used for accuracy of data The float usually stores a maximum of 6 digits after the decimal point where as double stores 16 significant digits after the decimal point Ex: 234.0000000000000000 Void: The void type has no values This is usually used for specify the type of functions This type of function called void does not return any value to the calling function
Primary data types can be used only to handle limited amount of data For large volumes of data, we need powerful data types which resolves many issues such as reading, processing and printing Those data types are treated as derived data types . They are: ARRAYS STRUCTURE FUNCTIONS POINTERS
Another user defined data type is ENUMERATED DATA TYPE This is used to declare variables that can have one of the values enclosed within braces Syntax: enum identifier { value1, value2,.valuen }; It automatically assigns integer digits beginning with 0 to all enumeration constants The automatic assignment can be overridden by defining explicitly Ex: enum day { Monday, Tuesday, .. Sunday }; Explicitly EX: enum day { Monday =1, Tuesday =2 ,..};
Datatype
int Short signed int Short unsigned int Long signed int
Range
-32,768 to +32767 -32,768 to +32767 0 to 65535 -2,147,483,648 to +2,147,483,64 8 0 to 4294967295 -128 to +127 0 to 255 3.4e-38 to 3.4e+38 1.7e-308 to 1.7e+308 3.4e-4932 to 1.1e+4932
Bytes
2 2 2 4
Format Specifier
%d %d %u %d
long unsigned int Signed char Unsigned char Float Double Long double
4 1 1 4 8 10
#include<stdio.h> main() { int I = 10; float f = 20.5; double d = 30.005e25; char s[20] = ICFAITECH; char c = a: printf(%d is integer value\n, i); printf(%f is a floating point\n, f); printf(%e is a double value\n, d); printf(%s is a string\n, s); printf(%c is a Character\n, c);
Output
10 is integer value 20.500000 is a floating point 3.000500e+26 is a double value ICFAITECH is a string a is a character
STORAGE CLASSES
Storage classes provides information regarding the location and visibility of variables They are very much useful in multifunction and multi file programs C provides a variety of storage class specifiers that can be used to declare the scope and lifetime of a variable Some of them are : auto (Default) register static extern
STORAGE CLASSES
Ex : int m; main() { int i; float balance; .. function1(); } function1() { int i; float sum; .. }