You are on page 1of 131

C Programming

Programming Knowledge

C is a very powerful functional programming language which is very widely used and has influenced
many other languages,most of the programming languages
Today are direct descendends of c or are heavily influenced by c

#include <stdio.h>

#include<stdlib.h>

Int main()

Int age= 22;

Float pi=3.14;

Char my_char=’a’;

Double pi=3.14

Printf(the age is %d age)

Return

In this cource I will tell you the basics of c and then we will go into the details of data types,strings and
variables and then I will tell you how these concepts fits into loops statements,control statements and if
statements
After that we will see how c organizes information in arrays and how it manages memory using pointers

Throught the course I will emphasize best programming practices


Learning c is a best 1 st step if you are beginners in programming and a very best way to broaden your
knowledge if you are being programming for a while
So lets get started with c programming tutorial for absolute beginners
Day 1:Installing Code Blocks and Getting Started

Now there are basic things you need to program 1 is editor,editor helps you to write code and other is
called compiler is a tool which converts you program into the machine language,because your computer
does not understand the c program you write,because the c program is just the interface which you
write and it is ok to understand if are programming in c,but your computer will understand binary
language 0,1 ,so your compiler will convert your program which you write in c into the machine
language,so that it can execute thje program,so these are the things that you need to get,now to obtain
these things,these are available free,there are different IDE’s(Integrated Development Environment)that
helps you to write the code,I will tell you how to install the IDE’s

Go to google and type”code blocks”

The first link which you will find is www.codeblocks.org

This the website which hosts,program called code,click on this


It is open source,cross platform,c++,fortrain ide,why I am recommending you this code blocks is that it is
cross platform that is it works both in linux os ,mac os windows os and it is open source that is you can
get it for free,to download it you can click the downloads button here in the main menu(downloads)you
will get this screen

There are the options to download


1)download the binary release

2)download the source code

3)retrieve the source code from SVN(sub version)

Click the download the binary release

There will be various links for windows,mac and linux,because I will be using windows for this so I will
click on this “windows/XP/Vista/8”
Then you will see the 3 choices of code blocks

1st is the code blocks,which is only code blocks which will help you to write the code,

Codeblocks-12.13-setup.exe

2 nd link will provide you the compiler also

Codeblocks-12.13-mngw-setup.exe

Code blocks 13 is the version used when I am using this windows,and mingw is the complier,that will
help you to compile the program,including your compiler and editor both and we want

Now click on the download link of code blocks “Berli os”

Code blocks will downloads

Click the code blocks after download

Code::Blocks Installation

“welcome to code blocks set up wizard”

The wizard will guide you to installation of code blocks click next to continue(next)
Code::Blocks Installation(Licence Agreement)click I agree Code::Blocks
Installation(Choose Components)-choose which features of code blocks do you want to install

Select the type of install-Full:all plug ins,all tools

Default install

Contribute plug ins

MnGW Compiler Suite

Click next Code::Blocks Installation(chose install location)

Destination Folder:C\Program Files(*86)\CodeBlocks

(this is the location where coide blocks will be installed)keep this default and click next

Code::Blocks Installation(Installing-please wait while code blocks is being installed)

You can use this code block IDE for other languages also like c,c++,fortrain,java

You can install code blocks for linux,mac and windows operation system it works for all the 3
operating systems

(now the installation is complete)Code::Blocks Installation-do you want to run code blocks now-click yes

Compilers auto detection

Compiler status

GCC Compiler Detected

Current default compiler-GCC Compiler-click ok

Code::Block Installation(Installation is complete)set up was completed successfully(click next)

Code::Blocks Installation(Completing the code blocks set up wizard)code blocks has been
installed on your computer(click ok)

Click next

File associations

Code Blocks is not the current default application for the C/C++ source files

Do you want to set up as default?

You can always change the associations from the environment settings later
-no leave everything as it is

-no leave everything as it is(but ask me again next time)

-yes associate Code::Blocks with C/C++ file types

(Select this choice)

--yes associate Code::Blocks with every supported type(including project files from other IDE’s)

But right now I don’t want to select this so leave it as it and close this

Day 2:First C program and understanding C Program Structure

In the previous tutorial we have seen how we can download the c++ code blocks whick is the c++ IDE

We will write,compile,run and see the c code in code blocks

So now we will write first c program and understant c program structure


So open code blocks

So to create a new file go to

File New Project

New From Template

AVR applications,Code::Blocks plugins,Console applications

Fltk Project,Fortran application,GLUT Project

Kernel mode driver,irrlich project

Select console application and click go(Console application)-the wizard will guide to create a
new console application click next(Console application)please select the language you want to
use-please make a selection-C/C++,we will select C since we are going to write a c program and click
next click next(Console application)

Please select the folder where you want your new project to be created

Project tile:My Prog

Folder to create project in:

C:\users\Programming Knowledge\Downloads\test(you click for browsing …)

Project Filename:MyProg.cyp

Resulting Filename: C:\users\Programming Knowledge\Downloads

Click for browsing(browse for folder)please select the folder you

want to create your project in(the directory in whick you want to

create your project

click next Console applications(please select the compiler to use

which configurations to be enabled on your computer)

Compiler:GCC Compiler

Create “Debug” Configuration-Debug

“Debug” choices

Output dir.:bin\Debug\

Objects Output dir.:obj\Debug\

Create “Release” Configuration-Release


“Release” Choices

Output dir.:bin\release

Object Output dir.:obj\Release

We will see in project explorer tab –MyProj-Sources-Main.c(whick is the hello world file)

So the the statements which start with hash sign # are called preprocessor commands

# include<stdio.h>,there are other preprocessor commands also but these are the most important,the
include includes the header file into the project,so whenever you create a c program there are always
some files that you want to include in your project in order to write the program,and this preprocessor
commands #include,this includes the file name<stdio.h> and these files <stdio.h>,<stdlib.h>are the most
important files we need to create out c project,the <stdio.h> is-standard input output,so this
command,#include<stdio.h> is responsible for taking the standard input and printing the output,so this
function printf,so function also we will see now,so we see that the function printf is actually due to this
function(#include<stdio.h>),now we see <stdlib.h> we also include this file stdlib.h because whenever
we use this function main because whenever you write a c program and try to run this program,your
compiler and whenever you try to run the program,the compiler searches for the main function in your
project,so this main is like an entry point to your program,so this main is a part of the standard lib
<stdlib.h>

So these <stdio.h>,<stdlib.h> are called preprocessor compilers,now this int main is a function this is a
basic function and it is a built in function which every program must has,and by this main fuction the the
c program knows that this is the entry point of the program,so every project must have a main
function,and what is a function?a function is a piece of code that you can execute,but for now you need
to know that the function is a piece of code,which you need to know to execute the piece of code,so the
main is the most important function,the function has 3 important types,the first one is return type that
is int is the integer,integer is a data type and the main is the function which you have to give in this this
letters”main()”and this bracket in the main takes in an argument ,but for now we are not passing any
argument,so every program must have a main function,because every program must has a main
function,because we are returning and integer,we must have a return type so we return 0,return 1,a
return type is just like a success for your program,but for now you need to know that if you reach return
0,return 1 and you reach to this point in the program,your program has run successfully,

Now the print f-this print f is a part of stdio.h,there is a file in stdio.h called print f,so this print f helps us
to print a string on the terminal ok so this print f,so whatever you write inside this print f in double
quotes inside the bracket so whatever you write,it will be printed in the terminal,so whatever you write
it will be printed in the terminal,so here you write hello world,it will be printed in the terminal,the
backslash n will change the line which I will tell you,this will not be printed,but this is a special string or
character ,int that will help you to,this line ends with a semicolon this semicolon tells that this line of
code and from the next line we are going to write another line of code so now,so every function has
started with these opening and closing curly braces,so now that you have understood that every line is
starting and ending with a semicolon,and you can write the indentation is not important,you can write
print f on the screen at any position,you can give spaces between each lines of code but here in the
program this way is ok for the program as shown above,now in order to run a program you need to
compile a program,in the previous tutorial I have told you what is compilation,compilation helps your
program to convert this program into a machine language ,the machine language your machine can
understand,this machine language is the code that your machine can understand,this c code is
understandable by human but is not understandable by machine so the compiler helps you to convert
this human readable code into a machine readable code,and how you can compile a code,there are 3
buttons on the code blocks,build,run and build and run,build is to compile your program whenever you
have to compile your program you have to click build,click compile,there is also a tab above to build you
can click to tab to get the 3 choices,but before compiling your program you have to save the code
first,click on save on the save tab above in then click to compile,you will get the output
The output of your compilation you can can see here in the build log as seen in the console screen will
give program is correct or not,once your program is there without any error or warnings you can click
the run button to run your program

So the output is shown by the output window so this is the thing which is shown by the terminal window
so other message is there which is just given by the terminal window,it tells press any key to continue
when you press the key the terminal window closes ,so your program is now running
successfully,whenever you do some mistake in writing the program your compiler will give an error so
here by mistake,if I leave some unknown letters after print f semicolon like mnm

Printf(“hello world!\n”);mnm

It will give us compilation error

Error:error :expected” ;” before”return”

Now if we give the semicolon after mnm it will then also give error

Printf(“hello world!\n”);mnm;

error:”mnm” undeclared{first use it in this function}

it will give the mark as to where the error is coming from

so you compile your program to check whether what you have written is correct and to convert it into
the machine language and then you run the program,so this is how the C structure is and this is how it
works

Day 3:Input and Output(printf,scanf and placeholder)

In the last video you have seen how to write your first program and also explained all the lines of
code,here we use printf to print any value to the terminal so whenever we run this code we see this
hello world is printed and you can print hello world 3 times, this \n just changes so if you see this hello
world printed without the line break so you remove the line break \n

Printf(“\”hello world!\n”)

So if you print a number like this

Printf(31)

It will give you error because it requires place holder like this

Printf(“%d”,31)

Comma,comma,semicolon and between the commas we put %d,we run this it gives output hello world
and number 31

This %d is called as place holder

%d is going to hold a place for the integer,if you want to hold d it will hold an integer,if you want to print
a decimal value there is a special symbol for this called float,f and then you can write

Printf(“%f”,31.3);

It will print 31.3

But if we write

Printf(“%d”,31.3)
And if we compile and run it it will not print the value because we are providing the integer place holder
and we are giving the decimal values,so you always need to remember what kind of value you need to
print,now with this place holder %d you can add texts also

Printf(“the number is=”%d”,31)

So whatever you write after % d and comma it will be replaced by that number in this is it 31

So now when you run the program you get the output:

Hello world

The number is=31

Now in order to print multiple decimal what you need to do is just print multiple decimal place ,now
suppose you need to give multiple decimal place holder you need to give separate value multiplier space
you need to put the %d place holder 3 times and after comma put the other values you need to put
there

Printf(“%d %d %d”,31,58,5)

We run the program and we see it will be printed 31 space 58 space 5

Because we have given spaces between the % d place holder as (“%d space %d space %d)

There are different kind of data that you can print,there is decimal value
You can print % 31

If you want to print % f you print the float value,if you want to print large float value or large decimal
value,in order to print a large integer value you use % ld,we use double slash // used as
comments,which is whatever you write after the c program will not be executed by your c program,we

Give the comments as integer,large integer,float,large float and char c,

A character value is always enclosed within single quotes’ ‘

Suppose you want to print string,a string is like a sentence thing,like hello world

We put %s and print the string inside the double quotes “ “

Suppose you want to print a hexadecimal value it will print the value we put for hexadecimal value %x
and 16

We will run the program but these will print in the same line because we have not provided the line
break here as we have provided in printf-hello world

So we will provide the line breaks between all the printf so that they will print in different lines
So we provide the values for the following place holders according to the place holder

Output:

hello world

32
121313313

32.2000000

32455445.2200000

“hello world”

16

Now in order to take the input suppose you want the user to give some input and then you want to print
this input,you want that the user should enter some value and then how can you do that the user has
entered some value,so we use scan f,scan f is a function we use to take the input from the user,suppose
the user gives some input in the form of a string then tehn it will give scan f and %s(for the string),so it
will take string value and print the string value,so we will see this later when we understand how
variable c works and how the c data types work

Scanf(“%s”,);

There is also other function which prints the value to the screen

Puts();

Puts(“this is my c program”);

You can print this by using puts also so when run the program ,

So it prints”this is my c program”

So in this way you can print input and ouput different data using c on the terminal

Day 4:Comments

Comments are used to provide description of your code and comments are not directly executed by
your c program,suppose you want to give descriptiin of your code,so the purpose of your line of code
you use comment,so in order to give comment to your c program you can use the ways 1 is multi line
comment,you can just write forward slash,asterix,and forward slash

/***/

And in between it will be called comment and it will not be executed or it will not be compiled by your
project,it is just described to tell in what you are doing in the program,you can give comments as

/*** this

Program Is

Printing
Hello

world

***/

Like this you can give multi line comments,the comments inside the asterix forward slash astexix will not
get executed it is just for your reference,or if any other person sees your comments he will be able to
understand what your program is doing and what you want to do in your program,now in order to
provide single line comment to this line

Printf(“hello world!\n”);

#include <stdio.h>

#include<stdlib.h>

/*this program is

Printing

Hello

World

*/

int main()

/*this is a printf statement*/

Printf(“hello world”)

Return 0;

Day 5:Variable and Data Types

We will see what are variables,how can we declare variables in c and how can we use them,so the first
question is that what are variables?variables are data holders,so you can save values in the variables,it is
like you want to save your age you can save it in some place and then you can extract using your age
using the varibles,so now the variables have two types 1 is the data type and the other then the name of
the variables that is the age,and then you give a semicolon after the age so this is the declaration of the
variable so what this declaration is that we want to declare the variable called age,age is the name of
the variable and age is the data type and int in data type,which is the type of data this variable is going
to store this variable is going to store integer so this is the you assign the age value 22,and then you give
print f(“the age is %d”,age);

Here age is the variable

Program:

#include<stdio.h>

#include<stdlib.h>

Int main()

Int age

Age-22;

Printf(“the age is %d”,age);

Return 0;

So now when we compile and run the program it will give

The age is 22

Now the age is variable,and there are some rules for declaring the variables suppose you want to
declare some variables

Program:

#include<stdio.h>

#include<stdlib.h>

Int main()

Int age;

/*
3yoo (must not begin with a digit)

My yoo (spaces not allowed in names)

$ yoo ($ not allowed in ---only letters,digits and _)

While (language keywords cannot be used as names)

*/

Printf(“the age is %d”,age);

Return 0;

The rules are:

1)the variable cannot start with a number like

Int 22age;

It can end with a number age 22;

It can have numbers in between a22ge;

2)the name of the variable cannot have any space in between like

