ERROR HANDLING
Types of Errors in C:
1. Syntax Errors
o Occur when rules of the language are violated.
o Detected at compile time.
o Example: Missing semicolon int a = 10
2. Runtime Errors
o Occur while the program is executing.
o Often caused by invalid operations (e.g., division by zero).
o Example: int a = 10 / 0;
3. Logical Errors
o Program compiles and runs but gives incorrect results.
o Caused by incorrect formula or logic.
o Example: area = 3.14 * r + r; // wrong formula
for circle area
Debugging Techniques:
• Definition: Debugging is the process of finding and fixing errors in a
program.
• Use of printf() to track variable values.
• Step-by-step execution in IDEs.
• Using breakpoints and watch variables.
BUILT-IN FUNCTIONS
Math Functions (from <math.h>)
1. sqrt(x) – Square root
o Example: sqrt(25) returns 5.0
2. pow(x, y) – Power
o Example: pow(2, 3) returns 8.0
3. fabs(x) – Absolute value for floating-point numbers
o Example: fabs(-4.5) returns 4.5
Integer Absolute Value (from <stdlib.h>)
• abs(x) – Absolute value for integers
o Example: abs(-4) returns 4
Character Functions (from <ctype.h>)
1. toupper(ch) – Converts character to uppercase
2. tolower(ch) – Converts character to lowercase
3. isalpha(ch) – Returns true if character is an alphabet
Example:
#include <ctype.h>
printf("%c", toupper('a')); // Output: A
FUNCTIONS IN C
Definition: A block of code that performs a specific task.
Declaration and Definition:
int add(int a, int b); // Declaration
int add(int a, int b) { // Definition
return a + b;
}
Calling a Function:
int result = add(5, 10); // Function call
Parameter Passing:
1. Call by Value
o Copies the actual value
o Changes don’t affect original
o Default in C
2. Call by Reference (using pointers)
o Passes the address
o Changes affect original
Example of Call by Reference:
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
Scope of Variables:
1. Local – declared inside function/block.
2. Global – declared outside all functions.
Example:
int g = 10; // Global
void func() {
int x = 5; // Local
}
Returning Values from Functions:
int square(int n) {
return n * n;
}
Recursion in C
Definition: Function calling itself to solve smaller sub-problems.
Types of Recursion:
Tail Recursion – Recursive call is the last operation
void tail(int n) {
if (n == 0) return;
printf("%d ", n);
tail(n - 1);
}
Non-Tail (Head) Recursion – Recursive call happens before other statements
void head(int n) {
if (n == 0) return;
head(n - 1);
printf("%d ", n);
}
Direct Recursion – A function calls itself directly
void direct(int n) {
if (n <= 0) return;
printf("%d ", n);
direct(n - 1);
}
Indirect Recursion – Function A calls B, and B calls A
void funA(int n);
void funB(int n) {
if (n <= 0) return;
printf("%d ", n);
funA(n - 1);
}
void funA(int n) {
if (n <= 0) return;
printf("%d ", n);
funB(n - 1); }
Examples:
// Factorial
int fact(int n) {
if (n == 0) return 1;
return n * fact(n - 1);
}
// Fibonacci
int fib(int n) {
if (n == 0 || n == 1) return n;
return fib(n - 1) + fib(n - 2);
}
// Perfect number
int sumDiv(int n, int i) {
if (i == 1) return 1;
if (n % i == 0) return i + sumDiv(n, i - 1);
else return sumDiv(n, i - 1);
}
ARRAYS
One-Dimensional Arrays:
int arr[5] = {1, 2, 3, 4, 5};
• Traverse:
for (int i = 0; i < 5; i++) printf("%d ", arr[i]);
• Linear Search:
for (int i = 0; i < n; i++) if (arr[i] == key) // Found
• Bubble Sort:
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1]) swap(&arr[j], &arr[j+1]);
Two-Dimensional Arrays:
int matrix[2][2] = {{1,2},{3,4}};
• Matrix Addition:
for(i=0;i<2;i++) for(j=0;j<2;j++) sum[i][j] = A[i][j] +
B[i][j];
• Matrix Multiplication:
for(i=0;i<2;i++)
for(j=0;j<2;j++)
for(k=0;k<2;k++)
C[i][j] += A[i][k] * B[k][j];
Multidimensional Arrays:
• Concept: Arrays with more than 2 dimensions
int arr[2][3][4];
SCOPE OF VARIABLES
Lifetime and Visibility:
1. Automatic (Local): Exists inside a function.
2. Static: Retains value between calls.
3. Extern: Declared outside.
Storage Classes:
• auto, static, register, extern
Example:
void func() {
static int count = 0;
count++;
printf("%d ", count);
}
TYPEDEF and MACROS
typedef
• Gives a new name to an existing type
typedef unsigned int ui;
ui x = 10;
Macros
• Replaces code using #define
#define PI 3.14
#define AREA(r) (PI * (r) * (r))
Advantages of Designators in C
S.No Advantage Description Example
1️⃣ Direct Index Initialize a specific index directly int a[] = {[5] =
Assignment without filling earlier values. 10};
2️⃣ Skipping Unused Unused elements are default- int a[] = {[3] =
Elements initialized to 0, so you can skip 7};
them easily.
3️⃣ Improved Code Makes code more readable, struct P p = {.y
Clarity especially for structs and large = 2, .x = 1};
arrays.
4️⃣ Order Doesn't You can initialize elements in any int a[] = {[2] =
Matter order, not just sequentially. 4, [0] = 1};
5️⃣ Perfect for Useful when only a few values are int data[1000] =
Sparse Arrays needed in a large array. {[999] = 1};
6️⃣ Reduces Eliminates manual counting and int a[] = {[6] =
Indexing Errors reduces off-by-one errors. 20};
<string.h> -> memcpy(a,b,sizeof(a))
<stdlib.h> -> exit(0)