0% found this document useful (0 votes)
0 views

Module 3

The document provides an overview of C programming concepts including pointers, functions, structures, unions, enumerations, typedef, and file I/O. It includes code examples demonstrating each concept along with their expected outputs. Key topics covered include pointer operations, function call methods, structure manipulation, and basic file handling in C.

Uploaded by

rachuuu321
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Module 3

The document provides an overview of C programming concepts including pointers, functions, structures, unions, enumerations, typedef, and file I/O. It includes code examples demonstrating each concept along with their expected outputs. Key topics covered include pointer operations, function call methods, structure manipulation, and basic file handling in C.

Uploaded by

rachuuu321
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

### 1.

Pointers

#### Pointer Variables and Operators

**Code:**

```c

#include <stdio.h>

int main() {

int a = 10;

int *p; // Pointer variable

p = &a; // Assigning the address of a to p

printf("Value of a: %d\n", a);

printf("Address of a: %p\n", (void*)&a);

printf("Value of p (address of a): %p\n", (void*)p);

printf("Value at address p (value of a): %d\n", *p); // Dereferencing

return 0;

```

**Output:**

```

Value of a: 10

Address of a: 0x7ffeefbff5d4 (this will vary)

Value of p (address of a): 0x7ffeefbff5d4 (this will vary)

Value at address p (value of a): 10

```

#### Pointer Expressions


**Code:**

```c

#include <stdio.h>

int main() {

int arr[] = {10, 20, 30};

int *p = arr; // Points to the first element of the array

printf("First Element: %d\n", *p); // 10

p++; // Now it points to the second element

printf("Second Element: %d\n", *p); // 20

return 0;

```

**Output:**

```

First Element: 10

Second Element: 20

```

#### Pointer Arrays

**Code:**

```c

#include <stdio.h>

int main() {

int a = 10, b = 20, c = 30;

int *arr[3] = {&a, &b, &c}; // Array of pointers


// Accessing values using pointer array

for (int i = 0; i < 3; i++) {

printf("Value: %d\n", *arr[i]);

return 0;

```

**Output:**

```

Value: 10

Value: 20

Value: 30

```

### 2. Functions

#### Call by Value

**Code:**

```c

#include <stdio.h>

void add(int x) {

x++;

printf("Inside add: %d\n", x);

int main() {
int a = 5;

add(a); // a remains unchanged

printf("In main: %d\n", a); // 5

return 0;

```

**Output:**

```

Inside add: 6

In main: 5

```

#### Call by Reference

**Code:**

```c

#include <stdio.h>

void add(int *x) {

(*x)++;

printf("Inside add: %d\n", *x);

int main() {

int a = 5;

add(&a); // Now a changes

printf("In main: %d\n", a); // 6

return 0;

}
```

**Output:**

```

Inside add: 6

In main: 6

```

#### Return Statements

**Code:**

```c

#include <stdio.h>

int add(int x, int y) {

return x + y;

int main() {

int sum = add(5, 10);

printf("Sum: %d\n", sum); // 15

return 0;

```

**Output:**

```

Sum: 15

```

#### Recursion
**Code:**

```c

#include <stdio.h>

int factorial(int n) {

if (n == 0) return 1; // Base case

return n * factorial(n - 1); // Recursive call

int main() {

printf("Factorial of 5: %d\n", factorial(5)); // 120

return 0;

```

**Output:**

```

Factorial of 5: 120

```

### 3. Structures

#### Defining and Accessing Structure Members

**Code:**

```c

#include <stdio.h>

struct Point {

int x;
int y;

};

int main() {

struct Point p1;

p1.x = 5;

p1.y = 10;

printf("Point: (%d, %d)\n", p1.x, p1.y);

return 0;

```

**Output:**

```

Point: (5, 10)

```

#### Arrays of Structures

**Code:**

```c

#include <stdio.h>

struct Student {

char name[50];

int id;

};

int main() {

struct Student s[2] = {{"Alice", 1}, {"Bob", 2}};


for (int i = 0; i < 2; i++) {

printf("Student %s with ID %d\n", s[i].name, s[i].id);

return 0;

```

**Output:**

```

Student Alice with ID 1

Student Bob with ID 2

```

#### Passing Structures to Functions

**Code:**

```c

#include <stdio.h>

struct Point {

int x;

int y;

};

void printPoint(struct Point p) {

printf("Point: (%d, %d)\n", p.x, p.y);

int main() {

struct Point p1 = {5, 10};


printPoint(p1); // Passing by value

return 0;

```

**Output:**

```

Point: (5, 10)

```

### 4. Unions

**Code:**

```c

#include <stdio.h>

union Data {

int i;

float f;

char c;

};

int main() {

union Data data;

data.i = 10;

printf("data.i: %d\n", data.i); // 10

data.f = 5.5;

printf("data.f: %f\n", data.f); // 5.5

// Note: data.i's value is now unpredictable


printf("data.i (after setting f): %d\n", data.i); // Unpredictable (typically 0 for int, but
could be any value)

return 0;

```

**Output:**

```

data.i: 10

data.f: 5.500000

data.i (after setting f): 0 (or unpredictable)

```

### 5. Enumerations

**Code:**

```c

#include <stdio.h>

enum Direction {North, South, East, West};

int main() {

enum Direction dir;

dir = North;

printf("Direction: %d\n", dir); // 0 (default first value)

return 0;

```

**Output:**

```
Direction: 0

```

### 6. typedef

**Code:**

```c

#include <stdio.h>

typedef unsigned long ulong;

int main() {

ulong num = 5000;

printf("Number: %lu\n", num);

return 0;

```

**Output:**

```

Number: 5000

```

### 7. File I/O

#### Opening and Closing a File

**Code:**

```c

#include <stdio.h>
int main() {

FILE *fp;

fp = fopen("test.txt", "w"); // Open for writing

if (fp == NULL) {

printf("Error opening file.\n");

return -1;

fprintf(fp, "Hello, World!\n");

fclose(fp); // Close the file

return 0;

```

**Output:**

(No output on the console, but a file named `test.txt` will be created with the content.)

```plaintext

Hello, World!

```

#### Reading and Writing Text Files

**Code:**

```c

#include <stdio.h>

int main() {

FILE *fp;

char buffer[100];

// Writing to a file
fp = fopen("test.txt", "w");

fprintf(fp, "This is a test.\n");

fclose(fp);

// Reading from a file

fp = fopen("test.txt", "r");

fgets(buffer, sizeof(buffer), fp);

printf("Content of file: %s", buffer);

fclose(fp);

return 0;

```

**Output:**

```

Content of file: This is a test.

```

You might also like