Int any age;

3)suppose you want to use $ sign to initialize a variable it is not allowed

Int $anyage;

Or you want to use asterix in between it is also not allowed

Int$any*age;

So avoid using special symbols in variables but you can use _

Int any_age;

Int my_age;

5)avoid using special function names,main is a variable already use in c,suppose you want to declare
main as variable name it is ok

Int main;
But c will see that it is a function name and you have declared it as a variable,it will give some conflicts

When you compile it will give warnings:main is usually used as a function

So avoid using function names as a special variable name;

So these are some rules for the variable names;

You can initialize the variable as int age=22;

And print it is ok the

There are different types of variables so you can declare integer,int,suppose you want to declare the
decimal type variables you can do it with the data type float as floating type varaibles or decimal
variables,suppose you want to store character you can do it by using char,and store the char withing
single quotes,and hello world in double quotes,you can also give double value as big pi; so these are the
most important data types in c you can use to declare the variables in c.

Program:

#include<stdio.h>

#include<stdlib.h>

Int main()

Int age

age-22;

float pi=3.14;

char my_char=’a’

double big_pi=3.1422332222222232322;

Printf(“the age is %d”,age);

Return 0;

Day 6:Math and and Operators


There are different operators in c to do the operations

Program:

#include<stdio.h>

#include<stdlib.h>

Int main()

/***Multiplicative and Additive operators

+ (addition)

- (subtraction)

* (multiplication)

/ (division)

% (modulus)

***/

Return ;

Suppose there are number a=23 and b=45 you want to add these numbers you can ddefine another
variable sum,and then you can add it as sum=a+b; and the result you can print it in printf sum,%d and
sum,this the variable sum you defined in int=sum;

Program:

Addition of numbers
#include<stdio.h>

#include<stdlib.h>

Int main()

/***Multiplicative and Additive operators

+ (addition)

- (subtraction)

* (multiplication)

/ (division)

% (modulus)

***/

Int a=23,b=45;

Int sum;

Sum=a+b;

Printf(“sum=%d”,sum);

Return 0 ;

Output:

Sum=68

Program:

Subtraction of numbers

#include<stdio.h>

#include<stdlib.h>
Int main()

/***Multiplicative and Additive operators

+ (addition)

- (subtraction)

* (multiplication)

/ (division)

% (modulus)

***/

Int a=23,b=45;

Int subtraction;

Subtraction=-a-b;

Printf(“subtraction=%d”,subtraction);

Return 1 ;

Output:

Subtraction:22

Program:

Multipication of numbers

#include<stdio.h>

#include<stdlib.h>

Int main()
/***Multiplicative and Additive operators

+ (addition)

- (subtraction)

* (multiplication)

/ (division)

% (modulus)

***/

Int a=23,b=45;

Int multiplication;

mutiplication=a+b;

Printf(“product=%d”,multiplication);

Return ;

Output:

Multiplication:1038

Program:

Division of numbers

#include<stdio.h>

#include<stdlib.h>

Int main()
/***Multiplicative and Additive operators

+ (addition)

- (subtraction)

* (multiplication)

/ (division)

% (modulus)

***/

Int a=39,b=3;

Int division;

Division=a/b;

Printf(“division=%d”,division);

Return ;

output

13

Here if we giving the values of a and b such that the division a/b will be a decimal number so we have to
specify the variables of the data type float,we get output as 0 because we have not changed the print
f(to the % f)so the %d int values gives whole numbers so it does not return the value for this

Program:

Division of numbers

#include<stdio.h>

#include<stdlib.h>
Int main()

/***Multiplicative and Additive operators

+ (addition)

- (subtraction)

* (multiplication)

/ (division)

% (modulus)

***/

Int a=9,b=2;

float division;

Division=a/b;

Printf(“division=%d”,division);

Return ;

output

Program:

Again after we chanced printf to % f it is giving value of 13 and not giving the decimal value because we
have specified here the division=a/b;

Which will give the whole number division result and will leave the decimal values remaining

Division of numbers

#include<stdio.h>

#include<stdlib.h>
Int main()

/***Multiplicative and Additive operators

+ (addition)

- (subtraction)

* (multiplication)

/ (division)

% (modulus)

***/

Int a=41,b=3;

float division;

Division=a/b;

Printf(“division=%d”,division);

Return ;

Output:

13.00000000000000

Division of numbers

#include<stdio.h>

#include<stdlib.h>

Int main()

/***Multiplicative and Additive operators


+ (addition)

- (subtraction)

* (multiplication)

/ (division)

% (modulus)

***/

Int a=41,b=3;

float division;

Modulus=a%b;

Printf(“modulus=%d”,modulus);

Return ;

Output:

Modulus=3

It will give you the remainder

We will see other operators also but here we will see the precedence of the operators by the rule

Day 7:If Statements in C

in this we will see what are if conditions and what are if statements,it will check whether the statement
is true or false and based on the contition it will execute or not execute some code,so now how we will
declare the if condition we want to check the age and if the age is 18 we will print the
age is 18 if the age is greater than 18 and also we want to print that age is greater than 18,and if the age
is less than 18 we want to print that age is less than 18,now to write the if statement we need to use the
if condition that is if keyword and then the brackets

if (/*condition goes here*/){

/*if the condition is non- zero(true),this code will execute*/

In contions in the bracket,The condition here can be true or false,so we see the conditions here in the
bracket

And then in the curly braces we evaluate and we execute the code on the basis of the validity of the
condition in the brackets(),if the condition is true,the condition inside the curly b races will be
executed,otherwise this code will not be executed,now lets see how we can evaluate the age of the
person

Now for taking the age from the user at runtime we use scan f

Program:

#include<stdio.h>

#include<stdlib.h>

Int main()

Int age;

Printf(“please enter the age”)

Scanf()

if (/*condition goes here*/){

/*if the condition is non- zero(true),this code will execute*/

}
So I will tell the user by printf to please enter the age and then I will use scanf to take the input from the
user and because this age is an integer we will use %d to take the integer and then the age,but the
correct way of using the scanf is by using this and this symbol before the variable name and what this &
symbol indicates is the address of the value ,age in scanf,so somewhere in the computer memory your
variable will be saved,and whenever we write this & symbol before this variable name ,age in scanf it is
that because you are sxcaninbg the address of this variable ,age in scan f

Scanf(“%d”,& age);

The user will enter some value and it will be stored at this address ,age in scanf and we can access the
age value after that,and when we will execute this program you will understand better,so it will take the
input and depending upon the age that the user has entered,if the age is>18 we will print that age
is>18,depending upon the input we are checking and in here we are checking some code

Printf(“please enter the age”);

Printf(“the age is greater than 18”);

If age is equal to 18,we can use some more if conditions

Now to compare if conditions we have to use double == sign

If (age==18)

If we use

If(age=18) it will assign 18 to age,but we want to compare these values(age and 18)

So we use double == sign

If age is ==18 we will print age is equal to 18

If (age<18)rhen we will print that the age is less than 18

So if age >18,this code will be executed(printf age is greater than 18)

If age==18,this code will be executed printf age is eqwual to 18)\

If age<18,this code will be executed

So this is condition bases code execution,i will compilke the program and I run the program

Now suppose I enter age 22 in the terminal window now since these conditions are not done(age==18
and age<18) so these code or the printf messages will not be executed

So we get the message age is greater than 18,so this condition is true(age>18)

But 22 not equal to 18,so printf will not be executed

And 22 is not less than 18 so this printf will not be executed


If we enter 18 the condition age==18 will be printed,and if enter 16 the age<18 will be printed

Program:

#include<stdio.h>

#include<stdlib.h>

Int main()

Int age;

Printf(“please enter the age”)

Scanf(“%d”,& age);

If (age>18/*condition goes here*/){

/*if the condition is non- zero(true),this code will execute*/

Printf(“the age is greater than 18”);

If (age==18/*condition goes here*/){

/*if the condition is non- zero(true),this code will execute*/

Printf(“the age is equal to 18”);

If (age<18/*condition goes here*/){

/*if the condition is non- zero(true),this code will execute*/

Printf(“the age is less than 18”);

}
Day 8:if else and Nested if else Statements in C

In this tutorial we will see how we can check the if else and nested if else statements,we will take the
same example in the previous and check for the example,we want to check if the age is >18 we will print
age>18 otherwise we will print the age not greater than 18,or we can print the age is less than 18,how
we can do this,we can write some condition from the previous after the forst if statement and then
again if,but there is a condition called else which touches all the conditions which is not true,so if the
condition age>18 is not true it goes to the else condition,the else condition-it suggests every other
condition which is not true it goes to the else condition

} else{

Curly bracket,alse,curly bracket

It does not check other condition because any other condition except age>18,goes here in the else
condition,now you want to just write that the age is just not greater than 18

Program:

#include<stdio.h>

#include<stdlib.h>

Int main()

Int age;

Printf(“please enter the age”)

Scanf(“%d”,& age);

If (age>18/*condition goes here*/){

/*if the condition is non- zero(true),this code will execute*/

Printf(“the age is greater than 18”);

}else{

Printf(“the age is not greater than 18”);

}
}

We see the output enter the age :22

First condition is executed and if we enter:

Enter the age 18

The else condition print f will be executed

But there are situaltions where you have to check multiple situations,so we can check multiple
conditions using if else statement,if age >18 you want to print age greater than 18,or age equal to
18,but if the age not greater than or equal to 18 you will print the else statement,so you can check the
other condition using else if statements,so prive the curly braces

}else if()

Since we are taking if condition here if(),so we need to check other condition,we can print the age is
equal to 18,otherwiswe we will print the age not greater than 18,or the age is not greater than or equal
to 18,so this iselse if,we can check multiple statements using else if ,so we go to if then else if then
else,so what will be the program flow over here the,the compter will firstr ask the age from the user
&age,if age entered is >18 it will get executed,if the condition is not true it will go to else if,if all the
condition is not true it will go to else and print age not greater than 18 and age not equal to 18

Program:

#include<stdio.h>

#include<stdlib.h>

Int main()

Int age;

Printf(“please enter the age”)

Scanf(“%d”,& age);

If (age>18/*condition goes here*/){

/*if the condition is non- zero(true),this code will execute*/


Printf(“the age is greater than 18”);

}else if (age==18)

Printf(“the age is equal to 18”);

} else

Printf(“the age is not greater than or equal to 18”);

Now suppose we want to check that age>18 and also the age is <22 or not than we can do this,is that
we can use nested if statements after the the first statements

Program:

#include<stdio.h>

#include<stdlib.h>

Int main()

Int age;

Printf(“please enter the age”)

Scanf(“%d”,& age);

If (age>18/*condition goes here*/){

/*if the condition is non- zero(true),this code will execute*/

Printf(“the age is greater than 18”);

If (age<22)

Printf(“the age is greater than 18 but less than 22”)

}
}else{

Printf(“the age is greater than 18 but is not less than 22”);

}else if (age==18)

Printf(“the age is equal to 18”);

} else

Printf(“the age is not greater than or equal to 18”);

Suppose you want to check that the age is greater than 18 but less than 22,so how can you check it,so
we want to print if the age is greater than 18 we want to check it and we also want to check it that,so
you can use nested if conditions here inside your if condition,you can use if once again you can check if
age is less than 22 you can print that the age is greater than 22,the age is greater than 18 but less than
22,in the other case you can provide an else statement and give that the age is greater than 18 but not
less than 22,inside the if condition we have nested if condition,it is not necessary you can use if
condition inside,you can write multiple if statements to check multiple conditions in c ,when we give age
27 the age is greater than 18but not less than 22,it goes to the if statement,then goes to the other if
statement and then sees the condition is true for the else statement which is after the other if
statement,if you provide age 19,so it will execute the other if statement age is greater than 18 but less
than 22,so in this way you can use if statements,and nested if statement in c,so the if,if,else statements
is called nested if statements inside if else statements,other is called else if statements and the last else
statement is called else statement inside ifs statements.

Day 9:the ternary(conditional) operator in C

In the last chapters we did the what the If else conditions in c are,now in this chapter we will see what
the ternary operator the ternary operator in conditional operator in c,this is used to make decisions in c
,but if you want to do in a single line,so how to use the ternary operator,suppose we have a integer
variables,suppose we have integer a=19 and integer b=22,suppose we want to compare two variables,I
can do it with the help of if condition but if you write if and then else,it can be a longer expression in
terms of code for comparing these integers a and c
Program:

<include stdio.h>

<include stdlib.h>

Int main()

///The ternary (conditional) operator in C

Int a=19,b=22

if

else

a normal expressions for comparing these two integers would be I can give an integer c and then
transfer the greater of these values in a and b to c,we will see the variables a=19,and b=22 whatever
variables are greater,whatever values are greater we want to transfer it to c,so there is an operator
called ternary operator

Program

<include stdio.h>

<include stdlib.h>

Int main()

///The ternary (conditional) operator in C

Int a=19,b=22

Int c;

(/*Logical expression goes here*/) ? (/*if non zero (true)*/) : (/*if 0 (false)*/)
(/*Logical expression goes here*/) -We have a bracket here which we write here as we write in the if
condition

