0% found this document useful (0 votes)
41 views

Lecture 3 CP - II

This document discusses data types and storage classes in C. It describes the primary data types like int, float, char, and double. It also covers derived data types like arrays, structures, functions, and pointers. User-defined data types using typedefs and enums are presented. Storage classes are introduced which control the scope and lifetime of variables. Examples of declaring and using different data types and storage classes in C programs are provided.

Uploaded by

Naeem Ahamad
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Lecture 3 CP - II

This document discusses data types and storage classes in C. It describes the primary data types like int, float, char, and double. It also covers derived data types like arrays, structures, functions, and pointers. User-defined data types using typedefs and enums are presented. Storage classes are introduced which control the scope and lifetime of variables. Examples of declaring and using different data types and storage classes in C programs are provided.

Uploaded by

Naeem Ahamad
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Data Types & Storage Classes

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

Primary Data types in C


PRIMARY DATA TYPES Integral Type

Integer
Signed Int short int long int unsigned type unsigned int unsigned short int unsigned long int

Character
char signed char unsigned char

Floating point Type


float double Long double

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:

Valid -248 44042 27246 +1996 0

Invalid 3,333 -34.0 +3,468.3 9999999 (out of range)

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:

Valid -236.238 2.63238 E + 02 0.0263238 E + 04 26323.8 E -02

Invalid 0,0 -2.2.2 - + 22.22

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

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 data types . They are: ARRAYS STRUCTURE FUNCTIONS POINTERS

USER-DEFINED DATA TYPES IN C


C supports a feature called type definition that allows users to define an identifier that would represent an existing data type. The main advantage of typedef is we can create meaningful data types which increases the readability of a program Syntax: typedef type identifier; EX: typedef int units; typedef float marks; Now, units symbolizes int and marks symbolizes float thereby we can write units batch1, batch2; marks name1[50], name2[50];

USER-DEFINED DATA TYPES IN C

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

%lu %c %c %f %lf %LF

/* Program to demonstrate data types*/

#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

// signed & unsigned type modifiers


#include<stdio.h> 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); }

//Example #include<stdio.h> main() { double a = 10.00776e123; printf(%g\n,a); printf(%e\n,a); printf(%lf\n,a); }

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; .. }

You might also like