KEMBAR78
C Programming | PDF | Computer Programming | Software Engineering
0% found this document useful (0 votes)
28 views36 pages

C Programming

Uploaded by

enikaoli336
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views36 pages

C Programming

Uploaded by

enikaoli336
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

1.

Introduction to C programming
The C language is one of the most powerful languages.
The C language is highly efficient programming language and easy
to understand. It has both the properties of high level language
and low level language, so it is also term as Middle Level
Language or intermediate language between high level language
and low level language. It is powerful programming language
because it is used to prepare system software as well as
application software. It is a kind of general purposed, structured
programming language. It has large numbers of vocabularies and
simple syntax to write a program.

C is a general-purpose programming language. C language has


been designed and developed by Dennis Ritchie at Bell
Laboratories in 1972. It is an offspring of the "Basic combined
programming language" called BCPL developed in the year 1967
at Massachusetts Institute of Technology. The source code for the
UNIX operating system is coded in C.

1.1. Structure of c programming


1. Documentation
2. Preprocessor Section
3. Definition
4. Global Declaration
5. Main() Function
6. Sub Programs

1
1.2. Program to calculate sum of two
numbers.
# include<stdio.h>
int main()
{
int a,b,sum;
printf("enter any two numbers\n");
scanf("%d%d",&a,&b);
sum=a+b;
printf("the sum is %d",sum);
return 0;
}

2
2. Operators in c
1. Arithmetic Operators in C
The purpose of this operator is to perform mathematical
operations like addition, subtraction, division, multiplication etc.
2. Relational Operators in C
Mainly used for checking relationships between operands. With
the help of this operator, you can check whether one operand is
equal to or greater than the other operand or not. It returns 1
when the relation is true. And when it is false, it returns 0.
3. Assignment Operator in C
The purpose of this operator is to assign value to a variable. The
most used assignment operator is “=”.
4. Logical Operators in C
In the C programming language, Logical operators are mostly
used for decision making. A logical operator returns either 0 or 1
whether the condition is true or false.
5. Conditional Operator in C
C offers a ternary operator which is the conditional operator (?: in
combination) to construct conditional expressions.
Syntax: (expression 1)? (expression 2): (expression 3);
6. Bitwise Operator in C
This is used for the manipulation of data at the bit level. These
operator also perform the shifting of bits from right to left.
7. Shift Operator in C
Bitwise shift operator is used to shift the binary bits either in the
left direction or right direction according to the program's
requirement.

3
2.1. Program to input four digit number and
reverse it.
# include<stdio.h>
int main()
{
int a,b,c,d,n;
printf("enter any number\n");
scanf("%d",&n);
a=n%10;
n=n/10;
b=n%10;
n=n/10;
c=n%10;
n=n/10;
d=n;
printf("The reverse of the given number is %d%d%d%d",a,b,c,d);
return 0;
}

4
2.2. Program to check whether the number is
odd or even using conditional statement.
# include<stdio.h>
int main()
{
int n;
printf("enter the number\n");
scanf("%d",&n);
(n%2==0)? (printf("even")): (printf("odd"));
return 0;
}

5
2.3. Program to enter any two number and
convert it into bitwise AND, OR and Exclusive OR.
#include<stdio.h>
int main()
{
int a,b;
printf("Enter the number\n");
scanf("%d%d",&a,&b);
printf("Bitwise AND= %d\n",a&b);
printf("Bitwise OR= %d\n",a|b);
printf("Bitwise Excluseve OR= %d\n",a^b);
return 0;
}

6
2.4. Program to enter any number and sift it into
right and left using bitwise sift operator.
#include<stdio.h>
int main()
{
int n,a,left,right;
printf("Enter the number\n");
scanf("%d",&n);
printf("How many bits to sift?\n");
scanf("%d",&a);
left=(n<<a);
right=(n>>a);
printf("After left sift the new value of %d=%d\n",n,left);
printf("After right sift the new value of %d=%d",n,right);
return 0;
}

3.Conditional statement
7
Conditional statements in C are programming constructs that
allow a program to execute different blocks of code based on
whether a certain condition is true or false. The most common
types of conditional statements in C are: If statement, If-Else
statement, Nested If…. Else statement and If…. Else If Ladder
statement.

3.1. Program to find greatest number among two


numbers using if statement.
#include<stdio.h>
int main()
{
int a,b;
printf("Enter any two numbers\n");
scanf("%d%d",&a,&b);
if(a>b)
printf("The greatest number is %d",a);
if(b>a)
printf("The greatest number is %d",b);
return 0;
}

3.2. Program to find whether the give year is leap year