?-then we have a question mark over here the,if the condition is true you can pass the value to your
result c from here otherwise if 0 is false then the value that is contained inside this colon,will be
transferred to your value,will be returned, for example I want c to be greater than those here to be so I
will compare these if c is greater than these values a and b we will compare if a is greater than b which is
not greater than b here the(so the condition is

C=(a>b) ?

If a>b we will return b or else I will return a and for example

The bracket will compare the condition

C = (a > b) ? b : a;

So if a is greater than b then I will return a that is, the ternary operator is that if the condition specied
in the bracket after equal to sign after c,that is the condition we are using here is(a>b)? it will check if a
is greater than b if the ,it will ask c if (a>b)? ,if the condition is true the first result before the semicolon
will be stored in c,if the condition is false the other result after the semi colon will be stored in c

C=(a > b)? a : b

We can specify the condition statement instead a : b ,the condition as b : a,that is

C=(a > b)? b : a

So here if condition (a>b)? is true b will be stored in c,if condition if false then a will be stored in c

Program:

<include stdio.h>

<include stdlib.h>

Int main()

///The ternary (conditional) operator in C

Int a=19,b=22;

Int c;

(/*Logical expression goes here*/) ? (/*if non zero (true)*/) : (/*if 0 (false)*/)
C = (a > b) ? b : a;

Printf(“ans= % d”, c);

Output:

19

According to the condition here the c compares a and b,according to the condition here a=19 and
b=22,so according to the condition a is not greater than b,the condition is false,for the condition to be
true we store the first value before semicolon in c that is b,and if condition is false we store the other
value which is after the semicolon that is a,in this case the condition is false so we store the the value
a,the value of a is 19,so we store the value 19 in c,and we get the output as 19 in the terminal window.

Program:

<include stdio.h>

<include stdlib.h>

Int main()

///The ternary (conditional) operator in C

Int a=27,b=22;

Int c;

(/*Logical expression goes here*/) ? (/*if non zero (true)*/) : (/*if 0 (false)*/)

C = (a > b) ? b : a;

Printf(“ans= % d”, c);

Output:

22
Now we change the value of a from 19 to 27,now when we compile and run the program we see
that,the c compares the values of a and b,the condition is(a>b)?,if a is greater than b than the condition
is true and it will print the first value which is before the semi colon,that is b,and if the condition is false
it will print the value which is after the semi colon, that is a,in this case the condition is true,so it will
print the first value that is before the semi colon that is b,so the value of b is 22,so 22 will be stored in
c,so the output we get as 22 in the terminal window

So the ternary operator is used if you want to write in the statement in a single line rather than
multiline statements of nested if else statement

Day 10:Switch Statement in C

in the last chpter we have seen if else condition and multiple if else condition,and switch statements are
also condition based,but they are different in the sense that you can use conditions with switch
statements,but conditions must be ok, so let me give an example how the switch statements are
declared in the program,the basic syntax of switch statements is like this

the syntax of if statements is:

# include <stdio.h>

#include <stdlib.h>

Int main()

switch(expression){

case constant-expression :

statement (s);

break;

case constant-expression :

statement (s);

break;
default:

statement (s);

we will see the structure later on the but now we see how the switch statements starts

Program:

# include <stdio.h>

#include <stdlib.h>

Int main()

switch(expression){

so switch statement starts with keyword switch and then comes the bracket and then comes the
expression,expression is something you want to validate on the basis of the case,and now inside the
curly braces comes some cases and then constant expressions as we see above,and then comes the
colon-case constant-expression : and then inside the statements comes the code which you want to
execute,after the code is executed you break the switch statement with this break keyword,this is an
choice statement,you can keep it or nor put it,depending upon the condition and you can have multiple
cases inside the switch statement,and whatever condition that is not covered by your cases is covered
by your default,this default is your default case

example we want to calculate the grade of a student and then we want to give a message,we give
integer in as marks,the student scores marks,on the basis of marks,you want to assign whether the
student is excellent,ok,fair
so we declare the switch statement and then comes the expression,this expression will be
marks,because on the basis of this marks,we are going to evaluate some cases and on the basis of the
cases we will execute the code,but first lets assisgn some marks 90,so marks is our expression that we
are going to evaluate and then comes the curly brackets and inside the curly braces comes the cases,and
inside the cases comes the marks which we are going to evaluate,so marks can be 60,70,80,90,98,so the
marks 90 is excellent,and then we will print f excellent and then we are going to evaluate,and then we
will put break,similarly it assigns grade excellent,ok satisfactory,for the range of marks,when we run the
program it gives output grade:Excellent,because it assigns the value 90,and then we are checking the
value of 90 using this switch statement-switch (marks),and because marks is 90 it goes to this case-case
90:case where marks =90,and then it prints the message “Excellent” and what the break does is when
the message is fullfilled,we want to break outside of this switch statement and we do not want to
execute any other statement which is not fullfilling our case,so it prints excellent and breaks out of the
switch statement,and when the c program sees your break statement it jump out of your case,right at
the end of your switch case,so now if we put marks int marks=75,it searches for the case 75 and prints
the message fair and jumps out of the switch case,the case 90 and the code inside it (printf) message is
not executed because the message,because the value of marks is not 50 or 60 or 90 so we have
executed this code,on the basis of cases,you can define other cases and whatever cases is fullfilled at
the time,so we have executed this code and so c has executed this code,so you can define multiple cases
and whatever cases you have defined,that code will be executed or the code inside the case will be
executed.otherwise the code will not be executed,if you have to cover other cae like marks=98

then you can write case 98 near the case 90,with a semicolon,and without any printf statement or the
break statement because it will use the statemets of the case 90 as given before,that is now the switch
will now check for case 90 and case 98 and whatever case is fullfilled it prints out the common
statements for these multiple conditions cases,since there is no break statements inside the case 98,so
it will jump on to the next case that is case 90 and check the condition and will print the statement of
the condition is fullfilled,if we put break keyword after case 98: break; if the condition is not fullfilled it
will not go to other case after that it will jump out of the switch case statements to the end,now
suppose we add more cases like case 86,case 96,case 97,case 98,case 99 all the 5 cases will be covered
by this one print f statement”Excellent”,so we want to assign excellent to all the 5 cases suppose you
want to include other grade for the other cases after the break that is for case 75,we want to add case
70 we will print fair grade to this,so you can cover different cases and execute one statement,without
using the break keyword,so this is how we use switch keword in c

Program:

#include <stdio.h>

#include<stdlib.h>

Int main()
Int marks=98;

Switch (marks)

{
case 99:

case 98:

case 97:

case 90:

case 86:

printf(“Excellent”);

break;

case 75:

case 70:

printf(“satisfactory”);

break;

case 60:

printf(“satisfactory ok”);

case 50:

printf(“ok”)

break;

default:

printf(“grade unavailable”)

output:

Excellent

Day 11:While loop in C


We will see what the while loops in c are,so loop is a piece of code which will execute again and again
and again until the condition is true,the values of 0 to 11 and we don’t want to use the values of printf
again and again and again,that is printf 1,printf 2,printf 3 we just want to use 1 line of code and we want
to print from 0 to 11 using the 1 line of code that is the,loop,so we can do this by using the loop called
while loop,a loop is something which executes the statement again and again until the statement is
true,so how to use while loop,we will use the same example to print the 11 numbers,

Int i=0,which is the variable and then I want to print the numbers 0 to 9 or 0 to 11 so we can use while
loop keyword,int i=0;then comes your condition,that is () in while statement,and then comes the curly
braces,and inside the curly braces comes the condition,so

Int=0;

While()

with the while loop we want to print the integer 11 times

int=0;

while()

Printf(“Value of i=% d”,i)

So we have print f then comes the condition() in the while

int=0;

while(i<11)

Printf(“Value of i=% d”,i);

So now while i<11 we want to execute this code,if it i>11 we want to go out of the loop,so this bracket
of i(i<11) in the while is evaluating our statement,we will put semi colon to printf statement and then
we will increment the statement,now this expression i=i+1,you can write the expression i=i+1 also as
i++,

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++;

What it is doing is it is checking if the condition is true,but this condition will not be true
because we have given i=0, and we are checking while(i<10),because this while condition is not true so it
will not execute the code,so now it will go and check whether 0 is less than 11 in the while(),so it will say
that 0 is less than 11 so it will print 0 and increment the value of i by 1,0+1 is 1 and then it will go on
checking on the condition is 1 less than 10 then it will print the condition,we will give the line break \n
after %d in the print f statement to print the values of I on different lines.

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

Here what is that we have declared i=0 so it goes to the while loop and checks the condition that
whether i<10 and it will print value of i=0 and then we are increasing the value of I by 1 i++ or i=i+1 now
the value of I is 1 it again goes to the while and checks the condition whether i<10,whether 1<10,yes
1<10 so it will print the value of I as 1 and then it will again increment the value of I by 1 i++ i=1+1 is 2
now value of i=2 so it goes and checks the condition in the while loop whether i<10,that is whether
2<10,yes 2<10 so it will print the value of I as 2 and increment the value of I by 1,i++ or i=i+1 so now the
value of I if 2+1 is 3,it goes and check the condition in while loop and checks whether i<10 that is
whether i<3,yes i<3 condition is true so it will print value of I as 3,and then it will increment the value of
I by 1 ,3+1 is 4 value of i=4,now it goes to while loop and checks the condition whether i<10 that is
whether 4<10,yes the condition is true so it prints the value of I as 4 and increments the value of I by
1,i++ or i=i+1,4+1 is 5,so the value of I is 5 so now similarly it will goes on checking the condition until
value of I is 10,till value of i=9 condition is true since 9<10 so till 9 it will print the value of 9,but when
value of I is 10,it will go to the while loop and checks whether i<10 that is whether 10<10,the condition
is false no 10 is not less than 10,so it will come out of the loop

Now if you want to print the values upto 10,you can give the condition (while i<=10),now equal to
condition is added along with less than so it will print the values upto 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 a loop is something which iterates or executes your code again and again until the condition is true in
this case the condition is i<=10
Day 13:do while loop in C

In the previous chapter we have seen how to use the loops,while loop in c,int this chapter we will see do
while loop and how to execute the numbers or integers from 0 to 10 but this time I am going to use the
do 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);

We will comment the while loop of the previous chapter because we will see the difference between
the while and do while loop,the do while starts with do,curly braces and then at the end of curly braces
while condition,we will get the output as the
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 what it is doing here is the value of I is 0 so it goes to do,do is to do that thing,that is to do that take
the value of 0 and print it,executes it,without checking the condition first,after it prints i=0,it increments
I by 1,i++ or i=1+1,so the value of i is 1 ,0+1 is 1 it goes go the while loop it checks the condition
while(i<=10) is true ,yes i<=10 since 1<=10,so it goes in the loop and again from the do ,it prints value of
I as 1,again the value of I is incremented by 1,1+1 is 2 so it again goes to the while loop and checks
while(i<=10),whether 2<=10,yes 2<=10,so it goes to the loop and from the do it prints i=2,then it
increments the value of I by 1,2+1 is 3,so goes to the while loop and checks the
condition,while(i<=10),the condition is true,yes 3<=10,so it will again go to the loop and from do it will
prints the value of I as 3,now again the value of I as incremented,i++ or i=i+1,so value of I is 4,3+1 is
4,and it will go to while loop and check the condition while(i<=10),yes the condition is true i<=10,so it
will go to the loop and from do it will print the value of I as 4,now it will increment the value of I by 1,i++
or i=i+1,so the value of i is 5,4+1 is 5,so the value of i=5 ,will again do to the loop,from the do it will print
the value of I as 5,and then it will go similarly till the value of I will be 10,the value of I will be printed till
10 but after incrementing the value of I by 1 when the value of I becomes 11,and it goes to while loop
and checks the condition while(i<=10),so the condition is false and it comes out of loop

So what is the difference between while and do while loop

The while loop checks the condition first and then executes the code,and the do while executes the
condition at least once,maximum 11,and once again it will go to the condition and check the condition
of the while and then it will again go to the loop,and when the statement becomes false,it comes out of
the loop,so do while loop executes the statement alteast once without checking the
statement,maximum 11,

But while loop checks the condition first and then executes the code

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(j<=10)

Printf(“Value of j=% d\n”,j);

/* j=i+1;*/
j++;

We will give a separator of printf between do while and while statements,below the print f separator is
the while statements and above the print f separator is the do while statements,we will use j integer in
while as we use I integer in do while,we see the output the

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

Now when we give a condition that is not true in do while and while as i>10 and j>10

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(j>10)

Printf(“Value of j=% d\n”,j);

}
/* j=i+1;*/

j++;

Output:

The value of i 0

So the difference between while and do while is the do while executes alleast once,maximum 11 so it
gives the output value of I is 0 but the while loop here does not execute since the condition is false in
this case as value is <10

Day 13:for loop

Here we will see for loop the for loop also depends upon initialization,condition,increment,so we will
see the structure of the for loop

Program:

# include <stdio.h>

#include<stdlib.h>

Int main()

For (init ; condition ; increment);

Statement(s);

}
If the parameters inside the for loop is true then this condition will be executed,the first parameter is
the initialization of for loop,the second parameter is to check some condition is true or not,which is
related to initialization,the third parameter is the increment of the operator that you have initialized in
the first parameter,so this is the main structure of for loop,lets take the same example as we have been
seeing in the last chapter,we will declare the variable integer a=0;then you have to give the while loop
and then condition and then increment,but in for loop things are done automatically what we will do is
just increment the value and replace it with for,inside the bracket the first parameter is initialization

Int a=0;

For (int i=0)

It is just like declaring int a=0;

You do not need to do the initialization separately,you can do this here in the for loop

For(int i=0; )

As we have seen in the previous tutorial in the while loop and do while loop,we check some condition to
check some condition and if the condition is fullfilled then we execute the code so in this for loop we are
checking if (i<10) because we have initialized the value of i as 0,so now it will go to the second
parameter condition and check whether i<10,so 0<10 and so on it will go to the third parameter and
then increment the value of I and then It will again go to the for loop

Program:

# include<stdio.h>

# include<stdlib.h>

Int main()

For ( int i=0 ; i<1 ; 1++)

So what we have done in the for loop,we have initialized a condition and on the basis of that
condition,we are checking whether the condition is true and then we are incrementing the variable that
we have initialized,now we will do what we need to do inside the for loop,for example

Printf(“the value of i=%d”,i);

So the for loop sees the initialization,condition,increment


And then you can do what conditions you need to put,this program will print values from 0 to 10

Program:

# include<stdio.h>

# include<stdlib.h>

Int main()

For ( int i=0 ; i<10 ; 1++)

Printf(“the value of i=%d\n”,i);

When we see the output we see the error that: ‘for’ loop initialization are only allowed in c 99
mode,then the latest version of c that is c 99 is not enabled in your ide,mainly in the code blocks,so to
enable c 99 in your code block,right click on you projects-workspace-c test,right click on c test or you
can also go to the settings,we will do right click on c test and then go to the properties-you will get the
projects tab-which contains project settings,build targets,build scripts,notes,c/c++ pointer
choices,debugger,editor coding choices,envariables choices-project build choices this will contain
selected compiler GNU GCC Compiler,compiler settings,linker settings,search directories,pre/post build
steps,custom variables,make commands,you will see policy-compiler flags,other choices,so click on
other choices and then type in the box(-std=c99) click ok,then click ok

Now when we see the output there will not be the previous error as we have put c99 in code blocks,and
we get the output the

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

here i as first initialized to 0 and then it checks the condition if i<10,so here i=0,so it checks whether
i<10,that is 0<10,so this is true so it goes to the printf statement and executes the statement,and prints
the value of I as 0,and then just before the ending of the curly braces of the printf statement it goes to
the for loop third parameter increment and increments the value of i by 1,that i++ or i=i+1,so now the
value of i=1,0+1 is 1,so now it again goes to the for loop,now the value of I is initialized to 1,i=1,it goes to
the condition and checks the condition whether i<10,so here 1<10,so it goes to the printf statement and
executes the statement and prints the statement value of i=1,now just before the ending of the curly
braces of the printf statement,it goes to the for loop third parameter increment and increments the
value of i by 1 so now the value of i is 1+1 is 2,so now it goes to the for loop and the value of I is
initialized to 2,that is i=2,it goes to the condition to check whether i<10, 2<10 is true so it goes to the
print f statement and executes the statement,it prints the statement the value of i=2,and then just
before the ending of the curly braces of the print f statement it goes to the third parameter of the for
loop,increment and increments the value of i by 1,so the value of i=3,2+1 is 3,now again the value of I is
initialized to 3,that is i=3,it goes to the condition and checks whether i<10,whether 3<10,yes the
condition is true,so it goes to the print f statement and executes the statement and prints the value of
i=3,then it goes to the third parameter of the for loop,increment and increments the value of i by 1,so
the value of i=4,3+1 is 4,so now it goes to the for loop the value of i is initialized to 4,i=4,it goes to thje
condition and checks whether i<10,whether 4<10,yes the condition is true,so it goes to the print f
statement and executes the statement,it prints the statement value of i is 4,now if goes to the third
parameter of the for loop,increment and increments the value of the i by 1,so the value of i=5,4+1 is
5,so now it again goes to the for loop and then and initializes the value of i to 5,i=5.it goes to the
condition and checks whether,i<10,whether 5<10,the condition is true so it goes to the print f statement
and then executes the statement and then prints the value of i=5,and similarly in goes to the loop till the
value of i reaches 9,i=9,so it prints the value of i=9 and when the condition becomes false when I
reaches 10,that is 10 is not equal to 10 so it comes out of the loop,if you need the value of i as 10 ,then
you can give the condition i<=10

Program:
# include<stdio.h>

# include<stdlib.h>

Int main()

For ( int i=0 ; i<=10 ; 1++)

Printf(“the value of i=%d\n”,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

Day 14:Functions in C

Function is a pice of code or block of code which we use to perform a particular task,suppose you want
to add numbers multiple times in a program so you can define the function and you can use the
function,or you can call your function again and again inside any other function,now lets see how we
can define the function,but we can say that every c program has 1 function called main,to execute your
program c searches for this main function and then executes whatever inside this,it has the return
type,it has the name of the function,main is the inbuild name of the function in c,but you can define
user defined function and give it any name,so lets see how is the definition or declaration of the
function

Program:

# include<stdio.h>

#include<stdlib.h>

/* Type name(type 1 arg 1,type 2 arg 2,type 3 arg 3………….)

/*code

*/

Int main()

This is not the actual function declaration,it is some pseudo declaration,first you give the type of
function and then you give the name to the function,then in te bracket you can pass argument or
parameters of the type,inside the curly braces you write the code,for now we can comment this code so
that we will see this as the reference,now you can define the function above your main function or
below your main function,but there is a difference in declaring the function above or below the main
function which I am going to tell you,but lets take an example now we want to add some function,so lets
do this or lets take some rather some example that we want to print some number using function,I have
told you that function starts with a return type,this time I have told you that,if we don’t want to return
anything then we return void,void means we are not returning anything,then comes the function name
you,then comes the bracket ,then the parameters,I depends on you if you want to give some
parameters(example: type 1 arg 1,type 2 arg 2,type 3 arg 3………….) to your function or not,right now are
not going to use any parameters but we will use some parameters functions in the next chapter,but now
we will not pass any parameter to the function,I want to execute some code,I want to print something

Program:
# include<stdio.h>

#include<stdlib.h>

/* Type name(type 1 arg 1,type 2 arg 2,type 3 arg 3………….)

/*code

} */

Void MyFunctionName()

Printf(“sum=%d”,10+53);

Printf(“we are inside a function\n”)

Int main()

You can write your code inside the function,you can add the numbers this is just an example,10+53 add
the numbers,so any number od line of code you can write inside the function and then you can call this
function inside the main and for calling the function inside your main,you just need to copy the name of
the function,main and then give a semicolon here the in MyFunctionName();so what it does is whenever
you call your function name after int main(),this function-void MyFunctionName() will be replaced by
this code – {

Printf(“sum=%d”,10+53);

Printf(“we are inside a function\n”)

Program:

# include<stdio.h>

#include<stdlib.h>
/* Type name(type 1 arg 1,type 2 arg 2,type 3 arg 3………….)

/*code

} */

Void MyFunctionName()

Printf(“sum=%d”,10+53);

Printf(“we are inside a function\n”)

Int main()

MyFunctionName();

So we are also calling the code above in place of this function also which is after-int main(),the function
is MyFunctionName();the logical name what we have given to this function-MyFunctionName,the same
function above also should reflect the code that we are writing-

Printf(“sum=%d”,10+53);

Printf(“we are inside a function\n”)

So we get the output of the program as:

Sum=63 we are inside a function

So what happens here is that it goes to the first second and third line and sees and remembers the
declaration of the function here that is void MyFunctionName(),which we have declared and whenever
our main function calls this,function we need to execute these lines {

Printf(“sum=%d”,10+53);

Printf(“we are inside a function\n”)

}
And because we are calling this function-MyFunctionName(), inside our main will execute this line

Printf(“sum=%d”,10+53);

Printf(“we are inside a function\n”)

You can use this function after main() that is-MyFunctionName(),multiple times in your code also

MyFunctionName();

MyFunctionName();

MyFunctionName();

Program:

# include<stdio.h>

#include<stdlib.h>

/* Type name(type 1 arg 1,type 2 arg 2,type 3 arg 3………….)

/*code

} */

Void MyFunctionName()

Printf(“sum=%d\n”,10+53);

Printf(“we are inside a function\n”)

Int main()

MyFunctionName();
MyFunctionName();

MyFunctionName();

Output:

Sum:63

We are inside a function

Sum:63

We are inside a function

Sum:63

We are inside a function

It will print this 3 times the sum and we are inside the function,because we are calling the function 3
times,this tells us that we can call a function multiple times in your code,that is the main use of
function,so function makes your code readable and fast to execute and you can call your function again
and again so this makes your code understandable,you can declare the fuction below or above the main

Program:

# include<stdio.h>

#include<stdlib.h>

/* Type name(type 1 arg 1,type 2 arg 2,type 3 arg 3………….)

/*code

} */

Void MyFuncitonName();
Int main()

MyFunctionName();

MyFunctionName();

MyFunctionName();

Void MyFunctionName()

Printf(“sum=%d\n”,10+53);

Printf(“we are inside a function\n”)

You can do this but you need to prototype this function,when you declare the void MyFunctionName ()
after int main(),that is put void MyFunctionName() above the int main(),we will see what happens if we
dont do this,you tell your c program that there is a function called my function name,which you are
going to declare somewhere in your program,there is a function called MyFunctionName(void
MyFunctionName()),that is to defined in our code somewhere in the code,so your c program comes
from the function-void MyFunctionName(),above the int main() to here to void MyFunctioName() after
the int main(),

so this is called the definition of the function,it comes after the int main()

Void MyFunctionName()

Printf(“sum=%d\n”,10+53);

Printf(“we are inside a function\n”)

And this is called prototype of the function/forward declaration of the function ,it comes before the int
main()

Void MyFunctionName()
After that it goes here int main() and whenever your main calls the function it knows that your function-
prototype of the function is defined here,as shown above in the program,the program runs even
without it,but it is best practice,that whenever you define your function after the int main() as seen
above prototype the function before the int main() as shown above in the program,you have to give a
semicolon for the prototype function before the int main() in the program,so this how a function is done
in c,

There are different types of function,a function which takes in a parameter,a function which returns
some value which we will see in the next chapter

Day 16:Passing Parameters and Arguments in Function

In the previoius chapter we have seen what is a function and How we can call function from our main
function,and how can we declare a function,void MyFunctionName(), and printf I this chapter we will
see how to,declare functions which takes in parameters and arguments,suppose we want to print a
function which adds the values and prints the sum,so we will declare a function which returns
nothing,void you can name it as something you find appropriate for your program,we will name it
sum,because we want to pass the number which we can sum we will pass it as as an argument for the
sum function,so how to pass an argument

Program:

#include<stdio.h>

#include<stdlib.h>

Void sum()

Int main()

In the bracket () of void sum() we will pass the argument,first we will give the type of the argument first
that is int-void sum(int….. ) and we will give the name of the argument that is a in this way-void sum(int
a…….) and in the similar way we will pass the other argument int b in this way-void sum(int a,int b)you
can pass any number of arguments in the function for example-void sum(int a,int b,int c,ind d,int
e………….) but here we want to use the number int a and int b so we write the function as-void sum(int
a,int b),but here we will pass 2 arguments,int a and int b,and we will give the curly braces

Program:

#include<stdio.h>

#include<stdlib.h>

Void sum(int a,int b,int c,int d,int e……………)

Int main()

Program:

#include<stdio.h>

#include<stdlib.h>

Void sum(int a,int b){

Int main()

From this we want to do is two add the functions and pass it into another variable,we will name it as int
sum,which will hold the sum of our 2 arguments which is a and b,so we will do this,int sum a+b; and now
we want to print this,so we write printf(“sum=%d”,sum);so whenever you need to add these 2 number
you can call the sum function inside the main function,int main() after the printf statement,suppose you
want to add 2 numbers

Program:

#include<stdio.h>

#include<stdlib.h>

Void sum(int a,int b){

Int sum=a+b;

Printf(“sum=%d”,sum);

Int main()

We will call the sum function inside the main function,suppose you want to add 2 functions 50 and 60
here sum(50,60); because the sum function takes in 2 arguments int a and int b,so you can pass 50 as a
in int a and you can pass 60 as b,in int b,so what this sum function will do?sum(50,60),it will replace a by
50 and b by 60,and then it will transfer this sum to the sum variable,int sum=a+b;so it add 50and 60 and
print 111 as the output the,you can call this function multiple times,suppose you want to get the sum
multiple times

Program:

#include<stdio.h>

#include<stdlib.h>

Void sum(int a,int b){

Int sum=a+b;

Printf(“sum=%d”,sum);

}
Int main()

Sum(50,60);

Output:

111

Program:

#include<stdio.h>

#include<stdlib.h>

Void sum(int a,int b){

Int sum=a+b;

Printf(“sum=%d\n”,sum);

Int main()

Sum(50,60);

Sum(1000,600);

Sum(111,227);

Sum(123,457);

Sum(130,60);

}
Output:

Sum=111

Sum=1600

sum=338

sum=579

sum=190

and you can call the function multiple times so it calls the functions and adds the sum multiple
times,suppose you want the user should define these 2 functions at a time,what you can do is you can
declare 2 integers x,y,then you can ask the user to print the 2 numbers so you can

printf(“please enter 2 numbers\n”)

printf(“number 1”);

sum(50,60);

So we will write printf which is used to take the input of 2 number

Printf(“number 1\n”);

Scanf(“%d” ,&x)

Sum(50,60);

So we are scanning 2 numbers x and y so we

Int x,y;

Printf(“please enter two numbers\n);

Printf(“number 1\n”);

Scanf(“%d” ,&x);

Printf(“number 2\n”);

Scanf(“%d” ,&y)

Sum(50,60);
}

Program:

#include<stdio.h>

#include<stdlib.h>

Void sum(int a,int b){

Int sum=a+b;

Printf(“sum=%d\n”,sum);

Int main()

Int x,y;

Printf(“please enter two numbers\n);

Printf(“number 1\n”);

Scanf(“%d” ,&x);

Printf(“number 2\n”);

Scanf(“%d” ,&y)

Sum(50,60);

Now we will pass first argument x for the first function and second argument y for the sum function

Program:

#include<stdio.h>
#include<stdlib.h>

Void sum(int a,int b){

Int sum=a+b;

Printf(“sum=%d\n”,sum);

Int main()

Int x,y;

Printf(“please enter two numbers\n);

Printf(“number 1\n”);

Scanf(“%d” ,&x);

Printf(“number 2\n”);

Scanf(“%d” ,&y)

Sum(x,y);

Output:

Please enter two numbers

Number 1

6458

Number 2

1038

sum=7496
so this is called function with arguments and these are arguments argument 1 int a and argument 2 int
b,so you can pass any number of arguments and you can call your function inside the program,so this is
function with arguments,or functions with parameters

Day16:Return Values in Functions

We have seen functions in c and passing parameter and arguments in c,in this chapter we will see
functions to return the value,till now we have been writing function with the void-void sum(int a,int
b),but any function you can return some value,value can be of any type int,double,char,string,so lets see
how we can use function to return a value,suppose we want to use the function which gives product or
value of something,I was asking the user to enter 2 values,then I scan first number and we passed this
function to this sum(x,y),now suppose we want to declare a function which gives us the product or value
or something so we will declare two functions which returns some value suppose we want to declarr a
function which gives us the multiplication or product of something and it will return something,so we
will write return value earlier we used to write void value,this time we will return an integer,so we are
writing int here so we are writing int here it will take 2 argument int a and int b and it is going to give the
product or multiplication

int Product(int a,int b)

Please enter 2 numbers,so in order to return 2 numbers so in order to return 2 numbers,use the
keyword return because we want to return the product of a and b so we will write a*b,that is return
(a*b)so this function will take 2 parameters a and b and then it will make the product of 2 numbers a
and b and then it will return the value as an integer-int Product(int a , int b);

Program:

#include<stdio.h>

#include<stdlib.h>

int Product (int a , int b)

return (a*b);

}
Int main()

Int x,y;

Printf(“please enter two numbers\n);

Printf(“number 1\n”);

Scanf(“%d” ,&x);

Printf(“number 2\n”);

Scanf(“%d” ,&y)

Sum(x,y);

And then we can use this number for other mathematical stuff or other thing else,for calling the
function we can call it by the name,last time whwn we had entered 2 numbers and we were asking the
user to enter 2 numbers so this is ok so by you might have known what the user is doing,it is taking the
values from the user and then we will pass these 2 values,(x,y) in the product function x,y will be
replaced by a and b in int product(int a,int b),and it is going to return the product as an integer,because
it is going to return the product as an integer,we will define an integer,that is

int prod;

Product x,y;

Program:

#include<stdio.h>

#include<stdlib.h>

int Product (int a , int b)


{

return (a*b);

Int main()

Int x,y;

Printf(“please enter two numbers\n);

Printf(“number 1\n”);

Scanf(“%d” ,&x);

Printf(“number 2\n”);

Scanf(“%d” ,&y);

int prod;

prod=Product(x,y);

printf(“Product=%d”,prod);

This is because this function is going to return this an an integer

We see there is an error when we compile and run the program-

Printf is undefined,in the int product,Printf should be printf

Output:

Please enter two numbers

Number 1

30

Number 2
60

Product 1800

we have given x=30 any y=60

the statement of printf-number 1,the scanf of it scans the value 30,the statement of printf-number 2,the
scanf of it scans the value 60,it stores the value of x and y in prod=product(x,y),in the(x,y) which if after
int product ;so x=30 is replaced here(x,y) and y=60 is replaced here (x,y),value of x and y 30 and 60 is
passed to this function-prod=Product(x,y),the function here is Product,and then it goes here-
int Product (int a , int b),goes here to the a and b of the int,and then the return function multiplies
the value of a and b- return ( a * b );and the return function will return the value of the variable to in
product,that is int prod;prod=Product(x,y),for the variable to be stored,this is the way

the other directly you can do this,you can directly pass the values of x and y as 10 and 60

Program:

#include<stdio.h>

#include<stdlib.h>

int Product (int a , int b)

return (a*b);

Int main()

int prod;

prod=Product(10,60);

printf(“Product=%d”,prod);

}
So this is 10 and 60 in-prod=Product(10,60);that we have passed to this function-int Product (int a , int
b),and because this function takes integers-int a,int b,so we are passing the integers,so 10 in
Product(10,60) will be replaced here in a of the int a in the function- int Product (int a , int b),and 60
in Product(10,60) will be replaced in b of the int b in the function- int Product (int a , int b),now 10
will be multiplied by 60 in-return(a * b) and will be returned to this function- int Product (int a , int
b),and we can store this value in another variable
prod=Product(10,60),printf(“Product=%d”,prod);which you can use for something,suppose you want to
use this variable for another use-printf(“Product=%d”,prod); you can do this whatever product you are
getting,the prod,Product-printf(“Product=%d”,prod);from the product function- Product(10,60),we
want to add 10 to it and then show the result,the code is the-

prod=Product(10,60);

printf(“Product=%d”,prod);

printf(“result=%d”,Product())

so we will do in direct and we will also remove the int prod;prod=Product(10,60);and the
Printf(“Product=%d”,prod); and replace these by-printf(“result=%d”,Product))

printf(“result=%d”,Product(9,9));

we give (9,9) so 81 will be returned by this,and we will add 16 to this so the result will be 98

printf(“result=%d”,Product(9,9)+16);

so you can use this function product in-printf(“result=%d”,Product(9,9)+16); which returns integer
values- int Product (int a , int b),which does math operation on this function also-the function-
Product(9,9)+16)

Program:

#include<stdio.h>

#include<stdlib.h>
int Product (int a , int b)

return (a*b);

Int main()

printf(“result=%d”,Product(9,9)+16);

Output:

98

Day 17:Scope Rules in C

There are two types of variables declaration in c,global variable and local variable which you
declare inside the function,for example int a=11 is called local variable,which you define inside the int
main()

Program:

# include<stdio.h>

# include<stdlib.h>

Int main()

///local variable

Int a=11;
}

A variable which is not inside any function is called global variable,it does not take any argument,and
prints something,we can print any variable int a int x

Program:

# include<stdio.h>

# include<stdlib.h>

Void NumberPrint()

///local variable

Int x;

Int a;

Int main()

///local variable

Int a=11;

And this is also a local variable Int x;Int a; because it is defined inside a function,but whenever you
define any variable c which int c;which is not inside any function is called global variable,what is the
difference between global variable and local variable?a local variable is only valid inside your function,it
is local to your function-void NumberPrint(),so you cannot use this a,int a=11,inside the number
function-void NumberPrint(),and you cannot use this x function int x,in number function-void
NumberPrint(),a global variable is visible to every function in the program,so you can use the global
variable int c,in the local variable Int x;Int a,which is after the number function-void NumberPrint(),and
local variable int a=11,which is after the main function

Program:
# include<stdio.h>

# include<stdlib.h>

///global variable

Int c;

Void NumberPrint()

///local variable

Int x;

Int a;

Int main()

///local variable

Int a=11;

So whenever you define a global variable it is initialized to 0 if it is int,float,double,you do not need to


initialize it,but a local variable is not initialized to 0 so this value-int a,inside main function can contain
any value ,garbage value,and you would not know the value of this,so we will print global variable inside
the local variable and similarly we will print the local variable inside the local variable after the main
function,we will name global variable as int global c;and local variable as local a;

Program:

# include<stdio.h>

# include<stdlib.h>

///global variable

Int global c;
Void NumberPrint()

///local variable

Printf(“global=%d”,c)

Int main()

///local variable

Int local a;

Printf(“local=%d”,a)

So lets print the global variable inside local variable inside the main function,because we we not use this
function-void NumberPrint() now,so we remove it from there and print it in the local variable after the
main function,and we put global c and local a for printf statements of global and local variable,we have
not initialized the global variable and local variable to the value,we see the output

Program:

# include<stdio.h>

# include<stdlib.h>

///global variable

Int global c;

Void NumberPrint()

///local variable

Int main()

{
///local variable

Int local a;

Printf(“local=%d\n”,local a);

Printf(“global=%d\n”,global c);

Output:

Local 79

Global 0

So we see in the output that the local variable as we have not given the value it takes a garbage
value here as 79,but we have also not give the value to global variable so it takes the value 0,so we see
that if we do not initialize the global variable it takes the value 0,and if we don not give the value to local
variable it takes a garbage value as we see here is 79,if there is the character not given value it will be
initialized by null ,and if there is a pointer not given the value it will be initialized by null

Now let us initialize the global variable int global c=31,and the global variable inside the local
variable,after the main function as into global c=27,we have given global variable in place of local
variable,we comment the printf statement of local variable for now

Program:

# include<stdio.h>

# include<stdlib.h>

///global variable

Int global c=31;

Void NumberPrint()

///local variable
}

Int main()

///local variable

Int global c=27;

/*Printf(“local=%d\n”,local a);*/

Printf(“global=%d\n”,global c);

So in this case whatever value is contained by the local variable,even if it is defined by the same name of
global variable in int global c=31,and int global c=27 after the main function in the local variable,the
value of the global variable global c=27 will be stored in the local variable,so it will print 27,so local
variable always has the preference over global variable if they have the same name,so it prints 27 the
printf statement after the main function

Output:

global

27

Now we will remove the comment from the printf statement of local variable after the main fucnction
and we will put it inside the function-void NumberPrint(),and we will printf global c+50,in place of local
a,and name the prinf as global+50 in place of local,so what it will do?it will print the value of global
variable which is 31 and + it will add 50 to this function-void NumberPrint(),which is 81,and we willc all
function NumberPrint() inside the main function after the printf statement of the global variable inside
the local variable

Program:

# include<stdio.h>

# include<stdlib.h>

///global variable

Int global c=31;

Void NumberPrint()
{

///local variable

Printf (“ global + 50 = %d\n”, global c + 50 );

Int main()

///local variable

Int global c=27;

/*Printf(“local=%d\n”,local a);*/

Printf(“global=%d\n”,global c);

NumberPrint();

Output:

global 27

global 50 -81

so global variables are visible to all your functions and all your code,here numberPrint();is visible to
function void Number Print() and all your functions,but if you want to print local variable,let us take a
local variable-int Num Local=30,if we print this here,printf(“Num Local=%d\n”,Num Local)inside the
main function it will give a compilation error because local variable is not visible to other parts of the
program,because the function Num Local exists in other function,that is in void NumberPrint(),other
than the main function,compilation error:Num Local undeclared

Program:

# include<stdio.h>

# include<stdlib.h>

///global variable

Int global c=31;


Void NumberPrint()

///local variable

Int Num Local=30;

Printf (“ global + 50 = %d\n”, global c + 50 );

Int main()

///local variable

Int global c=27;

Printf(“Num Local=%d\n”,Num Local);

Printf(“global=%d\n”,global c);

NumberPrint();

Day 18:Arrays in C

Arrays are variables that contain more than 1 values of the same data type,for example you want to
store similar data,in the variable you can do it with the help of the array scoreof student,age of persons
and name of the person,you can do it using the array,so how to declare an array,it is same as declaring
variables,so first what is the data type,the kind of data you want to store in your array and then the
name of the array,int MyNumberArray [],this square bracket [] tells us that this is an array,and whatever
number you insert inside the square bracket,this tells us the number of elements

Int MyNumberArray[3]

And store 3 numbers inside the array,because I have defined that it can store 3 values[3],so I have
stored 3 values in the array {20,30,60}

Int MyNumberArray[3]={20,30,60};

If there would be 5 values so I would add more values

Int MyNumberArray[5]={20,30,60,55,80};
Now you may see how can we access the value of an array,by index of an array is a position at which any
value is placed,the index of 20 is 0,the index of an array starts from 0,the value at the first index of the
array is 30,and the value at the 2 nd index of array is 60 and value at 3 rd index of array is 55,so the
index of an array is 0,1,2,3,4,5,but now can you access the value of an array we give ,suppose we want
to store this value 30 into the array,I can put the name of the array

int accessArray=MyNumberArray[1]

in the square bracket the index of an array whose value,for example we want to store 30,so the index of
30 is(0,1),so we will write 1 here in MyNumberArray[],now the value 30 from the array

Program:

# include<stdio.h>

# include<stdlib.h>

Int main()

Int MyNumberArray[3]={20,30,60}

Int accessArray=MyNumberArray[1]

Printf(“value=%d\n”,access array)

So now the value of 30 will be stored in this access array variable,so not to access the value of an array
we can example

Int access array=MyNumberArray[1]

Char charArray[5]={********************;

Char charArray[5]={‘a’,’b’,’c’,’d’,’e’);

So this is a character array you can define string array

But see this the data type of array,declare in integer values,if we declare integer arrays,if we declare
char arrays,it will contain char arrays ,if we declare string arrays,it contains string values,if you have float
arrays,you can have float values,suppose you want to print all the values of an array,so the index array
contains 6 values,suppose we want to print all the values of an array,and the index of these values for
array[6] are 0,1,2,3,4,5,suppose I want to print the index of the arrays I can use for loop for that,I is less
than index of the array or the size of the array,that here is 6,array-[6] the for loop is-for (int i=0 ; i<8 ;
i++), and then I can print these values- Char charArray[4]={‘a’ , ’b’ , ’c’ , ’d’}; after the for loop that if the
print f is- Printf (“value=%d \n”, accessArray); I will write element %d[] and I will print the value of I,and
then I will print the index one by one,index-Int MyNumberArray[6]={20,30,60,50,55,30};then I will print
the name of the array,I,why have I written i?because I will be increased in the for loop,so first the value
of I is 0 in the for loop initialization of i=0,so it will print 20 from MyNumberArray[8]-the first index
value,that is for the index value 0 array value is 20,now I will increment in the for loop ,i++ so now i=1,so
value of array at index 1 is 30,that is for the index value 1 array value is 30,so now I will increment in the
for loop i++ so now i=2,so value of array at index 2 is 60,that is for the index value 2 value of array is
60,now I will increment in the for loop i++,so now i=3,so value of array at index 3 is 50,and similarly the
index value and array values for the elements are stores till [6]

Program:

# include<stdio.h>

# include<stdlib.h>

Int main()

Int MyNumberArray[6]={20,30,60,50,55,30};

Int accessArray=MyNumberArray[1];

Printf (“value=%d \n”, accessArray);

Char charArray[4]={‘a’ , ’b’ , ’c’ , ’d’};

for (int i=0 ; i<6 ; i++)

Printf (“value=%d \n”, accessArray);

Output:

Value=30

element[0]=20
element[1]=30

element[2]=60

element[3]=50

element[4]=55

element[5]=30

so this is how you print the value of array,now if you want to change the value of array
elements,suppose you want to change the value of array 30 to 45,so you can change it like this the index
of value 30 is 0,1 to 45,so you can do it like this

MyNumberArray=[1]=45;

Program:

# include<stdio.h>

# include<stdlib.h>

Int main()

Int MyNumberArray[6]={20,30,60,50,55,30};

MyNumberArray[1]=45;

for (int i=0 ; i<6 ; i++)

Printf (“value=%d \n”, accessArray);

Output:

element[0]=20

element[1]=45
element[2]=60

element[3]=50

element[4]=55

element[5]=30

so we can see here from the output element[1] is replaced from 30 to 45,so element[1] is now 45,so this
type of array which can contain 1 or more variable or elements are called single dimentional
arrays,there are multidimentional arrays that we will see in the next chapter

Day 19:Multi-Dimensional Arrays in C

Arrays can contain more than 1 values of same data type in a single variable,you can declare 2
dimensional arrays as-int MyNumber Array [2] [3] ;

You can declare 3 dimensional arrays as this

Int MyNumberArray [2] [3] [4] ;

For 4 or 5 dimensional array-[2] [3] [4] [5],[2] [3] [4] [5] [6]

But mostly we use 2 dimensional arrays

So here[2] [3] you can see it as a box or table,here 2 is number of rows in the array,and 3 is the number
of columns in the array,so now lets give a bracket and give 2 curly braces,you can declare your array,first
we will define our row 0{ } and this will be our row 1,and columns can be defined this way

{1,3,2 }

{ 5,6,8 }

{ }

1,3,2 are row 1 of index 0 and 5,6,8 are row 2 of index and 5,6,8 are 3 columns so this a 2 dimentional
array which contains 2 rows and 3 columns,for 2 dimensional array we have to use 2 for loops,loop
inside loop,for iterating over the values you can do this this way,inside the curly braces of this first for
loop we will declare another for loop,within the curly braces for this for loop,so we have loop inside
loops,so we have for loop to iterate the values of the row and we have for loop to iterate the values of
the columns,to print all the values inside the column for loop,so we will print the array printf MyNumber
Array and we will also print the index of the array
Program:

# include <stdio.h>

# include<stdlib.h>

Int main()

Int MyNumberArray [2] [3]={

{1,3,2} ///row 0

{5,6,8} ///row 1

/{3,8,6} ///

}for (int i=0 ; i<2 ; i++) ///for loop for the rows

For (int j=0 ; j<3 ; j++) ///for loop for the columns

Printf(“MyNumberArray[%d][%d]=%d”)

So the first % d will give the rows and the second % d will give the column,and the other third % % will
give the values,the rows first % d can be iterated by i from first for loop int i=0,and column second % d
can be iterated by j from other for loop,and the values are iterated by index int MyNumberArray,row
indicator and the value of j indicator that is the column,the row 0 in the comments-we see that row it is
row 1 but the index value it takes for 1 is 0,so row 0,the row 1 in the comments-we see that it is row 2
but the index value it takes for 2 is 1 so,row 1, for row 2 in the comments-we see that it is row 3 but the
index value it takes for 3 is 2 so row 2

Output:

My Number Array[0][0]=1

MyNumber Array[0][1]=3

MyNumber Array[0][2]=2
MyNumber Array[1][0]=5

MyNumberArray[1][1]=6

MyNumber Array[1][2]=8

Day 20:Passing Arrays as Function Arguments in C

In this chapter we will see how can we pass arrays as arguments of the function,so lets see how can we
do this,we will take an example,we will calculate the sum of all the elements of the array,we have 6
elements here and we want to add each and every element and display every element,Int
MyNumberArray[6]={20,30,60,50,55,30} sum them up and show the result of the summation using the
function,but first lets see,what are the different ways of passing the arrays as an argument of the
function,I will write void Array sum () as my function,the void does not return any value and there are 3
different ways that you can pass arrays as an argument of a function,1 way is to use a pointer you can
give a star or asterix here -void Array sum (int *),and with array name also as MyArray- void Array sum
(int * MyArray) we can pass pointer array to our function but we haven’t learned about pointer now

Program:

# include<stdio.h>

# include<stdlib.h>

Void ArraySum(int * MyArray)

Int main()

Int MyNumberArray [6] = {20,30,60,50,55,30};

So we will leave this now,and when in other chapter we will learn pointers then we will see how we will
pass this kind of array as a function argument,the second way is to pass the sum of a definite sized
array,suppose I want to get the sum of this array-Int MyNumberArray [6] = {20,30,6050,55,30};I can
declare this array as int My Array[],then I will give the array as-void Array sum (int MyArray [6] )and I
will give the size of the array as [6],but there is a problem with this,you can only pass array of size 6 to
this function,so this will kill the general functionality of a function,and the functionality of the function
should be used in different conditions,and this condition restrains the use of function-int MyArray[6] in
the function-void Array sum (int MyArray [6]) of the size[6] of the array,to remove this restriction we
can pass,the unsized array as the argument,this is that you want to pass arrays of different size or which
does not have any fixed size,that is different sizes-void Array sum (int MyArray [ ]) ,plus we will pase size
as an argument also- void Array sum (int MyArray [ ]), int size) we have the size of the array and the
name of the array,now we can pass different size array to the function,now using this function-void
Array sum (int MyArray [ ]), int size) we want to add the sum of all the elements of the array,int
MyNumberArray[6],and because we want to calculate the sum,so we will define sum and initialize it by
0, int sum=0,we will put this after the void array sum function,we will return int that is return integer, to
the function so the function is now the- int void Array sum (int MyArray [ ]), int size),we will put for loop
after int sum=0,the for loop is-for(int i=0;i<6;i++)it will iterate over the array MyArray [ ] in the function-
int void Array sum (int MyArray [ ]), int size),one by one,and it will add the values in (int sum=0) of the
elements{20,30,60,50,55,30},the initial value of the sum is 0,sum=0,it will add 20,it will add 30,it will add
60 to the sum in int sum;as we iterate over the array in the for loop,so the declaration of thr foe loop is
like this,condition or bracket we will initialize i by 0 and i should be less that the size of the array,so pass
this size,that is int size in the function-int void Array sum (int MyArray [ ]), int size) here in the for loop
for (int i=0 ; i <size ; i++),the size,here size is the size of the array,so I should be less than the size of
the array,and what we want to do is sum is the previous value of sum plus the array name and the
index,that is I,we write this after the for loop within curly braces this way-sum=sum+MyArray[i]

