Chapter 1: Review of C Programming Concepts
Basic Input/Output Functions
Examples of basic programs demonstrating input and output.
WAP to add any two numbers 3 & 5
/*WAP to add any two numbers 3 & 5 */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum;
a=3;
b=5;
sum=a+b;
printf("The sum is %d",sum);
getch();
}
WAP to input any two numbers and add them
/* WAP to input any two numbers and add them */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum;
printf("Enter first number: ");
scanf("%d",&a);
printf("Enter second number: ");
scanf("%d",&b);
sum=a+b;
printf("The sum is %d",sum);
getch();
}
WAP to find simple interest
/*WAP to find simple interest*/
#include<stdio.h>
#include<conio.h>
void main()
{
float p,t,r,si;
printf("Enter the value of principle, rate & time: ");
scanf("%f%f%f",&p,&r,&t);
si=(p*r*t)/100;
printf("\n Simple Interest (SI) = %f",si);
getch();
}
WAP to find the area & perimeter of circle
/* WAP to find the area & perimeter of circle*/
#include<stdio.h>
#include<conio.h>
#define pi 3.14
void main()
{
float r;
printf("Enter the radius of the circle: ");
scanf("%f",&r);
printf("The area of the circle is %f ",pi*r*r);
printf("\n and the perimeter is %f.",2*pi*r);
getch();
}
WAP to find square root of any number
/* WAP to find square root of any number */
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a;
float r;
printf("Enter any number: ");
scanf("%d",&a);
r=sqrt(a);
printf("The square root of %d is %f.",a,r);
getch();
}
WAP to convert hours into minute & seconds
/* WAP to convert hours into minute & Seconds*/
#include<stdio.h>
#include<conio.h>
void main()
{
int h;
printf("Enter the time in hours: ");
scanf("%d",&h);
printf("%d Hours = %d Minutes",h,h*60);
printf("\n %d Hours = %d Seconds",h,h*60*60);
getch();
}
WAP to add the first & last digit of any 4 digit number
/* WAP to add the first & last digit of any 4 digit number */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a,b,sum=0;
printf("Enter a four digit number: ");
scanf("%d",&n);
a=n/1000; /* to seperate first digit*/
b=n%10; /* to seperate the last digit */
sum=a+b;
printf("The sum of first & last digit is %d",sum);
getch();
}
Control Structures
WAP to check if a given number is odd or even
/* WAP to check the given number is odd or even */
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf("Enter any number: ");
scanf("%d",&n);
if(n%2==0)
{
printf("\n The number is even");
}
else
{
printf("\n The number is odd");
}
getch();
}
WAP to find the factorial of a given number
/*WAP to find factorial of a given number*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,fact=1;
printf("Enter a number to find factorial: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("\n The factorial is %d",fact);
getch();
}
WAP to find reverse of a given number & check if it is a palindrome
/*WAP to find reverse of a given number & check if the number is
palindrome or not*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,temp,rev=0,pal;
printf("Enter any number: ");
scanf("%d",&n);
pal=n;
while(n!=0)
{
temp=n%10;
rev=rev*10+temp;
n=n/10;
}
printf("\n Reverse of the given number is %d",rev);
if(rev==pal)
{
printf("\n The number is palindrome");
}
else
{
printf("\n The number is not palindrome");
}
getch();
}
WAP to find fibonacci series up to 'n' terms
/*WAP to find fibonacci series up to 'n' terms*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=1,c,i,n;
printf("Enter a number for fibonacci series: ");
scanf("%d",&n);
printf("%d \t %d",a,b);
for(i=3;i<=n;i++)
{
c=a+b;
printf("\t%d",c);
a=b; b=c;
}
getch();
}
Chapter 2: Arrays and Strings
Array
An array is a special variable that can store more than one similar type of value under the same name with different index numbers.
Syntax:
datatype arrayName[array-size];
// e.g. -> int a[10];
Q. WAP to ask for 5 numbers and display the same numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[5], i;
for(i=0; i<5; i++)
{
printf("enter %d number\n", i+1);
scanf("%d", &a[i]);
}
printf("The numbers you have entered are \n");
for(i=0; i<5; i++)
{
printf("%d ", a[i]);
}
getch();
}
Output:
Enter 1 number 5 Enter 2 number 3 Enter 3 number 4 Enter 4 number 2 Enter 5 number 1 The number you have entered are 5 3 4 2 1
Q. WAP to ask for computer marks of 10 students & display the mark which is increased by 2.
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int mark[10], i;
for (i=0; i<10; i++)
{
printf("enter mark of %d student \n", i+1);
scanf("%d", &mark[i]);
}
printf("The marks after adding 2 marks on previous mark are\n");
for (i=0; i<10; i++)
{
printf("%d ", mark[i]+2);
}
getch();
}
Q. WAP to ask for 2 matrices of order 2x2 and display the matrix addition.
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int A[2][2], B[2][2], C[2][2], i, j;
printf("enter matrix A\n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
scanf("%d", &A[i][j]);
}
}
printf("enter matrix B\n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
scanf("%d", &B[i][j]);
}
}
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
C[i][j] = A[i][j] + B[i][j];
}
}
printf("The addition is \n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("%d ", C[i][j]);
}
printf("\n");
}
getch();
}
String Functions
String function is a library function which is used to find the length of a given string, reverse the string, copy the string, change into upper case or lower case, compare the two strings, etc.
There are 7 main string functions. They are:
- strlen()
- strrev()
- strcat()
- strupr()
- strlwr()
- strcmp()
- strcpy()
1. strlen()
This string function is used to find the length of string. Its keyword is strlen().
For example:
#include<stdio.h>
#include <conio.h>
#include<string.h>
void main()
{
clrscr();
int x;
char word[50];
printf("enter a word ");
scanf("%s", word);
x = strlen(word);
printf("The length of word is %d ", x);
getch();
}
Output:
enter a word prapti The length of word is 6
2. strrev()
This string function is used to reverse the given string.
For example:
#include<stdio.h>
#include<conio.h>
#include <string.h>
void main()
{
clrscr();
char word[25];
printf("enter a word\n");
scanf("%s", word);
strrev(word);
printf("The reversed word is %s", word);
getch();
}
Output:
Enter a word prapti The reversed word is itparp
3. strcat()
This string function is used to join two strings together.
For example:
#include <stdio.h>
#include<conio.h>
#include <string.h>
void main()
{
clrscr();
char word1[25], word2[25];
printf("enter two words\n");
scanf("%s %s", word1, word2);
strcat(word1, word2);
printf("The combined word is %s", word1);
getch();
}
Output:
Enter two words gita sita The combined word is gitasita
4. strupr()
The string function is used to convert lower case into upper case letter.
For example:
#include<stdio.h>
#include<conio.h>
#include <string.h>
void main()
{
clrscr();
char word[25];
printf("enter a word\n");
scanf("%s", word);
strupr(word);
printf("The upper case letter is %s", word);
getch();
}
Output:
Enter a word prapti The upper case letter is PRAPTI
5. strlwr()
The string function is used to convert upper case into lower case letter.
For example:
#include<stdio.h>
#include<conio.h>
#include <string.h>
void main()
{
clrscr();
char word[25];
printf("enter a word\n");
scanf("%s", word);
strlwr(word);
printf("The lower case letter is %s", word);
getch();
}
Output:
Enter a word PRAPTI The lower case letter is prapti
6. strcmp()
This string function is used to compare two strings.
For example:
#include<stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
clrscr();
char word1[25], word2[25];
printf("enter two words\n");
scanf("%s %s", word1, word2);
if (strcmp(word1, word2) == 0)
{
printf("Both strings are same");
}
else
{
printf("Both strings are not same");
}
getch();
}
Output:
enter two words prapti prapti Both strings are same
7. strcpy()
This string function is used to copy a string from one string variable to another variable.
For example:
#include<stdio.h>
#include<conio.h>
#include <string.h>
void main()
{
clrscr();
char word1[25], word2[25];
printf("enter a word\n");
scanf("%s", word2);
strcpy(word1, word2);
printf("The copied word is %s", word1);
getch();
}
Output:
enter a word prapti The copied word is prapti
Q. WAP to check whether the given word is a palindrome or not.
#include <stdio.h>
#include<conio.h>
#include <string.h>
void main()
{
clrscr();
char word1[25], word2[25];
printf("enter a word \n");
scanf("%s", word1);
strcpy(word2, word1);
strrev(word1);
if(strcmp(word1, word2) == 0)
{
printf("palindrome");
}
else
{
printf("Not palindrome");
}
getch();
}
Output:
Enter a word prapti Not palindrome
Chapter 3: Functions
Concept of function
A function is a self-contained program segment or the block of statements that perform some specific, well-defined task. Every C program comprises one or more such functions. The execution of any program always begins by executing the instructions in the main() function.
Types of Function
- Library / Built-in Functions
- User-defined functions
a. Library / Built-in functions
The library function is defined by the developer of the C-language. It cannot be changed or altered. We can use a library function by simply adding a header file.
For example:
printf(),scanf()are defined in header filestdio.hgetch(),clrscr()are defined in header fileconio.hstrlen(),strupr()are defined in header filestring.hpow(),sqrt()are defined in header filemath.h
b. User-defined Function
This type of function is defined by the user or programmer as per their need. We can use a user-defined function by simply declaring a function.
For example: aor(), vol(), sum(), factorial(), etc.
Components of function
- Function prototype (Function declaration)
- Function call
- Function Definition
- return and void statements of a function.
Actual argument and Formal Argument
Arguments defined inside the function are called Formal arguments. The argument from which the arguments have been passed to a function is known as actual arguments.
Categories of user-defined function
- Function without any arguments and return values
- Function with arguments but no return values
- Function with arguments and with return values
- Function without any arguments but with return values
1. Function without any arguments and return values
Here, we simply call a function without passing arguments and the called function doesn't have to return any values to the calling function. If we do not want to return a value, we must use the return type void and miss out on the return statement.
For example: //WAP to find the area of a rectangle using function.
#include<stdio.h>
#include<conio.h>
void AOR();
void main()
{
clrscr();
AOR();
getch();
}
void AOR()
{
int l, b, area;
printf("write the length\n");
scanf("%d", &l);
printf("write the breadth\n");
scanf("%d", &b);
area = l*b;
printf("The area of rectangle is %d", area);
}
Output:
write the length 5 write the breadth 10 The area of rectangle is 50
2. Function with arguments but no return values
This type of function has one-way communication. Here, an argument is passed from the calling function to the called function, but there is no need for a return statement in the called function.
For example: // WAP to find the area of a rectangle using function.
#include<stdio.h>
#include<conio.h>
void AOR(int, int);
void main()
{
clrscr();
int l, b;
printf("enter length & breadth\n");
scanf("%d %d", &l, &b);
AOR(l, b);
getch();
}
void AOR(int l, int b)
{
int area;
area = l*b;
printf("The area of rectangle is %d", area);
}
3. Function with arguments and with return values
This type of function has two-way communication. Here, an argument is passed from the calling function to the called function, and there will also be a returning statement in the called function.
For example:
#include<stdio.h>
#include<conio.h>
int AOR(int, int);
void main()
{
clrscr();
int l, b, x;
printf("enter length and breadth\n");
scanf("%d %d", &l, &b);
x = AOR(l, b);
printf("The area of rectangle is %d", x);
getch();
}
int AOR(int l, int b)
{
int area;
area = l*b;
return area;
}
4. Function without any arguments but with return values
This type of function also has one-way communication. Here, an argument is not passed from the calling function to the called function, but there is a need for a return statement in the called function.
For example:
#include <stdio.h>
#include <conio.h>
int AOR();
void main()
{
clrscr();
int x;
x = AOR();
printf("The area of rectangle is %d", x);
getch();
}
int AOR()
{
int l, b, area;
printf("enter length and breadth\n");
scanf("%d %d", &l, &b);
area = l*b;
return area;
}
Concept of Recursion
A recursive function is one, which calls itself. A recursive function contains the following features:
- The function should call itself.
- The function should have a stopping condition.
The concept of using the recursive function to repeat the execution of a statement many times is known as recursion.
For example: WAP to find the factorial of a given number using recursive function.
#include<stdio.h>
#include<conio.h>
int factorial(int);
void main()
{
clrscr();
int n, fact;
printf("enter a number\n");
scanf("%d", &n);
fact = factorial(n);
printf("The factorial of %d is %d", n, fact);
getch();
}
int factorial(int n)
{
if (n == 0 || n == 1)
{
return 1;
}
else
{
return (n * factorial(n-1));
}
}
Output:
enter a number 2 The factorial of 2 is 2
Chapter 4: Structures and Unions
Structure
A structure is the collection of one or more variables of different datatypes (e.g: int, float, char), grouped together under a single name. We can have an array of structure. The keyword struct is used to declare a Structure.
For example: WAP to store five employees records (EID, name, post and department) and display them using structure.
#include<stdio.h>
#include <conio.h>
struct employee
{
int eid;
char name[50];
char post[50];
char dep[50];
} emp[5];
void main()
{
clrscr();
int i;
for (i=0; i<5; i++)
{
printf("Enter eid, name, post and department of %d employee\n", i+1);
scanf("%d %s %s %s", &emp[i].eid, emp[i].name, emp[i].post, emp[i].dep);
}
clrscr();
printf("The records of employees are listed below\n");
printf("EID\t\tName\t\tpost\t\tDepartment\n");
for(i=0; i<5; i++)
{
printf("%d\t\t%s\t\t%s\t\t%s\n", emp[i].eid, emp[i].name, emp[i].post, emp[i].dep);
}
getch();
}
WAP to store 3 students records (grade, rollno, name and faculty) and display them using structure.
#include<stdio.h>
#include<conio.h>
struct students
{
int grade;
int rollno;
char name[50];
char faculty[50];
} std[3];
void main()
{
clrscr();
int i;
for(i=0; i<3; i++)
{
printf("Enter grade, rollno, name and Faculty of %d Student\n", i+1);
scanf("%d %d %s %s", &std[i].grade, &std[i].rollno, std[i].name, std[i].faculty);
}
clrscr();
printf("The records of students are listed below\n");
printf("Grade\t\tRollno\t\tName\t\tFaculty\n");
for(i=0; i<3; i++)
{
printf("%d\t\t%d\t\t%s\t\t%s\n", std[i].grade, std[i].rollno, std[i].name, std[i].faculty);
}
getch();
}
Chapter 5: Pointers
What is a pointer? Why should we use a pointer?
A pointer is a variable that holds the memory location of another variable rather than the actual value. We use a pointer variable because it stores an address or memory location and points to whatever that memory location contains.
Advantages of Pointers:
- Pointers enable us to access a variable that is defined outside the function.
- Pointers reduce the length and complexity of the program and increase execution speed.
- Pointers are more efficient in handling data tables.
- Pointers are also more efficient in handling the data array.
- Pointers are used to create complex data structures such as linked lists, trees.
What are the two operators used in pointers?
The two operators used in pointers are as follows:
- a. Address of operator (&) ampersand: Address of operator is the ampersand, i.e., &. It is a unary operator i.e. applied to a variable to assign its memory address.
- b. Indirection or dereference operator (*) Asterisk: It is denoted by "*" sign. This unary operator accesses the values residing at a memory address.
How do you declare a pointer?
Pointer declaration:
data-type *pointer-variable;
i.e. int *p;
Pointer Examples
Find the output of the following program.
// Program 1
int main()
{
int b = 25;
// memory location of b is 1234
int *p;
p = &b;
printf("%d %d", &b, p);
return 0;
}
// Output: 1234 1234
// Program 2
int main()
{
int a = 5;
int *q;
q = &a;
printf("%d %d", a, *q);
return 0;
}
// Output: 5 5
// Program 3
int main()
{
int x = 5;
// memory address of x is 2000
int *p;
p = &x;
printf("%d %d", p, *p);
return 0;
}
// Output: 2000 5
// Program 4
int main()
{
int x = 5, y = 10;
int *p = &x, *q = &y;
*q = 5;
printf("%d", *p + *q);
return 0;
}
// Output: 10
Chapter 6: Working with Files
File Handling
Why do you need a file?
As we know, while the program is processing, the content of variables are stored in RAM. But the problem is if such values of a variable are needed in the future, then this causes the problem because the value of the variable is only within the program being processed. After a program is over, the content of the RAM is erased. To overcome this problem we can use a data file to save the content into it, so that we can retrieve content from the data file whenever we want.
File Handling Functions and Modes
Operators/Functions that We Use for File Handling in C
| Description of Function | Function in Use |
|---|---|
| used to open an existing file or a new file | fopen() |
| writing data into an available file | fprintf() |
| reading the data available in a file | fscanf() |
| writing any character into the program file | fputc() |
| reading the character from an available file | fgetc() |
| used to close the program file | fclose() |
| writing an integer into an available file | putw() |
| used to read an integer from the given file | getw() |
| sets an intended file pointer to the file's beginning itself | rewind() |
Describe file handling modes in C
These are different ways a file is to be opened. The different file opening modes are:
| Mode | Meaning | Action if file does not exist | Action if file exists |
|---|---|---|---|
r |
In this mode, the file is opened for reading the existing file. | fopen() returns NULL. |
Reads from start. |
w |
In this mode, the file is opened for writing. | New file is created. | Previous contents will be erased. |
a |
In this mode, the file is opened to append. | The new file will be created. | Data is added to the end of the file. |
r+ |
In this mode, the file is opened for both reading and writing. | fopen() returns NULL. |
Opens for reading and writing. |
w+ |
The file is opened for both writing and reading. | New file is created. | Contents are overwritten. |
a+ |
The file is opened for both reading and appending. | New file is created. | Opens for reading and appending. |
File Handling Examples
Q. WAP to read name & salary of 10 employees and store all the data in a file named "emp.txt" and also display all the records from the file to the output screen.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, sal;
char name[50];
FILE *fp;
fp = fopen("emp.txt", "w");
for (i=0; i<10; i++)
{
printf("Enter name & salary of %d employee\n", i+1);
scanf("%s %d", name, &sal);
fprintf(fp, "%s\t%d\n", name, sal);
}
fclose(fp);
printf("The records of employees are\n");
printf("Name\t\tSalary\n");
fp = fopen("emp.txt", "r");
for(i=0; i<10; i++)
{
fscanf(fp, "%s %d", name, &sal);
printf("%s\t\t%d\n", name, sal);
}
fclose(fp);
getch();
}
Write a program to create and write data into file.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
clrscr();
int rollno;
char name[50], address[50], choice[2];
FILE *fp;
fp = fopen("std.txt", "w");
do
{
printf("Enter rollno, name and address of a student\n");
scanf("%d %s %s", &rollno, name, address);
fprintf(fp, "%d\t%s\t%s\n", rollno, name, address);
printf("Do you want to enter more data (y/n)\n");
scanf("%s", choice);
strupr(choice);
} while (strcmp(choice, "Y") == 0);
fclose(fp);
printf("All data are saved successfully");
getch();
}