Download as pdf or txt
Download as pdf or txt
You are on page 1of 85

C- Programming Language Be Coder….

Be Programmer

C – Language History

Que: Why we learn the programming?


We learn programming to design particular Software.
Que: What is software?
Software is collection of programs.
Que: What is use of software?
It is use for communication purpose.
Que: What is the program?
Program is define as collection of instructions.
Que: What is the Instruction?
Instruction is collection of tokens.
Que: Who is develop c language?
It is develop in 1972 by “Dennis Ritchie”.
----------------------------------------------------------------------------------------------------------------------------
 The C programming language is a structure oriented programming language, developed at Bell Laboratories
in 1972 by Dennis Ritchie
 C programming language features were derived from an earlier language called “B”.
 C language was invented for implementing UNIX operating system.

USES OF C PROGRAMMING LANGUAGE:


The C programming language is used for developing system applications that forms a major portion of
operating systems such as Windows, UNIX and Linux. Below are some examples of C being use.
 Database systems
 Graphics packages
 Word processors
 Spreadsheets
 Operating system development
 Compilers and Assemblers
 Network drivers
 Interpreters
KEY POINTS TO REMEMBER IN C LANGUAGE:
1. The C language is a structure oriented programming language developed by Dennis Ritchie.
2. The C language is belonging to middle level programming language.
3. Operating system programs such as Windows, UNIX, and Linux are written in C language.
Structure oriented language:
 In this type of language, large programs are divided into small programs called functions
 Program structure follows “Top Down Approach”
 Examples: C, Pascal, ALGOL and Modula-2
Object oriented language:
 In this type of language, programs are divided into objects
 Data is hidden and cannot be accessed by external functions(using modifiers)
 Program structure follows “Bottom UP Approach”
 Examples: C++, JAVA and C# (C sharp)

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

Non structure oriented language:


 There is no specific structure for programming this language. Examples: BASIC, COBOL, FORTRAN
-----------------------------------------------------------------------------------------------------------------
Structured Programming Flow:

a. Sequence Control Structure (Sequence Logic or Sequential Flow).


If nothing is specified then the modules the executed in the sequence in which they are written. Thus
in it the modules are executed one after the other.
b. Selection Control Structure (Selection Logic or Conditional Flow).
In it there are various conditions on the basis of which one module is selected out of several given
modules. The structures which implement this logic are called conditional structures.
c. Repetition Control Structure (Iteration Logic or Repetitive Flow).
It loops are implemented. Loop is used to implement those statements which are to be repeated again
and again until some condition is satisfied. It implements while and for loops. Each of these begin with Repeat
statement.

C TOKENS:
 C tokens are the basic buildings blocks in C language which are constructed together to write a C program.

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

 Each and every smallest individual units in a C program are known as C tokens.
C tokens are of six types. They are, (KICS-OS)
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Special symbols
6. Operators

1. Keyword:
 Keywords are pre-defined words in a C compiler.
 The words in program which meaning is already known by compiler is called keyword.
 C language supports 32 keywords which are given below.

Note: Keywords is the part of syntax we cant use keywords as Indentifier.

2. Identifiers:

• Each program elements in a C program are given a name called identifiers.


• Names given to identify Variables, functions and arrays are examples for identifiers. eg. x is a name
given to integer variable.
RULES FOR CONSTRUCTING IDENTIFIER NAME IN C: (Naming Convention)
1. First character should be an alphabet or underscore. Number not allow.
2. Punctuation and special characters aren’t allowed except underscore.
3. Identifiers should not be keywords.
Variable: Variable is act as a container which store some value in memory location.
Variables having some size and data type like int, float, char etc.
RULES FOR NAMING C VARIABLE:
 Variable name must begin with letter or underscore. Starting from number is not allowed.
 Variables are case sensitive. i.e. int a; and int A; is treated as different.
 They can be constructed with digits, letters. No special symbols are allowed other than
underscore.

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

 Example:
1. int a; //correct
2. int 1a; //incorrect
3. int a1; //correct
4. int a@1; //incorrect
5. int a_1; //correct

THERE ARE TWO TYPES OF VARIABLES:


 Local variable
 Global variable
 LOCAL VARIABLE IN C:
 The scope of local variables will be within the function only.
 These variables are declared within the function and can’t be accessed outside the function.
 GLOBAL VARIABLE IN C:
 The scope of global variables will be throughout the program. These variables can be
accessed from anywhere in the program.
 This variable is defined outside the main function. So that, this variable is visible to main
function and all other sub functions.
Program demonstrate local/global variable:
#include<stdio.h>
#include<conio.h>
void test();
int a = 50, b = 80;
int main()
{
printf("Global variable are-");
printf("a=%d b=%d",a,b);
}
void test()
{
int m=10,n=5;
printf(“Local Variables are -”);
printf”m=%d n=%d"m,n);
}

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

OUTPUT:-
Global variable are-
a = 50 b = 80
Local Variables are -
Values: m = 10 n = 5
-------------------------------------------------------------------------------------------------------
Data Types: C data types are defined as the data storage format that a variable can store a data.
There are four basic data types in C language. They are,

Note: sizeof(var_name) function is used to find the memory space allocated for each C data
types.
Demonstrate sizeof() function:
#include <stdio.h>
#include <limits.h>
void main()
{
int a;
char b;
float c;
double d;
clrscr();
printf("Storage size for int data type:%d \n",sizeof(a));
printf("Storage size for char data type:%d \n",sizeof(b));
printf("Storage size for float data type:%d \n",sizeof(c));
printf("Storage size for double data type:%d\n",sizeof(d));
getch() ;
}

Output:-
Storage size for int data type:2
Storage size for char data type:1
Storage size for float data type:4
Storage size for double data type:8

1. BASIC DATA TYPES IN C LANGUAGE:

A. INTEGER DATA TYPE:

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

 Integer data type allows a variable to store numeric values.


 “int” keyword is used to refer integer data type.
 int (2 byte) can store values from -32,768 to +32,767
B. CHARACTER DATA TYPE:
 Character data type allows a variable to store only one character.
 Storage size of character data type is 1. We can store only one character using
character data type.
 “char” keyword is used to refer character data type.
C. FLOATING POINT DATA TYPE:
Floating point data type consists of 2 types. They are,
1. float
2. double
1. FLOAT: Float data type allows a variable to store decimal values. Size of float data type
is 4.
We can use up-to 6 digits after decimal using float data type.
For example: 10.456789 can be stored in a variable using float data type.

3. DOUBLE: Double data type is also same as float data type which allows up-to
10 digits after decimal.
FORMAT SPECIFIER:
The format specifier is used during input and output. It is a way to tell the compiler what
type of data is in a variable during taking input using scanf() or printing using printf(). Some examples are %c,
%d, %f, etc.

1. Integer%d
2. Float%f
3. Char%c
4. String%s etc.

ENUMERATION DATA TYPE IN C LANGUAGE:


Enumeration data type consists of named integer constants as a list. It start with 0
(zero) by default and value is incremented by 1 for the sequential identifiers in the list.
Enum syntax in C:
enum identifier [optional{ enumerator-list }];

Enum example in C:
enum month { Jan, Feb, Mar }

C – ENUM EXAMPLE PROGRAM


#include <stdio.h>
int main()

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

{
enum MONTH { Jan , Feb, Mar };
enum MONTH month = Mar;
if(month == 0)
printf("Value of Jan");
else if(month == 1)
printf("Month is Feb");
if(month == 2)
printf("Month is Mar");
}

Output:-
Month is March

4. C – Operators:

TYPES OF C OPERATORS: C language offers many types of operators. They are,

 Arithmetic operators
 Assignment operators
 Relational operators
 Logical operators
 Bit wise operators
 Conditional operators (ternary operators)
 Increment/decrement operators
 Special operators

1.Arithmetic Operators: Arithmetic operators are used to perform


mathematical calculations like addition, subtraction, multiplication,
division and modulus in C programs.

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

EXAMPLE PROGRAM FOR C ARITHMETIC OPERATORS:


In this example program, two values “40” and “20” are used to perform arithmetic
operations such as addition, subtraction, multiplication, division, modulus and output
is displayed for each operation.

#include <stdio.h>
#include<conio.h>
void main()
{
int a=40,b=20,
int add,sub,mul,div,mod;
add = a+b;
sub = a-b;
mul = a*b;
div = a/b;
mod = a%b;
printf("Addition of a, b is : %d\n", add);
printf("Subtraction of a, b is : %d\n", sub);
printf("Multiplication of a, b is : %d\n", mul);
printf("Division of a, b is : %d\n", div);
printf("Modulus of a, b is : %d\n", mod);
}
Output:-
Addition of a, b is : 60

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