Program:

# include<stdio.h>

# include<stdlib.h>

int ArraySum (int MyArray [ ] , int size)

int sum=0;

for (int i=0 ; i < size ; i++)

///sum = sum + MyArray[ i ]

Sum + = MyArray[ i ]

}
return sum;

int main()

int MyNumberArray [6] = {20,30,60,50,55,30};

So I will change in this MyArray[i] of the sum,it will go to the for loop for i=0,the value will be 20 from
the elements of int MyNumberArray[6],so the value of MyArray at 0,that is value of MyArray[0] will be
20 from the elements when we pass this MyArray(which is in this function) as an argument to this
function- int ArraySum (int MyArray [ ] , int size),we can write sum=sum+MyArray[i] as
sum+=MyArray[i],then we will return sum;once we have sum of all the elements of the array,now we
will return the sum here as an integer the-int ArraySum (int MyArray [ ] , int size),suppose we have
the array of elements[6] and we want to calculate the sum of all the elements,we will print int ArraySum
again after the int My NumberArray[6],but we will change the name-int sum of array=ArraySum(); value
is the value returned by the function when we pass,this function- int ArraySum (int MyArray [ ] , int
size),to the int NumberArray and also the size of the array,we will pass these to the,int sum of
arrays=ArraySum(),this way-int sum of arrays=ArraySum(MyNumberArray,6) and then we will print f the
sum of the array-printf(“Array Sum=%d”,),since this will return the sum of the array,so this value int sum
of array,is assigned to this variable sum of array and then we will pass this here in array sum=%d”,sum
of array