of not using if else statement.

8
#include<stdio.h>
int main()
{
int year;
printf("Enter the year\n");
scanf("%d",&year);
if((year%400==0)&&(year%100==0)||(year%4==0))
printf("The year is leap year");
else
printf("The year is not leap year");
return 0;
}

3.3 Program to display largest number among three


numbers using nested if…. Else statement.
#include<stdio.h>
int main()

9
{
int num1,num2,num3;
printf("Enter the three numbers\n");
scanf("%d%d%d",&num1,&num2,&num3);
if(num1>num2)
{
if(num1>num3)
{
printf("Largest number = %d",num1);
}
else
{
printf("Largest number = %d",num3);
}
}
if(num2>num3)
{
printf("Largest number = %d",num2);
}
else
printf("Largest number = %d",num3);

return 0;
}

3.4 Program to check whether the input character is


alphabet, number or special character using if…. Else if
ladder statement.
#include<stdio.h>
int main()
{
char n;

10
printf("Enter the character\n");
scanf("%c",&n);
if((n>='a'&&n<='z')||(n>='A'&&n<='Z'))
{
printf("%c is alphabet\n",n);
}
else if(n>='0'&&n<=9)
{
printf("%c is number",n);
}
else
{
printf("%c is special character\n",n);
}

return 0;
}

4. Switch case statement


A switch case statement in C is an alternative of if else if ladder statement
which allows to execute multiple operations for the different possible
values of a single variable called switch variable.
Syntax :
Switch(expression)
{
Case constant 1: statements; break;
Case constant 2: statements; break;

11
…………………………………………………..
…………………………………………………..
default: default statement;
}

4.1 Program to calculate basic arithmetic operations


using switch case statement.
#include<stdio.h>
int main()
{
int a,b,c,choice;
printf("Enter ant two numbers\n");
scanf("%d%d",&a,&b);
printf("Enter your choice to perform\n");
printf("1.Addition\n 2.substractin\n 3.multiplication\n 4.division\
n");
scanf("%d",&choice);
switch (choice)
{
case 1:c=a+b;
printf("Addition=%d",c);
break;
case 2:c=a-b;
printf("Substraction=%d",c);
break;
case 3:c=a*b;
printf("Multiplication=%d",c);
break;
case 4:c=a/b;
printf("Division=%d",c);
break;
default:printf("please enter the right choice");
}
return 0;
}

12
13
5. Loop in C
In computer programming, a loop is a sequence of instruction s that is
continually repeated until a certain condition is reached. Typically, a
certain process is done, such as getting an item of data and changing it,
and then some condition is checked such as whether a counter has
reached a prescribed number.
Basically there are three types if loop. They are:
1. For loop:
Syntax: for(initialization; condition; incrementation or
decrementation)
{
Statement block;
}

2. While loop:
Syntax: while ( test condition)
{
Statement block;
}
3. Do…. While loop:
Syntax: do
{
Statement block;
}while(testocnditions);

14
5.1. Program to input any number and check whether the
given number is perfect square or not using for loop.
#include<stdio.h>

int main()

int i,n;

printf("Enter the number\n");

scanf("%d",&n);

for(i=0;i<=n;i++)

if(n==i*i)

printf("It is a perfect square\n");

return 0;

printf("It is not a perfect square\n");

return 0;
}

15
5.2. Program to print the sum of all odd numbers from 1 to n
using while loop.
#include<stdio.h>

int main()

int i=1,n,sum=0;

printf("Enter the upper limit\n");

scanf("%d",&n);

while(i<=n)

if(i%2!=0)

sum=sum+i;

i++;

printf("The sum is: %d\n",sum);

return 0;

16
5.3.Program to input any 3 digit number and reverse it
by using do…. While loop.
#include<stdio.h>

int main()

int i,n,rev=0;

printf("Enter the number\n");

scanf("%d",&n);

while(n!=0)

i=n%10;

rev=rev*10+i;

n=n/10;

printf("The reverse of the number is: %d\n",rev);

return 0;
}

17
6. Function in C

A function is a self contained block of statement that together performs a


task. Every C program has at least one function, which is main ( ), and all
the most trivial programs can define additional functions.

You can divide up your code into separate functions. How you divide up
your code among different functions is up to you, but logically the division
is such that each function performs a specific task.

6.1. Types of function:


There are two types of functions in C programming:

1) Library Functions:

Library functions are the functions which are declared in the C header files
such as scanf(), printf(), gets(), puts() etc.

2) User-defined functions:

User defined functions are the functions which are created by the C
programmer, so that he/she can use it many times. It reduces the
complexity of a big program and optimizes the code.