Subtraction of a, b is : 20
Multiplication of a, b is : 800
Division of a, b is : 2
Modulus of a, b is : 0

2. Assignment Operators: In C programs, values for the variables are assigned using
assignment operators.

For example, if the value “10” is to be assigned for the variable “sum”, it can
be assigned as “sum = 10;”

There are 2 categories of assignment operators in C language. They are,

1. Simple assignment operator (=) Example: sum = 10;”

2. Compound assignment operators (Example: +=, -=, *=, /=, %=, &=, ^= )
OPERATORS EXAMPLE/DESCRIPTION
= sum = 10;
10 is assigned to variable sum
+= sum += 10;
This is same as sum = sum + 10
-= sum -= 10;
This is same as sum = sum – 10
*= sum *= 10;
This is same as sum = sum * 10
/= sum /= 10;
This is same as sum = sum / 10
%= sum %= 10;
This is same as sum = sum % 10
&= sum&=10;
This is same as sum = sum & 10
^= sum ^= 10;
This is same as sum = sum ^ 10

EXAMPLE PROGRAM FOR C ASSIGNMENT OPERATORS:

Que:In this program, values from 0 – 9 are summed up and total “45” is displayed as
output.Assignment operators such as “=” and “+=” are used in this program to assign the
values and to sum up the values.

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

# include <stdio.h>
#include<conio.h>
void main()
{
int Total=0; //assign 0 in variable total
int i;
clrscr();
for(i=0;i<10;i++)
{
Total+=i; // This is same as Total = Total+i
}
printf("Total = %d", Total);
getch();
}
OUTPUT:
Total = 45
3. Relational Operators: Relational operators are used to find the relation between
two variables. i.e. to compare the values of two variables in a C program.
Operators Example/Description
> x > y (x is greater than y)
< x < y (x is less than y)
>= x >= y (x is greater than or equal to y)
<= x <= y (x is less than or equal to y)
== x == y (x is equal to y)
!= x != y (x is not equal to y)

EXAMPLE PROGRAM FOR RELATIONAL OPERATORS IN C:


 In this program, relational operator (==) is used to compare 2 values whether they are
equal are not.
 If both values are equal, output is displayed as ” values are equal”. Else, output is
displayed as “values are not equal”.
#include <stdio.h>
#include <conio.h>
void main()
{

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

int m=40,n=20;
clrscr();
if (m == n)
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
}
Output:- m and n are not equal
----------------------------------------------------------------
4. Logical Operators: These operators are used to perform logical operations on the given
expressions. There are 3 logical operators in C language. They are, logical AND (&&), logical
OR (||) and logical NOT (!).

Operators Example/Description
&& (logical (x>5)&&(y<5)
AND) It returns true when both conditions are true
|| (logical OR) (x>=10)||(y>=10)
It returns true when at-least one of the
condition is true
! (logical NOT) !((x>5)&&(y<5))
It reverses the state of the operand “((x>5) &&
(y<5))”
If “((x>5) && (y<5))” is true, logical NOT
operator makes it false
EXAMPLE PROGRAM FOR LOGICAL OPERATORS IN C:
#include <stdio.h>
#include <conio.h>
void main()
{
int m=40,n=20;
int o=20,p=30;
if (m>n && m !=0)
{
printf("&& Operator : Both conditions are true\n");
}

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

if (o>p || p!=20)
{
printf("|| Operator : Only one condition is true\n");
}
if (!(m>n && m !=0))
{
printf("! Operator : Both conditions are true\n");
}
else
{
printf("! Operator : Both conditions are true. " \
"But, status is inverted as false\n");
}
}
OUTPUT:
&& Operator : Both conditions are true
|| Operator : Only one condition is true
! Operator : Both conditions are true. But, status is inverted as false

5. Bit wise Operators

These operators are used to perform bit operations. Decimal values are converted into binary
values which are the sequence of bits and bit wise operators work on these bits.

 Bit wise operators in C language are & (bitwise AND), | (bitwise OR), ~ (bitwise OR), ^ (XOR), << (left shift)
and >> (right shift).
TRUTH TABLE FOR BIT WISE OPERATION & BIT WISE OPERATORS:

BELOW ARE THE BIT-WISE OPERATORS AND THEIR NAME IN C LANGUAGE.

1. & – Bitwise AND


2. | – Bitwise OR
3. ~ – Bitwise NOT
4. ^ – XOR

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

5. << – Left Shift


6. >> – Right Shift
Consider x=40 and y=80. Binary form of these values are given below.

x=00101000
y= 01010000

All bit wise operations for x and y are given below.


1. x&y = 00000000 (binary) = 0 (decimal)
2. x|y = 01111000 (binary) = 120 (decimal)
3. ~x = 11111111111111111111111111 11111111111111111111111111111111010111 = -41 (decimal)
4. x^y = 01111000 (binary) = 120 (decimal)
5. x << 1 = 01010000 (binary) = 80 (decimal)
6. x >> 1 = 00010100 (binary) = 20 (decimal)
Note:
 Bit wiseNOT: Value of 40 in binary is 00000000000000000000000000000000
00000000000000000010100000000000. So, all 0’s are converted into 1’s in bit wise NOT operation.
 Bit wise left shift and right shift : In left shift operation “x << 1 “, 1 means that the bits will be left
shifted by one place. If we use it as “x << 2 “, then, it means that the bits will be left shifted by 2 places.

EXAMPLE PROGRAM FOR BIT WISE OPERATORS IN C:

In this example program, bit wise operations are performed as shown above and output is displayed in decimal
format.
#include <stdio.h>
int main()
{
int m = 40,n = 80,AND_opr,OR_opr,XOR_opr,NOT_opr ;
AND_opr = (m&n);
OR_opr = (m|n);
NOT_opr = (~m);
XOR_opr = (m^n);
printf("AND_opr value = %d\n",AND_opr );
printf("OR_opr value = %d\n",OR_opr );
printf("NOT_opr value = %d\n",NOT_opr );
printf("XOR_opr value = %d\n",XOR_opr );
printf("left_shift value = %d\n", m << 1);
printf("right_shift value = %d\n", m >> 1);

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

}
OUTPUT:
AND_opr value = 0
OR_opr value = 120
NOT_opr value = -41
XOR_opr value = 120
left_shift value = 80
right_shift value = 20

6. CONDITIONAL OR TERNARY OPERATORS IN C:

 Conditional operators return one value if condition is true and returns another value is condition is false.
 This operator is also called as ternary operator.
Syntax : (Condition? true_value: false_value);

Example : (A > 100 ? 0 : 1);

 In above example, if A is greater than 100, 0 is returned else 1 is returned. This is equal to if else conditional
statements.

W.A.P: EXAMPLE PROGRAM FOR CONDITIONAL/TERNARY OPERATORS IN C:

#include<stdio.h>
#include<conio.h>
void main()
{
int a=2,b=4;
clrscr();
(a>b?printf("a is greater !!"):printf("B is greater !!"));
getch();
}
OUTPUT: B is greater !!

7. SPECIAL OPERATORS IN C: Below are some of the special operators that the C programming
language offers.

Operators Description

& This is used to get the address of the variable.

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

Example : &a will give address of a.

This is used as pointer to a variable.

* Example : * a where, * is pointer to the variable a.

This gives the size of the variable.

Sizeof () Example : size of (char) will give us 1.

EXAMPLE PROGRAM FOR & AND * OPERATORS IN C:


In this program, “&” symbol is used to get the address of the variable and “*” symbol is used to get
the value of the variable that the pointer is pointing to. Please refer C – pointer topic to know more
about pointers.
#include <stdio.h>
void main()
{
int *ptr, q;
q = 50;
ptr = &q; /* address of q is assigned to ptr */
printf("%d", *ptr); /* display q's value using ptr variable */
getch();
}
OUTPUT:
50

EXAMPLE PROGRAM FOR SIZEOF() OPERATOR IN C:


sizeof() operator is used to find the memory space allocated for each C data types.

#include <stdio.h>
#include <conio.h>
void main()
{
int a;
char b;
float c;
double d;
printf("Storage size for int data type:%d \n",sizeof(a));
printf("Storage size for char data type:%d \n",sizeof(b));
printf("Storage size for float data type:%d \n",sizeof(c));
printf("Storage size for double data type:%d\n",sizeof(d));
getch()

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

}
OUTPUT:

Storage size for int data type:4


Storage size for char data type:1
Storage size for float data type:4
Storage size for double data type:8