Program:

# include<stdio.h>

# include<stdlib.h>

int ArraySum (int MyArray [ ] , int size)

int sum=0;

for (int i=0 ; i < size ; i++)

///sum = sum + MyArray[ i ]


Sum + = MyArray[ i ]

return sum;

int main()

int MyNumberArray [6] = {20,30,60,50,55,30};

int sum of array = ArraySum (MyNumberArray , 6);

printf(“ Array Sum = %d”, sum of array);

Output:

Array Sum=245

Suppose we add some more elements to the array and make the array of 6 elements [6],to array of 8
elements [8],we have the array as the-int MyNumberArray [8] = {20,30,60,50,55,30,100,38},so now the
sum of array will be we see the output the

Program:

# include<stdio.h>

# include<stdlib.h>

int ArraySum (int MyArray [ ] , int size)

int sum=0;

for (int i=0 ; i < size ; i++)

///sum = sum + MyArray[ i ]

Sum + = MyArray[ i ]
}

return sum;

int main()

int MyNumberArray [8] = {20,30,60,50,55,30,127,138};

int sum of array = ArraySum (MyNumberArray , 8);

printf(“ Array Sum = %d”, sum of array);

Output:

Array Sum=511

Passing array as the argument of a function using c

Day 22:Pointers in C

a pointer is a variable whose value is the address of other variable,now you may ask what is the address
of a variable,let us declare a variable int val=30,whenever c declares a variable,it is stored in some
memory address on your computer and in order to access this value or access this address of the
variable-val,in int val=30,where it is stored in your computer memory,you can access it this way,if you
want to print this address,we use printf(“address of val=%d”,& )we use & symbol to show the address
of any variable.if we use & symbol before any variable it will show the address of that variable,but since
address is hexadecimal value this % d will not work here the,d is used to print decimal values,but to print
hexadecimal values we use % x,when we use % x it is because,address is hexadecimal value,we see the
output the

