C Programming for Problem Solving – Detailed Notes
1. Basic Structure of a C Program
A C program is divided into different sections:
1. Documentation Section
- Contains comments.
Example:
// This is a comment
2. Preprocessor Section
- Includes header files.
Example:
#include
3. Global Declaration Section
- Declares global variables and functions.
4. main() Function
- Execution starts from main().
5. User Defined Functions
- Additional functions created by programmer.
Example Program:
#include<stdio.h>
int main()
{
printf("Hello World");
return 0;
}
2. Data Types in C
Data types specify the type of data stored in variables.
Basic Data Types:
- int : Integer values
- float : Decimal values
- char : Single character
- double : Large decimal values
Example:
int a = 10;
float b = 5.5;
char ch = 'A';
Type Modifiers:
- short
- long
- signed
- unsigned
3. Operators in C
Operators perform operations on variables.
1. Arithmetic Operators
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
2. Relational Operators
==, !=, >, <, >=, <=
3. Logical Operators
&&, ||, !
4. Assignment Operators
=, +=, -=, *=
5. Increment/Decrement
++, --
4. Conditional Statements
Conditional statements control program flow.
1. if Statement
Syntax:
if(condition)
{
statements;
}
2. if-else Statement
Syntax:
if(condition)
{
statements;
}
else
{
statements;
}
3. Nested if
4. switch Statement
Used for multiple choices.
Example:
#include<stdio.h>
int main()
{
int n = 2;
switch(n)
{
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
default:
printf("Invalid");
}
return 0;
}
5. Loops in C
Loops are used for repeated execution.
1. for Loop
Used when number of iterations is known.
2. while Loop
Condition checked before execution.
3. do-while Loop
Condition checked after execution.
Example of for loop:
#include<stdio.h>
int main()
{
int i;
for(i=1; i<=5; i++)
{
printf("%d ", i);
}
return 0;
}
6. Arrays in C
Array stores multiple values of same data type.
Syntax:
datatype arrayname[size];
Example:
int a[5] = {1,2,3,4,5};
Types of Arrays:
1. One Dimensional Array
2. Two Dimensional Array
Advantages:
- Easy data storage
- Fast access
Disadvantages:
- Fixed size
Example:
#include<stdio.h>
int main()
{
int a[3] = {10,20,30};
printf("%d", a[1]);
return 0;
}
7. Strings in C
String is an array of characters ending with '\0'.
Functions used with strings:
- strlen()
- strcpy()
- strcat()
- strcmp()
Header file:
#include
Example:
#include<stdio.h>
#include<string.h>
int main()
{
char str[20] = "Hello";
printf("%s", str);
return 0;
}
8. Functions in C
Function is a block of code performing a task.
Advantages:
- Code reusability
- Modularity
- Easy debugging
Types:
1. Library Functions
2. User Defined Functions
Function Syntax:
returntype functionname(parameters)
{
statements;
}
Example:
#include<stdio.h>
int add(int a, int b)
{
return a+b;
}
int main()
{
int result;
result = add(5,3);
printf("%d", result);
return 0;
}
9. Searching Algorithms
Searching means finding an element.
Linear Search:
- Checks each element one by one.
- Simple but slower.
Algorithm:
1. Start from first element.
2. Compare with key.
3. Continue until found.
Time Complexity:
O(n)
Linear Search Program:
#include<stdio.h>
int main()
{
int a[5]={1,2,3,4,5};
int i,key=4;
for(i=0;i<5;i++)
{
if(a[i]==key)
{
printf("Found");
break;
}
}
return 0;
}
10. Sorting Algorithms
Sorting arranges data in order.
Bubble Sort:
- Adjacent elements are compared.
- Largest element moves to end.
Steps:
1. Compare adjacent elements.
2. Swap if needed.
3. Repeat process.
Time Complexity:
O(n²)
Bubble Sort Program:
#include<stdio.h>
int main()
{
int a[5]={5,4,3,2,1};
int i,j,temp;
for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<5;i++)
{
printf("%d ",a[i]);
}
return 0;
}
11. Important Viva and MCQ Questions
1. What is the use of #include?
2. Difference between while and do-while.
3. What is an array?
4. Define string.
5. Explain function in C.
6. What is bubble sort?
7. Difference between linear search and binary search.
8. What is the use of break statement?
9. Define operator.
10. Explain switch statement.