Lec 10.pptxknjbaicibc nihomq wdn jqocoqndoqhd

poojavajpayee1 1 views 33 slides Oct 15, 2025
Slide 1
Slide 1 of 33
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
Slide 32
32
Slide 33
33

About This Presentation

dsvgrwe


Slide Content

Data types, Type Conversion Session No. 10 Course Name: Programming for Problem Solving Course Code: E1UA102C Instructor Name: Duration: 50 min Date of Conduction of Class:03/10/2025 Galgotias Student-Centered Active Learning Ecosystem 1

Galgotias Student-Centered Active Learning Ecosystem 2 Difference between variable and constants. RECAP #include < stdio.h > int main() { const int DAYS_IN_WEEK = 7; int days = 5; printf ("Days: %d", days); days = 10; // valid printf ("Updated Days: %d\n", days); return 0; }

Galgotias Student-Centered Active Learning Ecosystem 3 Opening If you had to store your age, your height in meters, and the first letter of your name in C — which data types would you choose? What happens if you add an int and a float ? Can a double fit into an int ? What might happen?

At the end of this session you will be able to LO1: Identify and use appropriate C datatypes (int, float, char, etc.) for storing and manipulating different kinds of data effectively. LO2: : A pply implicit and explicit type conversions (casting) in C to ensure accurate data handling and computation. 4

Outline Data types in C Basic data type Derived data type User-defined data type Type casting 5

In C programming , data types define the type of data a variable can hold and how much memory it will occupy . 6 DATA TYPE IN C

7

8 BASIC DATA TYPE IN C Data Type Size (depends on system, typically 32-bit/64-bit) Description Format Specifier int 2 or 4 bytes Stores integers (whole numbers). %d float 4 bytes Stores single-precision decimal numbers. %f double 8 bytes Stores double-precision decimal numbers. % lf char 1 byte Stores a single character or small integer. %c Void — Represents no value / empty type. bool 1 byte Represents two values : true (1) and false (0). Use #include < stdbool.h > %d

9 BASIC DATA TYPE IN C #include < stdio.h > // Function with void return type void showMessage () { printf ("This is a function with void return type.\n\n"); } int main() { // Basic data types int age = 25; // Integer float height = 5.9f; // Floating point double salary = 55000.75; // Double precision char grade = 'A'; // Character // Printing values printf ("Values of Basic Data Types:\n"); printf ("Integer (age) : %d\n", age); printf ("Float (height) : %.2f\n", height); printf ("Double (salary) : %.2lf\n", salary); printf ("Char (grade) : %c\n\n", grade); // Printing sizes printf ("Sizes of Basic Data Types:\n"); printf ("Size of int : % zu bytes\n", sizeof (int)); printf ("Size of float : % zu bytes\n", sizeof (float)); printf ("Size of double : % zu bytes\n", sizeof (double)); printf ("Size of char : % zu bytes\n", sizeof (char)); printf ("Size of void : N/A (no storage)\n"); // Call void function showMessage (); return 0; }

10 BASIC DATA TYPE IN C #include < stdio.h > #include < stdbool.h > // Required for bool, true, false int main() { bool isEven = true; bool isOdd = false; if ( isEven ) { printf ("The number is even.\n"); } if (! isOdd ) { printf ("The number is not odd.\n"); } return 0; }

11 MODIFIERS TO BASIC DATA TYPE IN C C provides modifiers to alter the size and range of basic data types: short , long , signed, unsigned Type Typical Size Range short int 2 bytes -32,768 to 32,767 unsigned short int 2 bytes 0 to 65,535 long int 4 bytes -2,147,483,648 to 2,147,483,647 unsigned long int 4 bytes 0 to 4,294,967,295 long long int 8 bytes Very large range unsigned long long int 8 bytes Only positive values

12 BASIC DATA TYPE IN C Format Specifiers in C (with Modifiers) 🔹 Integer Types Data Type Format Specifier (printf) Example int %d or %i printf("%d", a); short int / short %hd printf("%hd", s); unsigned short int %hu printf("%hu", us); unsigned int %u printf("%u", u); long int / long %ld printf("%ld", l); unsigned long int % lu printf("%lu", ul); long long int %lld printf("%lld", ll); unsigned long long int %llu printf ("% llu ", ull );

13 BASIC DATA TYPE IN C Format Specifiers in C (with Modifiers) 🔹 Character Types Data Type Format Specifier Example char %c printf("%c", ch); signed char %c printf("%c", sch); unsigned char %c (but prints as integer if %d) printf ("%d", uch ); 🔹 Floating-Point Types Data Type Format Specifier Example float %f printf("%f", f); double %lf printf("%lf", d); long double %Lf printf ("% Lf ", ld );

14 DERIVED DATA TYPE IN C These are derived from the basic types: Arrays → collection of similar data types (int arr [10]). Pointers → store the address of a variable (int *p). Functions → return values and take arguments of specific types.

15 DERIVED DATA TYPE IN C Derived Data Type Description Syntax Example Notes Array Collection of elements of the same type , stored in contiguous memory . datatype arrayName [size]; int marks[5]; Index starts at 0. Size must be constant (in C89), but C99 allows variable-length arrays. Pointer Variable that stores the address of another variable . datatype *pointerName; int *ptr; Powerful but error-prone; can be used for dynamic memory, arrays, and function references. Function Block of code that performs a specific task and can return a value . returnType functionName(parameters); int add(int a, int b); Functions themselves are first-class citizens; function pointers allow passing functions as arguments.

