0% found this document useful (0 votes)
8 views8 pages

General Notes

The document provides an overview of C programming, covering essential tools such as compilers, IDEs, and debuggers, as well as the layout of a program in memory. It discusses data types, structs, file handling, pointers, arrays, dynamic memory allocation, and recursion with relevant examples. Each week focuses on different aspects of C programming, illustrating concepts through code snippets.

Uploaded by

joxran03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

General Notes

The document provides an overview of C programming, covering essential tools such as compilers, IDEs, and debuggers, as well as the layout of a program in memory. It discusses data types, structs, file handling, pointers, arrays, dynamic memory allocation, and recursion with relevant examples. Each week focuses on different aspects of C programming, illustrating concepts through code snippets.

Uploaded by

joxran03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Week 1: Tools & Program Layout in

Memory
1. Tools for C Programming
To start coding in C, you need:

●​ A Compiler: Converts C code into machine-readable instructions.


○​ Examples: GCC (GNU Compiler), Clang, MSVC
●​ An IDE or Text Editor: Helps write and debug code.
○​ Examples: VS Code, Code::Blocks, Dev-C++, Vim
●​ A Debugger: Helps find and fix errors in your code.
○​ Example: gdb (GNU Debugger)

2. Program Layout in Memory


When a C program runs, memory is divided into sections:

●​ Code Segment: Stores the compiled machine code.


●​ Stack: Stores local variables and function calls.
●​ Heap: Stores dynamically allocated memory.
●​ Global/Static Segment: Stores global and static variables.

Example: Memory Usage


c
CopyEdit
#include <stdio.h>
#include <stdlib.h>

void example() {
int a = 10; // Stored in Stack
int *ptr = malloc(4); // Allocated in Heap
}

int globalVar = 20; // Stored in Global Segment

int main() {
example();
return 0;
}

●​ globalVar is in the Global Segment.


●​ a is in the Stack (local variable).
●​ ptr is a pointer in the Stack, but the memory it points to is in the Heap.

Week 2: Program Layout (cont.), Data


Types, and Structs
1. Data Types in C
C has several built-in data types:

Data Type Size Description


(bytes)

char 1 Stores a single character

int 4 Stores whole numbers

float 4 Stores decimal numbers (less precision)

double 8 Stores decimal numbers (more


precision)

void 0 Represents "no type"

Example: Using Data Types


c
CopyEdit
char c = 'A';
int num = 100;
float f = 3.14;
double d = 3.1415926535;
2. Structs (Grouping Multiple Variables)
A struct allows you to group multiple related variables into a single entity.

Example: Using a Struct


c
CopyEdit
#include <stdio.h>

struct Student {
char name[50];
int age;
float gpa;
};

int main() {
struct Student s1 = {"Alice", 20, 3.5};
printf("Name: %s, Age: %d, GPA: %.2f\n", s1.name, s1.age, s1.gpa);
return 0;
}

●​ struct Student groups name, age, and gpa into a single object.

Week 3: Files and Pointers


1. File Handling in C
C provides functions to work with files:

Function Description

fopen() Opens a file


fclose( Closes a file
)

fscanf( Reads formatted input from a file


)

fprintf Writes formatted output to a file


()

fgets() Reads a line of text from a file

fputs() Writes a line of text to a file

Example: Reading a File


c
CopyEdit
#include <stdio.h>

int main() {
FILE *file = fopen("data.txt", "r");
char line[100];

if (file) {
while (fgets(line, sizeof(line), file)) {
printf("%s", line);
}
fclose(file);
} else {
printf("Error opening file!\n");
}

return 0;
}

●​ Opens "data.txt" in read mode ("r").


●​ Reads and prints each line.
●​ Closes the file after reading.

2. Pointers in C
A pointer is a variable that stores the memory address of another variable.

Example: Declaring and Using Pointers


c
CopyEdit
#include <stdio.h>

int main() {
int num = 42;
int *ptr = &num; // Stores address of num

printf("Value: %d\n", *ptr); // Dereferencing: Accesses value at


ptr
return 0;
}

●​ ptr holds the address of num.


●​ *ptr (dereferencing) retrieves the value stored at that address.

Week 4: Pointers, Arrays, and Memory


Allocation
1. Relationship Between Pointers and Arrays
●​ Arrays and pointers are closely linked in C.
●​ The name of an array is a pointer to its first element.

Example: Accessing Array Elements with Pointers


c
CopyEdit
int arr[] = {10, 20, 30};
int *ptr = arr; // Points to arr[0]

printf("%d\n", *(ptr + 1)); // Accesses arr[1]


●​ ptr + 1 moves one position ahead in memory.

2. Dynamic Memory Allocation


Memory is dynamically allocated using:

Function Description

malloc( Allocates memory but doesn’t initialize it


)

calloc( Allocates and initializes memory to 0


)

free() Frees allocated memory

Example: Using malloc()


c
CopyEdit
#include <stdio.h>
#include <stdlib.h>

int main() {
int *arr = malloc(5 * sizeof(int)); // Allocates memory for 5
integers

if (arr) {
for (int i = 0; i < 5; i++) arr[i] = i * 10; // Assign values

for (int i = 0; i < 5; i++) printf("%d ", arr[i]);

free(arr); // Free memory to prevent memory leak


}

return 0;
}

●​ Allocates memory dynamically for 5 integers.


●​ Frees memory after usage to prevent memory leaks.
Week 5: Recursion
1. What is Recursion?
Recursion is when a function calls itself to break a problem into smaller parts.

2. Example: Factorial Using Recursion


c
CopyEdit
#include <stdio.h>

int factorial(int n) {
if (n == 0) return 1; // Base case
return n * factorial(n - 1); // Recursive case
}

int main() {
printf("%d\n", factorial(5)); // Output: 120
return 0;
}

●​ The base case (n == 0) stops infinite recursion.


●​ The recursive case calculates n * factorial(n - 1).

3. When to Use Recursion?


●​ Mathematical computations (factorials, Fibonacci).
●​ Tree-based problems (binary trees, directory structures).
●​ Sorting algorithms (QuickSort, MergeSort).

4. Example: Fibonacci Sequence


c
CopyEdit
int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}

●​ Calls itself twice to compute Fibonacci numbers.

You might also like