8. C – Increment/decrement Operators: Increment operators are used to increase the value of the
variable by one and decrement operators are used to decrease the value of the variable by one in C
programs.
Syntax:
Increment operator: ++var_name; (or) var_name++;
Decrement operator: – -var_name; (or) var_name – -;
Example:
Increment operator : ++ i ; i ++ ;
Decrement operator : – – i ; i – – ;
EXAMPLE PROGRAM FOR INCREMENT OPERATORS IN C:
In this program, value of “i” is incremented one by one from 1 up to 9 using “i++” operator and output is
displayed as “1 2 3 4 5 6 7 8 9”.

//Example for increment operators


#include <stdio.h>
int main()
{
int i=1;
while(i<10)
{
printf("%d ",i);
i++;
}
}
OUTPUT: 1 2 3 4 5 6 7 8 9

EXAMPLE PROGRAM FOR DECREMENT OPERATORS IN C:

In this program, value of “I” is decremented one by one from 20 up to 11 using “i–” operator and output
is displayed as “20 19 18 17 16 15 14 13 12 11”.

//Example for decrement operators

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

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

int i=20;
while(i>10)
{
printf("%d ",i);
i--;
}
}
OUTPUT: 20 19 18 17 16 15 14 13 12 11

DIFFERENCE BETWEEN PRE/POST INCREMENT & DECREMENT OPERATORS IN C:


Below table will explain the difference between pre/post increment and decrement operators
in C programming language.
Operator Operator/description

Pre increment operator (++i) Value of i is incremented before assigning it to the variable i

Post increment operator (i++) Value of i is incremented after assigning it to the variable i

Pre decrement operator (--i) Value of i is decremented before assigning it to the variable i

Post decrement operator (i--) Value of i is decremented after assigning it to variable i

EXAMPLE PROGRAM FOR PRE – INCREMENT OPERATORS IN C:


//Example for increment operators
#include <stdio.h>
#include<conio.h>
void main()
{
int i=0;
while(++i < 5 )
{
printf("%d ",i);
}
getch();
}
OUTPUT: 1 2 3 4

Step 1 : In above program, value of “i” is incremented from 0 to 1 using pre-increment operator.
Step 2 : This incremented value “1” is compared with 5 in while expression.
Step 3 : Then, this incremented value “1” is assigned to the variable “i”.
Above 3 steps are continued until while expression becomes false and output is displayed as “1 2 3 4”.

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

EXAMPLE PROGRAM FOR POST – INCREMENT OPERATORS IN C:


#include <stdio.h>
#include <conio.h>
void main()
{
int i=10;
while(--i > 5 )
{
printf("%d ",i);
}
return 0;
}
OUTPUT: 9 8 7 6

Step 1 : In above program, value of “i” is decremented from 10 to 9 using pre-decrement operator.
Step 2 : This decremented value “9” is compared with 5 in while expression.
Step 3 : Then, this decremented value “9” is assigned to the variable “i”.
Above 3 steps are continued until while expression becomes false and output is displayed as “9 8 7 ”.

Operators Description

& This is used to get the address of the variable.

Example : &a will give address of a.

* This is used as pointer to a variable.

Example : * a where, * is pointer to the variable a.

Sizeof () This gives the size of the variable.

Example : size of (char) will give us 1.

Printing Statements in C:
Printf(“Statement”);
Example1:
Printf(“Hello world”); o/p: Hello World
Example2:
Printf(“Value of A is %d”,a); o/p: Value of A is 10.
Taking input in c:
Scanf(“%d”,&var_name);
Example:
int a;
Printf(“Enter the value of A:”);
scanf(“%d”,&a);
o/p: Enter the value of A: 10

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

Library Files/Header Files:


1. #include<stdio.h>: This header file store the function definition for input/output related
functions like printf() and scanf() function.

2. #include<conio.h>: This header file store the function definition for console(window) related
functions like clrscr() and getch() function.

a. Clrscr(): this function is used to clear the previous output in the screen.
b. getch(): This function is used to capture/hold the output window of the
program.
3. #include<math.h>: This header file store the function definition for mathematical related
operation functions like pow() and sqrt().
a. pow(): this function is used for calculating power of any integer value.
Syntax: pow(number,power);
Example:
int num=10;
int res;
res=pow(num,2);
printf(“The Square of 10 is %d”,res);
o/p: The Square of 10 is 100.
b. Sqrt(): this function is used for calculating the square root of any number.
Syntax: sqrt(number);
Example:
int num=4;
int res;
res=sqrt(num);
printf(“The Square root of 4 is %d”,res);
o/p: The Square root of 4 is 2.
First Program in C:
 W.A.P for Print “Hello World ” statement.
----------------------------------------------------------------------
 W.A.P for addition of two number.
#include<stdio.h>
#include<conio.h>
void main()
{
int num1=2,num2=4;
int sum;
clrscr();
printf("Values:\n \tnum1=%d \n\tnum2=%d",num1,num2);
sum=num1+num2;
printf("\nAdd=%d",sum);
getch();
}
Output: Values:

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

Num1:2
Num2:4
Add=6
----------------------------------------------------------------------
 W.A.P for addition of two number the values take from user.
#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2;
int sum;
clrscr();
printf("Enter the no1:");
scanf("%d",&num1);
printf("\nEnter the no2:");
scanf("%d",&num2);
sum=num1+num2;
printf("\nAddition=%d",sum);
getch();
}
Output: Enter the no1: 2
Enter the no2: 2
Addition=4
 Swapping of two number.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=2,b=4,temp;
clrscr();
printf("Before Swaping:\n \t value of a=%d and b=%d",a,b);
printf("\n-----------------------");
temp=a;
a=b;
b=temp;
printf("\nAfter Swaping: \n \t value of a=%d and b=%d",a,b);
getch();
}

----------------------------------------------------------------------
 W.A.P to find the Area of circle.
#include<stdio.h>
#include<conio.h>
void main()
{

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

float r;
float pi=3.14;
float res;
clrscr();
printf("Enter the radius:");
scanf("%f",&r);
res=pi*r*r;
printf(“\n Area of circle:%f",res);
getch();
}
Output: Enter the radius: 2
Area of circle: 7.67
----------------------------------------------------------------------
 W.A.P to find the circumference of circle.
#include<stdio.h>
#include<conio.h>
void main()
{
float r;
float pi=3.14;
float res;
clrscr();
printf("Enter the radius:");
scanf("%f",&r);
res=2*pi*r;
printf("\nCircumference of circle:%f",res);
getch();
}
Output: Enter the radius: 2
Circumference of circle: 12.560000
-----------------------------------------------------------------------
Algorithm: Alogrithm is the set of rule and instructions that step by step define the program.

Flowchart:

Command Line Arguments in C:


The arguments passed from command line are called command line arguments. These arguments are
handled by main() function. To support command line argument, you need to change the structure of main()
function as we want to pass the arguments in main function – as given below
Syntax:

int main(int argc, char *argv[] )

Where,
1. argc counts the number of arguments. It counts the file name as the first argument.

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

2. argv[] contains the total number of arguments. The first argument is the file name always.
Example:
#include <stdio.h>

void main(int argc, char *argv[] ) {

printf("Program name is: %s\n", argv[0]);

if(argc < 2){

printf("No argument passed through command line.\n");

}
else{
printf("First argument is: %s\n", argv[1]);
}
}
Output:
Run: program.exe hello
O/p:
Program name is: program
First argument is: hello

Control Statement in C:
1. Multiple IF statement:
SYNTAX:
if(condition)
{ -------
-------
}
if (condition)
{ -------
-------
}
Etc….
2. if…else statement:
syntax:
if (condition)
{ -------
-------
}
else
{ -------
-------
}

3. Ladder if (if…. else if….else):

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

Syntax:
if (condition)
{ -------
-------
}
else if (condition)
{ -------
-------
}
else
{ -------
-------
}

4. Nested if:
syntax
if (condition)
{
if (condition)
{ -------
-------
}
else
{
-------
}

}
else
{ ------
------
}

QUE1. W. A. P take two number from user and find out which number is greater or numbers
are equals.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the Value for A:");
scanf("%d",&a);

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

printf("Enter the Value for B:");


scanf("%d",&b);
if(a!=b)
{
if(a>b)
{
printf("A is greater Number");
}
else
{
printf("B is greater Number");
}
}
else
{
printf("A and B is Equal");
}
getch();
}
Output Case-1: Enter the value of A: 1
Enter the value of B: 2
B is greater Number
Output Case-2: Enter the value of A: 2
Enter the value of B: 2
A and B is Equal

QUE2. W. A. P take one number from user and find out number is odd or even.

