0% found this document useful (0 votes)
20 views

One Shot

One shot fir programming in c

Uploaded by

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

One Shot

One shot fir programming in c

Uploaded by

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

PROGRAMMING IN C

Unit 2

ONE SHOT
Unit-2 Syllabus
 Control structures: Decision statements; if and switch statement; Loop
control statements: while, for and do while loops, jump statements,
break, continue, goto statements.
 Arrays: Concepts, One dimensional array, declaration and initialization
of one dimensional arrays, two dimensional arrays, initialization and
accessing, multi dimensional arrays.
 Functions: User defined and built-in Functions, storage classes,
Parameter passing in functions, call by value, Passing arrays to functions:
idea of call by reference, Recursion.
 Strings: Arrays of characters, variable length character strings, inputting
character strings, character library functions, string handling functions.
A Basic C Preprocessor :-
#include
Program Preprocessors are programs that process the source
code before compilation

Header File :-
<stdio.h>
It allows us to conduct input and output operations in C.
printf() and scanf()

Main Function :-
The main function serves as the starting point for program
execution.
Decession Control Statements

If statement
If – else statement
Switch statement
If statement
Example:- Syntax:-
#include <stdio.h>
 if(condition)
int main()
 {
{

int age= 9;
 // Statements to execute if condition is true
if (age > 18) {
printf("You are an adult");  }
}
if (age < 18) {
printf("You are not an adult");
}
return 0;
}
Flowchart
of
If statement
#include <stdio.h>
void main(){

int scount = 0;

Program for a if (scount == 0){


printf("Mass Bunk Successful");
mass bunk }
if (scount != 0){
printf("Mass Bunk not succesful");
}
}
#include <stdio.h>
void main(){

int scount = 0;
Program for a
mass bunk if (scount == 0){
printf("Mass Bunk Successful");
(More Correct }
One) else{
printf("Mass Bunk not succesful");
}
}
If- else statement
Example:- Syntax:-
 if(condition)
#include <stdio.h>
int main()
 {
{ 
int age= 9;  // Statements to execute if condition
is true
if (age > 18) {
printf("You are an adult");  }
} else {
else { // Statements to execute if condition
printf("You are not an adult"); is false
} }
return 0;
}
Flowchart of
If- else
statement
Lets consider you are a teacher and you have
to give grades to students

Conditions :

 Marks >= 90 => Grade A


 Marks >= 80 => Grade B
