Upload
naeem-ahamad
View
219
Download
0
Embed Size (px)
Citation preview
7/28/2019 Lecture 3 CP - II.ppt
1/20
Data Types & Storage Classes
7/28/2019 Lecture 3 CP - II.ppt
2/20
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 andfunctions)
3. User-Defined data types
PRIMARY DATA TYPES:
All C compilers support five fundamental data types namely:
1. int
2. float3. char
4. double
5. void
7/28/2019 Lecture 3 CP - II.ppt
3/20
Primary Data types in CPRIMARY DATA TYPES
void
Integral Type
Integer Character
Floating point Type
char
signed char
unsigned char
float double Longdouble
Signed unsigned type
Int unsigned int
short int unsigned short intlong int unsigned long int
7/28/2019 Lecture 3 CP - II.ppt
4/20
PRIMARY DATATYPES IN C
A look on data type, keyword and their sizes in bytes
Types Keyword Size (in bytes)
Integer
Real
Double
Characternon-specific
int
float
double
charvoid
2
4
8
1
7/28/2019 Lecture 3 CP - II.ppt
5/20
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:
Valid Invalid
-248
44042
27246
+1996
0
3,333
-34.0+3,468.3
9999999 (out of range)
7/28/2019 Lecture 3 CP - II.ppt
6/20
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-EXPONENTnotation
Following are valid and invalid floating point numbers:
Valid Invalid
-236.238
2.63238 E + 02
0.0263238 E + 04
26323.8 E -02
0,0
-2.2.2
- + 22.22
7/28/2019 Lecture 3 CP - II.ppt
7/20
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 characterenclosed within a pair of apostrophes
Ex : a, p, $, 2 etc.,
Each character constant has an integer value and is given by theASCII character set
Character constant ASCII value
A
B
Z
az
0
9
&
65
66
90
97
122
48
57
38
7/28/2019 Lecture 3 CP - II.ppt
8/20
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.,
7/28/2019 Lecture 3 CP - II.ppt
9/20
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 digitsafter 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 anyvalue to the calling function
7/28/2019 Lecture 3 CP - II.ppt
10/20
DERIVED DATA TYPES IN C
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 datatypes . They are:
ARRAYS
STRUCTURE FUNCTIONS
POINTERS
7/28/2019 Lecture 3 CP - II.ppt
11/20
USER-DEFINED DATA TYPES IN C
C supports a feature called type definition that
allows users to define an identifier that wouldrepresent an existing data type.
The main advantage of typedef is we can createmeaningful data types which increases the
readability of a programSyntax: typedef type identifier;
EX: typedef int units;
typedef float marks;
Now, units symbolizes int and marks symbolizes floatthereby we can write
units batch1, batch2;
marks name1[50], name2[50];
7/28/2019 Lecture 3 CP - II.ppt
12/20
USER-DEFINED DATA TYPES IN C
Another user defined data type is ENUMERATED DATATYPE
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
explicitlyEx:enum day { Monday, Tuesday, .. Sunday };
EX:enum day { Monday =1, Tuesday =2 ,..};Explicitly
7/28/2019 Lecture 3 CP - II.ppt
13/20
Datatype Range Bytes Format Specifier
int -32,768 to +32767 2 %d
Short signed int -32,768 to +32767 2 %d
Short unsigned int 0 to 65535 2 %u
Long signed int -2,147,483,648 to
+2,147,483,64
8
4 %d
long unsigned int 0 to 4294967295 4 %lu
Signed char -128 to +127 1 %c
Unsigned char 0 to 255 1 %c
Float 3.4e-38 to 3.4e+38 4 %f
Double 1.7e-308 to 1.7e+308 8 %lf
Long double 3.4e-4932 to 1.1e+4932 10 %LF
7/28/2019 Lecture 3 CP - II.ppt
14/20
/* Program to demonstrate data types*/ #include
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);
}
7/28/2019 Lecture 3 CP - II.ppt
15/20
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
7/28/2019 Lecture 3 CP - II.ppt
16/20
// signed & unsigned type modifiers
#include
main()
{
signed int a = 10569;
unsigned int b = 56000u;
unsigned c = 56645;
Printf(signed int a = %d, a);
Printf(\n unsigned int b = %u, b);Printf(\n unsigned int c = %u, c);
}
7/28/2019 Lecture 3 CP - II.ppt
17/20
//Example
#includemain()
{
double a = 10.00776e123;printf(%g\n,a);
printf(%e\n,a);
printf(%lf\n,a);}
7/28/2019 Lecture 3 CP - II.ppt
18/20
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 avariable
Some of them are : auto (Default)
registerstatic
extern
7/28/2019 Lecture 3 CP - II.ppt
19/20
STORAGE CLASSES
Ex : int m; function1()
main() {
{ int i;
int i; float sum;float balance; ..
.. }
function1();}
7/28/2019 Lecture 3 CP - II.ppt
20/20