#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("Enter the Number:");
scanf("%d",&num);
if(num%2==0)
{
Printf(“Number is Even”)
}
else
{
printf(“Number is Odd");
}
getch();

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

}
Output:
QUE3. W. A. P take one number from user and check number is positive / negative / zero.
QUE4. W. A. P to check whether triangle is equilateral , isosceles, scalene or right angle
triangle.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int s1,s2,s3;
clrscr();
printf("Enter the sides:");
scanf("%d%d%d",&s1,&s2,&s3);
if(s1==s2 && s1==s3)
{
printf("Triangle is equilateral ....");
}
else if((s1==s2 && s1!=s3)||(s1==s3 && s1!=s2))
{
printf("Triangle is Isoscales Triangle ....");
}
else
{
printf("Triangle is Scalene....");
}
if(((pow(s1,2)+pow(s2,2))==pow(s3,2))||((pow(s2,2)+pow(s3,2))==pow(s1,2)))
{
printf("\nTriangle is Right angle Triangle !!!");
}
getch();
}
------------------------------------------------------------------------------------------------------------
Que. Check whether enter year is Leap year or not.

#include<stdio.h> //error program


#include<conio.h>
void main()
{
int year;
clrscr();
printf("Enter the Year:");
scanf("%d",&year);
if(year%4==0)
{
printf("Year is Leap Year !!!");

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

else
{
printf("Year is Not Leap Year !!!");
}
getch();
}
-------------------------------------------------------------------------------------------------------
Que:W.A.P to print the sum of squares of all odd numbers from 1-50.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,sum=0;
clrscr();

for(i=1;i<=50;i++)
{
if(i%2!=0)
{
sum=sum+pow(i,2);
}
}
printf("Sum:%d",sum);
getch();
}

Que:W.A.P Which reads mark of three subject from Student and check whether student
is Pass/Fail if student is pass display percentage among with msg.

5. Switch Case:

#include<stdio.h>
#include<conio.h>
void main()
{
int week;
clrscr();
printf("Enter the choice for Day:");
scanf("%d",&week);
switch(week)
{
case 1:
printf("Monday");
break;

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf(“Thursday”);
break;
case 5:
printf(“Friday”);
break;
case 6:
printf(“Saturday”);
break;
case 7:
printf(“Sunday”);
break;
default:
printf("Invalid input day");
break;
}
getch();
}
---------------------------------------------------------------------------------------------------------

QUE. W. A. P to get the number and print the number in words (0-9) only.

#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("Enter the number:");
scanf("%d",&num);
switch(num)
{
case 0:
printf("Zero");
break;
case 1:
printf("One");
break;
case 2:
printf("Two");
break;

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

case 3:
printf("Three");
break;
case 4:
printf("Four");
break;
case 5:
printf("Five");
break;
case 6:
printf("Six");
break;
case 7:
printf("Seven");
break;
case 8:
printf("Eight");
break;
case 9:
printf("Nine");
break;
default:
printf("Invalid Number Enter");
break;
}
getch();
}

 C break and continue:


3. break:
The break statement ends the loop immediately when it is encountered.
Its syntax is:
break;

------------------------------------------------------------------------------------------

4. continue:
The continue statement skips the current iteration of the loop and continues with
the next iteration.
Its syntax is:
continue;

Que: W.A.P to demonstrate continue statement in C programming.

#include<stdio.h>
#include<conio.h>
void main()

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

{
int i;
clrscr();
for(i=1;i<=10;i++)
{
if(i==5)
{
continue;
}
printf(" %d",i);
}
getch();
}
Output: 1 2 3 4 6 7 8 9 10 (here 5 is skip due to continue statement)
--------------------------------------------------------------------------------------------------------
Que: W.A.P for making calculator, take two numbers from user and do operation as per user
choice (Addition, Subtraction, Multiplication, Division) use (+,-,*,/ ) for the operations .
#include<conio.h>
#include<stdio.h>
void main()
{
char opr;
int a,b;
float res=0;
int flag=0;
clrscr();
printf("Enter the two numbers:");
scanf("%d%d",&a,&b);
printf("Enter which operation we like to perform (+,-,*,/):");
scanf("%s",&opr);
switch(opr)
{
case '+':
res=a+b;
break;
case '-':
res=a-b;
break;
case '*':
res=a*b;
break;
case '/':
res=a/b;
break;

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

default:
printf("Sorry !! bhai ... You have enter wrong operation try again !!");
flag=1;
break;
}
if(flag!=1)
{ printf("\n Result: %f",res);
}
getch();
}
Output Case-1: Enter the two numbers: 2
2
Enter which operation we like to perform (+,-,*,/): +
Result: 4.0
Output Case-2: Enter the two numbers: 2
2
Enter which operation we like to perform (+,-,*,/): add
Sorry !! bhai ... You have enter wrong operation try again !!

---------------------------------------------------------------------------------------------

Loop in C:

In Programming loop is the repeat a block of code until specified condition.

Types of Loop:

1. for loop:
2. while loop:
3. do..while loop:
1. for loop:
Syntax:
for (initialization; condition; updation)
{
//statements body

Que: W.A.P to Print numbers from 1 to 10

#include <stdio.h>
#include <conio.h>
void main()
{

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

int i;
clrscr();
for (i = 1; i < 11; ++i)
{
printf("%d ", i);
}
getch();
}
Output: 1 2 3 4 5 6 7 8 9 10

Que. W.A.P to find the factorial of the number.


#include<stdio.h>
#include<conio.h>
void main()
{
int i,num,fact=1;
clrscr();
printf("Enter the Number:");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
fact=fact*i;
}
printf("Factorial of %d is %d.",num,fact);
getch();
}
------------------------------------------------------------------------------------------

2. While loop:
Syntax:

Pyramid Programms-

1. Star Patterns:

Example-1:
****
****
****
Program:
#include<stdio.h>
#include<conio.h>
void main()

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

{
int i,j;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=4;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
----------------------------------------------------------------------------------------------------
Example-2:
*
**
***
****
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
----------------------------------------------------------------------------------------------------
Example-3:
****
***
**
*
Program:

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=5-i;j++)
{
printf("*");
}
printf("\n");
}
getch();
}

Example 4: * * * *
* * *
* * * *
* * *
* *
* *
* *
* *
*
*

Solution:
1. Let, consider table with 6 rows i.e. outer loop (i) and 7 columns inner loop (j) and for drawing
the * with respect to particular condition for drawing particular pattern.
2. Divide the number of condition logic to above pattern.
3. Get logic for
First Logic- row 1:
(i===0 && j%3!=0)  print *
Second Logic - row 2,3:
(i===1 && j%3==0) || (i===2 && j%3==0)  print *
Third Logic- row 4,5,6:
Let Consider (I-J)==2
I=3 & j=1 (3-1=2) print *
I=4 & j=2 (4-2=2) print *
I=5 & j=3  (5-3=2) print *

Fourth Logic- row 4,5,6:

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

Let Consider (I+J)==9


I=3 & j=6 (3+6=9) print *
I=4 & j=5 (4+5=9) print *
I=5 & j=4  (5+4=9) print *
Fifth Logic: if no one condition is true as per above four logic print space “ “
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=0;i<6;i++)
{
for(j=0;j<7;j++)
{
if((i==0 && j%3!=0) || (i==1 && j%3==0) || (i==2 && j%3==0) || ((i-j)==2) || ((i+j)==9))
{
printf(" *");
}
else
{
printf(" ");
}
}

printf("\n");
}
getch();
}
---------------------------------------------------------------------------------------------------------------------
2. Number Paterns:

Example-1:
1111
1111
1111
1111
Program:
#include<stdio.h>
#include<conio.h>
void main()
{

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

int i,j;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=4;j++)
{
printf("1”);
}
printf("\n");
}
getch();
}
---------------------------------------------------------------------------
Example-2:
1
11
111
1111

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("1");
}
printf("\n");
}
getch();
}
---------------------------------------------------------------------------
Example-3:
1
22

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

333
4444
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
getch();
}
---------------------------------------------------------------------------
Example-4:
1
23
456
7 8 9 10
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,v=1;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",v);
v=v+1;
}
printf("\n");

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

}
getch();
}
---------------------------------------------------------------------------
Example-5:
4444
333
22
1
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,v=4;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=5-i;j++)
{
printf("%d",v);
}
V=v-1;
printf("\n");
}
getch();
}
OR
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,v;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=5-i;j++)
{
V=5-i;

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

printf("%d",v);
}
printf("\n");
}
getch();
}

Example-6:
1111
222
33
4
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=5-i;j++)
{
printf("%d",i);
}
printf("\n");
}
getch();
}
---------------------------------------------------------------------------
Example-7:
1
12
123
1234
Program:
#include<stdio.h>
#include<conio.h>
void main()
{

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

int i,j;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
getch();
}

