Loading...
Loading...
C is a general-purpose, procedural programming language developed by Dennis Ritchie at Bell Labs in 1972. It is called the "mother of all languages" because most modern languages (C++, Java, Python) are influenced by C.
Why C?
Structure of a C Program:
#include <stdio.h> // Header file
int main() { // Entry point
printf("Hello, World!\n");
return 0;
}
Basic Data Types:
| Type | Size | Range | Example | |---|---|---|---| | int | 4 bytes | -2,147,483,648 to 2,147,483,647 | int age = 20; | | float | 4 bytes | ~6 decimal digits | float pi = 3.14; | | double | 8 bytes | ~15 decimal digits | double x = 3.14159; | | char | 1 byte | -128 to 127 | char grade = 'A'; |
Format Specifiers: %d (int), %f (float), %lf (double), %c (char), %s (string)
Variable Declaration Rules:
| Category | Operators | |---|---| | Arithmetic | + - * / % | | Relational | == != > < >= <= | | Logical | && (AND), || (OR), ! (NOT) | | Assignment | = += -= *= /= | | Bitwise | & | ^ ~ << >> | | Increment/Decrement | ++ -- |
Operator Precedence (High to Low): (), ++ --, * / %, + -, relational, logical, assignment
if (marks >= 60) {
printf("Pass");
} else if (marks >= 40) {
printf("Compartment");
} else {
printf("Fail");
}
switch(day) {
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
default: printf("Invalid");
}
// for loop
for (int i = 0; i < 5; i++) { printf("%d ", i); }
// while loop
int i = 0;
while (i < 5) { printf("%d ", i); i++; }
// do-while (executes at least once)
do { printf("%d ", i); i++; } while (i < 5);
break — exits the loop immediately continue — skips current iteration, goes to next
A function is a reusable block of code that performs a specific task.
// Function declaration (prototype)
int add(int a, int b);
// Function definition
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 3); // Function call
printf("%d", result); // Output: 8
return 0;
}
Types of Function Arguments:
Recursion: Function calling itself.
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
// factorial(5) = 5*4*3*2*1 = 120
An array stores multiple values of the same type.
int marks[5] = {85, 90, 78, 92, 88};
// Access: marks[0]=85, marks[4]=88
// 2D Array (Matrix)
int matrix[3][3] = {1,2,3},{4,5,6},{7,8,9};
String (char array):
char name[20] = "WohoTech";
printf("%s", name); // Output: WohoTech
printf("%d", strlen(name)); // Output: 8
A pointer stores the memory address of another variable.
int x = 10;
int *p = &x; // p holds address of x
printf("%d", x); // 10 (value)
printf("%p", p); // 0x7fff... (address)
printf("%d", *p); // 10 (value at address — dereferencing)
*p = 20; // Changes x to 20 through pointer
Pointer Arithmetic:
int arr[] = {10, 20, 30};
int *p = arr;
printf("%d", *p); // 10
printf("%d", *(p+1)); // 20
printf("%d", *(p+2)); // 30
A structure groups different data types under one name.
struct Student {
char name[50];
int rollno;
float cgpa;
};
struct Student s1;
strcpy(s1.name, "Rahul");
s1.rollno = 101;
s1.cgpa = 8.5;
printf("%s scored %.1f", s1.name, s1.cgpa);
typedef — creates alias for structure:
typedef struct { char name[50]; int age; } Person;
Person p1;
Q1 (2023): Write a C program to find factorial using recursion.
#include <stdio.h>
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n-1);
}
int main() {
int n;
printf("Enter n: "); scanf("%d", &n);
printf("Factorial = %d", factorial(n));
return 0;
}
Q2 (2023): What is the output?
int x = 5;
printf("%d %d", x++, ++x);
Answer: Output depends on compiler evaluation order. Post-increment (x++) uses current value then increments; pre-increment (++x) increments first.
Q3 (2022): Difference between array and pointer. Array is a fixed-size collection; pointer is a variable storing an address. Array name acts as a constant pointer to its first element, but you cannot reassign an array name.
Q4 (2022): Write program to reverse a string without library function.
void reverse(char str[]) {
int n = strlen(str);
for (int i = 0; i < n/2; i++) {
char temp = str[i];
str[i] = str[n-1-i];
str[n-1-i] = temp;
}
}
Complete C programming notes for B.Tech CS Semester 1 — variables, data types, control flow, functions, arrays, pointers, and structures with solved PYQs.
42 pages · 2.1 MB · Updated 2026-03-11
C teaches memory management, pointers, and low-level thinking which makes learning any other language easier.
while checks condition before executing. do-while executes at least once, then checks condition.
A pointer is a variable that stores the memory address of another variable. Declared with *. Example: int *p = &x;
Engineering Mathematics 1 — Calculus, Matrices, Differential Equations
Engineering Mathematics 1
Digital Logic Design — Boolean Algebra, Logic Gates, Combinational Circuits
Digital Logic Design
DBMS Complete Notes — B.Tech CS Sem 4
Database Management Systems
Compiler Design — Complete Notes CS Sem 6
Compiler Design
Machine Learning Complete Notes — B.Tech CS Sem 6
Machine Learning
Your feedback helps us improve notes and tutorials.