Types of Function According to Arguments and Return Value:


Functions can be differentiated into 4 types according to the arguments
passed and value returns these are:

1. Function with no arguments and no return value

2. Function with arguments and no return value

3. Function with no arguments and with return value


4. Function with arguments and return value

18
6.2. Program to print the multiplication table of n number by
using function.
#include<stdio.h>

void table(int);

int main()

int n;

printf("MULTIPLICATION TABLE OF :\n");

scanf("%d",&n);

table(n);

void table(int n)

int i;

for(i=1;i<=10;i++)

printf("%dx%d=%d\n",n,i,n*i);

19
6.3. Program to input any number and calculate the value of the given
power using user defined function.

#include<stdio.h>

int power(int,int);

int main()

int x,y,c;

printf("Enter base and power respectively\n");

scanf("%d%d",&x,&y);

c=power(x,y);

printf("The result is %d",c);

int power(int x, int y)

int i,p=1;

for(i=1;i<=y;i++)

p=p*x;

return p;
}

20
7.Recursive function
The recursion process in C refers to the process in which the program
repeats a certain section of code in a similar way. Thus, in the
programming languages, when the program allows the user to call any
function inside the very same function, it is referred to as a recursive
call in that function.

7.1. Program to calculate the factorial of a given number using


recursive function.
#include<stdio.h>

int factorial(int);

int main()

int n,f;

printf("Enter any number:\n");

scanf("%d",&n);

f=factorial(n);

printf("The factorial is %d",f);

int factorial(int n) {

if(n==0)

return 1;

else

return (n*factorial(n-1));
}

21
8. Array
An array is the collection of items stored at contiguous memory location.
The idea is to store multiple items of the state type together. It falls under
the category of linear data structure. Most of the algorithm and problem
solving approaches use array in their implementation. Array have a fixed
size where the size of array is defined when the array is initialized. Array is
classified into two types i.e. One Dimensional Array and Two Dimensional
Array.

8.1. Program to calculate sum and average of 10


elements using one dimensional array.
#include<stdio.h>

int main()

int i,n[10],average,sum=0;

printf("Enter the elements\n");

for(i=0;i<10;i++)

scanf("%d",&n[i]);

for(i=0;i<10;i++)

sum=sum+n[i];

printf("The sum is : %d\n",sum);

average=sum/10;

printf("The average is : %d",average);

22
return 0;}

8.2. Program to input 2x3 matrix and display the sum of that
matrix using two dimensional array.
#include<stdio.h>
int main()
{
int i,j,a[2][3],b[2][3],s[2][3];
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("ENTER THE ELEMENTS OF FIRST MATRIX\t");
scanf("%d",&a[i][j]);
}}
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("ENTER THE ELEMENTS OF SECOND MATRIX\t");
scanf("%d",&b[i][j]);
}}
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
s[i][j]=a[i][j]+b[i][j];
}
printf("THE SUM OF TWO MATRICES IS\n");
for(i=0;i<2;i++)
{

23
for(j=0;j<3;j++)
printf("%d\t",s[i][j]);
printf("\n");
}
return 0;}

9. String
A String in C programming is a sequence of characters terminated with a
null character ‘\0’. The C String is stored as an array of characters. The
difference between a character array and a C string is that the string in C
is terminated with a unique character ‘\0’.

9.1. String manipulation function


The pre defined function which are designed to handle the string are
known as string manipulation function. The string manipulation
functions in C are:

Function Work of Function

strlen() computes string's length

strcpy() copies a string to another

strcat() concatenates(joins) two strings

strcmp() compares two strings

24
Function Work of Function

strlwr() converts string to lowercase

converts string to uppercase


strupr()

9.2. Program to shot string in alphabetical