OR
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,v=1;
clrscr();
for(i=1;i<=4;i++)
{
v=1;
for(j=1;j<=i;j++)
{
printf("%d",v);
v=v+1;
}
printf("\n");
}
getch();
}
----------------------------------------------------------------------------------------------------------------------------- ---------

3. Alphabets Pattern:

Example-1:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

int ch=65;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("%c",ch);

}
printf("\n");
}
getch();
}

Output:
A
AA
AAA
AAAA

Example-2:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int ch=65;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("%c",ch);

}
ch=ch+1;
printf("\n");
}
getch();
}

Output:

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

A
BB
CCC
DDDD

Example-3:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int ch=65;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=5-i;j++)
{
printf("%c",ch);

}
printf("\n");
}
getch();
}

Output:
AAAA
AAA
AA
A

Example-4:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int ch=65;
clrscr();
for(i=1;i<=4;i++)

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

{
for(j=1;j<=5-i;j++)
{
printf("%c",ch);

}
Ch=ch+1;
printf("\n");
}
getch();
}

Output:
AAAA
BBB
CC
D

Example-5:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int ch=68;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=5-i;j++)
{
printf("%c",ch);

}
Ch=ch-1;
printf("\n");
}
getch();
}

Output:
DDDD

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

CCC
BB
A

Example-6:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int ch=65;
clrscr();
for(i=1;i<=4;i++)
{
Ch=65;
for(j=1;j<=i;j++)
{
printf("%c",ch);
ch=ch+1;
}
Ch=ch+1;
printf("\n");
}
getch();
}

Output:
A
AB
ABC
ABCD
Example-7:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int ch=65;
clrscr();

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

for(i=1;i<=4;i++)
{
Ch=65;
for(j=1;j<=5-i;j++)
{
printf("%c",ch);
ch=ch+1;
}
Ch=ch+1;
printf("\n");
}
getch();
}

Output:
ABCD
ABC
AB
A

Example-6:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int ch=65;
clrscr();
for(i=1;i<=4;i++)
{

for(j=1;j<=i;j++)
{
printf("%c",ch);
ch=ch+1;
}
printf("\n");
}
getch();
}

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

Output:
A
BC
CDE
DEFG

Task-1:
1
24
135
2468
13579
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
if(i%2==0)
{
for(j=2;j<=i+i;j=j+2)
{
printf("%d",j);
}
}
else
{
for(j=1;j<=i+i;j++)
{
if(j%2!=0)
{
printf("%d",j);
}
}

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

}
printf("\n");
}
getch();
}
---------------------------------------------------------------------------------------------
Que: W.A.P to check the number is prime or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,num;
int flag=0;
clrscr();
printf("EnterNo=");
scanf("%d",&num);
if(num==1)
{
flag=1;
}
for(i=2;i<num;i++)
{

if(num%i==0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("Prime");
}
else
{
printf("Not Prime");
}
getch();
}

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

----------------------------------------------------------------------------------------------------------------
W.A.P to convert case of alphabet.
1. If user enter lower case result is upper case.
2. If user enter upper case result is lower case.
3. If user enter any characters rather than alphabets show “Sorry!! Invalid Characters”

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Enter any Character:");
scanf("%c",&ch);
if(ch>=65 && ch<=90)
{
ch=ch+32;
printf("Lower case is : %c",ch);
}
else if(ch>=97 && ch<=122)
{
ch=ch-32;
printf("Upper case: %c",ch);
}
else
{
printf("Sorry !!! Invalid Character Entered!!");
}
getch();
}

Output:

------------------------------------------------------------------------------------------------------------------------------------------------------------
Function in C Programming:
A function is a block of statements that used to perform a specific task.
Types:
1. Pre-define function:
2. User-define Function:
1. Pre-define Function: getch(), pow(),sqrt(), printf(), scanf() etc – These are the functions which
already have a definition in header files (.h files like stdio.h), so we just call them whenever there is a
need to use them.
a. Pow()

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

This pow() function is use for calculate power of any number. It has two integer value
argument first is number and another is its power.
Syntax: pow(number,power);
Example: pow(5,2); //it return answer 25
Pow(2,3); //it return answer 8
b. Sqrt()

This function is use for calculate square root of any number. It has one argument i.e. number
and it return square root of that particular number.
Syntax: sqrt(number);
Example: sqrt(25); //it return 5

Example: W.A.P to demonstrate pre-define function pow() and sqrt() using header file
#include<math.h>

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int num,res;
char ch;
clrscr();
printf("What operation would you like to perform (Square=s/Square root=r):");
scanf("%c",&ch);
printf("Enter the Number:");
scanf("%d",&num);
if(ch=='s')
{
res=pow(num,2);
}
else if(ch=='r')
{
res=sqrt(num);
}
else
{
printf("Invalid choice !!!");
}
printf("Result=%d",res);

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

getch();
}
Output:
What operation would you like to perform (Square=s/Square root=r): s
Enter the number: 2
Result=4
What operation would you like to perform (Square=s/Square root=r): r
Enter the number: 4
Result=2
--------------------------------------------------------------------------------------------------------
2. User-define Function:

Three basic fundamentals in user define function are-


1. Function Prototype
2. Function Call
3. Function Body
1. Function Prototype/Declaration
A function declaration tells the compiler about the number of parameters function
takes, data-types of parameters and return type of function. Putting parameter
names in function declaration is optional in the function declaration, but it is
necessary to put them in the definition.
Syntax: returnType functionName (type1 argument1, type2 argument2, ...);
Example: int max(int, int); // A function that takes two integers as parameters
and returns an integer
2. Calling Function:
Control of the program is transferred to the user-defined function by
calling it.
Syntax: functionName(argument1, argument2, ...);
Example: max(4,5);

3. Function Body/defination:
Function definition contains the block of code to perform a specific task. In our
example, adding two numbers and returning it.
Syntax:
returnType functionName(type1 argument1, type2 argument2, ...)
{
//body of the function
}
Example:

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

int max(int n1, int n2)


{
int max;
if (n1>n2)
{ max=n1;
}
Else
{
max=n2;
}
return max;
}

Types of User-Define Function with respect to parameters and return values are:
a. Function with parameter and return value
b. Function with parameter and no return value.
c. Function with no parameter and return value.
d. Function with no parameter and no return value.

Function with default Argument: function can have default values of it's parameters. It means while
defining a function, We set some values bydefault. It makes function more generic.

Que. W.A.P to demonstrate the function with default argument.