16 USER DEFINED DATA TYPE IN C C allows creating custom data types using: Structures (struct) → group of different data types. Unions (union) → similar to structures, but shares memory. enum → Used to assign names to integer constants, improving readability . typedef → gives a new name to an existing type.

17 USER DEFINED DATA TYPE IN C User-Defined Type Description Syntax Example Notes struct (Structure) Groups variables of different data types under one name. struct tag { datatype member1; datatype member2; ... }; c struct Student { int id; char name[20]; }; Members stored separately , each has its own memory. Access using dot (.). union Similar to struct, but members share the same memory location . union tag { datatype member1; datatype member2; ... }; c union Data { int i; float f; char c; }; Memory efficient . Only one member active at a time (size = largest member). enum (Enumeration) Defines a set of named integer constants . enum tag { CONST1, CONST2, ... }; c enum Days { MON, TUE, WED, THU, FRI, SAT, SUN }; By default, values start from 0. Can assign custom values. typedef Provides a new name (alias) for an existing data type. typedef existingType newName; c typedef unsigned int uint; uint x = 10; Improves readability and portability. Commonly used with struct, enum .

ACTIVITY 1 : Problem Based Learning Sort them into categories: Basic, Derived, User-defined, Enumeration. int marks; float temperature; char grade; double salary; int arr [10]; struct student { int id; char name[20]; }; typedef unsigned int uint ; enum days { MON, TUE, WED, THU, FRI, SAT, SUN };

ACTIVITY 2 : Problem Based Learning “Fix the Code” Challenge #include < stdio.h > int main() { char age = 25; float grade = 'A’; int name = "Alice"; double salary = 5000.75; printf ("%d %f %s % lf ", age, grade, name, salary); return 0; } Task: Identify errors and suggest correct data types and format specifiers .

20 TYPE CONVERSION IN C Type conversion in C is the process of converting a value of one data type into another. It allows you to perform operations between different types of data. There are two main types of type conversion in C: implicit (automatic) conversion and explicit (manual) conversion .

21 TYPE CONVERSION IN C 1. Implicit Type Conversion (Type Casting / Type Promotion) This is done automatically by the compiler when different data types are used in an expression. C promotes the smaller or lower-ranked data type to a higher-ranked type to avoid loss of information. Rules of implicit conversion (usual arithmetic conversions): char → int short → int int → float → double float → double

22 TYPE CONVERSION IN C #include < stdio.h > int main() { int a = 5; float b = 2.5; float c; c = a + b; // 'a' is automatically converted to float printf ("%f\n", c); // Output: 7.500000 return 0; }

23 TYPE CONVERSION IN C 2. Explicit Type Conversion (Type Casting) This is when the programmer manually converts a variable from one type to another using type cast operator: (type) . Syntax: (type) expression

24 TYPE CONVERSION IN C #include < stdio.h > int main() { float a = 9.7; int b; b = (int)a; // explicit conversion from float to int printf ("%d\n", b); // Output: 9 return 0; } Example of Explicit Type Conversion (Type Casting) Here, (int)a explicitly converts the float 9.7 to the integer 9 by truncating the decimal part.

25 TYPE CONVERSION IN C Quick Rules to Remember Implicit conversion : Happens automatically in expressions; smaller types are promoted to larger types (char → int, int → float). Explicit conversion (casting): Programmer specifies the target type using (type). Precision loss occurs when converting from float/double → int. Overflow/underflow can happen when converting between types with different ranges (int → char or signed ↔ unsigned).

26 Comprehensive table of type conversions in C CCONVERSION IN C From Type To Type Conversion Type Example Notes int float Implicit/Explicit float f = 10; f = (float)10; Integer converted to float automatically or manually. float int Explicit int i = (int)3.9; Decimal part is truncated. char int Implicit/Explicit char c = 'A'; int i = c; 'A' becomes 65 (ASCII). int char Explicit char c = (char)65; ASCII value converted to character. double float Explicit float f = (float)3.14159; Precision may be lost. float double Implicit double d = 2.5f; Float promoted to double automatically. short int Implicit short s = 10; int i = s; Small integer promoted to int. int short Explicit short s = (short)1000; May cause overflow if value too large. unsigned int int Implicit/Explicit unsigned int u = 300; int i = (int)u; Watch out for negative values if overflow occurs. int unsigned int Explicit unsigned int u = (unsigned int)(-1); Converts negative number to large positive.

Activity #include < stdio.h > int main() { int a = 5; float b = 2.0; double c = 3.5; // Example 1: Implicit conversion float result1 = a + b; // Example 2: Explicit conversion int result2 = (int)c + a; printf ("result1 = %f\n", result1); printf ("result2 = %d\n", result2); return 0; } Task: Predict the output of result1 and result2. 2. Identify where implicit and explicit conversions occur.

Summary In C programming, data types define the type of data a variable can hold and how much memory it will occupy. Type conversion in C is the process of converting a value of one data type into another. It allows you to perform operations between different types of data. There are two main types of type conversion in C: implicit (automatic) conversion and explicit (manual) conversion.

LO1: Identify and use appropriate C datatypes (int, float, char, etc.) for storing and manipulating different kinds of data effectively. LO2: : A pply implicit and explicit type conversions (casting) in C to ensure accurate data handling and computation. 29 Attainment of LOs in alignment to the learning

Discussion on the post session activity Attempt the Post-session activity on LMS, before next session

Information to next topic of the course Next session: Operators and expressions

Review and Reflection from students