Lab1 C Programs
Lab1 C Programs
Pointers
DS2040, Satyajit Das
January 9, 2025
1 Introduction
This document contains 15 C programs designed to help you understand fundamental C
programming concepts, basic data structures, and pointers. Each program is accompanied
by:
• A short description
• The source code
• Instructions for compilation and execution
• Key concepts
2 Programs
2.1 1. Hello World
Purpose: The classic introductory program—verifies your environment is set up and
shows basic C syntax.
Listing 1: hello.c
1 #include <stdio.h>
2
3 int main(void) {
4 printf("Hello, World!\n");
5 return 0;
6 }
Instructions:
• Compile: gcc hello.c -o hello
• Run: ./hello
Key Concepts:
• Basic structure of a C program (#include, main(), return 0;)
• Printing output to the console.
1
2.2 2. Simple Input and Output
Purpose: Demonstrates reading integers from the user and printing results.
Listing 2: simple_io.c
1 #include <stdio.h>
2
3 int main(void) {
4 int number;
5 printf("Enter an integer: ");
6 scanf("%d", &number);
7 printf("You entered: %d\n", number);
8 return 0;
9 }
Instructions:
• Compile: gcc simple_io.c -o simple_io
• Run: ./simple_io
Key Concepts:
• Using scanf() and printf().
3 int main(void) {
4 int i, sum = 0;
5
• Run: ./control_flow
2
Key Concepts:
3 #define SIZE 5
4
5 int main(void) {
6 int arr[SIZE] = {1, 2, 3, 4, 5};
7 int sum = 0;
8
• Run: ./array_sum
Key Concepts:
• Accumulating values.
3 int main(void) {
4 int x = 10;
5 int *ptr = &x; // pointer to x
6
3
8 printf("Address of x: %p\n", (void*)&x);
9 printf("Value of ptr: %p\n", (void*)ptr);
10 printf("Value pointed by ptr: %d\n", *ptr);
11
12 return 0;
13 }
Instructions:
• Run: ./pointer_basics
Key Concepts:
• Dereferencing (*ptr).
3 int main(void) {
4 int arr[3] = {10, 20, 30};
5 int *p = arr; // points to arr[0]
6
13 return 0;
14 }
Instructions:
• Run: ./pointer_arithmetic
Key Concepts:
4
2.7 7. Swapping Values with Pointers
Purpose: Demonstrates using pointers in function calls to swap two variables.
Listing 7: swap.c
1 #include <stdio.h>
2
9 int main(void) {
10 int x = 5, y = 10;
11 printf("Before swap: x = %d, y = %d\n", x, y);
12 swap(&x, &y);
13 printf("After swap: x = %d, y = %d\n", x, y);
14 return 0;
15 }
Instructions:
• Run: ./swap
Key Concepts:
4 int main(void) {
5 int n;
6 printf("Enter the number of elements: ");
7 scanf("%d", &n);
8
15 // Store values
5
16 for (int i = 0; i < n; i++) {
17 arr[i] = i + 1;
18 }
19
20 // Print values
21 printf("Allocated array elements:\n");
22 for (int i = 0; i < n; i++) {
23 printf("%d ", arr[i]);
24 }
25 printf("\n");
26
• Run: ./dynamic_allocation
Key Concepts:
• malloc() for dynamic memory allocation.
4 struct Student {
5 char name[50];
6 int age;
7 };
8
9 int main(void) {
10 struct Student s1;
11
12 strcpy(s1.name, "Alice");
13 s1.age = 20;
14
17 return 0;
18 }
Instructions:
6
• Compile: gcc structure_basics.c -o structure_basics
• Run: ./structure_basics
Key Concepts:
• Declaring a structure.
4 struct Node {
5 int data;
6 struct Node *next;
7 };
8
25 int main(void) {
26 struct Node *head = NULL;
27
28 insertAtHead(&head, 30);
29 insertAtHead(&head, 20);
30 insertAtHead(&head, 10);
31
32 printList(head);
33 return 0;
34 }
Instructions:
7
• Compile: gcc linked_list.c -o linked_list
• Run: ./linked_list
Key Concepts:
4 #define MAX_SIZE 5
5
6 typedef struct {
7 int top;
8 int arr[MAX_SIZE];
9 } Stack;
10
8
36 return s->arr[(s->top)--];
37 }
38
39 int main(void) {
40 Stack myStack;
41 initStack(&myStack);
42
43 push(&myStack, 10);
44 push(&myStack, 20);
45 push(&myStack, 30);
46
50 return 0;
51 }
Instructions:
• Run: ./stack_array
Key Concepts:
4 #define MAX_SIZE 5
5
6 typedef struct {
7 int front, rear;
8 int arr[MAX_SIZE];
9 } Queue;
10
9
18 }
19
51 int main(void) {
52 Queue myQueue;
53 initQueue(&myQueue);
54
55 enqueue(&myQueue, 1);
56 enqueue(&myQueue, 2);
57 enqueue(&myQueue, 3);
58
62 return 0;
63 }
Instructions:
• Run: ./queue_array
10
Key Concepts:
16 int main(void) {
17 int arr[] = {64, 34, 25, 12, 22, 11, 90};
18 int n = sizeof(arr) / sizeof(arr[0]);
19
20 printf("Original array:\n");
21 for(int i = 0; i < n; i++) {
22 printf("%d ", arr[i]);
23 }
24 printf("\n");
25
26 bubbleSort(arr, n);
27
28 printf("Sorted array:\n");
29 for(int i = 0; i < n; i++) {
30 printf("%d ", arr[i]);
31 }
32 printf("\n");
33
34 return 0;
35 }
Instructions:
11
• Run: ./bubble_sort
Key Concepts:
10 if (arr[mid] == target) {
11 return mid;
12 } else if (arr[mid] < target) {
13 low = mid + 1;
14 } else {
15 high = mid - 1;
16 }
17 }
18 return -1;
19 }
20
21 int main(void) {
22 int arr[] = {2, 4, 6, 8, 10, 12};
23 int size = sizeof(arr) / sizeof(arr[0]);
24 int target = 8;
25
33 return 0;
34 }
Instructions:
• Run: ./binary_search
12
Key Concepts:
4 int main(void) {
5 FILE *fp = fopen("output.txt", "w");
6 if (fp == NULL) {
7 printf("Error opening file for writing.\n");
8 return 1;
9 }
10 fprintf(fp, "Hello, file!\n");
11 fclose(fp);
12
13 fp = fopen("output.txt", "r");
14 if (fp == NULL) {
15 printf("Error opening file for reading.\n");
16 return 1;
17 }
18
19 char buffer[100];
20 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
21 printf("%s", buffer);
22 }
23 fclose(fp);
24
25 return 0;
26 }
Instructions:
2. Run: ./file_io
3. This will write "Hello, file!" to output.txt and then read it back.
Key Concepts:
13
3 Tips for Running and Understanding the Programs
• Compilation: Use gcc filename.c -o outputname on Linux or macOS. On
Windows, the command is similar, though you may end up with an .exe file.
• Common Pitfalls:
• Further Exploration:
14