#include<stdio.h>
#include<conio.h>
void Add(int,int);
void main()
{
int num1,num2;
clrscr();
printf("Enter the Value of Num2:");
scanf("%d",&num2);
Add(num1=10,num2);
getch();
}
void Add(int n1,int n2)
{
int sum;
sum=n1+n2;
printf("Addition:%d",sum);

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

Que1: Addition of two numbers using user-define function : Function with


parameters and return value:

#include <stdio.h>
#include<conio.h>
int addNumbers(int,int); // function prototype

void main()
{
int n1,n2,sum;
clrscr();
printf("Enters two numbers: ");
scanf("%d %d",&n1,&n2);
sum = addNumbers(n1, n2); // function call
printf("sum = %d",sum);
getch();
}

int addNumbers(int a, int b) // function body/definition


{
int result;
result = a+b;
return result; // return statement
}

--------------------------------------------------------------------------------------------------------------------

Que2: Addition of two numbers using user-define function : Function with parameters and
no return value:

#include <stdio.h>
#include<conio.h>
void addNumbers(int a, int b); // function prototype
void main()
{
int n1,n2;
clrscr();
printf("Enters two numbers: ");

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

scanf("%d %d",&n1,&n2);
addNumbers(n1, n2); // function call
getch();
}
void addNumbers(int a, int b) // function definition
{
int result;
result = a+b;
printf(“Sum=%d”,result);
}
---------------------------------------------------------------------------------------------------------------------

Que3: Addition of two numbers using user-define function : Function with no parameters
and return value:

#include <stdio.h>
#include<conio.h>
int addNumbers(); // function prototype

void main()
{
int sum;
clrscr();
sum = addNumbers(); // function call
printf("sum = %d",sum);
getch();
}

int addNumbers() // function definition


{
int a,b,result;
printf("Enters two numbers: ");
scanf("%d %d",&n1,&n2);
result = a+b;
return result; // return statement
}
-----------------------------------------------------------------------------------------------------

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

Que4: Addition of two numbers using user-define function : Function with no parameters
and no return value:
#include <stdio.h>
#include<conio.h>
void addNumbers(); // function prototype
void main()
{
clrscr();
addNumbers(); // function call
getch();
}
void addNumbers() // function definition
{
int a,b,result;
printf("Enters two numbers: ");
scanf("%d %d",&n1,&n2);
result = a+b;
printf("sum = %d",result);
}

----------------------------------------------------------------------------

Que1: W.A.P to find the factorial of number using function.

#include<stdio.h>
#include<conio.h>
int factorial(int);
void main()
{
int Rfact,num;
clrscr();
printf("Enter the Number:");
scanf("%d",&num);
Rfact=factorial(num);
printf("Factorial of %d is %d.",num,Rfact);
getch();
}
int factorial(int num)

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

{
int i,fact=1;
for(i=1;i<=num;i++)
{
fact=fact*i;
}
return fact;
}
---------------------------------------------------------------------------------------------------------------------

1. Call by Value:

Que. W.A.P to demonstrate call by value , by swaping of two number using swap function.

#include<stdio.h>
#include<conio.h>
void swap(int,int);
void main()
{
int a=2,b=4;
clrscr();
printf("Before Swaping:\n \t value of a=%d and b=%d",a,b);
printf("\n-----------------------");
swap(a,b);
getch();
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("\nAfter Swaping: \n \t value of a=%d and b=%d",a,b);
}
2. Call by Reference:

#include<stdio.h>
#include<conio.h>
void show(int *,int *);
void main()
{
int a=2,b=4;
clrscr();
printf("Before Swaping:\n \t value of a=%d and b=%d",a,b);
printf("\n-----------------------");

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

show(&a,&b);
printf("\nAfter Swaping: \n \t value of a=%d and b=%d",a,b);
getch();
}
void show(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
Que. W.A.P to find the factorial of number using function.

Recursion Function in C:
A function that calls itself is known as a recursive function. And, this technique is known as
recursion.
Syntax:
int main()
{
recursion();
}
void recursion()
{
Statements…;
recursion(); /* function calls itself */
}

Que: W.A.P to find the Factorial of Number Using Recursive Function:


#include<stdio.h>
#include<conio.h>
int factorial(int);
void main()
{
int fact;
int n=5;
clrscr();
fact=factorial(n); //function call
printf("Factorial=%d",fact);
getch();
}
int factorial(int n) //function body
{
if(n==1)
{
return 1;
}

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

return (n*factorial(n-1));
}
Output: Factorial=120
Execution:
factorial(5) call
5!=1 so 5*factorial(4)
4!=1 so 5*(4*factorial(3))
3!=1 so 5*4*3*(3*factorial(2))
2!=1 so 5*4*3*2(2*factorial(1))
1=1 so 5*4*3*2*1 //(5*4*3*2*1)=120
-----------------------------------------------------------------------------------------------
Que: W.A.P to find GCD of two Number Using Recursive Function:

----------------------------------------------------------------------------------------------------------------------------
Pointer in C:
The pointer in C language is a variable which stores the address of another variable. This
variable can be of type int, char, array, function, or any other pointer.
Consider, One Example-
int a=10;
int *p=&a; // declaring pointer variable.
This pointer variable p store the address of variable a.
Note:
&  Use for store the address of variable
*  Use for access the value at address.
QUE: W.A.P to Demonstrate Pointer:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10;
int *p; //declaring pointer Variable
clrscr();
p=&a; //assign address of another variable in pointer variable.
printf("Address of a is =%d",&a);
printf("Address of P is =%d",p);
printf("\nValue at address of P is:%d",*p);
getch();
}
Output: Address of a is= 8005
Address of P is= 8005
Value at address of P is: 10
Need of Pointer:
1. To pass arguments by reference.

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

#include<stdio.h>
#include<conio.h>
void swap(int *,int *);
void main()
{
int a=10;
int b=20;
clrscr();
printf("Before Swap: \n \t Value of A=%d and B=%d",a,b);
swap(&a,&b);
printf("\nAfter Swap: \n \t Value of A=%d and B=%d",a,b);
getch();
}
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a= *b;
*b=temp;
}
Output: Before Swap:
Value of A=10 and B=20
After Swap:
Value of A=20 and B=10
2. For accessing array elements.

int main()
{
int arr[] = { 100, 200, 300, 400 };
// Compiler converts below to *(arr + 2).
printf("%d ", arr[2]);

// So below also works.


printf("%d\n", *(arr + 2));
return 0;
}

3. To return multiple values in function.


#include<stdio.h>
#include<conio.h>
void cal(int *,int *,float *,float *,float *,float *);
void main()
{
int num1,num2;
float add,sub,mul,div;
clrscr();
printf("Enter the two number:");

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

scanf("%d%d",&num1,&num2);
cal(&num1,&num2,&add,&sub,&mul,&div);
printf("Add=%f",add);
printf("\nSub=%f",sub);
printf("\nMul=%f",mul);
printf("\nDiv=%f",div);
getch();
}
void cal(int *num1,int *num2,float *add,float *sub,float *mul,float *div)
{
*add=*num1+*num2;
*sub=*num1-*num2;
*mul=*num1**num2;
*div=*num1/ *num2;
}
Output:
Enter the two Number: 20 10
Add=30.00
Sub=10.00
Mul=200.00
Div=2.00

-------------------------------------------------------------------------------------------------------------

Array: Array is the collection of elements having similar data type. Also having contiguous
memory allocation.

Types:
1. One dimensional Array:
 Array declaration by specifying size

e.g. int var[10];


------ it create 10 variables i.e. var[0],var[1],var[2]……upto var[9].

 declare an array of user specified size

int n = 5;
int var[n];
------ it create 5 variables i.e. var[0],var[1],var[2]……upto var[4].

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

 Array Scanning:
Method 1:
int a[10];
printf(“Enter the element 1:”);
scanf(“%d”,&a[0]);
printf(“Enter the element 2:”);
scanf(“%d”,&a[1]);
printf(“Enter the element 3:”);
scanf(“%d”,&a[2]);
-----
----- and so on.
Method 2:

int a[10],i;
for(i=0;i<=9;i++)
{
Printf(“Enter the %dth element:”,i+1);
Scanf(“%d”,a[i]);
}
 Array Printing:
Method 1:
int a[10];
printf(“Element 1 is %d”,a[0]);
printf(“Element 2 is %d”,a[1]);
printf(“Element 3 is %d”,a[2]);
-------------
----------- and so on.
Method 2:

int a[10],i;
for(i=0;i<=9;i++)
{
Printf(“Element %d is: %d”,i+1,a[i]);
}

2. Two dimensional Array:


 Array declaration by specifying size
Syntax: int var[i][j];
--- where i=row …j=column
e.g. int var[2][3];
------ it create array of two rows and three columns.

a[o][o] a[0][1] a[0][2]


a[1][0] a[1][1] a[1][2]

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

 Array Initialization
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
 declare an array of user specified size

int n = 2,m = 2;
int var[n][m];
------ it create array of two row and two columns.

a[o][o] a[0][1]
a[1][0] a[1][1]

 Array Scanning:
Method 1:
int a[2][2];
printf(“Enter the element:”);
scanf(“%d”,&a[0][0]);
printf(“Enter the element:”);
scanf(“%d”,&a[0][1]);
printf(“Enter the element:”);
scanf(“%d”,&a[1][0]);
----- and so on.
Method 2:

int a[2][2];
int i,j;
for(i=0;i<2;i++)
{
For(j=0;j<2;j++)
{
Printf(“Enter the element“);
Scanf(“%d”,a[i][j]);
}
}
 Array Printing:
Method 1:
int a[2][2];
printf(“Element 1 is %d”,a[0][0]);
printf(“Element 2 is %d”,a[0][1]);
printf(“Element 3 is %d”,a[1][0]);
-------------
----------- and so on.
Method 2:

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

int a[2][2];
int i,j;
for(i=0;i<2;i++)
{
For(j=0;j<2;j++)
{
Printf(“%d”,a[i][j]);
}
Printf(“\n”);
}
----------------------------------------------------------------------------------------------------------------
Que.W.A.P to reverse of the number.
#include<stdio.h>
#include<conio.h>
void main()
{
int num,rev=0,rem;
clrscr();
printf("Enter the Number:");
scanf("%d",&num);
while(num!=0)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
printf("Reverse=%d",rev);
getch();
}

Exercise on One dimensional Array:

Que1: W.A.P to demonstrate array take the array element from user and display on user screen.

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
int a[10];
clrscr();
for(i=0;i<10;i++)

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

{
printf("Enter the %dst element:",i+1);
scanf("%d",&a[i]);
}
printf("\n-----------------------------------------");
printf("\nYour entered Elements are:");
for(i=0;i<10;i++)
{
printf("\n%d",a[i]);
}
getch();
}

