Author | Published |
---|---|
Gokulnath G | 04-01-2025 |
Character Set – Tokens – Identifiers - Keywords - Constants – Variables. Operators - Arithmetic Operators - Relational Operators - Logical Operators -Assignment Operators Increment and Decrement Operators - Conditional Operator - Bitwise Operators Special Operators Operator Precedence and Associativity - Expressions and their evaluation. Datatypes - Statements – Formatted I/O statements – Comments Basic Structure of a C Program. Conditional Statements – if statements - switch -Iterative Statements – for – while - do- while.
The set of valid characters used in a programming language, including letters, digits, special symbols, and white spaces.
The smallest unit in a program, such as keywords, identifiers, constants, operators, and special symbols.
Names used to identify variables, functions, or other user-defined elements in a program.
Predefined reserved words with special meaning in C, such as int
, return
, if
, etc.
Fixed values in a program that do not change during execution, such as numbers, characters, and strings.
Memory locations used to store data that can be modified during program execution.
+
, -
, *
, /
, %
).<
, >
, <=
, >=
, ==
, !=
).&&
, ||
, !
).=
, +=
, -=
, etc.).++
, --
).? :
).&
, |
, ^
, ~
, <<
, >>
).sizeof
, &
(address), and *
(pointer dereference).Define the order in which operators are evaluated in an expression.
Combine variables, constants, and operators to compute values.
Define the type of data a variable can hold, such as int
, float
, char
, double
.
Instructions in a program that perform specific actions. Types include expression statements, conditional statements, and iterative statements.
Functions like printf
and scanf
for formatted input and output in C.
Annotations in code to improve readability, ignored during execution:
//
/* ... */
#include
).int main()
).{ }
.return 0;
).Control flow based on conditions:
Used for loops to repeat blocks of code:
Need for functions - Library Functions – User-defined Functions - Elements of User-defined Functions - Definition of Functions - Return Values and their Types - Function Calls - Function Declaration - Category of Functions - No Arguments and No Return Values - Arguments but no return values - Arguments with return values - No Arguments but Returns a Value – Recursion – Scope - Visibility and Lifetime of variables - Nested Functions
Functions help in modular programming by breaking a program into smaller, manageable parts. They improve code readability, reusability, and maintainability.
These are pre-defined functions available in standard C libraries, such as printf()
, scanf()
, sqrt()
, and strlen()
.
Functions created by the user to perform specific tasks. They are defined using the return_type function_name(parameters) { }
syntax.
Functions can return values of different data types, such as int
, float
, or void
(no return value).
A function calling itself to solve a problem in a repetitive manner. Example:
int factorial(int n) { if (n == 0) return 1; return n * factorial(n - 1); }
A function defined within another function. In C, true nested functions are not allowed, but function calls within functions are commonly used.
One-dimensional Arrays: Declaration of One-dimensional Arrays - Initialization of One dimensional Arrays - Example programs - Bubble sort - Linear search - Binary search. Two-dimensional Arrays: Declaration of Two-dimensional Arrays - Initialization of Two dimensional Arrays - Example programs - Matrix Multiplication - Transpose of a matrix. Strings in C: character arrays and string manipulation functions - Example Programs (with and without using built-in string functions) - Passing arrays and strings to functions
Arrays store multiple elements of the same type in contiguous memory locations. A one-dimensional array is declared as:
int arr[10];
Arrays can be initialized during declaration:
int arr[5] = {1, 2, 3, 4, 5};
void bubbleSort(int arr[], int n) { for (int i = 0; i < n - 1; i++) for (int j = 0; j < n - i - 1; j++) if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } }
int linearSearch(int arr[], int n, int key) { for (int i = 0; i < n; i++) if (arr[i] == key) return i; return -1; }
int binarySearch(int arr[], int left, int right, int key) { while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == key) return mid; if (arr[mid] < key) left = mid + 1; else right = mid - 1; } return -1; }
A two-dimensional array is declared as:
int matrix[3][3];
They can be initialized during declaration:
int matrix[2][2] = {{1, 2}, {3, 4}};
void multiplyMatrices(int a[][2], int b[][2], int result[][2], int size) { for (int i = 0; i < size; i++) for (int j = 0; j < size; j++) { result[i][j] = 0; for (int k = 0; k < size; k++) result[i][j] += a[i][k] * b[k][j]; } }
void transpose(int matrix[][3], int transposed[][3], int size) { for (int i = 0; i < size; i++) for (int j = 0; j < size; j++) transposed[j][i] = matrix[i][j]; }
Strings are character arrays that end with a null character (\0
).
char str[] = "Hello";
int stringLength(char str[]) { int length = 0; while (str[length] != '\0') length++; return length; }
void printString(char str[]) { printf("%s", str); }
Structures: Introduction -Defining a structure - declaring structure variables - Accessing structure members - Structure initialization - Array of structures - Nested Structure. Introduction to pointers and their significance - Pointer arithmetic and pointer operations. Dynamic memory allocation (malloc, calloc, realloc, free) - Pointer and array relationship - Pointers to functions and their applications - Call by value and call by reference. File input/output operations (fopen,fclose,fread,fwrite) - Error handling with file operations.
A structure is a user-defined data type that allows grouping related data of different types.
struct Student { char name[50]; int age; float marks; };
struct Student s1;
s1.age = 20; printf("Age: %d", s1.age);
struct Student s2 = {"John", 21, 85.5};
struct Student students[3];
struct Address { char city[50]; int zip; }; struct Student { char name[50]; struct Address addr; };
Pointers store memory addresses, enabling efficient data manipulation and dynamic memory allocation.
int *ptr;
ptr++; ptr--;
Allocating memory dynamically at runtime using functions from <stdlib.h>
.
malloc()
: Allocates memory but does not initialize.calloc()
: Allocates memory and initializes to zero.realloc()
: Resizes previously allocated memory.free()
: Deallocates memory.int *arr = (int*)malloc(5 * sizeof(int)); free(arr);
Pointers can be used to access array elements efficiently.
int arr[] = {1, 2, 3}; int *ptr = arr; // Points to first element printf("%d", *(ptr + 1)); // Access second element
Pointers can store addresses of functions, allowing function calls through pointers.
void hello() { printf("Hello, World!"); } void (*funcPtr)() = hello; funcPtr(); // Calls hello()
Functions can receive arguments either by value (copy) or by reference (address).
void increment(int x) { x++; }
void increment(int *x) { (*x)++; }
Files in C are handled using the FILE
pointer and functions from <stdio.h>
.
FILE *fp = fopen("data.txt", "r"); fclose(fp);
// Writing to a file FILE *fp = fopen("data.txt", "w"); fprintf(fp, "Hello, File!"); fclose(fp); // Reading from a file char buffer[100]; fp = fopen("data.txt", "r"); fgets(buffer, 100, fp); printf("%s", buffer); fclose(fp);
Checking if a file operation was successful to prevent errors.
FILE *fp = fopen("data.txt", "r"); if (fp == NULL) { printf("Error opening file!"); } else { fclose(fp); }