Program:

# include<stdio.h>
#include<stdlib.h>

/// a pointer is a variable whose value is the address of other variable

int main()

int val = 30;

printf (“address of the val = %d”, & val);

Output:

Address of val=31ff1c

So this is hexadecimal memory address in your computer where this variable val is stored in your
computer,a pointer can also be declared as also before the & val,a pointer can do something this ,it has
the data type int and then you put *,it tell c that you want to declare a pointer,int*pointer p ,that is if
you put the * before a variable it becomes a pointer,suppose pointer p without * is a variable,now you
put * before it that is *pointer p,it becomes a pointer,so we declare pointer p = & val;this is because
pointer is a variable(which here is pointer p),whose value(& val)is the address of another variable(val is
another variable whose address is & val,so its address is the value of the pointer variable)and whenever
you assign this address & value to your pointer,you need to add the *,so when declaring only you have
to give the * before pointer p,for assignment you do not need this asterix before the variable pointer
p,now this pointer p has the address of this variable(& val),now how can you access the address of this
variable val of value 30 using the pointer

Program:

# include<stdio.h>

#include<stdlib.h>

/// a pointer is a variable whose value is the address of other variable

int main()

int val = 30;


int * pointer p;

pointer p = & val ;

printf (“address of the val = %x\ n”, & val );

printf (“ value of pointer variable = %x \ n” , pointer p );

First lets print what is in the pointer p,

Printf(“address of the val =%x\n”,pointer p); now let us see what is the value of the pointer variable
without the & symbol,so this value-& val in printf and this value-& val in pointer p should be same
because we have assigned this to the pointer variable and t,so the address of value and value of pointer
variable should print the same value,lets seethe output of the program

Output:

Address of val=29ff18

Value of pointer variable=29ff18

So the pointer just stores the address of variables,now inn order to access the values which is stored in
this address,pointer p of the printf”value of pointer variable),for example you want to access the value
of 30 using this value,so using this pointer you want to access this value which is 30

So we use

Printf(“value of pointer variable=%x”\n”,pointer p)

But the pointer(pointer p=& val) can only store the value of this variable(int val=30),the value that is
30,and the address of 29ff18,now to access the value of the address (& val) in the-(pointer p=&
val),value of the pointer can be declared as we put * before pointer p

Printf(“value of pointer =%x”\n”,* pointer p);

So what is does is that,this pointer contains the address(pointer p) in printf-value of pointer,and when
you add this asterix before,it shows the value at that address,and lets change the % x to % d in printf-
value of pointer,because we want to access this integer 30(int val=30)

Printf(“value of pointer =%d”\n”,* pointer p);

Program:
# include<stdio.h>

#include<stdlib.h>

/// a pointer is a variable whose value is the address of other variable

int main()

int val = 30;

int * pointer p;

pointer p = & val ;

printf (“address of the val = %x\ n”, & val );

printf (“ value of pointer variable = %x \ n” , pointer p );

printf(“value of pointer=%d\n”,*pointer p);

Output:

Address of val=29ff18

Value of pointer variable=29ff18

Value of pointer=30

So we will see this,every variable in c is stored on your computer at a particular address,and this address
you can access by adding this & val before a variable,a pointer is a variable whose value is the address of
another variable,so the pointer is by adding * before your variable name,variable name-pointer
p,pointer-* pointer p,you can assign some value to the pointer using the address of another
variable(pointer p=& val);int val=30 is a variable if you want to assign it to the pointer you can assign it
this way(pointer p=& val),