order.
#include<stdio.h>
#include<string.h>
int main()
{
char name[50][30],temp[30];
int n,i,j;
printf("How many string to input\n");

scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("Enter the strings\n");
scanf("%s",name[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(name[i],name[j])>0)
{

strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf("The sorted name are : \n");
for(i=0;i<n;i++)
printf("%s\t",name[i]);
return 0;

25
}

10. Structure
The structure in C is a user-defined data type that can be used to group
items of possibly different types into a single type. The struct keyword is
used to define the structure in the C programming language. The items in
the structure are called its member and they can be of any valid data
type.

10.1. Program to input name and mark of 5 student and display using
structure.

#include <stdio.h>
struct student {
char firstName[50];
int roll;
float marks;
}
s[5];
int main()
{
int i;
printf("Enter information of students:\n");
for (i = 0; i < 5; ++i)

26
{
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("The Information are:\n\n");

for (i = 0; i < 5; ++i)


{
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
return 0;
}

27
11. Pointer

28
A pointer is defined as a derived data type that can store the address of
other C variables or a memory location. We can access and manipulate
the data stored in that memory location using pointers. A pointer can be
used to store the memory address of other variables, functions, or even
other pointers. The use of pointers allows low-level memory access,
dynamic memory allocation, and many other functionality in C.

11.1. Program to add two numbers using pointer.


#include<stdio.h>
int main()
{
int first,second,*p,*q,sum;
printf("Enter two integers to add\n");
scanf("%d%d",&first,&second);
p=&first;
q=&second;
sum=*p+*q;
printf("The address of first variable is : %x\n",p);
printf("The address of second variable is : %x\n",q);
printf("The sum of the number is : %d\n",sum);
return 0;
}

29
11.2. Dynamic memory allocation
C dynamic memory allocation refers to performing manual memory
management for dynamic memory allocation in the C programming
language via a group of functions in the C standard library, namely malloc,
realloc, calloc, aligned_alloc and free.

malloc() function
The malloc() function allocates single block of requested memory.It doesn't
initialize memory at execution time, so it has garbage value initially. It
returns NULL if memory is not sufficient.
The syntax of malloc() function is given below:
ptr=(cast-type*)malloc(byte-size)

calloc() function
The calloc() function allocates multiple block of requested memory.It
initially initialize all bytes to zero.It returns NULL if memory is not
sufficient.The syntax of calloc() function is given below:

ptr=(cast-type*)calloc(number, byte-size)

realloc() function
If memory is not sufficient for malloc() or calloc(), you can reallocate the
memory by realloc() function. In short, it changes the memory size.The
syntax of realloc() function.

ptr=realloc(ptr, new-size)

free() function
The memory occupied by malloc() or calloc() functions must be released by
calling free() function. Otherwise, it will consume memory until program
exit. The syntax of free() function.

free(ptr)

11.3. program to calculate the sum of n numbers using


malloc function
30
#include<stdio.h>
#include<stdlib.h>
int main(){
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int));
if(ptr==NULL)
{
printf("Sorry! unable to allocate memory");
exit(0);
}
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
return 0;
}

11.4. Program to calculate the sum on n numbers


using calloc function.
#include<stdio.h>
#include<stdlib.h>
31
int main(){
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)calloc(n,sizeof(int));
if(ptr==NULL)
{
printf("Sorry! unable to allocate memory");
exit(0);
}
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
return 0;
}

11.5. Program to realloc memory using realloc


function.
#include <stdio.h>
#include <stdlib.h>
int main() {

32
int *ptr;
int n, i;
n = 5;
ptr = (int*)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
printf("Original memory block:\n");
for (i = 0; i < n; i++) {
ptr[i] = i;
printf("%d ", ptr[i]);
}
printf("\n");
n = 10;
ptr = (int*)realloc(ptr, n * sizeof(int));
if (ptr == NULL) {
printf("Memory reallocation failed\n");
return 1; }
printf("Reallocated memory block:\n");
for (i = 0; i < n; i++) {
printf("%d ", ptr[i]);
}
printf("\n");
free(ptr);
return 0;
}

12. File handling


A file handling or data file in C refers to store any shot of information in the
secondary memory permanently. Like other programming language C

33
programming language also supports sequential access data file and random
access data file. Some of the files handling functions In C are:

 Fopen()
 Fclose()
 Fscanf()
 Fprintf()
 Fgets()
 Fputs()
 Rewind()

12.1. Program to store name, address and id of 5 students and


store in a student.txt file
#include<stdio.h>
int main()
{
FILE *fp;
char name[5][20],address[5][20];
int id[5],i;
fp=fopen("student.txt","w");
printf("Enter the name, address and id respectively\n");
for(i=0;i<5;i++)
{
scanf("%s%s%d",name[i],address[i],&id[i]);
fprintf(fp,"%s\n%s\n%d\n",name[i],address[i],id[i]);
}
fclose(fp);
}

34
35
12.2. Program to read the data from student.txt file
#include<stdio.h>
int main()
{
FILE *fp;
char name[5][20],address[5][20];
int id[5],i;
fp=fopen("student.txt","r");
printf("The name, address and id student are:\n");
for(i=0;i<5;i++)
{
fscanf(fp,"%s%s%d",name[i],address[i],&id[i]);
printf("%s\n%s\n%d\n",name[i],address[i],id[i]);
}
fclose(fp);
}

36

You might also like