Programming in C (GXEST204)
Programming in C (GXEST204)
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.
● Conditional Statements:
○ for Loop:
1
Syntax:
Example:
○ while Loop:
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);
○ 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:
Example
3
○ continue Statement:
■ Skips the current iteration and proceeds to the next iteration of the loop.
Example:
Example:
#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
1. strlen():
Example:
2. strcpy():
5
Example:
3. strcmp():
Example:
4. strcat():
Example:
6
5. strchr():
Example:
6. strstr():
Example:
#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;
}
3.1 Functions
3.2 Structures
○ 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:
3.3 Unions
○ 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];
};
9
○ Structures allocate separate memory for each member, while unions use shared
memory.
○ Unions are useful for memory-efficient data storage.
3.4 Pointers
Example:
Example:
○
● Basic Operations:
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);
}
Example:
fseek(file, 0, SEEK_END);
long size = ftell(file);
printf("File size: %ld bytes\n", size);
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