Dereference Pointer-to access the address that is stored at this place which is hold by

The third print f statement of the program above


printf(“value of pointer=%d\n”,*pointer p);

you use this * sign before the pointer,this * sign before the pointer p tells c,that we want the value of
the address which is stored in this pointer,so in the next chapter we will see what are the array of
pointers

Day 22:Array of Pointers

So what is a pointer?a pointer is a variable whose value is the address of another variable,the & symbol
which we add before any variable that is & val shows that it is assigned to a pointer variable this
way(pointer p=& val);in this chapter we will see how we will use array in pointersshppose we have an
array which contains 3 elements,int whose val is array-(int val array[3])and we can initialize the array
as,30,40,50,-(int val array[3]={30,40,50})now we can assign this array-(int val array[3]={30,40,50}) to a
pointer(pointer p)in pointer p=& val in the way we have assigned the variable to a pointer
variable(pointer p=& val),and we will declare it as pointer array this way,and we will give size to the
pointer array the same way as we have given size to array-(int *pointer array[3]),now to assign values
from

Array-int val array[3]={30,40,50}) to

Pointer Array-int *pointer array[3]

,we need a for loop to iterate over the array,the

for loop-for (i=0 , i<3 , i++);

The value of should be less than 3 because the the array if of size 3,now to assign values from array to
pointer array we use this”pointer array[ }”inside the curly braces of the for loop

Program:

# include<stdio.h>

# include<stdlib.h>

///a pointer is a variable whose value is the address of the other variable

int main()

int val array[3]={30,40,50};

int * pointer array[3];


for(int i=0 ; i<3 ; i++)

Pointer array [ i ] = & val array [ i ] ;///assign the address of array elements

for(int i=0 ; i<3 ; i++)

Printf (“ value of val array = [ %d ] = % d \n ”, i ,*pointer array [ i ] ) ;

The pointer array i is address of the array variable,we will pass the address one by one,the & value is the
value of the variable that we write after this the value of index [i] increases as the value of i
increases,the array index,this variable(int val array[3])={30,40,50};we are going to access the address of
the variable,which we are going to write after the & symbol(pointer array[i]=& val array,the & val array
is the variable we see in-int val array[3],so we will access the address of variable-val array at index
I,how to accesss arrays using for loop and how to access the values of arrays using for loop,so you know
this concept- Pointer array [ i ] = & val array [ i ] ;of iterating the value of I using for loop,so this for loop
we are going to use to iterate over the array one by one-(the array to be iterated over the)-int val
array[3])={30,40,50},if the index[i} in & val array[i] is 0 we are going to access this value-30 in int val
array[3],if the index[i] is 1 we are going to access this value-40,if the index[i] is 2 we are going to access
this value-50,so what we have done?we have assigned address of variable(& val array[i]) our pointer
array,pointer array[i] is whatever value is there at this index[i] in & val array[i],the address of this & val
array[i],so what we have done here the?we have assigned the address of array elements,once we have
assigned all the elements to the pointer,so now by using this pointe- pointer array[i],we can access the
values of the int val array[3]={30,40,50};how can we do this the?we will once again use a for loop,and in
this for loop we are going to access our values of int val array[3]={30,40,50};using pointer-pointer
array[i],we will access our array like this,printf value of variable array of value of val array at index[i]
which will be equal to the value,so instead of the value I we will use %d, and after that we write %
d,which will give you the value and then we will use I,then we will write the pointer array[i],this is the
address,so after this when we are in the for loop,this-pointer array [ i ] contains the address of the index
which is contained by[i[ so this value-pointer array[i] is this value is the address of the variablepointer
array[i],but if you want to access the value of this address-pointer array[i] which is contained at this
address-pointer array[i],you need to access this dereferencing pointer-* pointer array,when we see the
output we see the

Output:
Value of val array[0]=30

Value of val array[1]=40

Value of val array[2]=50

So you can assign your array to pointer array like this- Pointer array [ i ] = & val array [ i ] ;

To access your array using a pointer array like this- Printf (“ value of val array = [ %d ] = % d \n ”, i
,*pointer array [ i ] )

Day 23:Passing Pointers as Function Arguments

in previous chapter we have seen how to pass pointers and array pointers in c,in this chapter we will see
how to pass pointer as the arguments of function,suppose we want to pass a variable as an argument to
the function which is a pointer,for example I will write a pointer which is a function,take pointer as an
argument,I can return void which is not returning any value and then getValue which is the value of the
pointer,and this the name of the function-void getValue(int),then I am passing an argument,which is the
pointer to this function,*my pointer

Program:

# include <stdio.h>

# include<stdlib.h>

///a pointer is a variable whose value is the address of another variable

void getValue(int * my pointer)

int main()

What I am doing is I am assigning this value my pointer this way,* my pointer=10000,what this is doing
is we are using the

Program:
# include <stdio.h>

# include<stdlib.h>

///a pointer is a variable whose value is the address of another variable

void getValue(int * my pointer)

*my pointer = 10000 ;

return;

int main()

What we are doing is we are using get value to assign,whatever you pass as an argument to this
function- void getValue(int * my pointer) and because we are not returning any value if you want to
return void in c you can write return; after *my pointer=10000,return-which does not return any value
this is that the function end here the after the return,now in order to use our getValue function-void
getValue(int * my pointer) in our main,we will declare variable-int get the value; after the main,we are
not initializing it,consider this function int get the value because we will store at the address of the
variable-int get the value,this value(int get the value)so we will cal a getValue function- void getValue(int
* my pointer)

Program:

# include <stdio.h>

# include<stdlib.h>

///a pointer is a variable whose value is the address of another variable

void getValue(int * my pointer)

*my pointer = 10000 ;/// & get the value=10000

return;

int main()
{

Int get the value;

getValue()

A pointer is a variable whose value is the address of another variable,So this pointer whenever you want
to pass variable-int * my pointer in place of this pointer(int get the value); as argument of this
function(int *my pointer),you can pass address of this varaiable as an argument,whenever we pass an
address(getValue(&) in the front of the variable,it is the address of this variable-getValue & get the
value,so this is a pointer in place of this pointer-void getValue(int *my pointer) you can pass the value of
another variable- getValue ( & getValue ) ;then we will do printf(,the value of get the value,we give %d
and then we will give the value as,get the value

,if we have assigned this variable(& get the value) to the function(getValue & get value) as argument,lets
see the answer and then let us see what is done

Output:

The value of get the value=10000

Now let us see what this function is doing?this function( void getValue( int*my pointer))is taking a
pointer * my pointer,or the address of a variable,so pointer is the address of another variable,so we
have passed another variable-getValue(& get the value)so this argument,get the value- getValue(& get
the value),can be replaced by this value,int*my pointer in the function-(void getValue( int*my
pointer)),the address of this variable-&get the value-getValue(& get the value),so what we have done is
we have indirectly assigned 10000 to the address of this variable(& get the value)

Program:

# include <stdio.h>

# include<stdlib.h>

///a pointer is a variable whose value is the address of another variable

void getValue(int * my pointer)

*my pointer = 10000 ;/// & get the value=10000

return;

}
int main()

Int get the value;

getValue (&get the value);

printf ( “the value of get the value = % d ”,get the value);

So whenever this variable(&get the value)is accessed the value of 10000 is accessed,so whenever you
assign some value to the address(& get the value) and you can access the value at the address (& get
value=10000) this way,but here you not returning any value but this address(&get the value) can store
this value(get the value)using pointers-*my pointer,next thing is that we have seen in the how to use
arrays as an argument of a function,we have seen that you can also pass pointers as an array
argument,how can we pass pointer array as an argument of a function we will return integer value and
we will take the sum of every element of the array,int my array[ ] and this array contains 5 variables and
you can assign these variables as anything,so you want to get,the sum of all the elements and then you
want to get the sum,we will make a function which returns integers and then tell return integer,get sum,
it takes array as an argument,but I have shown you that you can pass arrays in 3 ways as a function of
the argument,1 way,-int array val[ ],or you can define the size of the array[3] ot the third way is that you
can pass array as a pointer-int getSum (int *array val)

Program:

# include <Stdio.h>

#include<stdlib.h>

/// a pointer is a variable whose value is the address of another variable

int getSum (int *array val , int size)

int main()

int my array[5]={10,20,30,40,50}
}

So we are passing this pointer array and we are also defining the size of the array as int size,- int getSum
(int *array val , int size),so first declare avariable which will contain a sum

Program:

# include <Stdio.h>

#include<stdlib.h>

/// a pointer is a variable whose value is the address of another variable

int getSum (int *array val , int size)

Int sum=0;

For (int i=0 ; i<size ; i++)

Sum += array val [i] ;

int main()

int my array[5]={10,20,30,40,50}

Size should be less that whatever we pass the size of the array,here we have size of array is 5 so
i<5,i<size,we have given this example as to how to pass array to a function,then sum+=every index of
the array we are summing,we use this array val from *array val from the int getSum function,so we have
sum, Sum += array val [i] ;we are adding the array val[i] to the sum and once we have all the sum,we
are passing this array-int my array[5]={10,20,30,40,50} as a pointer-int getSum (int *array val , int size)

Program:
# include <Stdio.h>

#include<stdlib.h>

/// a pointer is a variable whose value is the address of another variable

int getSum (int *array val , int size)

Int sum=0;

For (int i=0 ; i<size ; i++)

Sum += array val [i] ;

return sum;

int main()

int my array[5]={10,20,30,40,50};

So we will call this function int my array[5={10,20,30,40,50}] get sum,and here we will also call int,and
because this function get sum will return,sum of the array- int getSum (int *array val , int size) which
we will pass as an argument store it into this variable getsum(),so we will take my arra- int my
array[5]={10,20,30,40,50} as the first argument and the second argument as the size of the array,which
is 5 we can see in the for loop,i<5 and,int getSum function-int size,the size is 5,we will print the value of
my sum=%d,so,int my sum and my sum in print f statement

Program:

# include <Stdio.h>

#include<stdlib.h>
/// a pointer is a variable whose value is the address of another variable

int getSum (int *array val , int size)

Int sum=0;

For (int i=0 ; i<size ; i++)

Sum += array val [i] ;

return sum;

int main()

int my array[5]={10,20,30,40,50};

int mySum = getSum (my array , 5 ) ;

printf(“the value of my sum is = %d , my sum) ;

Output:

The value of my sum=160

Similary we can take the array of [8] elements and take the sum,we see the output sum is 360

Program:

# include <Stdio.h>

#include<stdlib.h>

/// a pointer is a variable whose value is the address of another variable


int getSum (int *array val , int size)

Int sum=0;

For (int i=0 ; i<size ; i++)

Sum += array val [i] ;

return sum;

int main()

int my array[8]={10,20,30,40,50,60,70,80};

int mySum = getSum (my array , 5 ) ;

printf(“the value of my sum is = %d , my sum) ;

Output:

360

This way we see the Array as a function to the pointer

Day 24:String in C
Here we will see what is string? And How string works in c,a string is a single dimensional array of
characters which is terminated by a null character,lets see how we can declare string in array and then
we will see the definition again,so we see that it as a character array,character array you can declare the
string as an array of 6 elements,and then comes the curly braces,inside these curly braces you can
define the initialization to these characters,so this is called a string

char my string [ 6 ] = { ‘ h ’ , ‘ e ’ , ‘ l ’ , ‘ l ’ , ‘ o ’ , ‘\0’ } ;

so string it is the array of characters which is null terminated,so this ‘\0’ is called null terminating ,hello
here is a string,and this string is terminated by a null character so we say that the string is a null
terminated character array, we can also define string as character array of non definite size,int,so we
say-char my other string[ ],and we give our array a non definite size,and you can something this way,so
you can define string in ways in c one way is a definite sized array,or undefined sized array that you can
initialize it like this,so now to print the string you can use print f-my string value is % s,so here we are
using % s to print string,and then we will use this variable and lets see what is the value of the
variable,not lets give a line break to the message we are printing,so that we can print in the next
message and in this line my other string value and we will print the other variable that is the

Program:

# include<stdio.h>

# include<stdlib.h>

Int main()

Char my string [ 6 ] = { ‘ h ’ , ‘ e ’ , ‘ l ’ , ‘ l ’ , ‘ o ’ ,’\0’ } ;

Char my other string [ ] = ”Hello” ;

Printf(“my string value = % s \n”,my string”);

Printf(“my other string value = % s \ n”,my other string”);

Return 0 ;
Output:

my string value= hello

my other string value= hello

this way you can define string in array,there is another header file in c called string,which you can add to
the main.c and then you can use some function from this header file,you can do some different
functionality using this header file

Program:

# include<stdio.h>

# include<stdlib.h>

# include<string.h>

Int main()

Char my string [ 6 ] = { ‘ h ’ , ‘ e ’ , ‘ l ’ , ‘ l ’ , ‘ o ’ ,’\0’ } ;

Char my other string [ ] = ”Hello” ;

Printf(“my string value = % s \n”,my string”);

Printf(“my other string value = % s \ n”,my other string”);

Return 0 ;

Now the first function we will use from string header file is string copy,that is strcpy

Program:
# include<stdio.h>

# include<stdlib.h>

# include<string.h>

Int main()

Char my string [ 6 ] = { ‘ h ’ , ‘ e ’ , ‘ l ’ , ‘ l ’ , ‘ o ’ ,’\0’ } ;

Char my other string [ ] = ”Hello” ;

Printf(“my string value = % s \n”,my string”);

Printf(“my other string value = % s \ n”,my other string”);

strcpy

Return 0 ;

Strcpy enables us to copy one string to another,we will remove this statement- Char my string [ 6 ] = { ‘
h ’ , ‘ e ’ , ‘ l ’ , ‘ l ’ , ‘ o ’ ,’\0’ } ;we will remove this statement- Printf(“my string value =
% s \n”,my string”); and we will remove this statement also for now- Printf(“my other string value = %
s \ n”,my other string”); and we will replace-char my other string[ ],by char string1[ ] and add the size of
the string as [13],we keep the “Hello” by suppose we have as string 1 of size 13,char string[13]=”Hello”
,we will declare the second string of size 13,and we will declare a third string of size 13 but we are not
initializing it,and now by using the string,now by using the file in string header we can use the strcpy
which takes arguments,one is destination and the other is source

Program:

# include<stdio.h>

# include<stdlib.h>

# include<string.h>
Int main()

char string1 [ 13 ] = ”Hello” ;

char string2 [ 13 ] = “World”;

char string3 [ 13 ];

Strcpy ( string3 , string1 ) ;///strcpy(dest,source

Return 0 ;

Suppose I want to copy string 1 to string 3 so this string copy function-strcpy,copies string 1 to string 3 in
this way,so you take a variable which you want to copy as the first argument of strcpy,the second
argument is the variable whose value you want to copy,in you can see this as strcpy is used to copy
destination from source,so we want to copy to the destination from the source,and destination is
string3 to which we want to copy,now there is another function in string header file called strcat,it is
called string concadenation,it is used to concadenate the strings,suppose you want to concadenate
string 1 and string 2,we want to concadenate firstr string and we want to join the second string we can
do this this way,strcat(string1,string2);so when you hover over this strcat-this says that this also can
return some value

Program:

# include<stdio.h>

# include<stdlib.h>

# include<string.h>

Int main()

char string1 [ 13 ] = ”Hello” ;

char string2 [ 13 ] = “World”;

char string3 [ 13 ];
strcpy ( string3 , string1 ) ;///strcpy(dest,source

strcat ( string1 , string2);///add string 2 to string 1

Return 0 ;

Now there is another function in this heard string.h,that is called strlen-length of the string,length is the
number of character = ”Hello”,or the size of this character array-char string1 [ 13 ] for example,string
length

Program:

# include<stdio.h>

# include<stdlib.h>

# include<string.h>

Int main()

char string1 [ 13 ] = ”Hello” ;

char string2 [ 13 ] = “World”;

char string3 [ 13 ];

strcpy ( string3 , string1 ) ;///strcpy(dest,source

strcat ( string1 , string2);///add string 2 to string 1

int length string1 = strlen (string1) ;

Return 0 ;

So this string 1 in this string,Returns the string of the variable which you pass in as an argument-
strlen(string1) ,so let us print all these values,the printf is the result of string copy(strcpy),that is
printf(“ strcpy = % s \ n “, string 3); that is copying into string 3 from string 1 using this function,strcpy,
printf (“ strcat = %s \ n ”,string 1); and now in the same way we will see how stringcat works and we will
see the value of string 1,because we are adding the value of string 1 to string 2 and we will also see
string length printf(“ strlen = % d \ n” ,length string 1);

Program:

# include<stdio.h>

# include<stdlib.h>

# include<string.h>

Int main()

char string1 [ 13 ] = ”Hello” ;

char string2 [ 13 ] = “World”;

char string3 [ 13 ];

strcpy ( string3 , string1 ) ;///strcpy(dest,source

strcat ( string1 , string2);///add string 2 to string 1

int length string1 = strlen (string1) ;

printf(“ strcpy = % s \ n “, string 3);

printf (“ strcat = %s \ n ”,string 1);

printf(“ strlen = % d \ n” ,length string 1);

Return 0 ;

We run this program and we see the output as


Strcpy=Hello

Strcat=Helloworld

Strlen=11

the first value of the output is true,Hello,because we have successfully copied the value of string 1 to
string 3,the output of strcat has concatenated string 1 and string 2,that is it has added string 1 hello to
stirng 2 world,and the output of strlen is 11,because total letters in helloworld is 11,so the string length
is 11,suppose we want to see the size of string 3 we compile and we see strlen as 5 because hello string
length is 5,there are other functions in the header file such as string compare and some other function
to compare two strings you can use strcmpy which compares the two strings,

int value = strcmp (string 1,string 2);

printf(“ strcmp = % d \ n ” , value );

return 0 ;

so this <string.h>header file is very important whenever you are working with string files

Day 27:unions in c

Unions in c are similar to Structures in c structures,structures,and then you can give any name to the
union like union myUnion{ and then comes the curly braces,and then semi colon and then inside the
curly braces comes the variables like you define in structures

If you know whenever we declare a variable it has some size,integer has size of 5 byte and character has
size of 1 byte,we are defining union so let us also define the union type also UNI and then we can use
uni. To give values to the my int variable uni.my int=5 and uni.myChar=8;

program:

# include<stdlib.h>

#include<string.h>

/*struct myStruct {

int my int;///5
char myChar;///1

} record;*/

union myUnion{

int myInt;///5

char my char;///1

};

Int main()

Return 0;

program:

# include<stdlib.h>

#include<string.h>

/*struct myStruct {

int my int;///5

char myChar;///1

} record;*/

union myUnion{

int myInt;///5

char my char;///1

}; UNI;

Int main()

UNI uni;

uni.myInt=5;

uni.myChar=8;
Return 0;

When we compile the program we see error

We should write in the Int main() instead of UNI uni; union myUnion uni;

Now we compile it compiles

Now let us print the value of int and char

Printf(“% i\n”,uni.mychar);

But in union it is different,whenever you declare a union it has a size of variable whose size is
maximum,so the maximum value of this variable inside the union,union myUnion is 5 of integer which is
greater than char 1,so we allocate the value of integer 5,so it allocates the size to itself from those
variable whose size is maximum

program:

# include<stdlib.h>

#include<string.h>

/*struct myStruct {

int my int;///5

char myChar;///1

} record;*/

union myUnion{

int myInt;///5

char my char;///1

};

Int main()

union myUnion uni;

uni.myInt=5;
uni.myChar=8;

Printf(“% i\n”,uni.mychar);

Return 0;

We see output :

Since we have declared the value of character as 8

But if we use int here we get the same result 8 but we have given int value

Printf(“% i\n”,uni.myint);

So what is happening is Whenever you define a structure like this,

struct myStruct {

int my int;///5

char myChar;///1

} record;

it allocates its variable or member variable,to some memory space,suppose you have defined my int and
my char in struct myStruct it allocates 5 bytes of space for integer in some memory space on the
computer and it allocates 1 byte ,in some memory space on the computer

structure

Int char

union

char=8
Int=5

In this case it allocates 5 bytes of space from int and I byte of space for char in the same box,members
inside this union shares some space,or memory size,but in case of structures they are allocated some
other space for each,and have their, place for sizes so when you allocate 5 bytes then first in prints 5 for
int and then,when you specify char 8 it will be overwritten by 8 bytes so you get the output as 8,integer
overwriting 9,structure allocates different memory location to its member variables,but union allocates
same memory location to the member variables and the size of the union is the maximum size of the
variable inside,so if int has maximum size of 5,it will allocate itself value 5,let us calculate the size of
structure and union,

Printf(“ % d \ n ”, sizeof record) );

Printf(“ % d \ n ” size of uni) );

The name of structure is record,name for union is union my union uni-uni

Printf(“ the size of struct % d \ n ”, sizeof record) );

Printf(“ the size of union % d \ n ” size of uni) );

