Overview of C Language Developed by Dennis Ritchie. Structured programming language. Supports functions for modularity and maintainability. Comments enhance readability. A powerful and versatile language. Compiling and Running C Programs (Linux/Unix) 1. Open the Terminal Keyboard shortcut: Ctrl + Alt + T Activities Menu: Search for "Terminal" Right-Click Menu: Right-click on desktop/folder, select "Open Terminal" 2. Setting up the Terminal Check GCC version: gcc --version Install build essentials (if needed): sudo apt install build-essential 3. Working with Directories Current directory: pwd (print working directory) Change directory: cd <path> (e.g., cd Documents ) Move up one level: cd .. Go to home directory: cd ~ or cd List contents: ls 4. Program Workflow Create a working directory (e.g., your PRN number). Create a C file (e.g., touch hello.c ). Write your C code in the file and save it. Compile: gcc hello.c -o test (creates an executable named test ). Run: ./test (executes the compiled program). C Program Structure #include <stdio.h> // Preprocessor directive: includes header file int main() { // Main function: entry point of the program // -- other statements -- // Comments after double slash (single-line) /* Multi-line comments can span multiple lines */ return 0; // Return value (0 typically indicates success) } Header Files Files specified in the #include section. Contain precompiled functions and macros. Extension: .h (e.g., stdio.h for standard input/output). C source files have .c extension. Main Function Entry point of every C program. Execution starts here. Is compulsory for any C program. Can call other functions. First Program Example #include <stdio.h> int main() { printf("Hello"); // Prints "Hello" to the console return 0; } Data Types in C Primitive: int , float , double , char Aggregate: Arrays (collection of homogeneous data) User-defined: Structures, enums Variables Data that can change during program execution. Declaration: <Data type> <variable name>; (e.g., int a; ) Definition/Initialization: <varname> = <value>; (e.g., a = 10; ) Usage: a = a + 1; // increments 'a' by 1 Variable Naming Rules Cannot be a reserved keyword (e.g., int ). Must start with a letter or an underscore ( _ ). Can contain letters, numbers, or underscores. No other special characters or spaces. Case-sensitive ( A and a are different). Input and Output Input: scanf("%d", &a); (Gets an integer value from user and stores in a ) Output: printf("%d", a); (Prints the value of a to the screen) Common C Programs Hello World #include <stdio.h> int main() { printf("Hello, World!\n"); return 0; } Sum of Two Integers #include <stdio.h> int main() { int num1, num2, sum; printf("Enter two integers: "); scanf("%d %d", &num1, &num2); sum = num1 + num2; printf("Sum = %d\n", sum); return 0; } Even or Odd Check #include <stdio.h> int main() { int num; printf("Enter an integer: "); scanf("%d", &num); if (num % 2 == 0) printf("%d is even.\n", num); else printf("%d is odd.\n", num); return 0; } Loops For Loop Syntax: for (initialization; condition; increment) { // set of statements } Example (Print "Hello" 10 times): for (int i = 0; i < 10; i++) { printf("Hello"); } While Loop Syntax: while (condition) { // statements } Example (Print 10 down to 1): int a = 10; while (a != 0) { printf("%d", a); a--; } // Output: 10987654321 Do-While Loop Syntax: do { // set of statements } while (condition); Example (Print 10 down to 1): int i = 10; do { printf("%d", i); i--; } while (i != 0); // Output: 10987654321 Conditional Statements If-Else Syntax: if (condition) { // stmt 1; (Executes if condition is true) } else { // stmt 2; (Executes if condition is false) } Switch-Case Syntax: switch (variable) { case 1: // statements; break; case 2: // statements; break; default: // statements; } Operators Arithmetic: $+, -, *, /, \%$ Relational: $<, >, <=, >=, ==, !=$ Logical: $&&, ||, !$ Bitwise: $&, |, \sim, \ll, \gg$ Assignment: $=$ Compound Assignment: $+=, -=, *=, /=, \%=, \&=, |=$ String Functions ( <string.h> ) strlen(str) : Returns length of string str . strcpy(dest, src) : Copies string src to dest . strcat(dest, src) : Appends string src to dest . strcmp(s1, s2) : Compares strings s1 and s2 (returns 0 if equal). strchr(str, char) : Finds first occurrence of char in str . strstr(haystack, needle) : Finds first occurrence of string needle in haystack . Numeric Functions ( <math.h> ) pow(base, exp) : Calculates $base^{exp}$. ceil(x) : Returns smallest integer not less than $x$. floor(x) : Returns largest integer not greater than $x$. abs(num) : Returns absolute value of num . log(x) : Returns natural logarithm of $x$. sin(x) , cos(x) , tan(x) : Trigonometric functions. Functions and Parameters Declaration (Prototype): <Returntype> funname(parameter list); Definition: <Returntype> funname(parameter list) { // body of the function } Function Call: Funname(parameter); Actual vs. Formal Parameters Actual Parameters: Used during a function call. Formal Parameters: Used in function definition and declaration. Call by Value Copies value of variable to another variable. Changes within the function do not affect the original variable. Example: fun(a); where a is a variable. Call by Reference Passes address of variable (pointers). Changes within the function affect the original variable. Example: fun(&a); where &a is the address of a . Arrays Aggregate data type, collection of homogeneous data. Elements accessed by index (position), starting from 0. Last index is $num - 1$ for an array of $num$ elements. Multi-dimensional arrays possible (e.g., float a[5][5]; ). Structures User-defined data type, collection of heterogeneous data. Can contain int , float , double , char , etc. Declaration: struct <structname> { members; }<element>; Access members: element.member; Typedef Creates an alias for a data type. Example: typedef int integer; allows using integer instead of int . Applicable to structures as well. Pointers Special variable storing the memory address of another variable. Addresses are integers. Size of pointer is typically same as int . Integer pointer: int *ip; Double pointer: double *dp; Character pointer: char *cp; Assigning value: int a; int *ip = &a; Dereferencing operator: * (e.g., *ip fetches the value at the address stored in ip ).