Que2: W.A.P to take the array element from user and display sum of all elements on user
screen.

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
int a[10],sum=0;
clrscr();
for(i=0;i<10;i++)
{
printf("Enter the %dst element:",i+1);
scanf("%d",&a[i]);
}
printf("\n-----------------------------------------");
printf("\nYour entered Elements are:");
for(i=0;i<10;i++)
{
printf("\n%d",a[i]);
sum=sum+a[i];
}
printf("\n------------------------------------------");
printf("\nSum of all elements:%d",sum);
getch();
}
Que3: W.A.P to read n number of values in an array and display it in reverse order..

#include<stdio.h>
#include<conio.h>
void main()
{

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

int i;
int n;
int a[100];
clrscr();
printf("Enter how may number you want in array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %dst element:",i+1);
scanf("%d",&a[i]);
}
printf("--------------------------------");
printf("\nEntered elements are:");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
printf("\n-------------------------");
printf("\nReverse Order are:");
for(i=n-1;i>=0;i--)
{
printf("\n%d",a[i]);
}
getch();
}

Que4: W.A.P to Search an element in one dimensional array using linear search method.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[]={1,2,9,4,8,6,7,0};
int i,search,found=0;
clrscr();
printf("Array Elements is:\n");
for(i=0;i<=7;i++)
{
printf(" %d\n",a[i]);
}
printf("Enter the Element to be search:");
scanf("%d",&search);
for(i=0;i<=7;i++)
{
if(a[i]==search)

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

{
printf("Number %d is found at location %d",search,i+1);
found=1;
break;
}
}
if(found==0)
{
printf("Number is not found");
}
getch();
}
Output Case-1: Array element is: 1 2 9 4 8 6 7 0
Enter the element to be search: 9
Number 9 is found at location 3
Output Case-2: Array element is: 1 2 9 4 8 6 7 0
Enter the element to be search: 5
Number is not found

Que5. Write a program in C to copy the elements of one array into another array.

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
int a[10];
int b[10];
clrscr();
for(i=0;i<10;i++)
{
printf("Enter the %dst element:",i+1);
scanf("%d",&a[i]);
}
printf("\n-----------------------------------------");
printf("\nYour entered Elements in array1 is:");
for(i=0;i<10;i++)
{
printf(" %d",a[i]);
}
printf("\n------------------------------------------\n");
printf("Copied elements from array1 to array2 are:\n");
for(i=0;i<10;i++)
{

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

b[i]=a[i];
printf(" %d",b[i]);
}

getch();
}

Que6. W.A.P to Find out max and min element from the array.

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
int a[10];
int b[10];
int max,min;
clrscr();
for(i=0;i<10;i++)
{
printf("Enter the %dst element:",i+1);
scanf("%d",&a[i]);
}
printf("\n-----------------------------------------");
printf("\nYour entered Elements in array1 is:");
for(i=0;i<10;i++)
{
printf(" %d",a[i]);
}
max=a[0];
min=a[0];
for(i=0;i<10;i++)
{
if(a[i]>max)
{
max=a[i];
}
if(a[i]<min)
{
min=a[i];
}
}
printf("\nMax=%d",max);
printf("\nMin=%d",min);

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

getch();
}

Que7. W.A.P to Find out Number of odd and even element from the array.

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
int odd=0,even=0;
int a[10];
int b[10];
clrscr();
for(i=0;i<10;i++)
{
printf("Enter the %dst element:",i+1);
scanf("%d",&a[i]);
}
printf("\n-----------------------------------------");
printf("\nYour entered Elements in array1 is:");
for(i=0;i<10;i++)
{
printf(" %d",a[i]);
}
for(i=0;i<10;i++)
{
if(a[i]%2==0)
{
even++;
}
else
{
odd++;
}

}
printf("\nEven numbers are %d and odd is %d",even,odd);
getch();
}
Exercise on two dimensional Array:

Que1. Write a program in C to store elements in an two dimensional array and print it.

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int a[2][3];
clrscr();
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("Enter element:");
scanf("%d",&a[i][j]);
}
}
printf("\n------------------------------\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %d",a[i][j]);
}
printf("\n");
}
getch();
}

Que2. Write a program in C to store elements in an two dimensional array and print sum of all
elements in it.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int sum=0;
int a[2][3];
clrscr();
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("Enter element:");

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

scanf("%d",&a[i][j]);
}
}
printf("\n------------------------------\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %d",a[i][j]);
sum=sum+a[i][j];
}
printf("\n");
}
printf("\n--------------------------------\n");
printf("Sum of all elements in array is %d",sum);
getch();
}

Que3. W.A.P to find the sum of two matrixes of two dimensional array.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int a[2][3];
int b[2][3];
int c[2][3];
clrscr();
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("Enter element%d for Array1:",i+1);
scanf("%d",&a[i][j]);
}
}
printf("\n-------------------------------\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %d",a[i][j]);
}

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

printf("\n");
}
printf("\n------------------------------\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("Enter element%d for Array2:",i+1);
scanf("%d",&b[i][j]);
}
}
printf("\n----------------------------\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %d",b[i][j]);
}
printf("\n");
}
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\n--------------------------\n");
printf("Sum of two array Matrix is as follows:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %d",c[i][j]);
}
printf("\n");
}
getch();
}

Que4. W.A.P to find the two matrixes of two dimensional array are equal or not.

#include<stdio.h>
#include<conio.h>

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

void main()
{
int i,j;
int equal=1;
int a[2][3];
int b[2][3];
clrscr();
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("Enter element%d for Array1:",i+1);
scanf("%d",&a[i][j]);
}
}
printf("\n-------------------------------\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %d",a[i][j]);
}
printf("\n");
}
printf("\n------------------------------\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("Enter element%d for Array2:",i+1);
scanf("%d",&b[i][j]);
}
}
printf("\n----------------------------\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %d",b[i][j]);
}
printf("\n");
}
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

{
if(a[i][j]!=b[i][j])
{
equal=0;
}
}
}
if(equal==1)
{
printf("Both Array are exactly equal !!!");
}
else
{
printf("Both Array are not equals !!!!");
}
getch();
}

Que.W.AP to find the transpose of matrix.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int a[2][3];
int b[2][3];
clrscr();
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("Enter element%d for Array1:",i+1);
scanf("%d",&a[i][j]);
}
}
printf("\n-------------------------------\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %d",a[i][j]);
}
printf("\n");
}

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

printf("\n------------------------------\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
b[i][j]=a[j][i];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(" %d",b[i][j]);
}
printf("\n");
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------
String Handling:(String Functions)
String is an collection (array) of characters. The difference between a character array and
a string is the string is terminated with a special character ‘\0’.

We will see how to compare two strings, concatenate strings, copy one string to another &
perform various string manipulation operations. We can perform such operations using the pre-
defined functions of “string.h” header file.

How to declare strings: Declaring a string is as simple as declaring a one dimensional array. Below is
the basic syntax for declaring a string.

Syntax: char str_var[size];

1. char name [] =”ADITYA”; //string initialization. Size=6 i.e. name[6]


2. char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; //string initialization with characters
3. char name [20]; //name is the string variable contain any string having
size less than or equal to 20 characters.

Memory allocation of string:

How to work with strings in C programming:

Que1: W.A.P to illustrate strings

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

#include<stdio.h>
#include<conio.h>
Void main()
{
char str[] = "Adi N Tech";
printf("%s",str);
getch();
}
Output: Adi N Tech
Que2: W.A.P to read string from user.
#include<stdio.h>
#include<conio.h>
Void main()
{
char str[20];
clrscr();
printf(“Enter the String:”);
scanf(“%s”,&str);
printf("Entered String is %s",str);
getch();
}
Output: Enter the string: Adi
Entered string is Adi

How to use the pre-defined string handling functions:

NO. FUNCTION DESCRIPTION


1) strlen(string_name) returns the length of string name.
2) strcpy(destination, source) copies the contents of source string to destination string.
3) strcat(first_string, second_string) concats or joins first string with second string. The result of the string is
stored in first string.
4) strcmp(first_string,second_string) compares the first string with second string. If both strings are same, it
returns 0.
5) strrev(string) returns reverse string.
6) strlwr(string) returns string characters in lowercase.
7) strupr(string) returns string characters in uppercase.
1. Strlen():

#include<stdio.h>
#include<conio.h>

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

void main ()
{
char str[20];
int len;
clrscr();
printf("Enter any String:");
scanf("%s",&str);
len=strlen(str);
printf("Length of String is %d",len);
getch();
}
Output: Enter any String: Adi
Length of String is 3
2. Strcpy():
The strcpy(destination, source) function copies the source string in destination.

