Aadhil Kassim P N
B.Tech - Computer Science
Programming in C (GXEST204)
Module 1: C Fundamentals and Control Statements
1.1 C Fundamentals
● Character Set: Letters (A-Z, a-z), Digits (0-9), Special Characters (+, -, *, etc.),
Whitespace characters.
● Constants:
○ Integer constants (e.g., 123, -45)
○ Floating-point constants (e.g., 3.14, -0.001)
○ Character constants (e.g., 'A', '@')
● Identifiers and Keywords:
○ Identifiers: User-defined names for variables and functions.
○ Keywords: Reserved words (e.g., int, float, return).
● Data Types:
○ Basic types: int, float, char, double
○ Derived types: Arrays, Pointers, Structures
● Operators:
○ Arithmetic (+, -, *, /, %)
○ Relational (==, !=, >, <, >=, <=)
○ Logical (&&, ||, !)
○ Bitwise (&, |, ^, ~, <<, >>)
○ Assignment (=, +=, -=, etc.)
● Input and Output Statements:
○ printf() and scanf() for formatted I/O.
1.2 Control Statements and Loops
● Conditional Statements:
○ if, if-else, nested if
○ switch-case
● Loops:
○ for Loop:
■ Executes a block of code a specific number of times.
1
Syntax:
for (initialization; condition; increment/decrement) {
// code block
}
Example:
for (int i = 0; i < 5; i++) {
printf("%d ", i);
}
○ while Loop:
■ Repeats a block of code while a condition is true.
Syntax:
while (condition) {
// code block
}
Example:
int i = 0;
while (i < 5) {
printf("%d ", i);
i++;
}
○ do-while Loop:
■ Executes the code block at least once, then repeats while the condition is
true.
2
Syntax:
do {
// code block
} while (condition);
Example:
int i = 0;
do {
printf("%d ", i);
i++;
} while (i < 5);
● Key Differences Between for and while Loops:
○ for loop is generally used when the number of iterations is known beforehand.
○ while loop is preferred when the number of iterations is not predetermined and
depends on a condition.
● Control Statements:
○ break Statement:
■ Terminates the loop immediately and transfers control to the statement
following the loop.
Example
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
printf("%d ", i);
}
3
○ continue Statement:
■ Skips the current iteration and proceeds to the next iteration of the loop.
Example:
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
printf("%d ", i);
}
● Nested Loops: Loops inside another loop.
Example:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
printf("i = %d, j = %d\n", i, j);
}
}
Example Problem: Print the Fibonacci sequence up to n terms.
#include <stdio.h>
int main() {
int n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
printf("%d ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
4
Module 2: Arrays and Strings
2.1 Arrays
● Single-Dimensional Arrays:
○ Syntax: data_type array_name[size];
○ Example: int arr[10];
○ Initialization: int arr[3] = {1, 2, 3};
● Two-Dimensional Arrays:
○ Syntax: data_type array_name[rows][columns];
○ Example: int matrix[3][3];
○ Applications: Matrix operations, sorting, searching.
2.2 Strings
● String Declaration and Initialization:
1. char str[20];
2. char str[] = "Hello";
● Common String Functions:
1. strlen():
■ Returns the length of a string excluding the null character (\0).
■ Syntax: int length = strlen(string);
Example:
char str[] = "Hello";
printf("Length of string: %d\n", strlen(str));
2. strcpy():
■ Copies the content of one string into another.
■ Syntax: strcpy(destination, source);
5
Example:
char source[] = "World";
char destination[20];
strcpy(destination, source);
printf("Copied string: %s\n", destination);
3. strcmp():
■ Compares two strings lexicographically.
■ Returns 0 if strings are equal, a positive value if the first string is greater,
or a negative value if the second string is greater.
■ Syntax: int result = strcmp(str1, str2);
Example:
char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal\n");
} else {
printf("Strings are not equal\n");
}
4. strcat():
■ Concatenates (joins) two strings.
■ Syntax: strcat(destination, source);
Example:
char str1[20] = "Hello ";
char str2[] = "World";
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
6
5. strchr():
■ Finds the first occurrence of a character in a string.
■ Syntax: char *ptr = strchr(str, character);
Example:
char str[] = "Programming";
char *ptr = strchr(str, 'g');
printf("First occurrence of 'g': %s\n", ptr);
6. strstr():
■ Finds the first occurrence of a substring within a string.
■ Syntax: char *ptr = strstr(str, substr);
Example:
char str[] = "Hello World";
char *ptr = strstr(str, "World");
printf("Substring found: %s\n", ptr);
Example Problem: Write a program to reverse a string.
#include <stdio.h>
#include <string.h>
int main() {
char str[100], temp;
int i, j;
printf("Enter a string: ");
gets(str);
j = strlen(str) - 1;
for (i = 0; i < j; i++, j--) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
printf("Reversed string: %s\n", str);
7
return 0;
}
Module 3: Functions, Structures, Unions, and Storage Classes
3.1 Functions
● Function Definition and Call:
○ Syntax: return_type function_name(parameters) { body }
○ Example: int sum(int a, int b) { return a + b; }
● Recursion:
○ Functions that call themselves.
○ Example: Calculating factorial of a number.
3.2 Structures
● Definition and Syntax:
○ Structures are user-defined data types that group related variables of different
data types.
Syntax:
struct structure_name {
data_type member1;
data_type member2;
};
Example:
struct Student {
char name[50];
int age;
float marks;
};
Accessing Members:
8
struct Student s1;
s1.age = 20;
printf("Age: %d\n", s1.age);
● Array of Structures:
Example:
struct Student students[3];
students[0].age = 18;
3.3 Unions
● Definition and Syntax:
○ Unions are similar to structures, but all members share the same memory
location.
Syntax:
union union_name {
data_type member1;
data_type member2;
};
Example:
union Data {
int i;
float f;
char str[20];
};
● Difference Between Structures and Unions:
9
○ Structures allocate separate memory for each member, while unions use shared
memory.
○ Unions are useful for memory-efficient data storage.
3.4 Pointers
● Declaration and Initialization:
○ Syntax: data_type *pointer_name;
○ Example: int *ptr;
● Pointer Operations:
○ Pointer arithmetic: Increment, decrement, and accessing array elements.
Example:
int var = 10;
int *ptr = &var;
printf("Value of var: %d\n", *ptr);
● Dynamic Memory Allocation:
○ Functions: malloc(), calloc(), free().
Example:
int *ptr = (int *)malloc(5 * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed\n");
} else {
// Use the memory
}
free(ptr);
○
3.5 File Handling
● Basic Operations:
○ fopen(): Opens a file.
10
○ fclose(): Closes a file.
○ fread(): Reads data from a file.
○ fwrite(): Writes data to a file.
Example:
FILE *file;
file = fopen("example.txt", "w");
if (file == NULL) {
printf("Error opening file\n");
} else {
fprintf(file, "Hello, World!\n");
fclose(file);
}
● File Pointer Manipulation:
○ fseek(): Moves the file pointer to a specified location.
○ ftell(): Returns the current file pointer position.
Example:
fseek(file, 0, SEEK_END);
long size = ftell(file);
printf("File size: %ld bytes\n", size);
Final Exam Tips
11
1. Practice Key Concepts: Focus on dynamic memory, file handling, and pointer arithmetic.
2. Write and Test Code: Hands-on coding will reinforce understanding.
3. Solve Past Papers: Practice with sample questions for time management.
4. Debugging: Develop skills to troubleshoot segmentation faults and logical errors.
By following these structured notes and practicing diligently, you should be well-prepared to
excel in your C Programming exam.
12