C Programming : data types and types of variable.pptx

DHIVYAB17 1 views 31 slides Apr 29, 2025
Slide 1
Slide 1 of 31
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
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31

About This Presentation

The document provides an introduction to data types in C, types of datatype, types of primary datatypes, explained with examples , size of datatypes, types of derived datatypes, types of user defined datatypes and also covering topics such as variables,
types of variables. Examples are provided...


Slide Content

C Programming By Mrs. B. Dhivya M.C.A., M.Phil., Technical Trainer / Assistant Professor, CTPCR Sri Ramakrishna College of Arts & Science, Coimbatore. Data Types and Variables

Data types in C Each variable in C has an associated data type. It specifies the type of data that the variable can store . Each data type requires different amounts of memory.

Why Data Types? Every variable must have a type. Every variable should get some space in the memory. Help the compiler decide how many bits should be reserved for a variable.

Datatypes in C Primary datatype Derived datatype User defined datatype

Datatypes in C (contd..)

Integer Data Type Range:   -2,147,483,648 to 2,147,483,647 Size:  4 bytes Format Specifier :  %d eg : int var_name ; int a;

Character Data Type Character data type allows its variable to store only a single character. Range:  (-128 to 127) or (0 to 255) Size:  1 byte Format Specifier :  %c Eg : char var_name ; char a;

Float Data Type Float in C is used to store decimal and exponential values. Range:  1.2E-38 to 3.4E+38 Size:  4 bytes Format Specifier :  %f Eg : float a;  

Double Data Type Double has more precision as compared to that float then it is much more obvious that it occupies twice the memory occupied by the floating-point type. Range: 1.7E-308 to 1.7E+308 Size: 8 bytes Format Specifier : %lf Eg : double var_name ; double a;

Void Data Type The void data type in C is used to specify that no value is present. It does not provide a result value to its caller. It has no values and no operations. // function return type void void exit( int check); // Function without any parameter can accept void. int print( void ); // memory allocation function which // returns a pointer to void. void * malloc ( size_t size);

Size of Data Types in C The size of the data types in C is dependent on the size of the architecture. sizeof () operator to check the size of the data types. Eg : printf ("Size of int : % zu bytes\n", sizeof ( int ));

Derived data types Array Pointers Structures Union

Userdefined data types function Enum Type def

Variables in C A  variable  is a name of the memory location. It is used to store data. Its value can be changed, and it can be reused many times. Eg ; int a; float b; char d;

Types of Variables in C There are many types of variables in c: local variable global variable static variable automatic variable external variable

Output? #include < stdio.h > int main() { int arr [10]; double dArr [5]; printf ("Size of arr : % zu bytes\n", sizeof ( arr )); printf ("Size of dArr : % zu bytes\n", sizeof ( dArr )); return 0; } Output: Size of arr : 40 bytes Size of dArr : 40 bytes

Output? #include < stdio.h > int main() { printf ("Size of int : % zu bytes\n", sizeof ( int )); printf ("Size of char: % zu byte\n", sizeof (char)); printf ("Size of float: % zu bytes\n", sizeof (float)); printf ("Size of double: % zu bytes\n", sizeof (double)); return 0; } Output: Size of int : 4 bytes Size of char: 1 byte Size of float: 4 bytes Size of double: 8 bytes

Output? #include < stdio.h > int main() { int x = 10; double y = 12.34; char z = 'A'; printf ("Size of x: % zu bytes\n", sizeof (x)); printf ("Size of y: % zu bytes\n", sizeof (y)); printf ("Size of z: % zu byte\n", sizeof (z)); return 0; } Output: Size of x: 4 bytes Size of y: 8 bytes Size of z: 1 byte

Predict the Output or error of the C Program 1) #include < stdio.h > int main() { int x = 2147483647; x = x + 1; printf ("%d\n", x); return 0; } A) Compiler Error B) -2147483648 C) 2147483648 D) Undefined Behavior Output: -2147483648 it overflows and wraps around to the minimum value of a signed int , which is -2147483648. This is an example of signed integer overflow.

2) #include < stdio.h > int main() { unsigned int x = 4294967295; x = x + 1; printf ("%u\n", x); return 0; } Output:

#include < stdio.h > int main() { int x = 5 / 2; printf ("%d\n", x); return 0; } A) 2 B) 2.5 C) 2.0 D) Compiler Error Answer: A) 2

4) #include < stdio.h > int main() { int x = -3 % 2; printf ("%d\n", x); return 0; } A) -1 B) 1 C) 0 D) Compiler Error Answer: -1

5) #include < stdio.h > int main() { float x = 1.1; printf ("%f\n", x); return 0; } A) 1.1 B) 1.100000 C) Compiler Error D) Undefined Behavior Answer: 1.100000

6) #include < stdio.h > int main() { float x = 0.1; if (x == 0.1) printf ("Equal\n"); else printf ("Not Equal\n"); return 0; } Answer: Not equal

7) #include < stdio.h > int main() { float x = 1.0 / 0.0; printf ("%f\n", x); return 0; } Answer: Infinity

8) #include < stdio.h > int main() { float x = 0.0 / 0.0; printf ("%f\n", x); return 0; } Answer: NaN ( not a number)

9) #include < stdio.h > int main() { double x = 1.0 / 3.0; printf ("%.10f\n", x); return 0; } Answer: 0.3333333333

10) #include < stdio.h > int main() { char ch = 'A'; printf ("%c\n", ch + 1); return 0; } A) A B) B C) 66 D) Compiler Error Answer: B

11) #include < stdio.h > int main() { char ch = 128; printf ("%d\n", ch ); return 0; } A) 128 B) -128 C) Compiler Error D) Undefined Behavior Answer: B) -128

12) #include < stdio.h >    int main() {    float c = 5.0;     printf ("Temperature in Fahrenheit is %.2f", (9/5)*c + 32);    return 0; } (A)  Temperature in Fahrenheit is 41.00 (B)  Temperature in Fahrenheit is 37.00 (C)  Temperature in Fahrenheit is 0.00 (D)  Compiler Error Answer: B)

Assessment 1 Program to check if the given number is prime or not. 2. Program to find factorial of a number in C.