C Module 2 & 3 Notes
C Module 2 & 3 Notes
INTRODUCTION
Operators are symbols that perform operations on data (variables/constants).
Expressions combine operands and operators to produce a result.
Example expression:
a+b*c
Here:
operands → a, b, c
operators → +, *
Key point
Expressions are evaluated using precedence and associativity rules.
Example Program
#include <stdio.h>
int main() {
int a = 10, b = 5;
int result = a + b * 2; // multiplication happens first
printf("Result = %d", result);
return 0;
}
Unary Operators
Examples
++a (pre-increment)
a++ (post-increment)
--a (pre-decrement)
-a (negation)
Syntax
operator operand
or
operand operator
Example Program
#include <stdio.h>
int main() {
int x = 5;
printf("%d\n", ++x); // 6
printf("%d\n", x--); // prints 6, then x becomes 5
return 0;
}
Binary Operators
Examples
Arithmetic → + - * / %
Relational → < > <= >= == !=
Logical → && ||
Assignment → = += -=
Syntax
operand1 operator operand2
Example Program
#include <stdio.h>
int main() {
int a = 7, b = 3;
printf("Sum = %d\n", a + b);
printf("Product = %d\n", a * b);
return 0;
}
Ternary Operator
Syntax
condition ? expression1 : expression2
Meaning
If condition is true → expression1 executes
Else → expression2 executes
Example Program
#include <stdio.h>
int main() {
int n = 10;
(n % 2 == 0) ? printf("Even") : printf("Odd");
return 0;
}
CLASSIFICATION BASED ON TYPE OF OPERATION
Arithmetic Operators
Operators
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (remainder)
Important Rules
/ between two integers → integer division
% allowed only for integers
% gives remainder
Syntax
result = operand1 operator operand2;
Example Program
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a %% b = %d\n", a % b);
return 0;
}
Conversion of Expressions
When an expression uses multiple data types, C converts lower-rank types into higher-rank
types automatically.
Type hierarchy
char → int → float → double
Example
int + float → float
float + double → double
Example Program
#include <stdio.h>
int main() {
int a = 5;
float b = 2.5;
float c = a + b; // a converted to float
printf("c = %.2f", c);
return 0;
}
Type Conversion
Two Types
1. Implicit conversion → automatic
2. Explicit conversion (type casting) → forced by programmer
1. Implicit Conversion
Automatically converts lower→higher type.
Example
int a = 10;
float b = a + 2.5; // a becomes 10.0
Example Program
#include <stdio.h>
int main() {
int x = 7, y = 2;
float wrong = x / y;
float correct = (float)x / y;
printf("Wrong = %.2f\n", wrong);
printf("Correct = %.2f\n", correct);
return 0;
}
2. Real Arithmetic
If at least one operand is float → result float.
Example
5 + 2.0 = 7.0
3. Mixed Arithmetic
C converts integer → float.
Example Program
#include <stdio.h>
int main() {
int a = 5;
float b = 2.0;
float c = a + b; // mixed arithmetic
printf("%f", c);
return 0;
}
Precedence of Arithmetic Operators
Order (Highest → Lowest)
1. ()
2. ++ --
3. * / %
4. + -
Example
a+b*c
Multiplication happens first.
Example Program
#include <stdio.h>
int main() {
int a = 2, b = 3, c = 4;
int result = a + b * c;
printf("%d", result); // 14
return 0;
}
Associativity of Operators
When operators have the same precedence, associativity decides the order.
Examples
Operators Associativity
+ - * / % Left → Right
= += -= Right → Left
Example Program
#include <stdio.h>
int main() {
int a = 10;
int b = 20;
int c = 30;
== Equal to
!= Not equal to
Syntax
expression1 operator expression2
Example
a>b
x == y
marks >= 35
Example Program
#include <stdio.h>
int main() {
int a = 10, b = 20;
return 0;
}
LOGICAL OPERATORS
Logical operators combine multiple conditions.
Logical Operators
Operator Meaning
! Logical NOT
🔹 Logical OR (||)
True if any one condition is true.
Example
(temp > 40) || (rain == 1)
🔹 Logical NOT (!)
Reverses truth value.
Example
! (a == b)
Example Program
#include <stdio.h>
int main() {
int age = 20;
int marks = 30;
printf("AND = %d\n", (age > 18 && marks >= 35));
printf("OR = %d\n", (age > 18 || marks >= 35));
printf("NOT = %d\n", !(marks >= 35));
return 0;
}
ASSIGNMENT OPERATORS
Assignment operators assign values to variables.
List of Assignment Operators
Operator Meaning
= Simple assignment
Syntax
variable operator value;
Examples:
a += 5; // same as a = a + 5;
b *= 3; // same as b = b * 3;
Example Program
#include <stdio.h>
int main() {
int a = 10;
a += 5; // now a = 15
a -= 3; // now a = 12
a *= 2; // now a = 24
a /= 4; // now a = 6
printf("Final Value of a = %d", a);
return 0;
}
INTRODUCTION
Decision-making statements allow a program to choose different paths based on conditions.
These statements check conditions using relational and logical operators.
Example decisions:
Is the number positive or negative?
Which number is largest?
Should a menu option run?
C supports the following decision statements:
1. if
2. if-else
3. nested if
4. else-if ladder
5. switch
Syntax
if (condition) {
statements;
}
Example Program – Even/Odd
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n % 2 == 0) {
printf("Even");
}
return 0;
}
If number is odd → no output (one-way selection).
Example Program
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if (a > b) {
printf("Largest = %d", a);
}
return 0;
}
Example Program
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter 3 numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b) {
if (a > c)
printf("Largest = %d", a);
else
printf("Largest = %d", c);
}
else {
if (b > c)
printf("Largest = %d", b);
else
printf("Largest = %d", c);
}
return 0;
}
CHECK WHETHER NUMBER IS POSITIVE OR NEGATIVE
Condition:
n>0
Example Program
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n > 0)
printf("Positive");
return 0;
}
Zero gives no message (one-way selection).
Syntax
if (condition) {
statements;
}
else {
statements;
}
if (n % 2 == 0)
printf("Even");
else
printf("Odd");
return 0;
}
PROGRAM 2 – Find Larger of Two Numbers
#include <stdio.h>
int main() {
int a, b;
scanf("%d %d", &a, &b);
if (a > b)
printf("Larger = %d", a);
else
printf("Larger = %d", b);
return 0;
}
return 0;
}
NESTED IF STATEMENT
Explanation
if inside another if
Used for multi-level decisions.
Syntax
if (condition1) {
if (condition2) {
statements;
}
}
Example – Find largest of 3 numbers (nested if)
ELSE-IF LADDER
Explanation
Used when multiple conditions must be checked.
Syntax
if (condition1)
statements;
else if (condition2)
statements;
else if (condition3)
statements;
else
default statements;
Example – Grade Based on Marks
#include <stdio.h>
int main() {
int marks;
scanf("%d", &marks);
return 0;
}
int main() {
float a, b, c, D, r1, r2;
D = b*b - 4*a*c;
if (D > 0) {
r1 = (-b + sqrt(D)) / (2*a);
r2 = (-b - sqrt(D)) / (2*a);
printf("Real and distinct roots: %.2f, %.2f", r1, r2);
}
else if (D == 0) {
r1 = r2 = -b / (2*a);
printf("Real and equal roots: %.2f", r1);
}
else {
printf("Imaginary roots");
}
return 0;
}
if (choice == 1)
printf("%d", a + b);
else if (choice == 2)
printf("%d", a - b);
else if (choice == 3)
printf("%d", a * b);
else if (choice == 4)
printf("%d", a / b);
else
printf("Invalid choice");
return 0;
}
CHECK TRIANGLE TYPE
Conditions:
Equilateral → a == b == c
Isosceles → any two sides equal
Scalene → all sides different
Program
#include <stdio.h>
int main() {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if (a == b && b == c)
printf("Equilateral");
else if (a == b || b == c || a == c)
printf("Isosceles");
else
printf("Scalene");
return 0;
}
SWITCH STATEMENT
Syntax
switch (expression) {
case value1: statements; break;
case value2: statements; break;
...
default: statements;
}
Rules
Only int/char allowed
break prevents fall-through
default is optional
switch(choice) {
case 1: printf("%d", a + b); break;
case 2: printf("%d", a - b); break;
case 3: printf("%d", a * b); break;
case 4: printf("%d", a / b); break;
default: printf("Invalid choice");
}
return 0;
}
Syntax
goto label;
...
label:
statements;
Example
#include <stdio.h>
int main() {
int i = 1;
start:
printf("%d ", i);
i++;
if (i <= 5)
goto start;
return 0;
}
BREAK STATEMENT
Explanation
Used to exit:
Loop
Switch
Example
for (i = 1; i <= 10; i++) {
if (i == 5)
break;
}
CONTINUE STATEMENT
Explanation
Skips the remaining statements in loop and goes to next iteration.
Example
for (i = 1; i <= 5; i++) {
if (i == 3)
continue;
printf("%d ", i);
}
RETURN STATEMENT
Used to exit a function and optionally return a value.
Syntax
return value;
Example
int add(int a, int b) {
return a + b;
}
REPETITION (LOOPING)
Loops allow executing a block of code multiple times.
C provides:
1. while
2. for
3. do-while
CONCEPT OF A LOOP
while(i <= n) {
sum += i;
i++;
}
scanf("%d", &n);
while(n != 0) {
sum += n;
scanf("%d", &n);
}
return 0;
}
REVERSE A GIVEN NUMBER
Logic:
Extract digit using % 10
Build reversed number
Program
#include <stdio.h>
int main() {
int n, rev = 0, digit;
scanf("%d", &n);
while(n > 0) {
digit = n % 10;
rev = rev * 10 + digit;
n /= 10;
}
temp = n;
while(temp > 0) {
digit = temp % 10;
rev = rev * 10 + digit;
temp /= 10;
}
if(n == rev)
printf("Palindrome");
else
printf("Not Palindrome");
return 0;
}
while(n > 0) {
digit = n % 10;
sum += digit;
n /= 10;
}
int main() {
int bin, dec = 0, i = 0, digit;
scanf("%d", &bin);
while(bin > 0) {
digit = bin % 10;
dec += digit * pow(2, i);
i++;
bin /= 10;
}
#include <stdio.h>
int main() {
int n, bin = 0, place = 1, rem;
scanf("%d", &n);
while(n > 0) {
rem = n % 2;
bin += rem * place;
place *= 2;
n /= 2;
}
Program
#include <stdio.h>
int main() {
int a, b, x, y, r;
scanf("%d %d", &a, &b);
x = a; y = b;
while(b != 0) {
r = a % b;
a = b;
b = r;
}
int gcd = a;
int lcm = (x * y) / gcd;
return 0;
}
DISPLAY ODD NUMBERS WITHIN n
for(i = 1; i <= n; i++) {
if(i % 2 != 0)
printf("%d ", i);
}
scanf("%d", &n);
printf("Odd\tEven\n");
return 0;
}
DISPLAY NUMBERS FROM n DOWN TO 1
for(i = n; i >= 1; i--) {
printf("%d ", i);
}
COMPARISON OF WHILE LOOP AND FOR LOOP
while →
Condition before loop
Used when number of iterations unknown
for →
All 3 parts (init, condition, increment) in one line
Best when iterations are fixed
FACTORIAL OF A NUMBER
Program
int fact = 1;
for(i = 1; i <= n; i++)
fact *= i;
NESTED LOOPS
Used for:
Patterns
Multiplication tables
Matrix operations
Example
for(i = 1; i <= 3; i++) {
for(j = 1; j <= 3; j++) {
printf("%d ", j);
}
printf("\n");
}
DO-WHILE LOOP
Syntax
do {
statements;
} while(condition);
Always executes at least once.
Module 3 Notes
ARRAYS
Index: 0 1 2 3 4
Value: 10 20 30 40 50
Address:100 102 104 106 108 (example)
return 0;
}
CLASSIFICATION OF ARRAYS
There are two types:
1️⃣ Single-dimensional arrays
2️⃣ Two-dimensional arrays (Chapter continues later)
SINGLE-DIMENSIONAL ARRAYS
return 0;
}
printf("%d", age[1]); // 25
return 0;
}
int main(){
int a[] = {1,2,3,4,5};
display(a,5);
}
PROGRAMMING EXAMPLES
Addition of N elements
int sum = 0;
for(i = 0; i < n; i++)
sum += a[i];
Largest of N numbers
Use a loop to track max.
SEARCHING
Two methods.
Program
for(i=0;i<n;i++){
if(a[i] == key){
found = 1;
break;
}
}
BINARY SEARCH
Works only on sorted arrays.
Algorithm:
1. mid = (low + high)/2
2. If key == a[mid] → found
3. If key < a[mid] → search left
4. Else → search right
BUBBLE SORT
Repeatedly compare adjacent values and swap.
TWO-DIMENSIONAL ARRAYS
Matrix form.
Syntax:
int a[3][3];
STRING CONCEPTS
What is a string?
A sequence of characters terminated by '\0'.
Example:
char name[] = "ANU";
Stored as:
'A' 'N' 'U' '\0'
Why strings?
To store text
Used for names, addresses, messages
Key Points:
String length does not include '\0'
String size must include space for '\0'
Initializing Strings
Method 1: Using double quotes (recommended)
char name[] = "Anu";
Compiler automatically adds '\0'.
Method 2: Using character array
char name[4] = {'A','n','u','\0'};
Method 3: Empty initialization
char s[50] = "";
Program
#include <stdio.h>
int main() {
char name[20];
scanf("%s", name);
printf("Name = %s", name);
return 0;
}
Program:
#include <stdio.h>
#include <string.h>
int main() {
char s[50];
gets(s);
strrev(s);
printf("%s", s);
return 0;
}
10.6.2 Palindrome string
"madam" → palindrome
Program:
#include <stdio.h>
#include <string.h>
int main() {
char s[50];
gets(s);
char temp[50];
strcpy(temp, s);
strrev(temp);
if(strcmp(s, temp) == 0)
printf("Palindrome");
else
printf("Not Palindrome");
}
⭐ If you want:
✔ A PDF of Chapter 9 & 10
✔ A combined textbook PDF (Chapter 4, 6, 7, 9, 10)
✔ Summary notes
✔ Flowcharts
✔ Question bank
✔ Programs-only booklet
Tell me what you want next.
You said:
programming examples for 10.6 and 10.7 nd for all subtopics
ChatGPT said:
Thought for 5s
Good — below are practical, ready-to-compile C programs covering every subtopic in
10.6 and 10.7 from your book.
Each item includes a short explanation, then a complete program you can paste into a C
compiler (gcc). I avoided unsafe gets() and non-standard strrev()/itoa() by implementing
equivalents.
int main() {
char s[200];
printf("Enter a string: ");
if (!fgets(s, sizeof s, stdin)) return 0;
s[strcspn(s, "\n")] = '\0'; // remove newline
reverse_str(s);
printf("Reversed: %s\n", s);
return 0;
}
int main() {
char s[300], tmp[300];
printf("Enter string: ");
if(!fgets(s,sizeof s,stdin)) return 0;
s[strcspn(s,"\n")] = '\0';
strcpy(tmp, s);
normalize(tmp); // optional normalization
char rev[300];
strcpy(rev, tmp);
reverse_str(rev);
if(strcmp(tmp, rev) == 0) printf("Palindrome\n");
else printf("Not a palindrome\n");
return 0;
}
int main() {
char line[1024];
printf("Enter a line: ");
if (!fgets(line, sizeof line, stdin)) return 0;
size_t len = strcspn(line, "\n"); // length without newline
printf("You entered (%zu chars): %.*s\n", len, (int)len, line);
return 0;
}
int main() {
char line[1024], longest[1024];
longest[0] = '\0';
printf("Enter lines (Ctrl+D to end):\n");
while(fgets(line, sizeof line, stdin)) {
if (strlen(line) > strlen(longest)) strcpy(longest, line);
}
if (longest[0]) {
longest[strcspn(longest, "\n")] = '\0';
printf("Longest line: %s\nLength: %zu\n", longest, strlen(longest));
} else {
printf("No input\n");
}
return 0;
}
int main() {
char s[200];
printf("Enter string: ");
if(!fgets(s,sizeof s,stdin)) return 0;
s[strcspn(s,"\n")] = '\0';
char ch;
printf("Character to remove: ");
if(scanf(" %c",&ch)!=1) return 0;
squeeze(s, ch);
printf("Result: %s\n", s);
return 0;
}
int main() {
char s[300];
printf("Enter string with spaces/tabs: ");
if(!fgets(s,sizeof s,stdin)) return 0;
s[strcspn(s,"\n")] = '\0';
trim(s);
printf("Trimmed: '%s'\n", s);
return 0;
}
int main() {
long long n;
printf("Enter integer: ");
if (scanf("%lld",&n)!=1) return 0;
if(n<0) n = -n;
int count = 0;
if(n==0) { printf("Number of '1's: 0\n"); return 0; }
while(n>0) {
if(n%10 == 1) count++;
n /= 10;
}
printf("Number of '1' digits: %d\n", count);
return 0;
}
int main() {
char s[500];
printf("Enter text: ");
if(!fgets(s,sizeof s,stdin)) return 0;
int vowels=0, cons=0;
for (int i=0; s[i]; i++) {
char c = tolower((unsigned char)s[i]);
if (isalpha((unsigned char)c)) {
if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u') vowels++;
else cons++;
}
}
printf("Vowels: %d, Consonants: %d\n", vowels, cons);
return 0;
}
int main() {
char s[300];
printf("Enter text: ");
if(!fgets(s,sizeof s,stdin)) return 0;
for(int i=0; s[i]; ++i) {
if (islower((unsigned char)s[i])) s[i] = toupper((unsigned char)s[i]);
else if (isupper((unsigned char)s[i])) s[i] = tolower((unsigned char)s[i]);
}
printf("Converted: %s", s);
return 0;
}
int main() {
char s[500];
printf("Enter text: ");
if(!fgets(s,sizeof s,stdin)) return 0;
int digits=0, spaces=0, others=0;
for(int i=0; s[i]; ++i) {
if (isdigit((unsigned char)s[i])) digits++;
else if (isspace((unsigned char)s[i])) spaces++;
else if (isalpha((unsigned char)s[i])) ; // ignore letter
else others++;
}
printf("Digits: %d, Spaces: %d, Others(punct): %d\n", digits, spaces, others);
return 0;
}
int main() {
char s[100];
printf("Enter number as string: ");
if(!fgets(s,sizeof s,stdin)) return 0;
s[strcspn(s,"\n")] = '\0';
int val = atoi(s); // note: atoi doesn't detect errors robustly
printf("Integer value = %d\n", val);
return 0;
}
If you want robust conversion, use strtol.
int main() {
int num;
char s[50];
printf("Enter integer: ");
if(scanf("%d",&num)!=1) return 0;
my_itoa(num, s);
printf("String: %s\n", s);
return 0;
}
int main() {
char s[200], ch;
printf("Enter string: ");
if(!fgets(s,sizeof s,stdin)) return 0;
s[strcspn(s,"\n")] = '\0';
printf("Enter char to count: ");
if(scanf(" %c",&ch)!=1) return 0;
printf("Count = %d\n", countchar(s, ch));
return 0;
}
int main() {
char s[200], ch;
printf("Enter string: ");
if(!fgets(s,sizeof s,stdin)) return 0;
s[strcspn(s,"\n")] = '\0';
printf("Char to find: ");
if(scanf(" %c",&ch)!=1) return 0;
int pos = findchar(s, ch);
if (pos >= 0) printf("Found at index %d (0-based)\n", pos);
else printf("Not found\n");
return 0;
}
int main() {
char s[200];
printf("Enter string: ");
if(!fgets(s,sizeof s,stdin)) return 0;
s[strcspn(s,"\n")] = '\0';
char ch;
printf("Char to append: ");
if(scanf(" %c",&ch)!=1) return 0;
insertend(s, ch, sizeof s);
printf("After append: %s\n", s);
return 0;
}
int main() {
char s[300];
printf("Enter string: ");
if(!fgets(s,sizeof s,stdin)) return 0;
s[strcspn(s,"\n")] = '\0';
int pos; printf("Position to split at (0-based): "); if(scanf("%d",&pos)!=1) return 0;
char *a,*b;
split_at(s,pos,&a,&b);
printf("Part1: '%s'\nPart2: '%s'\n", a, b);
free(a); free(b);
return 0;
}
10.7.5 — Search for a string in an array of strings
Return index of match or -1.
/* search_string_array.c */
#include <stdio.h>
#include <string.h>
int main() {
char arr[5][30] = {"apple","banana","cat","dog","elephant"};
char key[30];
printf("Enter key to search: ");
if(!fgets(key, sizeof key, stdin)) return 0;
key[strcspn(key,"\n")] = '\0';
int idx = find_in_array(arr, 5, key);
if (idx >=0) printf("Found at index %d\n", idx);
else printf("Not found\n");
return 0;
}
int main() {
char name[100];
printf("Enter name: ");
if(!fgets(name,sizeof name,stdin)) return 0;
name[strcspn(name,"\n")] = '\0';
char *g = make_greeting(name);
if (g) {
printf("%s\n", g);
free(g);
}
return 0;
}
int main() {
char s[100];
printf("Enter float as string: ");
if (!fgets(s, sizeof s, stdin)) return 0;
s[strcspn(s,"\n")] = '\0';
double x = atof(s); // weak error checking
printf("Value = %f\n", x);
return 0;
}