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

Data Type in C

data type in c

Uploaded by

Sourav Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Data Type in C

data type in c

Uploaded by

Sourav Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Data Types in C

• Data types in C are used to declare variables (or functions) of


different types.
• The type of a variable determines how much space (size) it occupies
in memory (storage).
• Data storage format a variable can store.
• There are three types of data:
• Fundamental/Basic/Primary data type
• Derived data type
• User defined data type
• Void data type
Fundamental/Basic/Primary Data Type

• Predefined/built-in data types.


Character data type
Integer data type
Floating point data type
Double data type

Character data type:


• Allows a variable to store only one character.
• char keyword is used to refer this data type.
• Example: ‘a’, ‘A’, ‘2’
• Declaration syntax: char a,b;
char grade;
Fundamental/Basic/Primary Data Type (contd.)
Integer data type
• Allows a variable to store numeric values (whole numbers with 0, negative or positive values).
• int keyword is used to refer this data type.
• The storage size depends upon the processor used (2, 4, or 8 bytes).
• Decimal values can not be stored in integer data type.
• Example: 10,200.
• Declaration syntax: int result, point;
int class_2020;
Floating point data type
• Allows a variable to store decimal values.
• The storage size depends upon the processor used.
• Maximum 6 digits can be used after decimal point.
• Example: 10.32, 23.234123.
• Declaration syntax: float result,id20;
float division;
Fundamental/Basic/Primary Data Type (contd.)
Double data type
• Same as float type data.
• Allows 10 digits after decimal points.
• Declaration syntax: double salary, level;
double payment;
Fundamental/Basic/Primary Data Type (contd.)
Data Types Memory Size Range

char 1 byte −128 to 127

signed char 1 byte −128 to 127

unsigned char 1 byte 0 to 255

signed int 2 byte −32,768 to 32,767

unsigned int 2 byte 0 to 65,535

short int 2 byte −32,768 to 32,767

signed short int 2 byte −32,768 to 32,767

unsigned short int 2 byte 0 to 65,535

long int 4 byte -2,147,483,648 to 2,147,483,647

signed long int 4 byte -2,147,483,648 to 2,147,483,647

unsigned long int 4 byte 0 to 4,294,967,295

float 4 byte

double 8 byte

long double 10 byte


Derived Data Type
• Derived from fundamental data type.
• Array, pointer, structure and union are derived datatypes.
• Example: int result[10];
User Defined Data Type
• Defined by the user.
• Enumerated data type
• Assigns names to integral constants as a list.
• Makes a program easy to read and maintain.
• Starts with 0 by default and the value is incremented by 1 for the sequential
identifiers in the list.
• enum keyword is used to denote this data type.
• Declaration syntax:
enum enum_name{const1,const2,….};
• Example: enum week{saturday, sunday, monday, tuesday, wednesday,
thursday, fryday} ;
Void Data Type
• Empty data type that has no value.
• Used in functions and pointers.
• void keyword is used to denote this data type.
• A function returns nothing (no values)).
void main();
• A function that has no parameters (arguments).
int calculator (void);
• Memory allocation function (malloc) returns a pointer void.
Reading Input
• Input: feeds some data to a program(for processing) .
• C provides a set of built-in functions to read input(s).
• getchar(): this function reads a single character from the screen.
• gets(): this function is used to read a string.
• scanf():
Standard input function.
All types of data (formatted data) can be read using this function from standard input
device.
Declared in stdio.h header file in C library.
Syntax: scanf(“%formatspecifier”,&variable_name);
example: scanf(“%d”,&a);
Getting Output
• Output: displays some data on the screen after processing(or save in a file).
• C provides a set of built-in functions to output the data.
• putchar(): displays a single character on the screen.
• puts(): this function is used to write a string.
• printf():
Standard output function.
All types of data (formatted data) can be written/displayed using this function.
Declared in stdio.h header file in C library.
Syntax: printf(“%formatspecifier”,variable_name);
example: printf(“%d”,a);
Format Specifier
Type Format Specifier

int %d

char %c

float %f

double %lf

short int %hd

unsigned int %u

long int %ld, %li

long long int %lld, %lli

unsigned long int %lu

unsigned long
%llu
long int

signed char %c

unsigned char %c

long double %llf


main() function
• Special user defined function.
• Indicates the beginning (entry point) of a C program.
• A program must have a main() function.
• A program can not have more than one main() function.
• Called by the operating system when the program is run.
• main(){
…..statements…..
}
Let us start our first program

You might also like