Marks >= 70 => Grade C
Marks >= 60 => Grade D
Marks < 60 => Fail
#include <stdio.h>
void main(){

int marks= 50;


if (marks>= 90){
printf("Grade A");}
if (marks >= 80){
1st thought printf("Grade B");}
if (marks >= 70){
printf("Grade C");
if (marks >= 60){
printf("Grade D");}
else {
printf("Fail);}
}
#include <stdio.h>
void main(){

int marks= 50;

if (marks>= 90){
printf("Grade A");}
The Correct else if (marks >= 80){

Solution printf("Grade B");}


else if (marks >= 70){
printf("Grade C");
else if (marks >= 60){
printf("Grade D");}
else {
printf("Fail);}
}
Else-if statement
#include <stdio.h> Example: Syntax:-
int main(){  if(condition) {
int age= 9;  // Execute if condition is true
if (age > 60) {  }
printf("You are Old");
} else if (condition) {
else if (age > 18) {
// Execute if condition is true
printf("You are an adult");
}
} else {
else {
// Execute if all above conditions
printf("You are child");
} are false
}
return 0;
}
Flowchart of
If- else
statement
Switch Statement
The replacement for if-else-if ladder.
Switch statement
#include <stdio.h> Example: Syntax:-
int main(){  switch(expression)
{
{
int num = 2;
case value1: statement_1;
switch (num) {
break;
case 1:
case value2: statement_2;
printf("Value is 1 is \n"); break;
break; .
case 2: case value_n: statement_n;
printf("Value is 2 is \n");
break; break;
default :
printf("Value is niether 1 nor 2\n");
break; default: default_statement;
}
return 0;
Break;
} }
Flowchart of
switch statement
Switch V/S If else if

Switch if else if
 It executes the different cases  It executes the different blocks
on the basis of the value of the based on the condition
switch variable. specified.
 It can only evaluate the int or  It can evaluate any type of
char type expressions. expression.
 Faster and easier to read for  It can get messy when there
the large number of conditions. are lots of conditions.
The one line for
Ternary Operator if-else
Syntax expression 1 ? expression 2 : expression 3

“if expression 1 is true (that is, if its value


is non-zero), then the value returned will
be expression 2, otherwise the value
returned will be expression 3
Ternary
Operator
Example
Now lets write a program to
print numbers from 1 to 5 .
What if you have to print till
100?
Now comes the concept of
Loops
Loops Classification
For Loop
Syntax:-
Example:
 for(initialization; check/test expression;
#include <stdio.h> updation)
 {
 // body consisting of multiple statements
int main()
{  }

for (int i = 1; i<= 5; i++)


{
printf("%d\n", i);
}

return 0;
}
Write a program
to print all even
numbers between
1 to 100 using for
loop
Flowchart of
for loop
While Loop
Example: Syntax:-
#include <stdio.h>  while (test expression)

int main()  {
 // body consisting of multiple statements
{ int i = 1;
 }
while (i<= 5)
{
printf("%d\n", i);
i++;
}

return 0;
}
Write a program
to print all even
numbers between
1 to 100 using
while loop
Flowchart of
while loop
Do-while Loop
Example:
Syntax:-
#include <stdio.h>  do {

int main()

 // body of do-while loop
{ int i = 1;

do {  } while (condition);
printf("%d\n", i);
i++;
}

while (i<= 5);

return 0;
}
Flowchart of
do-while loop
While Loop V/S Do-while Loop

While Do-while
 The test condition is  The test condition is checked after
checked before the loop body executing the body.
is executed.
 The body of the do…while loop is
 When the condition is false, executed at least once even
the body is not executed not when the condition is false.
even once.
 It is a type of post-tested or exit-
 It is a type of pre-tested or controlled loop.
entry-controlled loop.
 Semicolon is required at the end.
 Semicolon is not required.
Jump Statements

Continue statement
Break statement
Goto statement
Continue Statement

The continue statement is use to :-

 Skip the current iteration.


 Control moves to the next iteration.
 Statements after the continue statement
in the loop are not executed.
Write a program
to print all even
numbers between
1 to 10 except
one number enter
by user.
Break Statement

The break statement :-

 It is use to break out of the loop.


 It can be used inside loops or switch statements to bring the control
out of the block.
 It can only break out of a single loop at a time.
The Goto Statement

The C goto statement is a jump statement


which is sometimes also referred to as
an unconditional jump statement. The goto
statement can be used to jump from
anywhere to anywhere within a function.
Write a program
to print all even
numbers between
1 to 10 except
one number enter
by user.
Disadvantages of Goto Statement

 The use of the goto statement is highly discouraged as it makes the


program logic very complex.

 The use of goto makes tracing the flow of the program very difficult.

 The use of goto can be simply avoided by


using break and continue statements.
Write a program
to print the
prime numbers
between 1 to 50
ARRAYS
Lets write a program to print
marks of 5 students
Types of
data types
in C
What is an Array ?

Derived data type .


Fixed-size .
Collection of similar data items .
Stored in contiguous memory locations .
data_type array_name [size];

or
Array
Declaration data_type array_name
[size1] [size2]...[sizeN];
Array Initialization
With Declaration
data_type array_name [size] = {value1, value2, ... valueN};

With Declaration without size


data_type array_name[] = {1,2,3,4,5};

Using loops
for (int i = 0; i < N; i++) {
array_name[i] = valuei;
}
Accessing
Elements
of Array

array_name [index];
WAP in which you take marks
of 5 subjects from the user and
print the maximum marks.
Program for
maximum
mark
Types of Arrays

1-D Array
2-D Array
Multi-Dimensional Array
 A Two-Dimensional array or 2D array
in C is an array that has exactly two
dimensions. They can be visualized in
2-D Array in C the form of rows and columns
organized in a two-dimensional
plane.
2-D Array

array_name[rows] [columns];
3-D Array

array_name [size1] [size2] [size3];


Write a program to get the
Transpose of a matrix
Transpose of a
matrix
Functions
Functions

Derived Data type


Enclosed with {}
Perform Specific Task
Provides Code reusibility
Understanding Function in C

Declaration
Defination
Calling
Declaration
of Function

return_type
name_of_the_function
(parameter_1,
parameter_2);
Defining a
Function
 return_type function_name
(para1_type para1_name,
para2_type para2_name)
{
 // body of the function
}
Calling a
Function
Write a program to give sum
of two numbers using
function
Built-in Vs User Defined Functions

Built-in Function User-Defined Function


 Also known as Library Function
 Already existed in the compiler  Created by the user
package
 Any name can be given by the user
 Has a fixed pre-defined name
 Must be declared and defined
 Directly useable without being before being used
defined
 E.g) pow(), sqrt(), strcmp(), strcpy()
etc.
Actual and
Formal
Parameters
Call By Value

Output
Call By Reference

Output
Call By Value VS Call By Reference

Call By Value Call By Reference


 There are two copies of  Both the actual and formal
parameters stored in different parameters refer to the same
memory locations.One is the locations.
original copy and the other is  Any changes made inside the
the function copy.
function are actually reflected
 Any changes made inside in the actual parameters of the
functions are not reflected in caller.
the actual parameters of the
caller.
Passing
Arrays to
Function
Write a program to get the
sum of all the elements of
an array using function
Sum of all
elements of
an Array
Strings
String

It is an array of characters


Terminated with a null character i.e ' \0'
The difference between a character array and a
C string is that the string in C is terminated with a
unique character ‘\0’.
String Declaration in C

Declaring a string in C is as simple as declaring a


one-dimensional array.

char str[] = "Shivang";


String Initialisation
It is also similar as initalising an array
char str[8] = { 'S','h','I','v','a','n','g','\0'};
char str[] = { 'S','h','I','v','a','n','g','\0'};

Also it can be initalise as:-


char str[] = "Shivang";
char str[30] = "Shivang";
Write a Program
in which you take
user name input and
greet him
Gets and Puts

Scanf() is not capable of receiving multi-word strings , So


this problem is solved by using function gets().
Gets() is use with its counterpart puts
Puts() work as a replaemment for printf in case of strings.

Note :- Scanf() can also receive multi-word string using scanset

scanf("%[^\n]s", str);
Function Name Description

strlen(string_name) Returns the length of string name.

C Library strcpy(s1, s2)

strcmp(str1, str2)
Copies the contents of string s2 to string s1.

Compares the first string with the second string. If

<String.h>
strings are the same it returns 0.

Concat s1 string with s2 string and the result is stored


strcat(s1, s2)
in the first string.

strlwr() Converts string to lowercase.

strupr() Converts string to uppercase.

strstr(s1, s2) Find the first occurrence of s2 in s1.


More Library
Functions
Storage Classes
and Recursion
Storage Classes

Automatic storage class


Register storage class
Static storage class
External storage class
Automatic Storage Class

Storage: Memory.
Default value: An unpredictable value, often called a
garbage value.
Scope: Local to the block in which the variable is
defined.
Life: Till the control remains within the block in which the
variable is defined.
Register Storage Class

Storage: CPU registers.


Default value: Garbage value.
Scope: Local to the block in which the variable is
defined.
Life: Till the control remains within the block in which the
variable is defined.
Static Storage Class

Storage: Memory.
Default value: Zero.
Scope: Local to the block in which the variable is
defined.
Life: Value of the variable persists between different
function calls.
How Static
presisits
between
different
Functions
External Storage Class

Storage: Memory.
Default value: Zero.
Scope: Global.
Life: As long as the program’s execution doesn’t come to
an end.
RECURSION
Recursion

When a function calls itself from within its body it is known


as a recursive function.
A function that calls itself directly or indirectly is called a
recursive function
such kind of function calls are called recursive calls.
Lets write a program to get the
sum of numbers from 0 to N
in which N is given by the user
Program to
get sum till
number N
Write a program to get
factorial of a number using
recursion
Factorial Of
a number
Self Practice
Questions
1. Make a calculator using switch
statement which can perform:-

=> Addition
=> Subtraction
Self Practice => Multiplication
=> Division
Solution of self
Practice 1
2. Write a program to print
Self Practice all the numbers divisible by
7 using do-while loop.
Solution of self
Practice 2
1. WAP in which you

=> Take number of subjects


from user

Self Practice => Take marks in each


subject

=> Print the AVERAGE marks


Solution of
Self Practice 1
2. Write a Program in
which you take two
matrix from user and
Self Practice return the addition of
these two matrix
Solution of Self Practice 2
Write a program to
get the maximum
Self Practice number from the
array using Function
Self Practice
Thank You

You might also like