program:

# include<stdlib.h>

#include<string.h>

/*struct myStruct {

int my int;///5

char myChar;///1

} record;*/

union myUnion{

int myInt;///5

char my char;///1

};

Int main()
{

union myUnion uni;

uni.myInt=5;

uni.myChar=8;

Printf(“ the size of struct % d \ n ”, sizeof record) );

Printf(“ the size of union % d \ n ” size of uni) );

Return 0;

Output:

The size of struct=8

The size of union=5

But here you might be seeing the size of the maximum value is of int that is 5 bytes and value of char is 1
so where the value of 8 comes from in size of struct,whenever you provide some member variable to
the structure it calculates the memory space size by adding the size of these variables char and integer
that is 1 and 5 and plus it adds the padding so that it can become the multiple of 4 or 8 and

/*struct myStruct {

int my int;///4

char myChar;///1+5

} record;*/

So structure provides some padding so that the size becomes 8 instead of 5

But the size of union is 5 since it takes the value that has maximum value,so the maximum value here if
of integer that is 5.but if union overwrites the memory then where can it be used,it can be
used,structures are used where separate memory has to allocated to this.
Day 27: (struct) Structures in C

We will see what are structures in c and how can we use structures in c

Structures are the collection of different variables which are clustered together to make some logical
cluster,

suppose we want to make a cluster of student,there is no direct variable,to define the attributes of c
and in the end when we have so many variables we can miss to see which variables are assigned to
which student so structures,can be defined when you have a cluster of variables,to which you want to
bind to a logical cluster,so we will see how we can define structures in c,so we can define the structures
as :struct keyword and then the name of the structure(struct student) and then comes the curly braces
which we can use to define the keyword,so what attributes a student can have,a student can have his
id,other attribute can be his name

program:

# include<stdio.h>

# include<stdlib.h>

# include<string.h>

struct student {

};

Int main()

Return 0;

program:
# include<stdio.h>

# include<stdlib.h>

# include<string.h>

struct student {

int id;

char name[22];

float percentage;

};

Int main()

Return 0;

So for each student we can define different instance of a structure,we can use structures to make a logic

cluster of these 3 variables

int id;

char name[22];

float percentage;

for these for a particular student,you can define it in your main function,so whenever you call a
function inside a structure you can call it inside a main function and how can you call it,you can copy it
inside a main function like this,you can copy struct keyword student inside the main function,suppose
you give the student structure name record,the student record for student 1 for example:

int main()

struct student record 1;

return 0;

program:
# include<stdio.h>

# include<stdlib.h>

# include<string.h>

struct student {

int id;

char name[22];

float percentage;

};

int main()

struct student record 1;

return 0;

So this is a variable you define for student structure(struct student record 1),the student can be treated
as a data type and the struct is the keyword which tells,the student data type this is structure,think it
this is your variable(record 1),so whenever you assign some value to this,record 1 which is the instance
to this student structure(struct student record 1),instance is 1 part of this student(struct student record
1),so we take record 1,we want to assign id to this record,you can see member variables of this student
record when you do record 1.(the choices it will show on the screen are
id:int,name:char,percentage:float),so these id,name and percentage are called the members of the
structure and you can call them using record 1.(the . separator after this variable- struct student record
1);so we have record.id=1;we have assigned 1 for the student and we can assign name for this
record1.name,in the last chapter we saw what are strings,we saw how we can copy string to a character
array,or you can assign or copy the string which is so the first argument of the string that is the
destination of the string where you want to assign the value strcpy,we have done is we have assigned
john to this name value variable (record 1.name),and we also assign record1.percentage once you assign
these values to the instance of the structure you can access these values using print function,you can
call id using print f statement,before that these are the structure for the student micheal

record 1.id=1

strcpy(record 1.name,”Micheal);

record 1.percentage=89.8;
program:

# include<stdio.h>

# include<stdlib.h>

# include<string.h>

struct student {

int id;

char name[22];

float percentage;

};

int main()

struct student record 1;

record 1.id=1;

strcpy(record 1.name,”Micheal);

record 1.percentage=89.8;

return 0;

and then we use

print f(“id = % d Name=%s Percent=%f” \n

record 1.id, record 1.name record 1.percentage,)

so % d for holding the decimal value,% s for holding the string value,and % f for holding the float values)

we will also give line break \ after that and then we can access the values assigned same as we have
assigned the values in the print f

record 1.id=1 will give the id


record 1.name will give the name

record 1.percentage will give the percentage

program:

# include<stdio.h>

# include<stdlib.h>

# include<string.h>

struct student {

int id;

char name[22];

float percentage;

};

int main()

struct student record 1;

record 1.id=1;

strcpy(record 1.name,”Micheal);

record 1.percentage=89.8;

print f(“id = % d Name=%s Percent=%f” \n

record 1.id, record 1.name record 1.percentage,)

return 0;

output:

id=1 Name=Micheal Percent=89.7999999


in the same way you can define different instance of the student structure and you can maintain the
record for the student,for example you maintain some different

program:

# include<stdio.h>

# include<stdlib.h>

# include<string.h>

struct student {

int id;

char name[22];

float percentage;

};

int main()

struct student record 1;

record 1.id=1;

strcpy(record 1.name,”Peter);

record 1.percentage=91.8;

print f(“id = % d Name=%s Percent=%f” \n

record 1.id, record 1.name record 1.percentage,)

struct student record 2;

record 2.id=2;

strcpy(record 2.name,”);
record 2.percentage=83.8;

print f(“id = % d Name=%s Percent=%f” \n

record 2.id, record 2.name record 2.percentage,)

struct student record 3;

record 3.id=3;

strcpy(record 3.name,”Micheal);

record 3.percentage=91.8;

print f(“id = % d Name=%s Percent=%f” \n

record 3.id, record 3.name record 3.percentage,)

return 0;

output:

id=1 Name=Peter Percent=89.7999999

id=2 Name= Percent=83.7999999

id=3 Name=Micheal Percent=91.7999999

so can define as many instance of the structure you want and you can create record for the student,so
this is a way of using structure in c ,there is another way of using structure in c,that is by directly giving
name to the student structure,we can delete this line struct student record 2 and instead of giving the
name to this line of student variable here we can just give the name and the declaration to the student
structure,we can just give the name to the record,and then this record will be the variable name,so the
student is the structure and record will be the variable name of the structure,we will use record to
access the value or assign the value to this record

program:
# include<stdio.h>

# include<stdlib.h>

# include<string.h>

struct student {

int id;

char name[22];

float percentage;

record;

};

int main()

struct student record 1;

record .id=1;

strcpy(record .name,”Micheal);

record 1.percentage=89.8;

print f(“id = % d Name=%s Percent=%f” \n,

record .id, record .name record .percentage,)

return 0;

thus we see the output as

id=1 name=Micheal Percentage=89.79999999

You might also like