ASSIGNMENT Unit-1 Programming in C Submitted by: SHANKUL SAHNI Submitted to Name: Ms. Indu Roll no.: 22 (PIC,ECE) Variables In C, variables are used to store data values. Every variable in C has a data type that determines the type and size of data the variable can hold, as well as the operations that can be performed on it. A variable is a named memory location used to store data. The value stored in a variable can be changed during the execution of a program. Types of Variables Variables in C are categorized based on the scope, lifetime, and data type. a. Based on Data Type: int (Integer) : Used to store whole numbers. For example, int x = 10; float (Floating Point) : Used to store decimal numbers with single precision. For example, float pi = 3.14; char (Character) : Used to store single characters or small integers. For example, char initial = 'A'; void : Indicates that the variable does not hold any value; often used with pointers or functions. b. Based on Scope and Lifetime: Local Variables : Declared inside a function or block and only accessible within that block. For example, int main() { int x = 5; } Global Variables : Declared outside of all functions and accessible throughout the entire program. For example, int x = 10; // Global variable int main() { printf ("%d", x); } Static Variables : Retains its value between function calls. void func () { static int count = 0; count++; printf ("%d\n", count); } Scope and Lifetime of Variables Scope refers to the region of the program where the variable can be accessed. Local variables : Have block scope and are accessible only within the block (e.g., function) in which they are declared. Global variables : Have file scope and can be accessed by any function in the same file. Lifetime refers to how long the variable exists in memory. Local variables : Exist only during the execution of the block. Static variables : Persist throughout the entire program execution. Global variables : Exist for the entire lifetime of the program. Naming Rules for Variables Must begin with a letter or underscore (_). Can contain letters, numbers, and underscores. No special characters (e.g., @, #, $) are allowed. Cannot use C keywords (like int, float, return) as variable names. Data Types In C, data types are classifications that specify which type of data a variable can hold. Understanding data types is crucial for managing memory, performing operations, and ensuring that data is handled correctly in your programs. 1. Basic Data Types a. Integer Types (int) Description : Used to store whole numbers (both positive and negative). For example: int a; b. Floating-Point Types (float) Description : Used to store fractional numbers. For e.g. float f = 3.14f; c. Character Type (char) Description : Used to store individual characters. For e.g. char ch = ‘A’; 2. Derived Data Types a. Arrays Description : A collection of elements of the same data type, stored in contiguous memory locations. For e.g. int numbers[10]; b. Pointers Description : Variables that store memory addresses of other variables. For e.g. int * ptr ; c. Structures (struct) Description : A user-defined data type that groups different data types under a single name. Example : struct Person { char name[50]; int age; } Input/Output In C, I/O (Input/Output) operations are used to take input from the user or a file and display output to the user or store it in a file. These operations are typically performed using standard input (stdin), standard output ( stdout ), and standard error (stderr). The functions for performing I/O operations are available in the < stdio.h > library. Let's break down I/O in C into two categories: Input and Output. 1. Input Operations (Reading Data) a. scanf () scanf () is used to take input from the user. It reads formatted data from standard input (usually the keyboard). Example : int num ; printf ("Enter a number: "); scanf ("%d", & num ); b. gets() gets() reads a line of text from standard input, including spaces. Example: char name[50]; gets(name); c. fgets () fgets () is a safer alternative to gets() for reading strings. It allows you to specify the maximum number of characters to read. Example : char name[50]; fgets (name, 50, stdin); 2. Output Operations (Displaying Data) a. printf () printf () is used to display formatted output to the console. Example : printf ("The value is: %d", num ); b. puts() puts() is used to display a string followed by a newline character. Example : char name[50] = "John"; puts(name); 3. File I/O Operation In C, file I/O is performed using functions that allow you to read from and write to files. a. Opening a File fopen () is used to open a file. Example : * fp = fopen ("data.txt", "r"); Modes : "r": Open for reading. "w": Open for writing (creates a new file or truncates the existing one). "a": Open for appending. "r+": Open for both reading and writing. b. Reading and Writing Files fprintf () : Used to write formatted data to a file. Example : fprintf ( fp , "Data: %d", data); fscanf () : Used to read formatted data from a file . Example: fscanf ( fp , "%d", &data); Interconversion Of Variables 1. Implicit Type Conversion (Automatic Type Conversion): Implicit type conversion, also known as automatic type conversion or type promotion, occurs when the compiler automatically converts one data type to another during an operation or assignment. This is done without the programmer explicitly requesting it. Rules of Implicit Conversion: If you mix data types in an expression (like adding an int and a float), C will automatically convert them to a common type. The smaller data type gets promoted to a larger type. For example: char → int int → float float → double Example of Implicit Conversion: #include < stdio.h > int main() { int a = 5; float b = 3.5; float result = a + b; printf ("Result: %f\n", result); return 0; } 2. Explicit Type Conversion (Type Casting): Explicit type conversion, also known as typecasting, is when you manually convert one data type to another by explicitly specifying the type. Example of Explicit Conversion: #include < stdio.h > int main() { int a = 10, b = 3; float result = (float) a / b; printf ("Result: %.2f\n", result); return 0; } 3. Type Conversion in Expressions When different types of variables are used in expressions, C applies implicit type conversion to ensure the operation is performed correctly. General Rules: Integer to Floating-point: If an int is combined with a float in an arithmetic operation, the int is promoted to float. Floating-point to Double: When performing arithmetic with float and double, float is promoted to double. Example : #include < stdio.h > int main() { int x = 5; float y = 2.5; float result = x * y; printf ("Result: %.2f\n", result); return 0; }