#include<stdio.h>
#include<conio.h>
void main ()
{
char str1[20];
char str2[20];
clrscr();
printf("Enter any String1:");
scanf("%s",&str1);
printf("Enter any String2:");
scanf("%s",&str2);
strcpy(str1,str2);
printf("String copied successfully !! \n\tString1=%s\n\tString2=%s",str1,str2);
getch();
}

3. Strcat():

#include<stdio.h>
#include<conio.h>
void main ()
{
char str1[20];
char str2[20];
clrscr();
printf("Enter any String1:");
scanf("%s",&str1);
printf("Enter any String2:");
scanf("%s",&str2);

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

strcat(str1,str2);
printf("String concatenation successfully !! \n\tString1=%s\n\tString2=%s",str1,str2);
getch();
}
4. Strcmp():

#include<stdio.h>
#include<conio.h>
void main ()
{
char str1[20];
char str2[20];
clrscr();
printf("Enter any String1:");
scanf("%s",&str1);
printf("Enter any String2:");
scanf("%s",&str2);
if(strcmp(str1,str2)==0)
{
printf("Both Strings are equal !! ");
}
else
{
printf("Both Strings are not equal !!");
}
getch();
}
5. Strrev():
#include<stdio.h>
#include<conio.h>
void main ()
{
char str[20];
clrscr();
printf("Enter any String:");
scanf("%s",&str);
printf("Reverse of String is %s",strrev(str));
getch();
}
6. Strlwr():
#include<stdio.h>
#include<conio.h>
void main ()
{
char str[20];
clrscr();
printf("Enter any String:");

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

scanf("%s",&str);
printf("Lower case of String is %s",strlwr(str));
getch();
}
7. Strupr():
#include<stdio.h>
#include<conio.h>
void main ()
{
char str[20];
clrscr();
printf("Enter any String:");
scanf("%s",&str);
printf("Upper case of String is %s",strupr(str));
getch();
}

Practice Program On String Function:

----------------------------------------------------------------------------------------------------------------------------

Storage classes in C:

----------------------------------------------------------------------------------------------------------------------------

Que1: Find the Occurrence of Character in String.

#include<stdio.h>
#include<conio.h>
void main ()
{
char str[20],ch;
int i,count=0;
clrscr();
printf("Enter any String:");
scanf("%s",&str);
printf("Enter the Character:");
scanf("%s",&ch);
for(i=0;i<=strlen(str);i++)
{
if(str[i]==ch)
{
count++;
}
}
printf("%c is available %d times in string !!",ch,count);

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

getch();
}
Output: Enter any String: Programmers
Enter the Character: m
m is available 2 time in String !!
---------------------------------------------------------------------
Que2: W.A.P to search vowels, consonants, digits and spaces in given string statement.
#include <stdio.h>
#include<conio.h>
void main()
{
char line[150];
int i, vowels=0, consonants=0, digits=0, spaces=0;
clrscr();
printf("Enter a line of string: ");
gets(line);
for(i=0;i<=strlen(line);i++)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' ||
line[i]=='U')
{
vowels++;
}
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
{
consonants++;
}
else if(line[i]>='0' && line[i]<='9')
{
digits++;
}
else if (line[i]==' ')
{
spaces++;
}
}
printf("Vowels: %d",vowels);
printf("\nConsonants: %d",consonants);
printf("\nDigits: %d",digits);
printf("\nWhite spaces: %d", spaces);
getch();
}
----------------------------------------------------------------------------------------------------------------------------

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

Structure:
Arrays allow to define similar type of variables that can hold several data items of the same kind
(data types). Similarly structure is another user defined data type available in C that allows to create a
elements with different data items (data types). Structures are used to represent a record.
Suppose you want to keep record details of employees in company. You might want to track the
following attributes about each employee –

4. Name
5. Id
6. Salary
7. Designation
8. Department… etc.

Declaring Structure: To define a structure, you must use the struct keyword –

Syntax: struct str_name


{
Data_type var1;
Data_type var2;
Data_type var3;
} str_var1; //str_var here optional

Example:

struct employee
{
Char empName[20];
Int empId;
Float empSalary;
};
struct employee emp1; //create str_var
struct employee emp2;

Que. W.A.P in C to demonstrate structure.

#include <stdio.h>
#include <conio.h>
struct employee {
char name[50];
int id;
float salary;

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

} e1;

void main() {
printf("Enter information:\n");
printf("Enter name: ");
scanf(“%s”,&e1.name);

printf("Enter Employee id: ");


scanf("%d", &e1.id);

printf("Enter salary: ");


scanf("%f", &e1.salary);

printf("Displaying Information:\n");
printf("Name: ");
printf("%s\n", e1.name);
printf("Id: %d\n", e1.id);
printf(“Salary: %f\n", e1.salary);

getch();
}

Output
Enter information:
Enter name: Adi
Enter Employee Id: 101
Enter salary: 4.5
Displaying Information:
Name: Adi
Roll number: 101
Marks: 4.5
Union:
A union is a special data type available in C that allows to store different data types in the
same memory location. You can define a union with many members, but only one member can contain
a value at any given time. Unions provide an efficient way of using the same memory location
variables for multiple-variables.

Syntax:

union union_name {
int i;
float f;
char str[20];
} union_var;

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

Example:

union Data {
int i;
float f;
char str[20];
} data;

----------------------------------------------------------------------------------------------------------------------------

File Handling:
Que: Read the number from the user and store in a file info.txt.
#include"stdio.h"
#include"conio.h"
void main()
{
FILE *fp;
int num;
char choice='y';
clrscr();
fp=fopen("info.txt","w");
while(choice=='y')
{
printf("Enter the number :");
scanf("%d",&num);
fprintf(fp,"%d\n",num);
printf("Do you wish to continue[y/n] :");
fflush(stdin);
scanf("%c",&choice);
}
printf("Information stored in a file info.txt");
fclose(fp);
getch();
} */

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

//append the content of file


/*void main()
{
FILE *fp;
int num;
char choice='y';
clrscr();
fp=fopen("info.txt","a");
while(choice=='y')
{
printf("Enter the number :");
scanf("%d",&num);
fprintf(fp,"%d\n",num);
printf("Do you wish to continue[y/n] :");
fflush(stdin);
scanf("%c",&choice);
}
printf("Information stored in a file info.txt");
fclose(fp);
getch();
} */

/*Read the info from the file and display on the user screen*/
/*void main()
{
FILE *fp;
int num;
clrscr();
fp=fopen("info.txt","r");
while(fscanf(fp,"%d",&num)!=EOF)
{
printf("%d\n",num);
}
fclose(fp);

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

getch();
}*/

//binary files

/*Read number from the user and store in a binary file infobin.txt*/
/*void main()
{
FILE *fp;
int num;
char choice='y';
clrscr();
fp=fopen("infobin.txt","wb");
while(choice=='y')
{
printf("Enter the number :");
scanf("%d",&num);
fwrite(&num,sizeof(num),1,fp);
printf("Do you wish to continue[y/n] :");
fflush(stdin);
scanf("%c",&choice);
}
printf("Information stored in a file infobin.txt");
fclose(fp);
getch();
}*/

//read the number from binary file and display on the user screen
/*void main()
{
FILE *fp;
int num;
clrscr();
fp=fopen("infobin.txt","rb");

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

while(fread(&num,sizeof(num),1,fp)==1)
{
printf("%d\n",num);
}
fclose(fp);
getch();
}*/

/*read name age and bs using structure*/


/*void main()
{
struct emp
{
char name[20];
int age;
float bs;
};
struct emp e;
FILE *fp;
char choice='y';
clrscr();
fp=fopen("empinfo.txt","wb");
while(choice=='y')
{
printf("Enter the name age & bs :");
scanf("%s%d%f",&e.name,&e.age,&e.bs);
fwrite(&e,sizeof(e),1,fp);
printf("Do you wish to continue[y/n]:");
fflush(stdin);
scanf("%c",&choice);
}
fclose(fp);
printf("Information stored successfully");
getch();
} */

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

/*read the info from the file and display on the user screen*/
void main()
{
struct emp
{
char name[20];
int age;
float bs;
};
struct emp e;
FILE *fp;
clrscr();
fp=fopen("empinfo.txt","rb");
while(fread(&e,sizeof(e),1,fp)==1)
{
printf("%s %d %f\n",e.name,e.age,e.bs);
}
fclose(fp);
getch();
}
---------------------------------------------------------------------------------------------------------------

- Adi N Tech ( youtuber )


C- Programming Language Be Coder…. Be Programmer

- Adi N Tech ( youtuber )

You might also like