Datatypes in c

913 views 16 slides Jan 23, 2020
Slide 1
Slide 1 of 16
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16

About This Presentation

illustrates various types of datatypes used in C programming with syntax and suitable examples.


Slide Content

DATA TYPES IN C PROGRAMMING Presented By: Er . Anupam Sharma Assistant Professor(CGC-TC) 1

DATA TYPES In the C Programming language , data types are defined as the data storage format that a variable can store a data to perform a specific operation and are used to define a variable before to use in a program. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted. 2

Name Description Size* Example with syntax Range* char Character or small integer. 1 byte char a=” abc ”; signed: -128 to 127 unsigned: 0 to 255 short int  ( short ) Short Integer. 2 bytes short int a; signed: -32768 to 32767 unsigned: 0 to 65535 int Integer. 4 bytes int s=10; signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 long int  ( long ) Long integer. 4 bytes long int s; signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 float Floating point number. 4 bytes float s=10.256; +/- 3.4e +/- 38 (~7 digits) double Double precision floating point number. 8 bytes double a=20.3568545 +/- 1.7e +/- 308 (~15 digits) long double Long double precision floating point number. 8 bytes Long double a; +/- 1.7e +/- 308 (~15 digits) 5

INT DATA TYPE  Integers are whole numbers with a range of values, range of values are machine dependent. Generally an integer occupies 2 bytes memory space and its value range limited to -32768 to + 32767 . Eg : int i =10; 6

CHAR DATA TYPE  Character type variable can hold a single character. As there are singed and unsigned int (either short or long), in the same way there are signed and unsigned chars; both occupy 1 byte each, but having different ranges . Unsigned characters have values between and 255 signed characters have values from –128 to 127 . Eg : char a=” abc ”; 7

FLOAT DATA TYPE  The float data type is used to store fractional numbers (real numbers) with 6 digits of precision. Floating point numbers are denoted by the keyword float. When the accuracy of the floating point number is insufficient, one can use the double to define the number. The double is same as float but with longer precision and takes double space (8 bytes) than float. To extend the precision further we can use long double which occupies 10 bytes of memory space . Eg : float a=3.10; 8

VOID DATA TYPE  The void type has no values therefore we cannot declare it as variable as we did in case of integer and float. The void data type is usually used with function to specify its type. Like in our first C program we declared "main ()" as void type because it does not return any value . Eg : void main(); 9

DERIVED DATA TYPE Array : An array in C language is a collection of similar data-type, means an array can hold value of a particular data type for which it has been declared. Arrays can be created from any of the basic C data-types . Eg : int a[10]; String  variable contains a collection of characters surrounded by double quotes. e.g string greeting = "Hello"; Pointer : C Pointer is a special variable that can be used to store address of another variable using & operator . *   operator is used to assign memory location to a Eg : * int a; 10

Function It is a block of code which only runs when it is called. And can pass data, known as parameters, into a function.Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times. C provides some pre-defined functions, such as main(), which is used to execute code. But you can also create your own functions to perform certain actions. To create (often referred to as  declare ) a function, specify the name of the function, followed by parentheses () Syntax : return type function name(argument list); Eg : void  myFunction () {   // code to be executed } 11

STRUCTURE DATA TYPE C S t ructure i s a col l e ction of d i f f e re n t d a t a t ypes w h i c h are grouped together and each element in a C structure is called member. Example: struct student { int roll_no ; char name[20]; char city[20]; } 12

Union It defines a type that contains a value that can be interpreted as different types. as it contains variables that uses the same space in the memory. Its syntax is: union union_name { type1 name1; type2 name2; ... } e.g union secretCode { int i ; char str [4]; } 13

User defined Data Types: ENUMERATED DATA TYPE (ENUM) Enumerated data type is a user defined data type having finite set of enumeration constants. The keyword 'enum' is used to create enumerated data type. Enumeration data type consists of named integer constants as a list. I t star t wi t h ( zero ) b y defau l t and v a l ue i s i ncremente d b y 1 for the sequential identifiers in the list. Syntax: Enum [ data_type ] {const1, const2… constn }; Eg : enum month { Jan, Feb, Mar }; or /* Jan, Feb and Mar v ariables will be assigned to 0, 1 and 2 respectively by default */ enum month { Jan = 1, Feb, Mar }; /* Feb and Mar variables will be assigned to 2 and 3 respectively by default */ enum month { Jan = 20, Feb, Mar }; /* Jan is assigned to 20. Feb and Mar variables will be assigned to 21 and 22 respectively by default */ 14

TYPEDEF DATA TYPE It is used to create new data type. But it is commonly used to change existing data type with another name. Syntax: typedef [ data_type ] new_data_type ; here data_type is the basic type you want to substitute, while new_ data_type is the name you want to give to it. Eg : typedef int integer; integer roll_no ; 15

THANK YOU 12 16