Fundamentals of C
Fundamentals of C
Key Features of C
Applications of C
Structure of a C Program
A C program follows a well-defined structure:
// Function Definition
void greet() {
printf("Welcome to C Programming!\n");
}
Components of a C Program
Types of Errors
int x = 10, y = 0;
int result = x / y; // Division by zero (Logical Error)
3. Runtime Errors: Errors occurring during execution (e.g., memory access violation).
Debugging Techniques
gdb ./program
Declaration of Variables
A variable is a name assigned to a memory location.
data_type variable_name;
Example
Modifiers in C
C provides type modifiers to alter data type properties.
Modifier Effect
signed Default for int, allows positive & negative values
unsigned Allows only positive values (e.g., unsigned int)
short Uses less memory (e.g., short int)
long Increases memory size (e.g., long int)
Example
Identifiers
Identifiers are the names of variables, functions, arrays, etc.
Rules for Identifiers
Example of Identifiers
int rollNumber;
float student_marks;
Keywords
C has 32 reserved keywords that cannot be used as identifiers.
List of Keywords in C
auto, break, case, char, const, continue, default, do, double, else,
enum, extern, float, for, goto, if, int, long, register, return,
short, signed, sizeof, static, struct, switch, typedef, union,
unsigned, void, volatile, while
Storage Classes in C
Storage classes in C determine the scope, lifetime, default value, and storage location
of a variable. They define how and where a variable is stored, its visibility, and its
existence throughout program execution.
Example
#include <stdio.h>
void function() {
auto int x = 10; // Auto storage class (local variable)
printf("Value of x: %d\n", x);
}
int main() {
function();
return 0;
}
Output:
Value of x: 10
Explanation:
Example
#include <stdio.h>
int main() {
register int count = 10; // Store variable in CPU register
printf("Count: %d\n", count);
return 0;
}
Output:
Count: 10
Key Points
#include <stdio.h>
void counter() {
static int count = 0; // Static variable retains value
count++;
printf("Count: %d\n", count);
}
int main() {
counter();
counter();
counter();
return 0;
}
Output:
Count: 1
Count: 2
Count: 3
Explanation:
• count retains its value between function calls.
#include <stdio.h>
void display() {
printf("num: %d\n", num);
}
int main() {
display();
return 0;
}
Key Points Local static variables retain values between function calls.
Global static variables are not accessible outside the file.
Uses memory for the entire program duration (not efficient for short-term variables).
File1.c
#include <stdio.h>
int num = 100; // Global variable
void display() {
printf("num: %d\n", num);
}
File2.c
#include <stdio.h>
int main() {
printf("Accessing num in File2: %d\n", num);
return 0;
}
Explanation:
Key Points
Enumerations (enum)
Enumerations provide a way to assign names to integer values.
Syntax
Example
#include <stdio.h>
enum Color {RED, GREEN, BLUE};
int main() {
enum Color myColor = GREEN;
printf("Value of GREEN: %d\n", myColor);
return 0;
}
Output:
Value of GREEN: 1
Macros in C
#include <stdio.h>
#define SQUARE(x) (x * x)
int main() {
printf("Square of 5: %d\n", SQUARE(5));
return 0;
}
The C Preprocessor
Directive Description
#include Includes header files (#include <stdio.h>)
#define Defines macros (#define PI 3.14)
Operators in C
Operators in C are symbols that perform operations on variables and values. They are
classified into different types based on their functionality.
1. Unary Operators
Unary operators operate on a single operand. Common unary operators in C include:
Example:
int a = 10;
int b = -a; // b becomes -10
Example:
int x = 5;
int y = ++x; // x becomes 6, y is assigned 6
int z = x--; // z is assigned 6, x becomes 5
Example:
int a = 0;
if (!a) {
printf("a is false");
}
Example:
int a = 5;
int b = ~a; // b becomes -6 (Two’s complement representation)
e. Address-of (&)
Example:
int a = 10;
printf("%p", &a); // Prints address of a
f. Dereference (*)
Example:
int a = 10;
int *ptr = &a;
printf("%d", *ptr); // Prints 10
Example:
int a = 10;
float b = (float)a / 3; // b becomes 3.3333
a. Arithmetic Operators
Example:
int a = 10, b = 3;
printf("%d", a % b); // Prints 1 (Remainder of 10/3)
b. Logical Operators
Example:
int a = 1, b = 0;
if (a && b) {
printf("Both are true");
} else {
printf("At least one is false"); // This will execute
}
3. Bitwise Operators
Bitwise operators perform bitwise manipulation.
Example:
int a = 5, b = 3;
printf("%d", a & b); // Prints 1
4. Assignment Operators
Used to assign values to variables.
Example:
int a = 10;
a += 5; // a becomes 15
Syntax:
condition ? expression1 : expression2;
Example:
int a = 10, b = 5;
int min = (a < b) ? a : b; // min is 5
b. Order of Evaluation
Example:
int a = 10, b = 5, c = 2;
int result = a + b * c; // Multiplication (*) happens first: 10 + (5 *
2) = 20
Control Statements in C
Control statements regulate the flow of execution in a program. The main control
statements in C are:
1. if-else Statement
Syntax:
if (condition) {
// Executes if condition is true
} else {
// Executes if condition is false
}
Example:
if (condition) {
// Executes if condition is true
}
• if-else-if ladder
if (condition1) {
// Executes if condition1 is true
} else if (condition2) {
// Executes if condition2 is true
} else {
// Executes if all conditions are false
}
2. switch Statement
Syntax:
switch (expression) {
case value1:
// Code to execute
break;
case value2:
// Code to execute
break;
default:
// Default case (if no match)
}
Example:
int day = 3;
switch (day) {
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
default:
printf("Invalid day");
}
3. break Statement
Example:
4. continue Statement
The continue statement skips the current iteration and moves to the next iteration in loops.
Example:
Example:
int a, b, c;
a = (b = 5, c = 10, b + c); // a = 15
printf("%d", a);
6. goto Statement
Syntax:
goto label;
...
label:
statement;
Example:
positive:
printf("Number is positive");
Loops in C
Loops allow repeated execution of a block of code.
1. for Loop
Syntax:
Example:
2. while Loop
Syntax:
while (condition) {
// Code to execute
}
Example:
int i = 0;
while (i < 5) {
printf("%d ", i);
i++;
}
3. do-while Loop
Syntax:
do {
// Code to execute
} while (condition);
Example:
int i = 0;
do {
printf("%d ", i);
i++;
} while (i < 5);
#include <stdio.h>
int main() {
int num;
Explanation
Example Output
Enter an integer: 7
7 is Odd.
Functions in C
A function in C is a block of code designed to perform a specific task. It can be called
multiple times within a program to avoid redundancy and improve modularity.
1. Built-in Functions
2. User-defined Functions
Syntax:
return_type function_name(parameters) {
// Function body
return value;
}
Example:
3. Parameter Passing
Call by Value
Example:
void func(int x) {
x = 20;
}
Call by Reference
Example:
4. Recursive Function
int factorial(int n) {
if (n == 0)
return 1;
return n * factorial(n - 1);
}
Arrays in C
An array is a collection of elements of the same data type stored in contiguous memory
locations. It allows storing multiple values under a single variable name and accessing
them using an index.
Syntax:
data_type array_name[size];
Example:
2. Multidimensional Arrays
Example:
Example: