๐Ÿ  Session 3 Homework

Variables & Data Types

๐Ÿ“š What You'll Practice

This homework focuses on understanding different data types in C programming and how to declare and use variables effectively.

Goal: Master variable declaration, data types, and memory concepts!

๐Ÿ“‹ Instructions

๐Ÿ“ Program 1: Basic Variable Declaration

Declare and use different data types!

What it does:

Declares variables of different data types, assigns values to them, and displays their values.

#include <stdio.h> int main() { // Integer variables int age = 20; int year = 2024; // Float variables float height = 5.8; float weight = 70.5; // Character variables char grade = 'A'; char initial = 'R'; // Display all variables printf("Age: %d\n", age); printf("Year: %d\n", year); printf("Height: %.1f feet\n", height); printf("Weight: %.1f kg\n", weight); printf("Grade: %c\n", grade); printf("Initial: %c\n", initial); return 0; }

๐Ÿงช Expected Output

  • Age: 20
  • Year: 2024
  • Height: 5.8 feet
  • Weight: 70.5 kg
  • Grade: A
  • Initial: R

๐Ÿ“Š Program 2: Variable Declaration and Assignment

Declare variables and assign values!

What it does:

Demonstrates how to declare variables of different data types and assign values to them.

#include <stdio.h> int main() { // Integer variables int student_id = 12345; int age = 20; // Float variables float gpa = 3.8; float height = 5.9; // Character variables char grade = 'A'; char initial = 'R'; // Display all variables printf("=== STUDENT RECORD ===\n"); printf("Student ID: %d\n", student_id); printf("Age: %d\n", age); printf("GPA: %.2f\n", gpa); printf("Height: %.1f feet\n", height); printf("Grade: %c\n", grade); printf("Initial: %c\n", initial); return 0; }

๐Ÿงช Expected Output

  • Student ID: 12345
  • Age: 20
  • GPA: 3.80
  • Height: 5.9 feet
  • Grade: A
  • Initial: R

๐Ÿ“ Program 3: Data Type Sizes

Learn about memory usage of different data types!

What it does:

Demonstrates the size of different data types in bytes using sizeof() operator.

#include <stdio.h> int main() { int integer_var; float float_var; char char_var; double double_var; printf("=== DATA TYPE SIZES ===\n"); printf("Size of int: %zu bytes\n", sizeof(integer_var)); printf("Size of float: %zu bytes\n", sizeof(float_var)); printf("Size of char: %zu bytes\n", sizeof(char_var)); printf("Size of double: %zu bytes\n", sizeof(double_var)); printf("\n=== DIRECT SIZE CHECK ===\n"); printf("Size of int: %zu bytes\n", sizeof(int)); printf("Size of float: %zu bytes\n", sizeof(float)); printf("Size of char: %zu bytes\n", sizeof(char)); printf("Size of double: %zu bytes\n", sizeof(double)); return 0; }

๐Ÿงช Expected Output

  • Shows memory size of each data type (typically: int=4, float=4, char=1, double=8 bytes)

๐Ÿ”ข Program 4: Variable Operations

Perform operations with different data types!

What it does:

Demonstrates how different data types behave in mathematical operations and type conversions.

#include <stdio.h> int main() { int a = 10; int b = 3; float x = 10.5; float y = 3.2; printf("=== INTEGER OPERATIONS ===\n"); printf("a = %d, b = %d\n", a, b); printf("a + b = %d\n", a + b); printf("a - b = %d\n", a - b); printf("a * b = %d\n", a * b); printf("a / b = %d\n", a / b); printf("a %% b = %d\n", a % b); printf("\n=== FLOAT OPERATIONS ===\n"); printf("x = %.1f, y = %.1f\n", x, y); printf("x + y = %.2f\n", x + y); printf("x - y = %.2f\n", x - y); printf("x * y = %.2f\n", x * y); printf("x / y = %.2f\n", x / y); printf("\n=== MIXED OPERATIONS ===\n"); printf("a + x = %.2f\n", a + x); printf("x * b = %.2f\n", x * b); return 0; }

๐Ÿงช Expected Output

  • Integer operations: 10+3=13, 10-3=7, 10*3=30, 10/3=3, 10%3=1
  • Float operations: 10.5+3.2=13.70, 10.5-3.2=7.30, etc.
  • Mixed operations: 10+10.5=20.50, 10.5*3=31.50

๐Ÿ”ค Program 5: Character Operations

Work with characters and ASCII values!

What it does:

Demonstrates character operations, ASCII values, and character arithmetic.

#include <stdio.h> int main() { char letter1 = 'A'; char letter2 = 'Z'; char small_a = 'a'; char digit = '5'; printf("=== CHARACTER INFORMATION ===\n"); printf("Letter: %c, ASCII: %d\n", letter1, letter1); printf("Letter: %c, ASCII: %d\n", letter2, letter2); printf("Letter: %c, ASCII: %d\n", small_a, small_a); printf("Digit: %c, ASCII: %d\n", digit, digit); printf("\n=== CHARACTER ARITHMETIC ===\n"); printf("Next letter after A: %c\n", letter1 + 1); printf("Previous letter before Z: %c\n", letter2 - 1); printf("Uppercase of 'a': %c\n", small_a - 32); printf("Numeric value of '5': %d\n", digit - '0'); return 0; }

๐Ÿงช Expected Output

  • Character info: A=65, Z=90, a=97, 5=53
  • Character arithmetic: A+1=B, Z-1=Y, a-32=A, '5'-'0'=5