Definition: Keywords are reserved words in C that have a predefined meaning to the compiler
. They cannot be used as identifiers (like variable or function names).Importance: Keywords form the basic syntax and structure of the C language, allowing the compiler to understand commands such as loops (for), data types (int), and control flows (if/else)
.Definition: An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations on data
.Types of Operators:
(i) for loop format:
for (initialization; condition; increment/decrement) {
// statements
}
(ii) GOTO format:
goto label; ... label: // statements
Decision making statements allow a program to execute specific blocks of code based on whether a condition is true or false
.The if-else Statement: This is the most common decision statement
.#include <stdio.h>
int main() {
int age = 20;
if (age >= 18) {
printf("Eligible to vote.");
} else {
printf("Not eligible.");
}
return 0;
}
History: C was developed by Dennis Ritchie at AT&T Bell Labs in 1972
. It was designed to build the Unix operating system and evolved from earlier languages like B and BCPL.Simple Interest Program:
#include <stdio.h>
int main() {
float p, r, t, si;
p = 1000; r = 5; t = 2; // Example values
si = (p * r * t) / 100;
printf("Simple Interest = %f", si);
return 0;
}
float arr[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
A character array is declared using the char data type followed by the array name and size in square brackets
char name[10] = "Hello";
Recursion: A function that calls itself to solve a smaller version of the same problem is known as a recursive function
.#include <stdio.h>
int factorial(int n) {
if (n == 0) return 1;
else return (n * factorial(n - 1));
}
int main() {
printf("Factorial of 5: %d", factorial(5));
return 0;
}
| Call by Value | Call by Reference |
|---|---|
| Passes a copy of the actual parameter. | Passes the address of the actual parameter. |
| Changes inside function do not affect original values. | Changes inside function affect the original values. |
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 10, y = 20;
swap(&x, &y);
printf("x=%d, y=%d", x, y);
return 0;
}
| Structure | Union |
|---|---|
| Every member has its own memory location. | All members share the same memory location. |
| Total size is the sum of sizes of all members. | Total size is the size of the largest member. |
#include <stdio.h>
struct Employee {
int id;
char name[30];
float salary;
};
int main() {
struct Employee e = {101, "Sujan Roy", 50000.0};
printf("ID: %d, Name: %s, Salary: %.2f", e.id, e.name, e.salary);
return 0;
}
A file is a place on a disk where a group of related data is stored
. In C, there are primarily two types: Text files and Binary files.Files are opened using the fopen() function
Modes:
*) correctly& in scanf() for non-array variables.Key Formulas/Syntax:
Simple Interest = (P * R * T) / 100
Pointer Syntax:data_type *pointer_name;
Prepared by Knowlet for Academic Excellence.