C Programming
C Programming
C is a very powerful functional programming language which is widely used and that has influenced
many other languages
And if statements
After that we will see how c organizes information in arrays
Now what you need to program c,there are two things you need to program c, one is the editor,the
editor helps you to write the code and second thing is compiler,a compiler is a program or a tool which
converts your program to the machine language,because your computer doesn’t understand this c
program that you write this c program is the interface which you write and it is easier to understand
programming when you program in c,so your computer understands 0’s and 1’s so the compiler will
convert the program what you write in c into the machine language so that you can execute your
program,so these are the two things which are needed,so in order to get these two things,they can be
obtained free in different ways there are,there are different IDE’s it is integrated development
environment which helps you to write your code,I will show you how to install this IDE which is free to
use
Code::Blocks
www.codeblocks.org
- The open source,cross platform,free c,c++ and fortrain IDE build to meet the most demanding
needs of its users.it is designed to be very extensible and fully configurable
Finally an IDE with all the featurs you need having a consistent thing feel and operation across
platforms.
Built around the plug in framework,code blocks can be extended with plugins,and kind of
functionality can be added by installing/coding a plugin.For instance compiling and debugging
functionality is already provided by plugins
-So how to download it,you can click the download button here in the “Main” left side of the
window,you will find download in this section of main or you can click download on the upper
bar
Click on downloads-Downloads
Codeblocks-13-12-setup.exe BerliOS
Codeblocks-13-12mingw-setup.exe BerliOS
Codeblocks-13-12mingw-setup-TDM-GCC-481.exe BerliOS
It contains the mingw which is the compiler and it will help to compile the code
-Code::Blocks installation
Choose components
-default install
-contrib plugins
-Config
-mingw compiler
Click next
-Code::Blocks installation
C:\Program Files(x86)\CodeBlocks
-Code::Blocks installation
Installing
-Code::Blocks installation
Click yes
-Compilers auto-detection
Note:after auto-detection atleast on compiler’s master path is still not there and therefore invalid
Inspect the list below and change the compiler’s master path in the compiler options
Compiler Status
Click next
-Code::Blocks installation
Click next
File associations
Code blocks is currently not the default application for Source files
In the last chapter we have seen how to download code blocks which is a c,c++ fortrain IDE in which we
are going to develop our c code and we are going to compile and run and test our code in code blocks in
this chapter we will see how to create your first c program and what is the program structure of this
program so first open code blocks
Go to file-new-project
New from template
Category
Click Next
Console application
Project Filename:
MyProg.cbp
Please select the folder where you want the new project to be created as well as its title
Project Title:
MyProg
C:\Users\Programming Knowledge\Downloads\test\
Resulting filename:
C:\Users\Programming Knowledge\Downloads\test\MyProgram
Click next
By browsing the directory you can save the project in the place where you want to see this
See the path so that you can see the source file in this
Console application
Please select the compiler to use and which configurations you want to be enabled in your project
Compiler :
“Debug” options
Output dir: bin\Debug\
“Release” options
This is GNU GCC compiler and it is going to create two versions of your project one is called debug and
the other is called release
When you click next you will not see anything on this window but on the left hand side on the projects
tab you will be able to see the project name which is given by you and inside this folder called source
there is a folder called main.c
Workspace #include<stdlib.h>
Sources {
Return 0;
We will see line by line and see what the code is there
These lines which starts with the # are called preprocessor commands,there are other preprocessor
commands also but these are the most preprocessor commands include includes a header file to your
project so whenever you create a c program there are already some files which we need to include in
our project in order to write a program and this preprocessor command # include includes the file name
and these are the most important files we need to create a program the first file is stdio.h standard
input output this file is responsible for taking the input or printing the output,we see explain the printf
after sometime but this printf statement is due to the inclusion of this file # include<stdio.h>,now there
is a file called stdlib and this file also we need to include because main whenever you write a c
program,whenever you try to compile and run this program your compiler whenever you run your
program the compiler sees the main function or searches for the main function in the project and this is
like the entry point for your program so this main is the part of standard lib,so these files #
include<stdio.h> and stdlib.h are called preprocessor compiler,now this int main is a function and it is
function and it is the built in function that every program must have,by this main c knows that it is the
entry point of your program so every program must have atleast and only one main function,and what is
a function,a function is a piece of code that you can execute,for now you need to know about this is a
function is a piece of code which you need to execute some piece of code,most function is main and
most important function is also main,the function has two or three things one is return type int in int
main() int is integer and this main is the name of the function which is by default which you have to give
in letters and this bracket after main takes an argument but now we are not passing any argument,so
every program must have this main function but because we are returning integer we need to end our
program with return 0,so this is like a for your main program so now you need this so if your program
reaches the return is that your program has run compile,this prinrtf is the part of standard output file
there is a function in this file # include<stdio.h> called printf,and this printf helps us to print a string or
that on the terminal,so this is printf and inside the brackets of printf,so whatever you write inside the
brackets in “ “so you need to write in “ “ inside the brackets whatever you write inside the bracket will
be printed on the terminal,this \n changes the line,this will not be printed but this is a special string or
character which will help you to change the line of your print,every line of code ends with a semicolon
this semicolon tells c that this line of code is ending here and from the next line we will write next line of
code,and every function is started with this opening and closing curly braces like this
Printf(“hello world!\n”);
Return 0;
So this is the structure of a c program and every line is ending with a semicolon so indentation is not
that necessary you can position your code any way you can,
Printf(“hello world!\n”);
Return 0;
Printf(“hello world!\n”);
Return 0;
But in order to make your program presentable,there is design of a code that is best seen,now in order
to run your program you need to compile and in the last chapter we have told you what,compilation
helps your program to convert your program into a machine language,which your machine can
understand,this code is readable by person but computer cannot understand this code,so the compiler
helps us to convert the code which is person readable to the machine readable code,there are three
buttons build,run and build and run,so to build your program build is to compile the program,so build
and compile are same,there is a tab here also which has the build,run and build and run so you can go
there also and compile your program,before compiling the program you have to save your program so
always make sure that you save your program,then you click compile then the output of your compile
will be shown in this build log,so compiler helps us to chech whether we have written our program
correct or not,if we have written the program correct there will be no errors we have written our code
successfully we run our program we get the output as hello world,so we see it tells to preea nay key to
continue we press it and close the terminal,whenever you do some mistakes in writing some code your
compiler will give you the error
Suppose you give mnmn at the end of printf which is not known to the c program
Program
# include <stdio.h>
#include<stdlib.h>
int main()
Return 0;
We give the semicolon after that mnmn but still it is not known to the c program
Program
# include <stdio.h>
#include<stdlib.h>
int main()
{
printf (“hello world!\n”);mnmn;
Return 0;
So whenever you write the function that shoud be known to the c program and declare it first as integer
or something another,
We will see here how to use printf and scan f and another things in c,in the last chapter we have seen
that we can use printf to print any value to the terminal we see that the hello worls is printed on the
terminal which is inside the printf brackets
Program
# include <stdio.h>
#include<stdlib.h>
int main()
Return 0;
Output:
Hello world
Hello world
Hello world
If we want to print hello world three times we give the printf and we see that hello world is printed
three times
Program
# include <stdio.h>
#include<stdlib.h>
int main()
Return 0;
Output:
Program
# include <stdio.h>
#include<stdlib.h>
int main()
Return 0;
}
Output:
Hello world
Hello world
Hello world
Program
# include <stdio.h>
#include<stdlib.h>
int main()
Return 0;
Output:
Hello world
Hello world
98
Here % d in printf is called a place holder,it is that % d is going to hold a place for an integer so d is
integer,so if you want to print decimal value then there is a special character called f it is called float it is
used if you want to print a decimal value
Program
# include <stdio.h>
#include<stdlib.h>
int main()
Return 0;
Output:
Hello world
Hello world
83.9
What if you want to print decimal value but you give place holder as %d
Program
# include <stdio.h>
#include<stdlib.h>
int main()
Return 0;
Output
Hello world
Hello world
It will not give the value because it is the decimal value and we are giving the integer place holder to
hold the decimal value which is invalid
Program
# include <stdio.h>
#include<stdlib.h>
int main()
Return 0;
So with this place holder in the program above %d we can also add text
Program
# include <stdio.h>
#include<stdlib.h>
int main()
Return 0;
This place holder %d will be replaced by 98 the number which you write after this comma
Hello world
Hello world
Now in order to print multiple decimal values you have to print multiple placeholder
Program
# include <stdio.h>
#include<stdlib.h>
int main()
Return 0;
Output:
Hello world
Hello world
27 83 98
Now there are different kinds of data that you can print
Suppose we want to print a decimal value then it will be float % f,now in order to print a big decimal
value or big float value you use a double and double if %lf large float in order to print a large integer
value we use ld we will write comments it is that whatever you write after the c program will not be
executed by the c program
Program
# include <stdio.h>
#include<stdlib.h>
int main()
printf(“%d\n”,98);///integer
printf(“%ld\n”,83457989);///large integer
printf(“%f\n”,83.8);///float
printf(“%lf\n”,834579.83);///integer
printf(“%c\n”, ’ c ’);///char
printf(“%d\n”,98);///integer
printf(“%s\n”,”hello world”);///string
printf(“%x\n”,83);///hex
Return 0;
Output:
Hello world
98
83457989
83.8
834579.83
98
Hello world
83
Suppose you want that user should enter some value and then it will be printed on the screen so how
can you do that you can do that by a special function called scanf this function you generally use to take
the input from the user for example I want to take input from the user as a string,then I will give here %
s and then any value here any value will be taked by scanf and then any value you can print this but this
we will see this later when we see this when we see how variable in c works and how these data types
work in c,these we will see later but scanf we will use to input values now there is another
Program
# include <stdio.h>
#include<stdlib.h>
int main()
printf(“%d\n”,98);///integer
printf(“%ld\n”,83457989);///large integer
printf(“%f\n”,83.8);///float
printf(“%lf\n”,834579.83);///integer
printf(“%c\n”, ’ c ’);///char
printf(“%d\n”,98);///integer
printf(“%s\n”,”hello world”);///string
printf(“%x\n”,83);///hex
scanf(“%s”,);
Return 0;
There is another special function called puts it also prints the value to the screen
Program
# include <stdio.h>
#include<stdlib.h>
int main()
{
printf(“%d\n”,98);///integer
printf(“%ld\n”,83457989);///large integer
printf(“%f\n”,83.8);///float
printf(“%lf\n”,834579.83);///integer
printf(“%c\n”, ’ c ’);///char
printf(“%d\n”,98);///integer
printf(“%s\n”,”hello world”);///string
printf(“%x\n”,83);///hex
puts(“this is my c program”);
Return 0;
Output:
Hello world
98
83457989
83.8
834579.83
98
Hello world
83
This is my c program
In this way you can input and output different kinds of data on the terminal
Day 4:Comments
As the name suggests comments are used to provide the description of your code and comments are
not executed by your c program and for example you want to give the description of the program what
it is doing,what is the purpose of the line of code you use,in order to give comments you can use two
ways one way is using multi line comments that is using more than one line comment give /* */and in
between whatever you will write will be called a comment and it will not be executed by your or it will
not be compiled even by your project,it is used to describe what your program is doing
/* this program is
printing
hello world*/
Program
# include <stdio.h>
#include<stdlib.h>
/* this program is
printing
hello world*/
int main()
return 0;
This comments which is between the forward slash * and forward slash will not be executed it is for your
reference,if another persons sees the code he will know what the program is doing what is you want to
do in the program,in order to give single line comment what this printf is doing then
Program
# include <stdio.h>
#include<stdlib.h>
/* this program is
printing
hello world*/
int main()
return 0;
You can place your comments anywhere in the code you can provide these types of comments like /* */
and this // //
We will see what are variables in c and how we can use variables in c,varaibles are data holders or you
can save values in the variables it is just like you want to save your age in some place you can save your
age in some place and then you can extract this age using the variables also variables has two things one
is the data type int is a data type and then the name of the variable age and then you give the semi
colon here and then this is the declaration of the variable,now what this variable is that you want the
variable,age is the name of the variable and int is the data type of the variable,it is the type of data this
variable is going to store,if it is going to save integer in our case then it will be initialized by this data
type called int,now you can assign some value to this age
Program
# include<stdio.h>
#include<stdlib.h>
int main()
return 0;
output:
the age is 22
so once again the variables are used to store the value or data,there are some rules for declaring the
variables also,suppse you want to store the variables using some number
Program
# include<stdio.h>
#include<stdlib.h>
int main()
int age;
Return 0;
For this the variable name must not begin with a digit
Program
# include<stdio.h>
#include<stdlib.h>
int main()
int 3age;
Return 0;
Program
# include<stdio.h>
#include<stdlib.h>
int main()
{
int a3g8e3;
Return 0;
We can have numbers between the words of the variable or at the end of the variable
Program
# include<stdio.h>
#include<stdlib.h>
int main()
Return 0;
You can not have spaces between variables like if you declare variable any age
Program
# include<stdio.h>
#include<stdlib.h>
int main()
int $anyage;
Return 0;
Program
# include<stdio.h>
#include<stdlib.h>
int main()
int any*age;
Return 0;
Also if you want to declare * in between the variables like int any*age it is not allowed
Program
# include<stdio.h>
#include<stdlib.h>
int main()
int any_age;
Return 0;
Avoid using special function names main is a function already declared in c,it will create that main is a
function name and you have declared as a variable,so avoid using any special function names as
variable names
Program
# include<stdio.h>
#include<stdlib.h>
int main()
int main;
Return 0;
Suppose you want to declare decimal variable name you can do this like this,float is used to
store some floating point value or decimal value,you can store decimal type data type using decimal
type float,you can store pi values as 3.14 using the data type float,you can use characters use can use
char my_char,so whenever you need to store a character you do it in single quotes’ ‘ char my_char=’a’
and then double_pi=3.14223213922,so these are data types you can use in c
Program
# include<stdio.h>
#include<stdlib.h>
int main()
float pi=3.14;
char my_char=’a’
double_pi=3.14223213922;
return 0;
So there are some operators you can use in c,these operators are
Program
#include<stdio.h>
#include<stdlib.h>
int main()
+(addition)
-(subtraction)
*(multiplication)
/(division)
%(modulus)
**/
Return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main()
+(addition)
-(subtraction)
*(multiplication)
/(division)
%(modulus)
**/
int a=16,b=83;
int sum;
Sum=a+b;
printf(“sum=%d”,sum)
Return 0;
}
Output:
Sum=99
Program
#include<stdio.h>
#include<stdlib.h>
int main()
+(addition)
-(subtraction)
*(multiplication)
/(division)
%(modulus)
**/
int a=16,b=83;
int subtraction;
subtraction=a-b;
printf(“subtraction=%d”,subtraction)
Return 0;
}
Output:
Subtraction=66
Program
#include<stdio.h>
#include<stdlib.h>
int main()
{
/**Multiplicative and additive operators
+(addition)
-(subtraction)
*(multiplication)
/(division)
%(modulus)
**/
int a=16,b=83;
int multiplication;
multiplication=a * b;
printf(“product=%d”,multiplication)
Return 0;
}
Output:
Product=1347
Program
#include<stdio.h>
#include<stdlib.h>
int main()
+(addition)
-(subtraction)
*(multiplication)
/(division)
%(modulus)
**/
int a=27,b=3;
int division;
division=a/b;
printf(“division=%d”,division)
Return 0;
}
Output:
Division=8
Program
#include<stdio.h>
#include<stdlib.h>
int main()
+(addition)
-(subtraction)
*(multiplication)
/(division)
%(modulus)
**/
int a=45,b=13;
float division;
division=a/b;
printf(“division=%f”,division)
Return 0;
}
Output:
Division=3
Here we give the division as float and % f in the print f but still it gives whole numbers it does
not gives decimal value because we have given the operator as /,to get the remainder or thye
decimal value also we use modulus operator
Program
#include<stdio.h>
#include<stdlib.h>
int main()
+(addition)
-(subtraction)
*(multiplication)
/(division)
%(modulus)
**/
int a=45,b=13;
int modulus;
modulus=a % b;
printf(“modulus=%d”,modulus)
Return 0;
}
Output:
If statements checks the condition whether it is true or false on the basis of condition it will
execute code and not execute another code,so how will we declare the if condition,we want to
check some person’s age and if this age is equal to 18,if this age is greater than 18 we want to
print that the age is greater than 18 and if this age is less than 18 we can do this,so how can we
do this we can do this by if condition,now to write the if condition the structure is like this you
need to use this if statement and then these two brackets and inside these two brackets comes
the condition,this condition can be false or true so we evaluate this condition inside this bracket
Program
# include<stdio.h>
# include<stdlib.h>
int main()
{
int age;
printf(“please enter the age”);
Then inside the curly brackets we execute some code,on the basis of the validity of the
condition,if this condition is true the code inside the curly braces will be executed,otherwise the
code will not be executed,so now let us enter the age of some user,now in order to enter the
age of the user at runtime we use scanf and we have also shown that
Program
# include<stdio.h>
# include<stdlib.h>
int main()
{
int age;
scanf();
We can use scanf to take inputs from the user,so we will tell the user first by printing this
message, printf(“please enter the age”);and in scanf we will take the input and because this age
is integer we will use %d to take the integer,scanf(“%d”);
Program
# include<stdio.h>
# include<stdlib.h>
int main()
{
int age;
scanf(“%d”,age);
But the correct way of using scanf is by using this & symbol
Program
# include<stdio.h>
# include<stdlib.h>
int main()
{
int age;
scanf(“%d”,& age);
}
This & symbol will include your variable name,and what this & symbol indicates is the address of this
value age after & symbol in scanf,somewhere in the memory it will be stored,and whenever you write &
before your variable name,so it indicates that you are scanning the address of this variable age in scanf
so whenever some user will enter some value it will be stored at this address & age in scanf,you can
access this age value after that so whenever we we will execute this code we can understand it better,so
it will take the input and depending upon the age the user has entered the age
Program
# include<stdio.h>
# include<stdlib.h>
int main()
{
int age;
scanf(“%d”,& age);
Program
# include<stdio.h>
# include<stdlib.h>
int main()
{
int age;
We will execute the code here in this, printf(“the age is greater than 18”);now if we want to
print the age is equal to 18
Program
# include<stdio.h>
# include<stdlib.h>
int main()
{
int age;
scanf(“%d”,& age);
Now to compare this we have to ==,because if you use = it will assign the integer to age,so to
compare these two values we use ==,now if the age is equal to 18 we will print the age is == 18
Program
# include<stdio.h>
# include<stdlib.h>
int main()
{
int age;
scanf(“%d”,& age);
we see the output,we enter the age as 22 and we will be seeing the first result and since the
other conditions are not true,so these printf messages will not be seen and we will see the printf
message of the first condition
output:
and since 22 is>18 so the first condition is executed,and since 22 is not== 18 so other condition
is not executed,and since this condition 22 is not<18 so that condition is also not executed ,and
this condition 22 is>18 so this condition is executed and this statement is printed
output:
since the condition 18==18 is true so the condition is executed and the statement is printed and
other conditions are not executed and when we again run the program and give the age less
than 18 that is 8 we see the output that the age is less than 18
output:
we see the output,we enter the age as 22 and we will be seeing the first result and since the
other conditions are not true,so these printf messages will not be seen and we will see the printf
message of the first condition
output:
and since 22 is>18 so the first condition is executed,and since 22 is not== 18 so other condition
is not executed,and since this condition 22 is not<18 so that condition is also not executed ,and
this condition 22 is>18 so this condition is executed and this statement is printed
Day 8: if else and nested if else statements in c
In the last chapter we have seen how to use if statements in c programming and in this chapter
we will see if else statements in c and also we will see how we can use nested if else statements
in c,in the previous chapter we have see we executed the code for whether the age is
<18,==18,>18,now we will see the if else statement
Program
# include<stdio.h>
# include<stdlib.h>
int main()
{
int age;
scanf(“%d”,& age);
We can write else which picks all the conditions which is not true
Program
# include<stdio.h>
# include<stdlib.h>
int main()
{
int age;
scanf(“%d”,& age);
}else{
So if the age is not greater than 18 than the program goes to the else condition,and this else
condition as the name suggests,it indicates every other condition which is not true by the if
condition,else after brackets and then curly brackets,else does not take any condition because it
does not need to check any condition everything other than this first if condition
Program
# include<stdio.h>
# include<stdlib.h>
int main()
{
int age;
scanf(“%d”,& age);
}else{
Output:
Output:
So if the age is not greater than 18 than the program goes to the else condition,and this else
condition as the name suggests,it indicates every other condition which is not true by the if
condition,else after brackets and then curly brackets,else does not take any condition because it
does not need to check any condition everything other than this first if condition
Program
# include<stdio.h>
# include<stdlib.h>
int main()
{
int age;
scanf(“%d”,& age);
}else{
# include<stdio.h>
# include<stdlib.h>
int main()
{
int age;
scanf(“%d”,& age);
}else{
We give else if condition and there is the curly braces,since we are taking if here in the else if we
have to check other condition if age==18
Program
# include<stdio.h>
# include<stdlib.h>
int main()
{
int age;
scanf(“%d”,& age);
}else{
Program
# include<stdio.h>
# include<stdlib.h>
int main()
{
int age;
scanf(“%d”,& age);
}else{
Program
# include<stdio.h>
# include<stdlib.h>
int main()
{
int age;
scanf(“%d”,& age);
}else{
Suppose you want to check whether the age is greater than 18 and also if the age is less than 22
we can use nested if conditions
Program
# include<stdio.h>
# include<stdlib.h>
int main()
{
int age;
scanf(“%d”,& age);
if (age<22)
{
Printf(“the age is greater than 18 but less than 22”)
}
} else if(age==18)
{
Printf(“the age is equal to 18”)
}
}else{
Program
# include<stdio.h>
# include<stdlib.h>
int main()
{
int age;
scanf(“%d”,& age);
if (age<22)
{
Printf(“the age is greater than 18 but less than 22”);
}else{
} else if(age==18)
{
Printf(“the age is equal to 18”);
}
}else{
Output:
if (age<22)
{
Printf(“the age is greater than 18 but less than 22”);
}else{
if (age<22)
{
Printf(“the age is greater than 18 but less than 22”);
}else{
} else if(age==18)
{
Printf(“the age is equal to 18”);
}
if (age<22)
{
Printf(“the age is greater than 18 but less than 22”);
}else{
} else if(age==18)
{
Printf(“the age is equal to 18”);
}
}else{
In this chapter we will see ternary or conditional operators in c this is also used to make
perspective in C,but whenever you need to make perspective which is less and you want to do in
a single line this ternary conditional operator can be useful,so how to use this ternary
conditional operator suppose you have two integer variable integer a =22 b=27 we can do this
by using if conditions but for comparing the by writing if and then else this can be a longer
expression in terms of code
Program
# include <stdio.h>
# include <stdlib.h>
Int main()
Int a=22,b=27;
If
else
the another way of doing is we can transfer the greater of two values a and b which is b to c so we want
to compare a and b and whatever variables are greater orwhatever values are greater,thenwe want to
transfer this value to c,so there is a operator called ternary operator which is like this
Program
# include <stdio.h>
# include <stdlib.h>
Int main()
Int a=22,b=27;
Int c;
c = ( a > b ) ? b : a;
In this bracket ( / * logical expression goes here * / ) you write the condition as write in the if
condition and then you give a question mark ? here and then if the condition is true you can
pass the value the result from here the ( / * if non zero (true) * / ) but if the value is false then
the value that is contained after this colon : that is ( / * if 0 (false) * / ) will be transferred to the
value or will be returned
We will give you an example of what this is the c is the greater of these two values so we will
compare this we will say if a is greater than b ,c = ( a > b ) ? which is not greater than b then we
are asking c so we will give this question mark ? so if a is >b then we want to return a,or if a is
>b we want to return b or then otherwise b,c = ( a > b ) ? b or else we will return a written after
the semi colon, c = ( a > b ) ? b : a
So this bracket in a > b will compare the condition ( a > b ) if a is greater than b which is not
greater than b,because 22 is not greater than 27 ,but again if a is greater than b then once again
it will return b,but in our condition since a is not greater than b so the condition a > b is nottrue
so in this a will be returned, c = ( a > b ) ? b : a,a will be returned which is after the semi colon,so
if the condition is fullfilled the first parameter is returned that is b after the question mark,if the
condition a > b if false then the another condition after the semi colon that is a will be returned
So in this program what will be the value of c so a will be the value that is 22
Program
# include <stdio.h>
# include <stdlib.h>
Int main()
Int a=22,b=27;
Int c;
c = ( a > b ) ? b : a;
printf(“ans = %d”,c);
Output:
ans=22
Program
# include <stdio.h>
# include <stdlib.h>
Int main()
Int a=31,b=27;
Int c;
///( / * logical expression goes here * / ) ? ( / * if non zero (true) * / ) : ( / * if 0 (false) * / )
c = ( a > b ) ? b : a;
printf(“ans = %d”,c);
Output:
ans=27
Program
# include <stdio.h>
# include <stdlib.h>
Int main()
Int a=31,b=38;
Int c;
c = ( a > b ) ? b : a;
printf(“ans = %d”,c);
Output:
ans=31
So in the last three chapters we have seen how to use if else condition,and multiple if else
condition in c,so switch conditions are also condition based statements,but they aredifferent in
the thing that you can use conditions in switch statements but what is the difference and how it
is declared we will see this
Program
# include<stdio.h>
# include<stdlib.h>
int main()
Switch(expression){
case constant-expression :
statement(s);
break;
case constant-expression :
statement(s);
break;
default :
statements(s);
Switch statement starts with the switch expression and then comes the expression,is something
you need to validate on the basis of the cases,case and then the constant expression,we will
give you an example first is the case,then comes some condition constant-expression and after
the constant- expression comes the statement or code which you want to execute here the,now
after the code is executed you break this switch statement with this break keyword,this is an
optional keyword sometimes you need sometimes you don’t need this break statement so you
can have multiple conditions inside your switch condition,so whenever,whatever condition is
not covered by your default so if none of the case is true this will be executed which is a default
case,default case starts with this keyword: and then the case so now lets see the example,so if
none of the cases is true then the default case will be executed,default:statement,now for
example we want to calculate the grade of the student and on the basis of grade
Program
# include <stdio.h>
# include <stdlib.h>
Int main()
Int marks=90;
switch (marks)
{
Case 90:
Printf(“Excellent”);
break;
case 75:
Printf(“Very Good”);
break;
Case 60:
Printf(“good”);
break;
Case 40:
Printf(“ok”);
break;
default:
printf(“Grade Unavailable”)
}
}
so when marks is 90 it assigns marks=90 and then we are checking 90 in marks in switch(marks)
using the switch statement,where marks=90,it goes to this case,case 90,and then it prints the
message excellent and then what this break does is that if the condition is fulfilled we want to
break out of the switch statement,so we don’t want to execute any other statement,so we
break out of the switch statement and executes this statement,so whenever your switch case
sees this print statement it breaks out of the switch case at the end of the switch case
output:
Excellent
Program
# include <stdio.h>
# include <stdlib.h>
Int main()
Int marks=75;
switch (marks)
{
Case 90:
Printf(“Excellent”);
break;
case 75:
Printf(“Very Good”);
break;
Case 60:
Printf(“good”);
break;
Case 40:
Printf(“ok”);
break;
default:
printf(“Grade Unavailable”)
}
}
now when marks =75 we run the program we see the output as very good
when marks =75 it goes to marks in switch(marks),it goes to the case 75 and then prints the
value or statement or then it executes the statement,and now when it sees the break statement
it jumps out of the switch statement,the thing is that the code 90 is not executed because the
code here in this case 90 is not executed because the marks is not 90 now,the marks is 75,the
value of marks we are checking int marks =75;so the value of mars is not 90,value of marks is
not 60,value of marks is not 40,but the value of marks is now 75 that is why we have executed
this code so c has executed this code,and other codes are not executed so on the basis of these
cases multiple casers and whatever cases are fullfilled at a time,that code will be executed or
the case inside that code will be executed,otherwise the code will not be executes you may ask
that the code can be 98 also and thgen the student has scored excellent marks,so in orderto
have the grade 98 still we want to print this excellent
Program
# include <stdio.h>
# include <stdlib.h>
Int main()
Int marks=75;
switch (marks)
{
Case 90:
Printf(“Excellent”);
break;
case 75:
Printf(“Very Good”);
break;
Case 60:
Printf(“good”);
break;
Case 40:
Printf(“ok”);
break;
default:
printf(“Grade Unavailable”)
}
}
Program
# include <stdio.h>
# include <stdlib.h>
Int main()
Int marks=90;
switch (marks)
{
Case 98:
Case 90:
Printf(“Excellent”);
break;
case 75:
Printf(“Very Good”);
break;
Case 60:
Printf(“good”);
break;
Case 40:
Printf(“ok”);
break;
default:
printf(“Grade Unavailable”)
}
}
Program
# include <stdio.h>
# include <stdlib.h>
Int main()
Int marks=90;
switch (marks)
{
Case 99:
Case 98:
Case 89:
Case 90:
Printf(“Excellent”);
break;
case 75:
Printf(“Very Good”);
break;
Case 60:
Printf(“good”);
break;
Case 40:
Printf(“ok”);
break;
default:
printf(“Grade Unavailable”)
}
}
So you can give many cases without the break if you want to print the same statement following
the condition
Program
# include <stdio.h>
# include <stdlib.h>
Int main()
Int marks=90;
switch (marks)
{
Case 99:
Case 98:
Case 89:
Case 90:
Printf(“Excellent”);
break;
case 75:
case 70:
Printf(“Very Good”);
break;
Case 60:
Printf(“good”);
break;
Case 40:
Printf(“ok”);
break;
default:
printf(“Grade Unavailable”)
}
}
You can also give 70 along with 75 case in that condition if you want to give good grades
In case og case 99,case 98,case 89,case 90 in that condition you want to give excellent grades
So you can execute different cases and cover the same statement,without using this break
keyword
We will see here while loop,we will see what are loops in c,loop is a piece of code which will
execute again and again and again until some condition is true,now for example we want to
print the values from 1 to 10 and we don’t want to use the value of printf again and again,that is
printf 1,printf 2,printf 3,instead of the we want to print a single line of code printf and we want
to print from 0 to 10,using single line of code printf we can do this by using while loop,so a loop
is something which executes your code again and again and again,until and unless some
condition is true so how to use while loop,so let’s use the same example to print 10 numbers
We will declare int i as the variable as 0,int i=0, and then we will print the numbers or integers
from 0 to 10 or 0 to 9,we can use while loop by the keyword while,while(),then comes your
condition and then comes your curly braces and inside those curly braces comes your
condition,so inside the while loop we want to print the integer 10 times,we will say printf,value
of I,printf(“value of i = %d”,i ),and inside the while bracket comes the condition so we will
check,while(i<10) we want to execute this code,printf(“value of i = %d”,i ),and whenever this i
goes greater than 10 like this,while(i>10),we want to go out of the loop so this bracket(i>10)of
while loop,while(i>10),is evaluating our condition,we will give semi colon after the printf
statement and the we will increment the value by 1,i=i+1,you can also write this as i=i++
Program
# include <stdio.h>
# include<stdlib.h>
int main()
Int i=0;
While (i > 10 );
printf(“value of i = %d”,i );
//i=i+1;
i++;
So what this while condition is doing it checking the condition if i>10 is true it will print the statement,
printf(“value of i = %d”,i ),but since I will not be true because since i is 0 and we have written the
condition in while loop that i is greater than 10,i>10,so this condition will not be executed because this
condition in while loop i>10 is not true so thia code, printf(“value of i = %d”,i ),will not be executed,so
when we run the code we will not get any output but when we put i<10
Program
# include <stdio.h>
# include<stdlib.h>
int main()
Int i=0;
While (i < 10 );
printf(“value of i = %d\n”,i );
//i=i+1;
i++;
When we check i<10 it checks in int i=0,and it goes to the while loop and it goes to check whether 0 is
less than 10,so it will say yes 0 is less than 10 and then it will print the statement, printf(“value of i =
%d”,i ) it will print 0,then it will increment the value of i by 1 i++,0+1=1,and then it will do again and
again until this condition is true,we will give the line break and run the program
Output:
value of i=0
value of i=1
value of i=2
value of i=3
value of i=4
value of i=5
value of i=6
value of i=7
value of i=8
value of i=9
so what is happening here this at the first we have declared the value of i=0,so it goes to the while loop
and checks condition that i<10 that 0 is <10,so it goes inside this loop of the printf statement because
the condition is true,so it will print the value of i=0 since i is 0 now and then we are incrementing the
value of i by 1,i++,i=i+1,i=0+1=1 and then it goes again to check the condition in the while loop whether
i<10 that is is 1<10,so the value of i is 1 which is less than 10,so now again it will print the value of i=1
and then increments the value of i by 1,i++,i=i+1,i=1+1=2,so now the value of i is 2,so it goes to the while
loop and checks the condition if i<10,that is if 2 is <10,so the value of i is 2 which is less than 10 so it will
print the value of i=2 and then it will increment the value of i by 1,i++,i=i+1,i=2+1=3,so the value of i is
now 3,so it will go to the while loop and then check the condition whether i<10,that is if 3 is <10,so the
value of i is 3 which is less than 10 so it will print the value of i=3 and then it will increment the value of i
by 1,i++,i=3+1=4,now the value of i is 4 it will go to the while loop and check the condition whether the
value of i<10,that is if 4 is <10,so the value of i is 4 which is less than 10 so it will print the value of i=4
and then it will increment the value of i by 1,i++,i=4+1=5,so the value of i is now 5,it will go to the while
loop and check the condition whether the value of i<10,that is if 5 is <10,so the value of I,so it will print
the value of i=5 and then it will increment the value of i by 1,i++,i=i=5=1=6,so now the value of i is 6,so
now it will go to the while loop and check the condition whether i<10 that is if 6 is <10,so the value of I is
6 which is less than 10,so it will print the value of i=6 and the it will increment the value of i and so on till
the value of i reaches 9,it will go to the while loop and check the condition whether i<10,that is if 9 is
<10 so the value of I is 9 which is less than 10 so it will print the value of i=9 so then after incrementing
the value of i by 1,the value of i becomes 10,so it will go to the while loop and check the condition
whether i<10,but since 10 is not <10 so it will not print the statement,because this condition is not true
so it goes out of the loop so this is the code
if you also want to include the value of 10 so we can give the condition in while loop as this while(i<=10)
Program
# include <stdio.h>
# include<stdlib.h>
int main()
Int i=0;
While (i <= 10 );
printf(“value of i = %d\n”,i );
//i=i+1;
i++;
Output:
value of i=0
value of i=1
value of i=2
value of i=3
value of i=4
value of i=5
value of i=6
value of i=7
value of i=8
value of i=9
value of i=10
so loop is something which iterates or executes your code again and again,until and unless some
condition is true so in the while loop condition is this thing that we have provided and it will iterate
again and again until this condition is true that
We will see here do while loop which is 1 more kind of loop,and what is the difference between the do
while loop and while loop,so let as see that we have to execute the numbers or integers from 1 to
10,but this time we want to do this by do while loop this was the code we have written in the last
chapter,which we will comment because we will see later that what is ythe difference between while
loop and do while loop,so let us see what is the syntax of the do while loop so you write the keyword do
we give the curly braces
Program
# include <stdio.h>
# include<stdlib.h>
int main()
Int i=0;
/* While (i <= 10 );
printf(“value of i = %d\n”,i );
//i=i+1;
i++;
} */
and at the end of the curly braces,we write while and then we give the condition in the brackets of the
while loop
do
}while();
Program
# include <stdio.h>
# include<stdlib.h>
int main()
Int i=0;
do
}while();
/* While (i <= 10 );
printf(“value of i = %d\n”,i );
//i=i+1;
i++;
} */
So again we want to print the values of integers from 0 to 10 we will see that
Program
# include <stdio.h>
# include<stdlib.h>
int main()
Int i=0;
do
printf(“value of i = %d\n”,i );
//i=i+1;
i++;
}while( i <= 10 );
/* While (i <= 10 );
printf(“value of i = %d\n”,i );
//i=i+1;
i++;
} */
Output:
value of i=0
value of i=1
value of i=2
value of i=3
value of i=4
value of i=5
value of i=6
value of i=7
value of i=8
value of i=9
value of i=10
so here what is happening here is that when i=0 int int i=0 it says do,do is that do something so before
checking the condition it prints the statement printf and prints the value of i as 0 and then increments
the value of i by 1,i++,i=i+1,so the value of i is 1 now so now it goes to the while loop and checks the
condition whether i<=10 that is if 1 is<=10,so the value of i is 1 which is less than 10,so it goes into the
loop again and prints value of i=1,and then increments the value of i by 1 again,i++,i=i+1,i=1+1=2,so the
value of I is 2 now,so it goes to the while loop and checks the condition whether i<=10 that is if 2 is
<=10,the value i is 2 which is less than 10 so it prints the value of i=2,it again increments the value of I by
1,i++,i=i+1,i=2+1=3,so the value of I is 3 now it goes to the while loop and checks the condition whether
i<=10,that is if 3<=10,the condition is true so it goes into the loop again and prints the value of i= 3,now
it increments the value of i by 1,i++,i=i+1,i=3+1=4,the value of i is 4 now,so it goes to the while loop and
checks the condition whether i<=10 that is if 4 is<=10,the condition is true so again it goes into the loop
and prints value of i=4,then it increments the value of i by 1,i++,i=i+!,i=4+1=5,so the value value of i is 5
now it goes to the while loop and checks the condition whether i<=10 that is if 5<=10,the condition is
true so again it goes into the loop and prints the value of i=5,then it increments the value of i by 1,i+
+,i=i+1,i=5+1=6,the value of I is 6 now it goes to the while loop and checks the condition whether i<=10
that is if 6 is <=10 the condition is true so it again goes into the loop and prints the value of i=6,and it
goes so on that until the value of i=9,it goes to the while loop and checks the condition whether i<=10
that is if 9<=10 the condition is true so it again goes into the loop and prints the statement,so after
incrementing the value of i 1s 10 it goes to the while loop and checks the condition if i<=10 that is if 10 is
<=10 the condition is true and then again it goes into the loop and prints the value of i=10,but after that
the value of i is incremented the condition is false so it goes out of the loop
so what is the difference between while loop and do while loop,so whenever we use while loop it checks
the condition first
Program
# include <stdio.h>
# include<stdlib.h>
int main()
Int i=0;
do
printf(“value of i = %d\n”,i );
//i=i+1;
i++;
}while( i <= 10 );
/* While (i <= 10 );
printf(“value of i = %d\n”,i );
//i=i+1;
i++;
} */
The while loop checks the condition first and then it executes the code,but in the do while loop it
executes your code atleast once and then checks the condition and if this condition is true it will once
again go into the loop that is once again go into the statement and execute the statement and when the
statement is false it goes out of the loop so do while loop executes your code atleast once without the
condition but while loop checks the condition first and then executes the code,so let us see that we will
print the seperater here that
Program
# include <stdio.h>
# include<stdlib.h>
int main()
Int i=0;
do
printf(“value of i = %d\n”,i );
//i=i+1;
i++;
}while( i <= 10 );
Printf(“********************************\n”);
While (i <= 10 );
printf(“value of i = %d\n”,i );
//i=i+1;
i++;
Output:
value of i=0
value of i=1
value of i=2
value of i=3
value of i=4
value of i=5
value of i=6
value of i=7
value of i=8
value of i=9
value of i=10
********************************************
Since the value of I is same for do while and while loop so we will take j instead of i in while loop
Program
# include <stdio.h>
# include<stdlib.h>
int main()
Int i=0;
do
printf(“value of i = %d\n”,i );
//i=i+1;
i++;
}while( i <= 10 );
Printf(“********************************\n”);
Int j=0;
While (j <= 10 );
printf(“value of j = %d\n”,j );
//j=j+1;
j++;
We have done this because to find the difference we have to declare different in do while and while
loop that is i and j
Output:
value of i=0
value of i=1
value of i=2
value of i=3
value of i=4
value of i=5
value of i=6
value of i=7
value of i=8
value of i=9
value of i=10
********************************************
value of j=0
value of j=1
value of j=2
value of j=3
value of j=4
value of j=5
value of j=6
value of j=7
value of j=8
value of j=9
value of j=10
to know the difference we will give the condition which is not true
Program
# include <stdio.h>
# include<stdlib.h>
int main()
Int i=0;
do
printf(“value of i = %d\n”,i );
//i=i+1;
i++;
}while( i > 10 );
Printf(“********************************\n”);
Int j=0;
While (j > 10 );
{
printf(“value of j = %d\n”,j );
//j=j+1;
j++;
This condition is not true for do while loop since in the while loop we have given i>10 but it is not true
since we have int i=0 so I is not greater than 10,and in the case of while loop also we have given the
condition j>10 which is false because we have j=0 so j is not greater than 10,we see the output
Output:
Value of i=0
**************************************************
So what it is doing in the do while loop is that it prints the value of i as 0 and then increments the value
of i by one so i is now 1 it checks in the while loop that 1 is not greater than 10 so it goes out of the loop
so the do loop executes the statement atleast once and then checks the condition in the while loop,but
in case of while loop j=0,in while loop condition j is not greater than 10 so no statement is executed,so
while loop checks the condition first and then executes the code and if the condition is false it will not
be executed but in the case of do while loop,the statement will be executed atleast once since it goes to
the do loop executes the statement and the checks the condition in the while loop
In the previous chapter we have seen how to use while loop and do while loop in c in this chapter we
will see how to use for loop,so for loop also depends upon the inialization,the condition and the
incrementation properties so first we will see what is the structure of for loop and then we will see how
we use this
Program
# include <stdio.h>
# include <stdlib.h>
Int main()
Statement(s);
We define for loop then brackets and then inside the brackets comes three arguments for( init ;
condition ; increment ) and these arguments are separated by a semi colon,and these three different
parameters are separated by the semi colon,and then comes these two curly braces and if the condition
in the for loop and if this condition inside the for loop is true then this condition inside the for loop will
be executed Statement(s),the first parameter in the for loop is called init or initialization of
something,the second parametet checks whether a condition is true or not which is related to
initialization and the third parameter is the increment of the parameter which we have initialized in the
first condition so this is the structure of for loop,now lets take the same example we have taken in the
last chapter
So we will declare a variable which is 0,int a=0,and this variable we will use to print 10 but in last chapter
we have seen that we have to first initialize it and then declare this,you need to give while loop
initialize,check the condition and then increment the condition,so initialization you can do like this
Program
# include<stdio.h>
# include<stdlib.h>
Int main()
Int a=0;
in for loop we don’t need to do this type of initialization Int a=0 but we can do this inside this,as we have
seen in the previous conditions in the while loop and the do while loop we check some condition to
fulfill and if this condition is fullfilled then we execute the code,so in this for loop we are checking if i<10
Program
# include<stdio.h>
# include<stdlib.h>
Int main()
if we initialize i by 0 the the value of i will be replaced here in i<10 in for loop,it will whether 0 is less
than 10,is 0<10,it will keep on going like this until the condition is fullfilled and the third parameter is
incrementing the values i++,then comes the curly braces
Program
# include<stdio.h>
# include<stdlib.h>
Int main()
So what we have done in the for loop,we have initialized the variables,we are checking on the basis of
that variable if the condition is true,and we are incrementing the variables we have initialized,now we
can do what we have to do inside the for loop,we can print the value of i
Program
# include<stdio.h>
# include<stdlib.h>
Int main()
So the for loop sees the initialization,condition and the increment,this loop is printing the values from 0
to 10,so now we will run the program
This is that c99 is not enabled in code blocks this is a newer version of c
To enable it
Go to
Projects
Workspace
Output:
value of i=0
value of i=1
value of i=2
value of i=3
value of i=4
value of i=5
value of i=6
value of i=7
value of i=8
value of i=9
and how it is executing initialiiy we initialize the value of i=0,then c checks the condition whether i<10
that is if 0 is <10 ,if the condition is true then it goes to printf statement and executes the code first,then
after the execution above the curly braces ending it increments the value of i,i++,i=i+1,i=0+1=1 now the
value of i is 1 so it checks the condition whether i<10,that is if 1<10,the condition is true so it prints the
value of i=1,and then above the curly braces ending in increments the value of i,i++,i=i+1,i=1+1=2,now
the value of i is 2 so it checks the condition whether i<10,that is if 2<10,the condition is true,so it prints
the value of i=2,and then above the curly braces ending it increments the value of i,i+
+,i=i+1,i=2+1=3,now the value of i is 3 so it checks the condition whether i<10,that is if 3<10,the
condition is true,so it prints the value of i=3,and then above the curly braces ending it increments the
value of i,i++,i=i+1,i=3+1=4,now the value of i is 4 so it checks the condition whether i<10,that is if
4<10,the condition is true,so it prints the value of i=4,and then above the curly braces ending it
increments the value of i,i++,i=i+1,i=4+1=5,now the value of i is 5 so it checks the condition whether
i<10,that is if 5<10,the condition is true,so it prints the value of i=5,and then above the curly braces
ending it increments the value of i,i++,i=i+1,i=5+1=6,now the value of I is 6 so it checks the condition
whether i<10,that is if 6<10,the condition is true,so it prints the value of i=6,and then so on it is till i=9,it
checks the condition if i<10,that is if 9<10,the condition is true,so it prints the value of i=9,now for i=10
the condition is false and then it goes out of the loop
Program
# include<stdio.h>
# include<stdlib.h>
Int main()
}
Output:
value of i=0
value of i=1
value of i=2
value of i=3
value of i=4
value of i=5
value of i=6
value of i=7
value of i=8
value of i=9
value of i=10
Day 14:Functions in C
A function is a piece of code or a block of code which we can use to do a task,suppose you can add two
numbers multiple times,the function to add two numbers or you can use your function again and again
or you can call your function again and again inside any other function,so lets see how we can declare
function but first we see that every c program has atleast 1 function and this function is called main at
the starting we have seen that this main is a function and every c program has atleast 1 function and this
function is called main and whenever you execute your c program searches for this main function and
executes anythinginside this,so function looks like this it has the return type it has the name of the
function,this is the in built name in c which is main but you can define user defined function and give
this any name and then comes this bracket and then these curly braces,so let us see what is the
definition or declaration of a function
Program
# include <Stdio.h>
# include<stdlib.h>
/* code*/
Int main()
This is not actual declaration but some type of declaration,first you should have the type of function it
returns then you give the name to the function and in the bracket you pass argument or parameter of
some type and inside the curly braces you write the code we will comment this type name now
Program
# include <Stdio.h>
# include<stdlib.h>
/* code*/
} */
Int main()
So that we can see it as a reference,so now you can define a function above your main function or after
your main function,but there is a difference in declaring the function above the main function or after
the main function which we will see,but lets take an example we want to add two numbers using
function,or we want to print something using function we had said that function starts with a return
type but in this case we don’t want to return anything then we write void,void is that we are not
returning anything,then comes the function name void MyFunctionName then bracket void
MyFunctionName() it can take some parmeters like (type 1 arg 1,type 2 arg 2,type 3 arg 3…........) inside
the bracket but now we don’t need we will see this later,for now we will not pass any parameter to the
function and then we can write a code printf
Program
# include <Stdio.h>
# include<stdlib.h>
/* code*/
} */
Void MyFunctionName()
Int main()
Suppose you want to add two numbers and show the sum you can write this before this Printf(“we are
printing a function\n”) that is printf(“sum=%d”,83+27);
Program
# include <Stdio.h>
# include<stdlib.h>
/* code*/
} */
Void MyFunctionName()
printf(“sum=%d”,83+27);
Printf(“we are printing a function\n”);
Int main()
And then you can call this function MyFunctionName() in Void MyFunctionName() in your main,you can
call this function in your main function,and for calling the function in the main function you have to copy
the name of the function and paste it inside the main function that is int main function and give a semi
colon after that function
Program
# include <Stdio.h>
# include<stdlib.h>
/* code*/
} */
Void MyFunctionName ()
printf(“sum=%d\n”,83+27);
Int main()
MyFunctionName ();
}
And what it does is that this function name MyFunctionName () is replaced by this code
printf(“sum=%d”,83+27);
So whenever you call the function MyFunctionName () inside the int main function this code
printf(“sum=%d”,83+27);
printf(“sum=%d”,83+27);
In place of this function MyFunctionName () inside the int main() function and the logical name
MyFunctionName () we have given to this function,so this function should show the code we are writing
Output:
Sum=113
# include <Stdio.h>
# include<stdlib.h>
/* code*/
} */
printf(“sum=%d\n”,83+27);
Then the program sees that there is the declaration of a function MyFunctionName () then it sees that it
is the function we have declared this whenever the main function calls this function MyFunctionName ()
then it executes these printf code which is given there and because we are calling this function
MyFunctionName () inside the function we can execute these printf code,you can call a function multiple
times in your code
Program
# include <Stdio.h>
# include<stdlib.h>
/* code*/
} */
Void MyFunctionName ()
printf(“sum=%d\n”,83+27);
Int main()
MyFunctionName ();
MyFunctionName ();
MyFunctionName ();
}
Output:
Sum=113
Sum=113
Sum=113
So it makes your code understandable and executable,you can call this function after the main function
also that
Program
# include <Stdio.h>
# include<stdlib.h>
/* code*/
} */
Int main()
MyFunctionName ();
MyFunctionName ();
MyFunctionName ();
}
Void MyFunctionName ()
printf(“sum=%d\n”,83+27);
There is no difference if you want to declare the function above the main function or after the main
function but if you want to delare the function after the main function you have to prototype it
Copy this
Void MyFunctionName ()
Void MyFunctionName ()
Program
# include <Stdio.h>
# include<stdlib.h>
/* code*/
} */
Int main()
MyFunctionName ();
MyFunctionName ();
MyFunctionName ();
Void MyFunctionName ()
printf(“sum=%d\n”,83+27);
We will see even if you don’t prototype this the function executes but when you prototype your
function you are telling that there is a function called MyFunctionName which we are goingto define in
our code so your c program comes here and then,we see the function with my function name which is
defines in our code so it goes from here MyFunctionName () above the main to this MyFunctionName ()
after the main function,we see the definition of the function and we see the prototyping of the function
or the forward declaration of the function,so whenever your main calls the function
MyFunctionName ();
MyFunctionName ();
MyFunctionName ();
Void MyFunctionName ()
Void MyFunctionName ()
printf(“sum=%d\n”,83+27);
There are different types of functions like functions which takes in some parameters or functions which
returns some value we will see in the next chapter
Day 15:Passing Parameters and Arguments in Function
We have seen what is a function and how we can call the function,from our main function and how we
can declare a function like this,in this chapter we will see how to declare a function which takes the
parameters or arguments,suppose we want a function which adds two values and prints the sum so we
will declare a function which doesn’t declares anything and then we give the name of the function
Program
# include <stdio.h>
# include<stdlib.h>
Void Sum()
Int main()
The two numbers which we will pass them then we can pass the parameters as an argument,how to
pass the arguments,in these two brackets then we will give int the type of the argument and the name
of the argument say b
Program
# include <stdio.h>
# include<stdlib.h>
Int main()
Program
# include <stdio.h>
# include<stdlib.h>
Int main()
Then we will give two curly braces,and now what we want to do from this function we want to add
these variables and pass it to another variables called sum which will hold the sum of two arguments
which is a and b,we will do a+b and now we want to print the sum
Program
# include <stdio.h>
# include<stdlib.h>
int sum=a+b;
printf(“sum=%d”,sum)
Int main()
And whenever we want to print the sum we will call the sum function inside the main function
Program
# include <stdio.h>
# include<stdlib.h>
int sum=a+b;
printf(“sum=%d\n”,sum);
Int main()
Sum(50,83);
}
We will write 50,83 because the sum function
takes in two arguments one is a and other is b so you can pass 50 as a here in int a in the void sum
function and 83 as b in the int b in the void sum function,so the sum function will replace a by 50 and b
by 83,and then it will do the 50+83 and transfer it to the sum variable parameter int sum=a+b; and the
sum will be printed here the printf(“sum=%d”,sum);we see the output
output:
sum=130
you can also call the function multiple times here like this
Program
# include <stdio.h>
# include<stdlib.h>
int sum=a+b;
printf(“sum=%d\n”,sum);
Int main()
Sum(50,83);
Sum(13000,83);
Sum(13000,27);
Output:
Sum=130
Sum=13083
Sum=13027
Suppose you want user defined functions then you can give the functions like this
You can give two integers that are this int x,y and we will ask the user to give these two numbers
Program
# include <stdio.h>
# include<stdlib.h>
int sum=a+b;
printf(“sum=%d\n”,sum);
Int main()
Int x,y;
Printf(“number 1\n”);
Scanf(“%d”,&)
Sum(50,83);
The scanf which takes in the input and then the & symbol in scanf to in the integer,then the value of x
which is the first number and then we will pass the second number y
Program
# include <stdio.h>
# include<stdlib.h>
int sum=a+b;
printf(“sum=%d\n”,sum);
Int main()
{
Int x,y;
Printf(“number 1\n”);
Scanf(“%d”,& x);
Printf(“number 1\n”);
Scanf(“%d”,& x);
Sum(50,83);
Now we will pass x and y as the first argument and another argument for the sum function
Program
# include <stdio.h>
# include<stdlib.h>
int sum=a+b;
printf(“sum=%d\n”,sum);
Int main()
Int x,y;
Printf(“number 1\n”);
Scanf(“%d”,&x);
Printf(“number2\n”);
Scanf(“%d”,&y);
Sum(x,y);
}
Output:
number 1
9089
Number 2
989
Sum
9999
In this way we can take the two number print the sum of two numbers and we can see the sum
argument are these argument 1 int a and argument 3 int b,in the same way you can pass any number of
arguments to a function,and you can call your function inside your main program with those arguments
We will see here how to use functions to return some value,so till now we have been writing functions
like this with a void, void Sum( int a,int b ){,by any function we can return some value which can be of
any type like integer,double,string,float,so lets see how we can use function to return some value,for
example we want to make a function which gives us a product of something,the example we have seen
Program
# include <stdio.h>
# include<stdlib.h>
int sum=a+b;
printf(“sum=%d\n”,sum);
}
Int main()
Int x,y;
Printf(“number 1\n”);
Scanf(“%d”,&x);
Printf(“number2\n”);
Scanf(“%d”,&y);
Sum(x,y);
And what we were doing in the function we were asking the user to enter two values,we scan 1 st
number and we scan the 2 nd number,and then we pass these two numbers to this function
Sum(x,y);now let us see the numbers which gives the numbers or product of something and then we will
process this number,we are going to write program which gives multiplication of two numbers or
product of two numbers,we will write return value this,this type we will give name of the function
We were writing void because we were not returning anything but this time we are going to return
integer from the function so we are writing int here,we write the name of the function called product
and what this will do is it will take two arguments int a and int b
Program
# include <stdio.h>
# include<stdlib.h>
return( a * b )
Int main()
{
Int x,y;
Printf(“number 1\n”);
Scanf(“%d”,&x);
Printf(“number2\n”);
Scanf(“%d”,&y);
Sum(x,y);
return( a * b )
and what it is that the product function in int Product( int a , int b ) will take in two parameters a and b
as seen from int a and int b and then here this it will make the product of a and b ( a * b ) as seen in this
return( a * b ) and then return the value of this number as an integer int which is seen in this
int Product( int a , int b ) and then we can use this number for doing other things,and for calling this
function Product in this int Product( int a , int b ) we can call it like this,after the scanf statement of
number 2,we have declared two numbers int x,y and we are asking the person to enter two numbers
Int x,y;
Printf(“number 1\n”);
Scanf(“%d”,&x);
Printf(“number2\n”);
Scanf(“%d”,&y);
Product(x,y);
Program
# include <stdio.h>
# include<stdlib.h>
return( a * b )
Int main()
Int x,y;
Printf(“number 1\n”);
Scanf(“%d”,&x);
Printf(“number2\n”);
Scanf(“%d”,&y);
Product(x,y);
And then we will pass these two numbers to the product function Product(x,y); and this x,y will be
replaced in the product function here as these a and b in this in the place of a and b int Product( int a ,
int b )it will multiply these two numbers a and b and return( a * b ) and then it will return the product as
an integer in this int Product( int a , int b ) and because it is going to return the product we are going to
define an integer int before the Product(x,y) statement as this int prod
Program
# include <stdio.h>
# include<stdlib.h>
{
return( a * b )
Int main()
Int x,y;
Printf(“number 1\n”);
Scanf(“%d”,&x);
Printf(“number2\n”);
Scanf(“%d”,&y);
int prod;
Product(x,y);
The variable we will use to store the product which will be returned by this product function
Product(x,y);
Program
# include <stdio.h>
# include<stdlib.h>
return( a * b )
Int main()
Int x,y;
Scanf(“%d”,&x);
Printf(“number2\n”);
Scanf(“%d”,&y);
int prod;
prod = Product(x,y);
Because this function is going to return something as an integer in this int Product( int a , int b ) it will
store the integer in int prod which this function prod = Product(x,y) is going to return in this variable and
then we will print the product printf(“Product=%d”,prod)
Program
# include <stdio.h>
# include<stdlib.h>
return( a * b )
Int main()
Int x,y;
Printf(“number 1\n”);
Scanf(“%d”,&x);
Printf(“number2\n”);
Scanf(“%d”,&y);
int prod;
prod = Product(x,y);
printf(“Product=%d”,prod);
Output:
Number 1
30
Number 2
30
Product 900
So 30 and 30 are replaced here in this x,y in Product(x,y) and this goes here the int Product( int a , int
b ),the values of x and y as a and b in int a and int b in this function,and then it will multiply in this
return( a * b ) and then return the result into this variable product that is prod in these statements
int prod;
prod = Product(x,y);
printf(“Product=%d”,prod);
Program
# include <stdio.h>
# include<stdlib.h>
return( a * b )
}
Int main()
int prod;
prod = Product(13,90);
printf(“Product=%d”,prod);
We can directly pass two values numbers 13 and 90 this is so this is 13 and 90 that we have passes to
this function Product ,prod = Product(13,90); and this int Product( int a , int b ) since this function int
Product( int a , int b ) takes integers int a and int b so we will pass integers here the prod =
Product(13,90) and this 13 will be replaced here in this a in int a,int Product( int a , int b ) and 90 will be
replaced here this b in int b, int Product( int a , int b ) and then we will multiply a and b in this
return( a * b ) and then return the value here this int Product( int a , int b ) and then this can be stored in
this prod function
int prod;
prod = Product(13,90);
printf(“Product=%d”,prod);
int prod;
prod = Product(13,90);
printf(“Product=%d”,prod);
printf(“result=%d”, );
}
So whatever product we get from here this
prod = Product(13,90);
int prod;
prod = Product(13,90);
printf(“Product=%d”,prod);
printf(“result=%d”,Product(13,13));
Program
# include <stdio.h>
# include<stdlib.h>
return( a * b )
Int main()
int prod;
prod = Product(13,90);
printf(“Product=%d”,prod);
printf(“result=%d”,Product(13,13));
Program
# include <stdio.h>
# include<stdlib.h>
return( a * b )
Int main()
printf(“result=%d”,Product(13,13));
The multiplication of 13 and 13 is 170 and this is returned in this product function printf(“result=
%d”,Product(13,13)); and int Product( int a , int b ) and we can add 13 to this printf(“result=
%d”,Product(13,13)+13);the result will be 183
Program
# include <stdio.h>
# include<stdlib.h>
return( a * b )
Int main()
printf(“result=%d”,Product(13,13)+13);
Output:
Result=183
So you can use this function Product in this printf(“result=%d”,Product(13,13)+13); which returns the
integer value here this
and parameters and which you can do these functions here this,in this way you can use functions which
returns a value or number
We will see here the scope rules in c,There are two kinds of variable declaration in c,one os called local
variable which we have seen in the previous chapter and global variable,so local variable is the variable
which you declare inside some function,so inside main function you can declare int a=10,it is called a
local variable,whenever you declare a variable which is not inside any function is called global
variable,and the variable which is inside some function is called local variable
Program
# include<stdio.h>
# include<stdlib.h>
Int main()
///local variable
Int a=5;
Program
# include<stdio.h>
# include<stdlib.h>
Void NumberPrint()
Int main()
///local variable
Int a=5;
We take another function void NumberPrint() which does not take any argument,but prints something
Program
# include<stdio.h>
# include<stdlib.h>
Void NumberPrint()
///local variable
int x;
int a;
Int main()
///local variable
Int a=5;
This void NumberPrint() declaration of int x and int a is also a local variable,because it is declared inside
some function,but whenever you declare a variable int g,it is called a global variable because it is not
inside any function
Program
# include<stdio.h>
# include<stdlib.h>
int g;
Void NumberPrint()
///local variable
int x;
int a;
Int main()
///local variable
Int a=5;
So what is the difference between global variable and local variable is that a localvariable is valid only
inside your function,it is local to your function so you cannot use this a,int a=5 inside this function, Void
NumberPrint() and you cannot use this x inside this function, Int main(),but global variable can be used
in any function in the program,but the local variable can be used only in that specific function within
which it is declared in the program
Program
# include<stdio.h>
# include<stdlib.h>
///global variable
int g;
Void NumberPrint()
///local variable
int x;
int a;
Int main()
///local variable
Int a=5;
So whenever you declare global variable you don’t need to initialize by 0 because it is initialed by 0 when
you give this int g,it is g=0,but the local variable when you have not initialized it by 0 it can contain some
value and you may not know the value of a
Program
# include<stdio.h>
# include<stdlib.h>
///global variable
int g;
Void NumberPrint()
///local variable
int x;
int a;
Int main()
///local variable
Int a;
Program
# include<stdio.h>
# include<stdlib.h>
///global variable
int g;
Void NumberPrint()
///local variable
printf(“Global=%d”,g)
Int main()
///local variable
Int a;
# include<stdio.h>
# include<stdlib.h>
///global variable
int g;
Void NumberPrint()
///local variable
printf(“Global=%d”,g)
Int main()
///local variable
Int a;
Printf(“Local=%d”,a)
Or we can name the global variable as global_variable and local variable as local_variable
Program
# include<stdio.h>
# include<stdlib.h>
///global variable
int global_variable;
Void NumberPrint()
///local variable
printf(“Global=%d”,g)
Int main()
///local variable
Int local_variable;
Printf(“Local=%d”,a)
Program
# include<stdio.h>
# include<stdlib.h>
///global variable
int global_variable;
Void NumberPrint()
///local variable
printf(“Global=%d”,global_variable)
Int main()
{
///local variable
Int local_variable;
Printf(“Local=%d”,local_variable)
Program
# include<stdio.h>
# include<stdlib.h>
///global variable
int global_variable;
Void NumberPrint()
///local variable
Int main()
///local variable
Int local_variable;
Printf(“Local=%d\n”,local_variable)
printf(“Global=%d\n”,global_variable)
Output:
Local=79
Global=0
We see that even if we have not initialized the value of local variable by any value it takes the any value
and displays the value number,and the global variable even if we have not initialized it by any value it
takes the 0 value even if we have not assigned any value to this,in case of character it will be initialized
by a null character,and if it is a pointer in the next chapter we will learn it will be initialized by a null
pointer,now a global
Program
# include<stdio.h>
# include<stdlib.h>
///global variable
int global_variable=31;
Void NumberPrint()
///local variable
Int main()
///local variable
Int global_variable=38;
// Printf(“Local=%d\n”,local_variable)
printf(“Global=%d\n”,global_variable)
Suppose we initialize the global variable by value 38 and we initialize the local variable which has the
same name of global variable by 38,so we have initialized my global variable by 31 and the same name
we have given to our local variable as global variable and initialized it by 38 so whatever value is
contained by the local variable,even if it is declared by the same name globally,the value in this global
variable that is the global_variable will be the value in local variable,so this printf,printf(“Global=%d\
n”,global_variable) will display the value in the local variable that is 38,so the local variable always has
the preference above the global variable if they have the same name
Output:
Global_variable=38
Program
# include<stdio.h>
# include<stdlib.h>
///global variable
int global_variable=31;
Void NumberPrint()
///local variable
Printf(“Global+50=%d\n”,Global_variable+50);
Int main()
///local variable
Int global_variable=38;
// Printf(“Local=%d\n”,local_variable);
printf(“Global=%d\n”,global_variable);
}
So what this program is doing is that it will print the value of global variable as 31 and add 50 to it
This
Output:
global_variable=38
global +50=81
so we see that global variable can be used inside any function in the program so it displays the value of
global +50=81,but local variable can be used only in the specific function in which it is declared
Program
# include<stdio.h>
# include<stdlib.h>
///global variable
int global_variable=31;
Void NumberPrint()
///local variable
Num_Local=30;
Printf(“Global+50=%d\n”,Global_variable+50);
Int main()
///local variable
Int global_variable=38;
printf(“Num Local=%d\n”,Num_Local);
printf(“Global=%d\n”,global_variable);
Now suppose you declare the, int Num_Local=30; inside the, void NumberPrint() function before
global+50 printf and you want to print it in this main function after int global_variable=38 as this
printf(“Num Local=%d\n”,Num_Local);because this Num_Local=30 inside the void NumberPrint()
function is not accessed in this main function as this printf(“Num Local=%d\n”,Num_Local);because thgis
function inside the void number print function exists in another function another than main function,we
run the program and see this error as this Num_Local is undefined first use it in the function,so you
cannot use the local variable inside another functions like declaring in main function
Day 18:Arrays in C
Arrays are variables which can contain more than 1 values of same data type,suppose you want to store
similar data,inside a variable you can do it with the using of array,suppose you want to store marks of
students or ages of persons,or the names of any number of persons,you can do this using an array using
a single variable,so declaringarray is same as declaring the variable so first is the data type,what kind of
data you want to store in the array,and then the name of the array that is int MyNumberArray [ ],this
square bracket tells us that it is an array and whatever number you insert inside the square bracket,this
is the number of elements you can store inside your array
Program
# include<stdio.h>
# include<stdlib.h>
int main()
}
Suppose we have written 3 here so we can store 3 numbers here the and you can initialize it by the
equal to sign = and inside the curly braces you can give values like 20,30,60
Program
# include<stdio.h>
# include<stdlib.h>
int main()
We have stored 3 values because here we have defined that we can store 3 values
Then we can assign 3 values to the array if it was 4 then we can add 1 more value here that is 55 to store
these values this
Program
# include<stdio.h>
# include<stdlib.h>
int main()
Suppose you have 5 values you can store 5 values here this
Program
# include<stdio.h>
# include<stdlib.h>
int main()
So like this you can access the values of the array and there is something called the index pf the
array,the index pf the array is the position at which any value is place so the index of value 20 is 0 so the
index of the array starts from 0 and not from 1,so the index of the array stats from 0 and at the 0 th
index the value is this 20 ,at the 1 st index the value is 30 ,at the 2 nd index the value is 60 ,at 3 rd index
the value is 55,at the 4 th index the value is 80, now to access the value of the array ,you can access the
value of the array like this,you can name the variable,int accessArray,suppose we want to store the 2 nd
value of the array inside this variable int accessArray,you can copy the name of the array
MyNumberArray from int MyNumberArray[3]
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int accessArray=MyNumberArray[ ]
}
And in the square bracket the index of the array whose value we want to store,int
accessArray=MyNumberArray[ ] so the index of the array value 30 is 1 so we will put this as this,int
accessArray=MyNumberArray[ 1 ];so the value 30 from the array will be stored inside this accessArray
variable and then we can write the value =%d\n”,access array
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int accessArray=MyNumberArray[ 1 ];
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int accessArray=MyNumberArray[ 1 ];
printf(“value=%d\n”,acccessArray);
}
So this will print 30 because we have assigned index at 30 which is 1 of this array,int
accessArray=MyNumberArray[ 1 ];
Output:
30
In the similar way you can define any character array or any double array and we want to store 3
characters inside this array char charArray[ 4 ]={ } we can store these values of the char array inside the
curly braces, char charArray[ 4 ]={‘ a ’ , ’ b ’ , ’ c ’ , ’ d ’ };
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int accessArray=MyNumberArray[ 1 ];
printf(“value=%d\n”,acccessArray);
But always see that if you have declared integer arrays it can contain only integers
if you can float array it can only have float values now for example
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int accessArray=MyNumberArray[ 1 ];
printf(“value=%d\n”,acccessArray);
Suppose you want to print the values of the array containing 6 values the index of the arrays are
0,1,2,3,4,5 we can use for loop for that
Program
# include<stdio.h>
# include<stdlib.h>
int main()
printf(“value=%d\n”,acccessArray);
We can use for(int i=0;i<the index of the array is less than the size of the array, )
we will print the index of the array 1 by 1 we will write the name of the array,why we have written I in
the brackets of the MyNumberArray because the value of I will be increased in the for loop,so for i=0 in
the forlop int i=0 it will print 20, int MyNumberArray [6]={20,30,60,50,55,80 }
array at index 1 is 30,now once again the value of i becomes 2 int i=2,so the value of I at index 2 is 60 so
it will print all the values at the array
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int accessArray=MyNumberArray[ 1 ];
printf(“value=%d\n”,acccessArray);
for(int i=0;i<6;i++)
{
Output:
value=30
element[ 0 ]=20
element[ 1 ]=30
element[ 2 ]=60
element [ 3 ]=50
element[ 4 ]=55
element[ 5 ]=80
suppose you want to change the value of the value of the array at index i=1 the value of this 30,so what
you can do is that we can copy this value this,suppose the name of the array
Program
# include<stdio.h>
# include<stdlib.h>
int main()
MyNumberArray[ 1 ]=45;
for(int i=0;i<6;i++)
}
So the index of the value we want to change is 1,I want to change the value to 45 so the value of index
at value of 30 is to be changed to 45,we will verify this function by using this for loop we can see the
value this
Output:
value=30
element[ 0 ]=20
element[ 1 ]=45
element[ 2 ]=60
element [ 3 ]=50
element[ 4 ]=55
element[ 5 ]=80
so we see in the output that the value of the array at index 1 is changed to 45
this is what we were seeing as single dimentional arrays there are another types of arrays also called
multi dimensional arrays which we will see here this
We have seen how to use a single dimensional arrays in c,you can declare the arrays like this
int MyNumberArray [6]={20,30,60,50,55,80 }; and the array can contain more than 1 values of the same
data type in a single variable data type in the same way if you want to declare a muli dimensional
arrays,or if you want to declare a 3 dimentional array or if you want to declare a array it can be a 2
dimensional array or 3 dimentional array or another type of array
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int MyNumberArray [ 2 ][ 3 ];
we have 2 which is the number of rows of the array and 3 is like the number of columns of the array,so
lets see how we can initialize this array first,inside your curly braces you can declare your arrays first we
will define the row 0
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int MyNumberArray [ 2 ][ 3 ] = {
{ 1 , 3 , 8 },///row 0
{ 5 , 8 , 9}///row 1
};
We will define 2 rows that our array contains 2 rows if we see this 2 rows and 3 columns
So 1,3,8 are columns of row 0 and 5,8,9 are the elements of the 2 nd row of index 1,and it has 3 columns
like 5,8,9 so this is a 2 dimensional array and it contains 2 rows and 3 columns
{ 1 , 3 , 8 },
{ 5 , 8 , 9}
The index of the array starts from 0 so this 0 th row contains elements 1,3,8 and 1 st row contains
elements 5,8,9 and the 1 st column contains elements 1,5 the 2 nd column contains elements 3,8, and 3
rd column contains elements 8,9
in order to iterate for these values we can use for loop,but a single for loop will not work for these 2
dimensional arrays,you need to use 2 for loops so loop inside a loop to iterate all the values in this
array,to iterating inside the arrays you need 2 for loops,you can write first for loop and this for loop will
be for rows
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int MyNumberArray [ 2 ][ 3 ] = {
{ 1 , 3 , 8 },///row 0
{ 5 , 8 , 9}///row 1
};
for()
We will declare a variable i or int i =0,and this I will be less than 2 that is i<2,that is the number of rows
and then i++ which is the increment operator the for loop is this, for( int i=0 ; i<2 ; i++ ) and inside this
for loop we will declare another for loop
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int MyNumberArray [ 2 ][ 3 ] = {
{ 1 , 3 , 8 },///row 0
{ 5 , 8 , 9}///row 1
};
Inside the curly braces of this for loop we will declare another for loop,so we have this for loop to iterate
for the columns for(int j=0 ; j<3 ; j++ )
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int MyNumberArray [ 2 ][ 3 ] = {
{ 1 , 3 , 8 },///row 0
{ 5 , 8 , 9}///row 1
};
To print the values inside the column for loop you can write printf and then the name of the array
MyNumberArray %d %d=%d and this will be the value of the array and we will also print the index of the
array,printf(“MyNumberArray[%d][%d]=%d”)
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int MyNumberArray [ 2 ][ 3 ] = {
{ 1 , 3 , 8 },///row 0
{ 5 , 8 , 9}///row 1
};
printf(“MyNumberArray[%d][%d]=%d”)
}
So here it will show the row ,the first % d in MyNumberArray,printf(“MyNumberArray[%d][%d]=
%d”),and the another % d followed by the first %d will show the column and the 3 rd %d followed by =
sign will give the value of the array,rows are iterated by i, in this for( int i=0 ; i<2 ; i++ ) so write i here in
this printf(“MyNumberArray[%d][%d]=%d”,i ),and colums are iterated by j,on this for(int j=0 ; j<3 ; j+
+ ) so write j here this printf(“MyNumberArray[%d][%d]=%d”, i , j , ) and the values can be iterated by
your array index int MyNumberArray [ 2 ][ 3 ] so write the name of the array
printf(“MyNumberArray[%d][%d]=%d”, i , j , MyNumberArray )and then the value of i which is the row
indicator printf(“MyNumberArray[%d][%d]=%d”, i , j , MyNumberArray[ i ] )and the value of j which is
the column indicator printf(“MyNumberArray[%d][%d]=%d”, i , j , MyNumberArray[ i ][ j ] ); we will give
next line\n so that it will iterate on the next line for different cases printf(“MyNumberArray[%d][%d]=
%d\n”, i , j , MyNumberArray[ i ][ j ] );
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int MyNumberArray [ 2 ][ 3 ] = {
{ 1 , 3 , 8 },///row 0
{ 5 , 8 , 9}///row 1
};
printf(“MyNumberArray[%d][%d]=%d\n”, i , j , MyNumberArray[ i ][ j ] );
}
}
Output:
MyNumberArray[ 0 ][ 0 ]=1
MyNumberArray[ 0 ][ 1 ]=3
MyNumberArray[ 0 ][ 2 ]=8
MyNumberArray[ 1 ][ 0 ]=5
MyNumberArray[ 1 ][ 1 ]=8
MyNumberArray[ 1 ][ 2 ]=9
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int MyNumberArray [ 3 ][ 3 ] = {
{ 1 , 3 , 8 },///row 0
{ 5 , 8 , 9}///row 1
{ 3 , 9 , 8}///row 2
};
printf(“MyNumberArray[%d][%d]=%d\n”, i , j , MyNumberArray[ i ][ j ] );
So you can have 3 rows and 3 columns also as seen above the program
We have seen in the previos chapters what are arrays and how to use arrays we have also seen how to
use multidimentional arrays,we will see how to pass arrays as an argument of the function,suppose we
want to calculate the sum of the values of the array suppose we have the arrays
Program
# include<stdio.h>
# include<stdlib.h>
int main()
Suppose we want to calculate the sum of the arrays,we have 6 elements and we want to display the sum
of the elements of the array,sum of each and all the elements,we will show the summation of the
elements of the arrays using the function,there are different ways of passing arrays as the argument of
the function,we will declare a function void which is not returning anything
Program
# include<stdio.h>
# include<stdlib.h>
Void ArraySum()
int main()
Array sum is the name of the function,we can pass arrays as the function of the argument,1 way is using
the pointer give * and then give the array name MyArray,so you can pass pointer array to your function
but we haven’t seen the pointer array now, void ArraySum(int * MyArray)
Program
# include<stdio.h>
# include<stdlib.h>
int main()
When we will see how to pass pointer in c then we will see this kind of arrays as a function argument,the
another way is to pass definite sized arrays
Suppose we pass the array,we can define array then we will give the size to the array of the array 6
Program
# include<stdio.h>
# include<stdlib.h>
int main()
But there is thing that you can only pass the array with size 6 in the function,it will not be the general
functionality of the function that the functions should be used in different conditions,this condition will
not be the general functionality of the function, the use of function,to provide the general functionality
of the function that is to be used in another conditions also, we can pass the unsized array,that is you
give the data type,the name of the array and in that you don’t give any size of the array,you can put
different sized arrays or unsized array,also we will pass the size as the argument also this
Program
# include<stdio.h>
# include<stdlib.h>
{
}
int main()
We can get the size of the array and the array name from here this void ArraySum(int MyArray[ ],int
size),from int size and int MyArray[ ],now we can pass different sized array to the function like this using
this function void ArraySum(int MyArray[ ],int size) we want to add all the elements of the array int
MyNumberArray [ 6 ] = {20,30,60,50,55,80},so we want to return the integer int ArraySum(int MyArray[
],int size) as the result
Program
# include<stdio.h>
# include<stdlib.h>
int sum = 0;
int main()
because we want to calculate the sum we will declare a variable called sum=0
int sum=0;
now we will use the for loop,
what this for loop is doing is it will iterate the array, int ArraySum(int MyArray[ ],int size),MyArray 1 by
1 and we will add the values of the element, int MyNumberArray [ 6 ] = {20,30,60,50,55,80}; to this sum,
int sum = 0; and it will add 20,30,60,50,55,80 1 by 1 to this sum,int sum = 0;so this array declaration is
like that for and in the bracket we will initialize i by 0,I should be less that the size of the array that is
6,so pass this size argument here this, int ArraySum(int MyArray[ ],int size),size in int size to this for
condition, for( int i=0 ; i < 6 ; i++ ), for( int i=0 ; i < size ; i++ ) then we will increment the value of i by 1
Program
# include<stdio.h>
# include<stdlib.h>
int sum = 0;
Sum=sum+
int main()
We see that sum =sum+ ,the sum in sum+ is the previous value of sum+the array name
int ArraySum(int MyArray[ ],int size),the array name MyArray from this function so the declaration of
sum will be sum = sum + MyArray [ i ],i is the index of the array,this i will change in the for loop, for( int
i=0 ; i < 6 ; i++ ),now the value of i is 0,for i=0 the value of the element of the array from this is the, int
MyNumberArray [ 6 ] = {20,30,60,50,55,80}; the value of i is 20,so the the value of MyArray at i=0 in this,
sum = sum + MyArray [ i ], will be 20 when we pass this array as the argument to the function here this,
int ArraySum(int MyArray[ ],int size),in this MyArray in int MyArray
Program
# include<stdio.h>
# include<stdlib.h>
int sum = 0;
int main()
Program
# include<stdio.h>
# include<stdlib.h>
int ArraySum(int MyArray[ ],int size)
int sum = 0;
Sum+ = MyArray [ i ];
Return sum;
int main()
int ArraySum;
And then we will return the sum after adding all the elements of the array we will return the sum of the
array as an integer here this, int ArraySum(int MyArray[ ],int size) now for using this function we have
the array, int MyNumberArray [ 6 ] = {20,30,60,50,55,80}; and we have to sum all the elements of the
array,we will call an integer called ArraySum after this int MyNumberArray [ 6 ] = {20,30,60,50,55,80};
That is int ArraySum;so this int ArraySum will contain the result of this,ArraySum in int Arraysum here
this, int ArraySum(int MyArray[ ],int size),but this ArraySum in int ArraySum; and int ArraySum(int
MyArray[ ],int size) are same so we will chage the name in int ArraySum as this int sum_of _array;and
what is its value,its value is the value returned by this function that is arraysum in int ArraySum(int
MyArray[ ],int size),so int sum_of_array=ArraySum();when we pass the array, int MyNumberArray [ 6 ] =
{20,30,60,50,55,80};so this array ArraySum(); takes two arguments,MyArray,in this int ArraySum(int
MyArray[ ],int size),so my array is this,MyNumberArray in this, int MyNumberArray [ 6 ] =
{20,30,60,50,55,80};so whe have this, int sum_of_array=ArraySum(MyNumberArray);and then it will
take the size of the array,the size of the array is 6 in this case so we have this, int
sum_of_array=ArraySum(MyNumberArray , 6 );
Program
# include<stdio.h>
# include<stdlib.h>
int sum = 0;
Sum+ = MyArray [ i ];
Return sum;
int main()
int sum_of_array=ArraySum(MyNumberArray , 6 );
And the we will print the value of the array,printf(“Array sum=%d”,),because this sum_of_array in, int
sum_of_array will return the array sum,ArraySum in this, ArraySum(MyNumberArray , 6 );and this, int
ArraySum(int MyArray[ ],int size) so this value ArraySum is assigned to this variable sum_of_array and
then we will pass this here this, printf(“Array sum=%d”,sum_of_array);
Program
# include<stdio.h>
# include<stdlib.h>
int sum = 0;
Sum+ = MyArray [ i ];
Return sum;
int main()
int sum_of_array=ArraySum(MyNumberArray , 6 );
printf(“Array Sum=%d”,sum_of_array);
Output:
Array Sum=300
Program
# include<stdio.h>
# include<stdlib.h>
int sum = 0;
Sum+ = MyArray [ i ];
Return sum;
int main()
int sum_of_array=ArraySum(MyNumberArray , 7 );
printf(“Array Sum=%d”,sum_of_array);
Output:
Array Sum=383
Suppose we want to print the array of size 8 then we can add 1 more element to the array that is 122
Program
# include<stdio.h>
# include<stdlib.h>
int sum = 0;
Sum+ = MyArray [ i ];
Return sum;
int main()
int sum_of_array=ArraySum(MyNumberArray , 8 );
printf(“Array Sum=%d”,sum_of_array);
Array Sum=500
In this way you can use the array or you can use the function,and pass array as the argument of the
function
Day 22:Pointers in C
We will see what are pointers and how to use pointers in c,a pointer is a variable whose value is the
address of another variable,you may see what is the address of the variable,so in c whenever you
declare variable x or variable val in c,the computer stores this variable at some particular memory
address
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int val=30;
Program
# include<stdio.h>
# include<stdlib.h>
int val=30;
This % will not be using here this because % we use to print the decimal values and this we are usingto
print the hexadecimal values,to print this hexadecimal values we use this % x and this % x is that we
want to print some hexadecimal values so whenever you use & before your variable name,it will give
you the address of the variable
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int val=30;
Output:
Address of val=8ffc1
This is that it is hexadecimal memory address on the computer,so where this variable val is stored,so
what is a pointer,a pointer is a variable whose value is the address of another variable and we are using
& before the variable & val we are accessing the address of the variable,so a pointer can be declared as
this value also.to declare a pointer in c you do this,you have the data type and then this * tells c that
you want to declare a pointer like this int * pointer_p;we defin e pointer as a variable but if you write
this * before this pointer like * pointer it becomes a pointer
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int val=30;
int * pointer_p;
So whenever you use this pointer variable the pointer p its value can be the address of another variable
Program
# include<stdio.h>
# include<stdlib.h>
/// a pointer is a variable whose value is the address of another variable
int main()
int val=30;
int * pointer_p;
So we see that pointer_p is a pointer which is a variable and whose values is= & val,which is the address
of another variable,so whenever you assign the address to this another variable you don’t need to use *
before this like this int * pointer_p you can define like this pointer_p = & val;so at the time of
declaration only you need to give this * and at the time of assignment you don’t need this asterix before
the pointer_p,so now this pointer pointer_p, has the address of this value, & val,now how can you
access this value 30 using the pointer,pointer_p ,*pointer_p
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int val=30;
int * pointer_p;
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int val=30;
int * pointer_p;
Lets see what is the value of this pointer variable without this & symbol, printf(“value of pointer
variable=% x\n”, & pointer_p); that is this printf(“value of pointer variable=% x\n”, pointer_p);so we see
that this value & val in this pointer_p = & val; and in this printf(“address of val=%d\n”, & val ); and this
value pointer_p in this printf(“value of pointer variable=% x\n”, pointer_p); should be same,because we
have assigned this to the variable here this pointer_p,so this pointer_p in this printf(“value of pointer
variable=% x\n”, pointer_p) and this pointer_p = & val; should print the same value
Output:
address of val=8ffc1
now to access the values which is stored on this address,you want to access the value 30,so using this
value we want to use this pointer_p to access this variable,but pointer can only store the address of the
variable,this variable has the value of 30 int val=30;now to access the value of the address,you use
something like this the value of this you can access the value of pointer so this pointer in this,
printf(“value of pointer=% x\n”, * pointer_p);* pointer _p contains the address
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int val=30;
int * pointer_p;
}
So whenever you add this * before the pointer, printf(“value of pointer=% x\n”, * pointer_p); pointer_p
that is * pointer_p it shows the value at that address,we want to access this, printf(“value of pointer=%
x\n”, * pointer_p); in place of this,printf(“value of pointer=% d\n”, * pointer_p); so we replace % x by %
d because we are accessing the integer value int val=30;now we run the program and see the output
that
Output:
address of val=8ffc1
value of pointer=30
so in all the variable in c is stored in your computer in a particular address int val=30; and this address
you can access using the & variable , printf(“address of val=%d\n”, & val );and a pointer variable whose
value is the address of another variable,you can declare pointer like this by adding * before your
variable name like this, int * pointer_p; and you can assign some value to the pointer that is
the,pointer_p using address of another variable,pointer_p=& val;you can assign this variable address
that is the,pointer_p in this pointer_p=& val and int * pointer_p to the pointer,so int val=30 is the
variable,for assigning this variable address to the pointer we assign it like this & val that is in this,
pointer_p = & val;
now to dereference the pointer, that is the pointer_p printf(“value of pointer=% d\n”, * pointer_p);the
value that is stored at this address * pointer_p is hold by this pointer,we use this * sign before
pointer_p and this * before the pointer tells c that,we want to get the value of the address which is
stored in this pointer
we have seen what are pointers in c and how we can use pointers in c,we have given an example how to
declare a pointer,we have seen how to assign values to the pointer,a pointer is a variable whose value is
the address of another variable which we have seen here this, pointer_p = & val;so this & symbol which
we add before any variable & val,and this value we can assign it to a pointer variable this
in this chapter we will see how we can use arrays with pointers
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int val=30;
int * pointer_p;
Suppose we have the array called val_array having3 values or we can define array of size 3,we can
initialize arrays with these 3 values this,now we can assign this array to the pointer in the same way we
have assigned the variable to the pointer in this, pointer_p = & val;
Program
# include<stdio.h>
# include<stdlib.h>
int main()
For that we need to declare the pointer array,that is the int * pointer_array and then similar to array we
will give size to the pointer,that is the int * pointer_array[ 3 ],now to assign values from array that is this
int val_array[ 3 ]={ 30 , 40 , 50 }; to this pointer int * pointer_array[ 3 ];we will use for loop to iterate the
array,for( int i=0 ; i< 3 ; i++ )
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int * pointer_array[ 3 ];
Then inside the for loop we can assign the values from array that is this array, val_array in this int
val_array[ 3 ]={ 30 , 40 , 50 }; to the pointer int * pointer_array[ 3 ]; in the for loop bracket we will
provide the pointer variable and then index of the array
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int * pointer_array[ 3 ];
Pointer_array[ i ]
Pointer_array[ i ]=&
So we are going to add this & or we are going add the address,or we are going to access the address of
the variable val_array in this, int val_array[ 3 ]={ 30 , 40 , 50 }; which we write after the & symbol this is
this,Pointer_array[ i ]=& val_array
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int * pointer_array[ 3 ];
So we are goingto access the address of val array at index I,we have seen how to use arrays in for loop
and how to access the value of array using for loop,we have seen iterarting arrays using for loop,so this
for loop we are going to use to iterate the array 1 by 1,so we are iteratingthe value of i 1 by 1 at index
i=0,the index is in & val_array[ i ], in this Pointer_array[ i ]=& val_array[ i ];so at index i=0 we access the
value 30 from this ,int val_array[ 3 ]={ 30 , 40 , 50 }; at index i=1 we access the value 40 from this, int
val_array[ 3 ]={ 30 , 40 , 50 }; and at index i=2 we access the value 50 from this, int val_array[ 3 ]={ 30 ,
40 , 50 };so what we have done is that we have assigned the address, & val_array[ i ] in
this;Pointer_array[ i ]=& val_array[ i ]; to the pointer array that is this, Pointer_array[ i ] so the index of
pointer is that whatever value is at this index i in this & val_array[ i ];the address of this & in this ,&
val_array[ i ];
So what we have done is we have assigned the address of array elements to the pointer
Program
# include<stdio.h>
# include<stdlib.h>
/// a pointer is a variable whose value is the address of another variable
int main()
int * pointer_array[ 3 ];
Pointer_array[ i ]=& val_array[ i ];/// assigned the address of array elements to the pointer
Now by using this pointer,Pointer_array[ i ] we can access all the values of the array that is
this{30,40,50} in this,int val_array[ 3 ]={ 30 , 40 , 50 };
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int * pointer_array[ 3 ];
Pointer_array[ i ]=& val_array[ i ];/// assigned the address of array elements to the pointer
Pointer_array[ i ]=& val_array[ i ];/// assigned the address of array elements to the pointer
In the another for loop we are going to access the value using this pointer, Pointer_array[ i ]=&
val_array[ i ];/// assigned the address of array elements to the pointer,so we are going to access our
array like this,we are going to printf,then we will write variable array i or the value,printf(“value of
val_array”) at index i, printf(“value of val_array[ i ]=”) instead of I we can write %d, printf(“value of
val_array[ %d]= %d”),the =%d will give the value this, printf(“value of val_array[ %d]= %d\
n,i,pointer_array”),the pointer_array here stores the address and if we write this, printf(“value of
val_array[ %d]= %d\n”,i,pointer_array[ i ] ), this contains the address of the index this,which is contained
by i so this address,pointer_array[ i ] ) in this, printf(“value of val_array[ %d]= %d\n”,i,pointer_array[ i ] )
is the address of the variable,if you want to access the value of this address or the value which is
contained at this address,you need to dereference this pointer_array[ i ] like this by putting * sign,this
printf(“value of val_array[ %d]= %d\n”,i,*pointer_array[ i ] )
Program
# include<stdio.h>
# include<stdlib.h>
int main()
int * pointer_array[ 3 ];
for( int i=0 ; i< 3 ; i++ )
Pointer_array[ i ]=& val_array[ i ];/// assigned the address of array elements to the pointer
Output:
so you can assign the array to the pointer array,and then access the array using the pointer array like
this pointer_array[ i ] ),in this way you can use array pointers in c
We were seen how to use pointers in c and how to use array pointers in c,in this chapter we will see
how to pass pointers as the argument function,let us see the example let us pass a pointer as the
argument to the function which is the pointer,so we will write a function which will take pointer as an
argument,so we can return void which is not returning anything,and then get value,void getValue,so this
getValue is the function and we are passing the argument which is the pointer,to this function
Program
# include<stdio.h>
# include<stdlib.h>
*my_pointer=10000;
return;
int main()
We will declare a variable in the main function, int get_the_value,we are not initializing this,consider
this because we will store at the address of this variable, get_the_value,this value, get_the_value,we will
call the getValue function
Program
# include<stdio.h>
# include<stdlib.h>
/// a pointer is a variable whose value is the address of another variable
*my_pointer=10000;
return;
int main()
int get_the_value;
getValue
So we have seen that a pointer, a pointer is a variable whose value is the address of another variable
this,we will call the getValue function , void getValue( int * my_pointer),so this pointer,*my_pointer in
this void getValue( int * my_pointer) whenever you want to pass some variable in place of this pointer,
int get_the_value; as an argument of this function,*my_pointer in this,void getValue( int *
my_pointer),you can pass the address of some variable in here as an argument in this, int
get_the_value;the address we can pass it like this,getValue(&),so whenever we write this address in
front of the variable, int get_the_value; it is the address of this variable this
Program
# include<stdio.h>
# include<stdlib.h>
*my_pointer=10000;
return;
int main()
int get_the_value;
getValue(&get_the_value);
So you can do something like this,we see this a pointer is a variable whose value is the address of
another variable,in this getValue(&get_the_value);so in place of this pointer,int get_the_value;we can
pass the address of another variable this,we do the printf as this,printf(“the value of get_the_value=
%d”,get_the_value);
Program
# include<stdio.h>
# include<stdlib.h>
*my_pointer=10000;
return;
int main()
{
int get_the_value;
getValue(&get_the_value);
So what will be the value of this variable get_the_value after %d in this, printf(“the value of
get_the_value=%d”,get_the_value);we have not initialized this variable, get_the_value by any value
this,but we have assigned this variable to this function,getValue in this, getValue(&get_the_value); as
the argument,lets see the answer ans see what we have done
Output:
What this function is doing,this function is taking the pointer,or the address of the variable, void
getValue( int * my_pointer),because the pointer is the address of another variable,so we have passed
address ofanother variable,so this argument,& get_the_value in this,getValue(&get_the_value);can be
replaced by this value, int * my_pointer in this,void getValue( int * my_pointer),the address of this
variable, getValue(&get_the_value);so what we have done here this is that we have indirectly assigned
10000 to the address of this variable, &get_the_value=10000
Program
# include<stdio.h>
# include<stdlib.h>
*my_pointer=10000;/// &get_the_value=10000
return;
}
int main()
int get_the_value;
getValue(&get_the_value);
So whenever we would access this variable, &get_the_value this value the 10000 we would be
accessing,so you can assign some value to the address, &get_the_value,and you can access the value at
the address,&get_the_value in this, getValue(&get_the_value);and here this printf(“the value of
get_the_value=%d”,get_the_value); you are not returning anything but this address &get_the_value in
this,getValue(&get_the_value); can store this value using pointers,we have seen how to use arrays the
argument of the function,you can also pass pointers as the array argument,we will see how to pass
pointer array as the argument of the function
Program
# include<stdio.h>
# include<stdlib.h>
int
int main()
We want to return integer and we want the sum of all the elements,suppose array contains 4 variables
and these 4 variables,int my_array[ 4 ]={10,20,30,40},we want the sum of all the elements of the
array,so we want the function which returns integer,it takes get sum
Program
# include<stdio.h>
# include<stdlib.h>
Int getSum()
int main()
We want to pass array as an argument but you can pass array in 3 ways as a function as an argument
this
Program
# include<stdio.h>
# include<stdlib.h>
int main()
}
Give size of the array
Program
# include<stdio.h>
# include<stdlib.h>
int main()
Program
# include<stdio.h>
# include<stdlib.h>
int main()
}
So we are passing the pointer array in this, Int getSum(int * array_val ) and we will also define the size
int size
Program
# include<stdio.h>
# include<stdlib.h>
int main()
then we will use this array to get the sum,we will declare the variable,which will contain the sum,sum=0,
Program
# include<stdio.h>
# include<stdlib.h>
int sum=0;
Sum+= array_val[ ]
int main()
We have seen how to pass arrays to the function,at every index of the array,we are adding it to the
sum,when we have all the sum,we will return sum
Program
# include<stdio.h>
# include<stdlib.h>
int sum=0;
Sum+= array_val[ ]
}
Return sum;
int main()
We will pass this array , Int getSum(int * array_val,int size ) as the pointer
We will call the function get sum and also we will declare the variable get sum,int mySum=get sum will
return the array which we will pass as an argument, Int getSum(int * array_val,int size ),you can store in
this variable,int mySum=getSum(my_array, size),the another argument is the size of the array,then we
will print the value of my sum
Program
# include<stdio.h>
# include<stdlib.h>
int sum=0;
Sum+= array_val[ ]
Return sum;
}
int main()
Int mySum=getSum(my_array, 4)
We will pass my array as the 1 st argument, Int my_array[ 4 ]={ 10 , 20 , 30 , 40 } and the another
argument is the size of the array which is 4,and we will print the value of my sum,the value of
printf,printf(“the value of my sum=%d”,mySum);
Program
# include<stdio.h>
# include<stdlib.h>
int sum=0;
Sum+= array_val[ ]
Return sum;
int main()
Output:
Program
# include<stdio.h>
# include<stdlib.h>
int sum=0;
Sum+= array_val[ ]
Return sum;
int main()
{
Int my_array[ 5 ]={ 10 , 20 , 30 , 40 , 50 }
Int mySum=getSum(my_array, 5)
Output:
Day 24:Strings in C
This chapter we will see what are strings in c,a string is the 1 dimensional array of characters,which is
terminated by the null character,now we will see how to declare string in array and then we will see
how to use this,the character arrya we can declare like this
Program
# include <stdio.h>
# include <stdlib.h>
int main()
Return 0;
}
In the curly braces you can do the initialization of characters,this \0 is called null terminated character,so
this hello is the string,string is terminated by the null character,we can say that string is null terminated
character array,string is a character array of non definite size,for example char,my_another_string
Program
# include <stdio.h>
# include <stdlib.h>
int main()
Char my_another_string[ ]
Return 0;
The array is of non definite size which is not defined, Char my_another_string[ ]
The way of initializing string is also like the defined sized array,like this which is also initialized bt hello,
Char my_string[ 6 ]={‘ h ‘ , ‘ e ‘ , ‘ l ‘ , ‘ l ‘ , ‘ o ‘ , ‘\0’};
Program
# include <stdio.h>
# include <stdlib.h>
int main()
Return 0;
Printf we will use this variable my_string,we will see what is the value of this variable,we will give line
break to the message we are printing,so that we will print another message at another line
Program
# include <stdio.h>
# include <stdlib.h>
int main()
Return 0;
And we will print the another variable,my_another_string in the printf like this,we see the output this
Output:
My string value=hello
My another string value=hello
This way we can define the string in array,now there is the header file in c called string which we can add
in this,main.c,then you can use some functions from the header file and then you can do different
functions from the header file,so we can use the function using the header file,in main .c so the file is #
include <string.h>
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int main()
Return 0;
So with this header file, # include <string.h> we can do different functions with string,the functions from
the sTringwhich we are going to use is this,strcpy enables us to copy 1 string from the another
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int main()
Char string3[13 ];
strcpy
Return 0;
This string.h header file we can use this strcpy,and it takes two arguments 1 is the destination and 1 is
the source,suppose we want to copy string 1 to string 3 so this string,so this strcpy function copies the
string 1 to string 3 in this way,so you write this,or you take the variable in which you want to copy as the
1 st argument of strcpy,that is this,strcpy(string 3, );and the another argument is the value of the
variable which you want to copy that is string 1,that is in this,strcpy(string 3,string 1);we can see this like
this that strcpy is used to copy from the source to the destination so source is string 1 and destination is
string 3
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int main()
{
Char string3[13 ];
Return 0;
There is another function in string.h header file that is this strcat,this allows us to concatenate the
strings,so we want to concatenate the 1 st string and another string we can do this like this strcat(string
1,string 3)
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int main()
Char string3[13 ];
The 1 st string and after that the another string,we will do this like this,when we hover at this we see
that strcat can also return some value this so it is adding another string to string 1
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int main()
Char string3[13 ];
Return 0;
There is another function in the header file string.h called strlen,which gives the string length,we will see
the size of this character array,we will see like this,and we save it in another variable this, int
length_string 1= strlen(string 1);
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int main()
Char string3[13 ];
printf(“strcpy=%s”,string 3);
Return 0;
So this strlen returns the string length of the strlen and pass it as the argument string 1 in
this,strlen(string 1);printf(“strcpy=%s”,string 3);because we are copying from string 1 to string 3 we are
see the value of string 3 so we give string 3 in printf statement of strcpy,similarly we see the strcat printf
statement,we will see the string1 because we are adding the string 3 to string 1 so we will see the string
1
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int main()
Char string3[13 ];
printf(“strcpy=%s\n”,string 3);
printf(“strcat=%s\n”,string 1);
Return 0;
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int main()
{
Char string1[13 ]=”hello”;
Char string3[13 ];
printf(“strcpy=%s\n”,string 3);
printf(“strcat=%s\n”,string 1);
printf(“strlen=%s\n”,length_string1);
Return 0;
Output:
Strcpy=hello
Strcat=helloworld
Strlen=5
Strlen is the length of the hello world,previously the string value was hello after contatenation the string
1 value is helloworld,so when you copied string 1 to string 3 it was hello from string 3
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int main()
Char string3[13 ];
printf(“strcpy=%s\n”,string 3);
printf(“strcat=%s\n”,string 1);
printf(“strlen=%s\n”,length_string1);
printf(“strcmp=%d\n”,value);
return 0;
Return 0;
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
/*struct myStruct{
int myInt;///4
char myChar;///1
} record; */
union myUnion{
int myInt;///4
char myChar;///1
};
Int main()
Return 0;
Like structures unions are also declared by the name union you can give the name union myUnion;and
then are the curly braces and then are the variable like this,whenever you declare a variable depending
upon the data type,so int has the size of 4 byte and char has the size of 1 byte,we declare the type also
UNI
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
/*struct myStruct{
int myInt;///4
char myChar;///1
} record; */
union myUnion{
int myInt;///4
char myChar;///1
}UNI ;
Int main()
UNI uni;
Return 0;
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
/*struct myStruct{
int myInt;///4
char myChar;///1
} record; */
union myUnion{
int myInt;///4
char myChar;///1
}UNI ;
Int main()
UNI uni;
uni.
Return 0;
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
/*struct myStruct{
int myInt;///4
char myChar;///1
} record; */
union myUnion{
int myInt;///4
char myChar;///1
}UNI ;
Int main()
UNI uni;
uni.myInt=4
Return 0;
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
/*struct myStruct{
int myInt;///4
char myChar;///1
} record; */
union myUnion{
int myInt;///4
char myChar;///1
}UNI ;
Int main()
{
UNI uni;
uni.myInt=3
uni.myChar=13;
Return 0;
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
/*struct myStruct{
int myInt;///4
char myChar;///1
} record; */
union myUnion{
int myInt;///4
char myChar;///1
};
Int main()
{
union myUnion uni;
uni.myInt=3
uni.myChar=13;
Return 0;
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
/*struct myStruct{
int myInt;///4
char myChar;///1
} record; */
union myUnion{
int myInt;///4
char myChar;///1
};
Int main()
uni.myInt=3
uni.myChar=13;
print(“%i\n”,uni.myChar);
Return 0;
Output:
13
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
struct myStruct{
int myInt;///4
char myChar;///1
} record;
union myUnion{
int myInt;///4
char myChar;///1
};
Int main()
uni.myInt=3
uni.myChar=13;
print(“%i\n”,uni.myInt);
Return 0;
Output:
13
What is happening is that It allocates the membervariable to some memory space,suppose you have
defined myInt and myChar in the myStruct structure then it allocates 4 byte of memory space in the
computer for this myInt and 1 byte of memory space for the myChar for the memory space,we see in
the memory map which contains ,4 space is for int and 1 space is for char,whenever we declare union,it
has the size of the variable whose size is maximum,so the maximum size of the variable inside this,is
since 4 is greater than 1,the size of this variable will be 4,so union allocates size to itself from the
variable whose size is maximum,so the union we have size 4,so forstructures the memory map is like this
4 spaces forint and 1 space for char
But in union the memory map is like this it has 4 space 4 bytes space is for int and it allocates the 1 byte
for char within the int itself this 1 byte is common for char and int,so members inside union share
memory space,but in structures they are allocated at different space,so when you assign 4 it will be
assigned to these 4 bytes of int ,but when you assign 13 it will be overwritten at these 4 bytes in
union,that is why we are getting the answer 13 for int and char,this is the difference between structures
and unions,structures allocates differentmemory location to its variables but union allocates same
memory location and the size of the union is the maximum size inside this,so myInt has the maximum
size so the size of the union will be 4,Whenever you use union the member
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
struct myStruct{
int myInt;///4
char myChar;///1
} record;
union myUnion{
int myInt;///4
char myChar;///1
};
Int main()
uni.myInt=3
uni.myChar=13;
Return 0;
Output:
We see the size od struct should be 4 but the answer is diferent,whenever you provide some variable to
the structure,it calculates the size by adding the size of the function and also the,in the multiples of the
int but that value is not the multiple,so c provides some padding to the,size of the union is the function
which has the maximum size that is 4,so where we can use union in embedded programming where you
are accessing the memory location so the structure allocates different memory location to the function
Day 27:Structures in C
This chapter we will see what are structures in c and how we can use structures in c,structures are
collection of different variable which are group together to make some logical,for example we want to
make the structure of the student there is no direct way we can give to assign the attribute name to the
student,we cant know that this attribute is for that particular student,there are students,we have to
declare many variables functions when we have many functions,we have to see which variable is
assigned to which student,so structures make it easy to define if we have many fuctions variables,to
which we need to bind in a logical,to declare structures we have to define the struct keyword
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
struct student{
};
int main()
return 0;
struct student{
};
you can define the variables,so what attributes the student can have this is the id,this id can be integer
so this int id;
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
struct student{
int id;
char name[ 20 ];
float percentage;
};
int main()
return 0;
The attributes of the student structure are int id,name , marks,we can define different instance of the
structure,we can use structures to make a logical of functions variable for the student,now when you
define this kind to structure
You can use it in the main function,whenever you define the structure you need to call this inside the
main function how you can call it inside the main function,you can call this inside the main function,you
can call this struct and then give the name to this student structure
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
struct student{
int id;
char name[ 20 ];
float percentage;
};
int main()
return 0;
This struct student record 1; indicates record of student 1,this record 1 is the variable that you define for
student structure,so student is the data type and struct is the keyword which tells it is the student data
type which is the structure and record 1 is the variable name the student is the data type,whenever you
need to assign some value to the record which is the instance of the student structure,instance is that 1
part of this student,then we have to take the record,and to assign id to this record you need to put the
dot separator,record1. ,we will be able to see the member variables of the student structure when
we see after the dot separator,the attributes of the student,tey are called the members of the
structure,record1.id;
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
struct student{
int id;
char name[ 20 ];
float percentage;
};
int main()
record1.id=1;
record1.name
return 0;
In previous chapter we have seen how to copy the string to the character array,you can assign or copy
the name of the string,the first argument of the strcpy is the destination where you want to assign,The
another argument is the function,we have assigned mark to this variable record1.name,and then we can
assign the percentage to mark,To the instance of this structure that is record1 you can access this
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
struct student{
int id;
char name[ 20 ];
float percentage;
};
int main()
record1.id=1;
strcpy(record1.name , mark);
record1.percentage=70.00;
return 0;
}
So in this printf we have this the % d in id for integer ,%s in name for char and %f in percent for float,
printf(“id=%d Name=%s percent=%f”),then we can access the values we have assigned
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
struct student{
int id;
char name[ 20 ];
float percentage;
};
int main()
record1.id=1;
strcpy(record1.name , mark);
record1.percentage=70.00;
return 0;
Output:
id=1
name=mark
percentage=70.00
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
struct student{
int id;
char name[ 20 ];
float percentage;
};
int main()
record1.id=1;
strcpy(record1.name , mark);
record1.percentage=70.00;
strcpy(record.name ,micheal);
record1.percentage=89.90;
return 0;
Output:
so you can define another instances of the student as we want and we can do record of the
student,there we can also use structures like this,by directly giving name to the student structure,we
can,we can remove this line struct student record 1;instead of giving the name,we can give the name at
the declaration of the student structure
Program
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
struct student{
int id;
char name[ 20 ];
float percentage;
} record;
int main()
record1.id=1;
strcpy(record1.name , mark);
record1.percentage=70.00;
record.id= ;
strcpy(record.name ,micheal);
record1.percentage=89.90;
return 0;
This record will be the variable name,record will be the variable name of the student structure,we can
use record to access the values or assign the values to thisn record,for example,so you don’t need to
declare record inside your main it can be that function,so we have the names and id and the names
and the marks we can declare the record in the main and this is like is and the that function