Lec 1-5
Lec 1-5
(Lesson 1 to 5)
LECTURE 1 :-
Introduction to Programming,
Introduction to C Programming,
Algorithm
programming language contains all the features of these languages and many more additional
concepts that make it unique from other languages.
‘C’ is a powerful programming language which is strongly associated with the UNIX
operating system. Even most of the UNIX operating system is coded in ‘C’. Initially ‘C’
programming was limited to the UNIX operating system, but as it started spreading around
the world, it became commercial, and many compilers were released for cross-platform
systems. Today ‘C’ runs under a variety of operating systems and hardware platforms. As it
started evolving many different versions of the language were released. At times it became
difficult for the developers to keep up with the latest version as the systems were running
under the older versions. To assure that ‘C’ language will remain standard, American
National Standards Institute (ANSI) defined a commercial standard for ‘C’ language in 1989.
Later, it was approved by the International Standards Organization (ISO) in 1990. ‘C’
programming language is also called as ‘ANSI C’.
C Basic Commands
Following are the basic commands in C programming language:
C Basic commands Explanation
This command includes standard input output header file(stdio.h) from the C libr
#include <stdio.h>
C program
int main() It is the main function from where C program execution begins.
{ Indicates the beginning of the main function.
Whatever written inside this command “/* */” inside a C program, it will
/*_some_comments_*/
compilation and execution.
printf(“Hello_World!
This command prints the output on the screen.
“);
getch(); This command is used for any character input from keyboard.
return 0; This command is used to terminate a C program (main function) and it returns 0.
} It is used to indicate the end of the main function.
Where is C used? Key Applications
1. ‘C’ language is widely used in embedded systems.
4. Most of the applications by Adobe are developed using ‘C’ programming language.
5. It is used for developing browsers and their extensions. Google’s Chromium is built
using ‘C’ programming language.
6. It is used to develop databases. MySQL is the most popular database software which is
built using ‘C’.
Nowadays, various compilers are available online, and you can use any of those compilers.
The functionality will never differ and most of the compilers will provide the features
required to execute both ‘C’ and ‘C++’ programs.
Following is the list of popular compilers available online:
Clang compiler
MinGW compiler (Minimalist GNU for Windows)
Portable ‘C’ compiler
Turbo C
The main features of the C language include:
General Purpose and Portable
Low-level Memory Access
Fast Speed
Clean Syntax
These features make the C language suitable for system programming like an operating
system or compiler development.
Why Should We Learn C?
Many later languages have borrowed syntax/features directly or indirectly from the C
language. Like syntax of Java, PHP, JavaScript, and many other languages are mainly based
on the C language. C++ is nearly a superset of C language (Only a few programs may
compile in C, but not in C++).
So, if a person learns C programming first, it will help him to learn any modern
programming language as well. As learning C help to understand a lot of the underlying
architecture of the operating system. Like pointers, working with memory locations, etc.
Beginning with C programming:
#include <stdio.h>
intmain() {
inta = 10;
printf("%d", a);
return0;}
Output
10
Let us analyze the structure of our program line by line.
ALGORITHM
What is an Algorithm?
In computer programming terms, an algorithm is a set of well-defined instructions to solve a
particular problem. It takes a set of input(s) and produces the desired output. For example,
An algorithm to add two numbers:
1. Take two number inputs
Algorithms should be most effective among many different ways to solve a problem.
An algorithm shouldn't include computer code. Instead, the algorithm should be written
in such a way that it can be used in different programming languages.
Algorithm in C language
There are many sorting algorithms, each with advantages and disadvantages. The most
common sorting algorithms in C are quick sort, merge, and sort.One of the key features of C is
pointer support. This allows efficient manipulation of data structures such as arrays, queues
etc. This makes it suitable for implementing algorithms that require complex data
manipulation, such as sorting and algorithmic searching.One of the famous examples of
software library implemented in C is the Standard Template Library (STL). This library
provides a wide variety of algorithms for tasks such as sorting, searching, and manipulating
data structures.
o Finiteness: The algorithm requires a limited steps. It means that it should be exited
after executing a certain number of commands.
o Validity: The algorithm must be valid. In other words, it should be able to produce a
solution to the problem that the algorithm is designed to solve in a reasonable amount
of time.
5. Sorting Algorithm:
Sorting is arranging a group of data in a particular manner according to the requirement. The
algorithms which help in performing this function are called sorting algorithms. Generally
sorting algorithms are used to sort groups of data in an increasing or decreasing manner.
6. Hashing Algorithm:
Hashing algorithms work similarly to the searching algorithm. But they contain an index with
a key ID. In hashing, a key is assigned to specific data.
7. Divide and Conquer Algorithm:
This algorithm breaks a problem into sub-problems, solves a single sub-problem, and merges
the solutions to get the final solution. It consists of the following three steps:
Divide
Solve
Combine
8. Greedy Algorithm:
In this type of algorithm, the solution is built part by part. The solution for the next part is
built based on the immediate benefit of the next part. The one solution that gives the most
benefit will be chosen as the solution for the next part.
9. Dynamic Programming Algorithm:
This algorithm uses the concept of using the already found solution to avoid repetitive
calculation of the same part of the problem. It divides the problem into smaller overlapping
subproblems and solves them.
10. Randomized Algorithm:
In the randomized algorithm, we use a random number so it gives immediate benefit. The
random number helps in deciding the expected outcome.
To learn more about the types of algorithms refer to the article about “Types of
Algorithms“.
Advantages of Algorithms:
It is easy to understand.
An algorithm is a step-wise representation of a solution to a given problem.
In an Algorithm the problem is broken down into smaller pieces or steps hence, it is
easier for the programmer to convert it into an actual program.
Disadvantages of Algorithms:
Writing an algorithm takes a long time so it is time-consuming.
Understanding complex logic through algorithms can be very difficult.
Branching and Looping statements are difficult to show in Algorithms(imp).
How to Design an Algorithm?
To write an algorithm, the following things are needed as a pre-requisite:
1. The problem that is to be solved by this algorithm i.e. clear problem definition.
2. The constraints of the problem must be considered while solving the problem.
3. The input to be taken to solve the problem.
4. The output is to be expected when the problem is solved.
5. The solution to this problem is within the given constraints.
Then the algorithm is written with the help of the above parameters such that it solves the
problem.
Example: Consider the example to add three numbers and print the sum.
Step 1: Fulfilling the pre-requisites
Step 1: Start
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 6: Stop
Step 1: Start
Step 4: If a > b
If a > c
Else
Else
If b > c
Else
Step 5: Stop
Step 1: Start
D ← b2-4ac
Step 4: If D ≥ 0
r1 ← (-b+√D)/2a
r2 ← (-b-√D)/2a
Else
rp ← -b/2a
ip ← √(-D)/2a
Step 5: Stop
Step 1: Start
factorial ← 1
i←1
5.2: i ← i+1
Step 7: Stop
Step 1: Start
flag ← 1
i←2
flag ← 0
Go to step 6
5.2 i ← i+1
Step 6: If flag = 0
else
Display n is prime
Step 7: Stop
QUESTIONS
LECTURE 2 :-
Installation of IDE Turbo C++, Basic
Structure of C programming
C program follows a particular structure. So first, let us see the structure of a C program.
1 #include<stdio.h> Header
3 {
5 return 0; Return
6 {
The first and most crucial element that must be included in a C program is the header files.
The term "header file" refers to a file with the extension ".h" that contains shared C function
declarations and macro definitions. A preprocessor program, which is a program called by
the compiler, processes all lines that begin with #. The preprocessor copies the preprocessed
code from <stdio.h> to our file in the example above. In C, the .h files are referred to as
header files.
A C program's main() function declaration comes next. At the beginning of the main()
function, a door is opened into which the program can enter, and the execution of the program
begins. The main doesn't take any parameters, as indicated by the empty brackets. The return
type of main() is indicated by the int that was written before the main. The value of the main
function, which represents the stage of program dismissal, is returned.
The next part of a basic program is the statements that are a component of a function in a C
program and are referred to as the function's body. Any modifications, searches, sorting, etc.,
are all permissible. A pair of curly brackets represent the body of a function. Note that every
separate function must start with and terminate with curly brackets.
Statements are forwarded to the compiler as instructions. A semicolon (;) always marks the
end of a sentence in C. Within this particular occurrence, the task of relaying to the compiler
and its associated hardware that a certain text should be made visible on a screen is
accomplished with the use of what's called the printf function, i.e., printf() function. The
textual content specified for display here happens to be "Hello World".
The return statement is the final component of any C function. The return statement makes
reference to the values that a function returns. The function's return type determines the return
value and this return statement. In our program, the value from main() is returned via the
return statement, which here is the statement return 0. An operating system might use the
value that was returned to determine if your program will be terminated. Usually, 0 indicates a
successful ending.
Compiling A Program In C
1. Download a complete IDE that includes a C language compiler, such as Turbo C++,
Microsoft Visual C++, or DevC++.
2. Alternately, you can download the C compiler separately, change the program files using
any text editor, and then run the C program via the command line.
Steps To Run Hello World Program In C With An IDE (Like Turbo C++)
1. Launch the Turbo C IDE (Integrated Development Environment), then select File and then
click New.
2. Write a program called Hello World. (As shown below, remember to add <conio.h> header
file and getch(). In the conio.h header file, there is a non-standard library function called
getch() for the C computer language. MS-DOS compilers such as Turbo C mostly use it.)
3. To compile the code, either click on the Compile menu's Compile option or press Alt + F9.
4. In order to execute the code, you may opt to click on the 'Run' feature or simply hit Ctrl +
F9. Note that C programs are first compiled to create object code, which is then executed.
#include <stdio.h>
int main()
{
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}
Output
Hello, World!
The #include is a preprocessor command that tells the compiler to include the contents
of stdio.h (standard input and output) file in the program.
The stdio.h file contains functions such as scanf() and printf() to take input and display output
respectively.
If you use the printf() function without writing #include <stdio.h>, the program will not
compile.
The execution of a C program starts from the main() function.
printf() is a library function to send formatted output to the screen. In this
program, printf() displays Hello, World! text on the screen.
The return 0; statement is the "Exit status" of the program. In simple terms, the program ends
with this statement.
Questions:
1) How to install IDE-Turbo C++ .
2) What are preprocessor directives in c.
3) Explain compilation process in c.
4) What functionality perform by main() in c.
5) What is the work of stdio.h in c
LECTURE 3 :-
Input Functions/Output Functions:
Printf(), scanf()
Input means to provide the program with some data to be used in it.
Output means to display data on the screen or write the data to a printer or a file.
The C programming provides standard library functions to read any given input and
display output on the console.
Before moving forward with input and output in C language, check these topics to understand
the concept better:
C Syntax Rules
Compile and Run C Program
Data Types in C
Variables in C
While dealing with input-output operations in C, we use the following two streams:
#include <stdio.h>
C language offers us several built-in functions for performing input/output operations. The
following are the functions used for standard input and output:
In C Language, output devices like computer monitors, printers, etc. are treated as files and the
same process is followed to write output to these devices as would have been followed to write
the output to a file.
Copy
It writes the C string pointed by the format pointer to the standard output (stdout).
On success, the total number of characters written is returned.
This function is used to print a simple text sentence or value of any variable which
can be of int, char, float, or any other datatype.
1. Print a sentence
#include <stdio.h>
int main() {
// using printf()
printf("Welcome to bansal");
return 0;
Welcome to bansal
To understand the complete code and structure of a basic C language program, check Hello
World Program in C.
We can use the printf() function to print an integer value coming from a variable using
the %d format specifier.
For example,
#include <stdio.h>
int main() {
int x = 10;
// using printf()
return 0;
Value of x is: 10
In the program, above we have used the %d format specifier, to specify the type of
value that will be added there.
The format specifiers %d and %i are used for integer values.
The %c format specifier is used to print character variable values using the printf() function.
#include <stdio.h>
int main() {
// using printf()
return 0;
In the code example below, we have used the printf() function to print values of
a float and double type variable.
For float value we use the %f format specifier and for double value we use the %lf format
specifier.
#include <stdio.h>
int main() {
// using printf()
return 0;
We have used the \n Escape sequence which is used for a newline at the end of the
first printf() statement so that the next printf() statement output is shown in the next line.
#include <stdio.h>
int main() {
return 0;
As you can see in the code example above, we can do the formatting and print values of
multiple variables using the printf() function.
We can also perform some simple calculations inside printf(). Here is a simple example of that,
#include <stdio.h>
int main()
int a = 5, b = 6;
printf("%d", a + b);
return 0;
11
Format Specifiers
To print values of different data types using the printf() statement and while taking
input using the scanf() function, it is mandatory to use format specifiers.
It is a way to tell the compiler what type of data is in a variable.
Some examples are %c, %d, %f, etc.
Int %d, %i
Char %c
Float %f
Double %lf
unsigned int %u
signed char %c
unsigned char %c
When we want to take input from the user, we use the scanf() function and store the input value
into a variable.
It reads data from stdin and stores it according to the parameter format into the
locations pointed by the additional arguments.
On success, the function returns the number of items of the argument
list successfully filled.
The scanf() function can be used to take input of any type from the user.
All you have to take care of is that the variable in which you store the value should
have the same data type.
scanf("%x", &variable);
Using the format specifier, we tell the compiler what type of data to expect from the
user.
The & is the address operator which tells the compiler the address of the variable so
that the compiler can store the user input value at that address.
If we have to take an integer value input from the user, we have to define an integer variable
and then use the scanf() function.
#include <stdio.h>
int main() {
// using scanf()
int user_input;
scanf("%d", &user_input);
return 0;
You entered: 7
NOTE: If you use our compiler, then while running the code example above, there is a button
for Input at the top-right corner of the editor, you can click on it and provide custom value for
input.
Just like integer value, we can take input for any different datatype. Let's see an example
of float type value.
#include <stdio.h>
int main() {
// using scanf()
float user_input;
scanf("%f", &user_input);
return 0;
We have used the %f format specifier and defined a float type variable.
Try doing the same for taking a double type value as user input.
The format specifier for double is %lf.
Let's see how we can take a simple character input from the user.
#include <stdio.h>
int main() {
// using scanf()
char gender;
scanf("%c", &gender);
return 0;
Your gender: M
In the below code example, we are taking multiple inputs from the user and saving them into
different variables.
#include <stdio.h>
int main() {
char gender;
int age;
return 0;
int i = printf("bansal");
bansalValue of i is: 6
Question:
1) Give the syntax and examples of usage of scanf() and printf().
2) What will the given code result in printf(“\n you are\”awesome \" ");?
3) Use a printf statement to print out the text string "Welcome", followed by a
newline.
4) Use a printf statement to print out the float variable discount?
5) Use a printf statement to print out the value of the integer variable sum?
6) Use a scanf statement to read a single character from the keyboard into the
variable operator. Skip leading blanks, tabs and newline characters.
LECTURE 4 :-
Discussion on Format Specifiers &
Escape Sequence
Format specifiers in C
In C programming language, values can be type integer, floating-point, single character, or
sequence of characters. We use format specifiers in C to display values of a variable of a
different type. C contains different format specifiers used in printf() and scanf() functions; in
this tutorial, we will go through a few important and most commonly used format specifiers in
our C programs.
Format specifiers in C are used to take inputs and print the output of a type. The symbol we use
in every format specifier is %. Format specifiers tell the compiler about the type of data that
must be given or input and the type of data that must be printed on the screen. Now that you
have a brief idea of format specifiers in C, we have a few lists of format specifiers moving
ahead.
%c Signed character
%f Signed float
%e A floating-point number
%lf Double
%o Octal integer
%x Hexadecimal integer
We will go through a few examples that will help you understand how to use format specifiers
in printf() and scanf() functions for a better understanding.
Escape Sequence in C
The escape sequence in C is the characters or the sequence of characters that can be used
inside the string literal. The purpose of the escape sequence is to represent the characters that
cannot be used normally using the keyboard. Some escape sequence characters are the part of
ASCII charset but some are not.
Different escape sequences represent different characters but the output is dependent on the
compiler you are using.
Escape sequences are used in the programming languages C and C++, and their design was
copied in many other languages such as Java, PHP, C#, etc. An escape sequence is a
sequence of characters that does not represent itself when used inside a character or
string literal, but is translated into another character or a sequence of characters that may be
difficult or impossible to represent directly.
In C, all escape sequences consist of two or more characters, the first of which is the
backslash, \ (called the "Escape character"); the remaining characters determine the
interpretation of the escape sequence. For example, \n is an escape sequence that denotes
a newline character.
Suppose we want to print out Hello, on one line, followed by world! on the next line. One
could attempt to represent the string to be printed as a single literal as follows:
#include <stdio.h>
int main() {
printf("Hello,
world!");
return 0;
}
This is not valid in C, since a string literal may not span multiple logical source lines. This
can be worked around by printing the newline character using its numerical value
(0x0A in ASCII),
#include <stdio.h>
int main() {
printf("Hello,%cworld!", 0x0A);
return 0;
}
This instructs the program to print Hello,, followed by the byte whose numerical value
is 0x0A, followed by world!. While this will indeed work when the machine uses the ASCII
encoding, it will not work on systems that use other encodings, that have a different
numerical value for the newline character. It is also not a good solution because it still does
not allow to represent a newline character inside a literal, and instead takes advantage of the
semantics of printf. In order to solve these problems and ensure maximum portability
between systems, C interprets \n inside a literal as a newline character, whatever that may be
on the target system:
#include <stdio.h>
int main() {
printf("Hello,\nworld!");
return 0;
}
In this code, the escape sequence \n does not stand for a backslash followed by the letter n,
because the backslash causes an "escape" from the normal way characters are interpreted by
the compiler. After seeing the backslash, the compiler expects another character to complete
the escape sequence, and then translates the escape sequence into the bytes it is intended to
represent. Thus, "Hello,\nworld!" represents a string with an embedded newline, regardless
of whether it is used inside printf or anywhere else. This raises the issue of how to represent
an actual backslash inside a literal. This is done by using the escape sequence \\, Some
languages don't have escape sequences, for example Pascal. Instead a command including a
newline would be used (writeln includes a newline, write excludes it).
writeln('Hello');
write('world!');
Escape Sequence List
The table below lists some common escape sequences in C language.
Escape
Sequence Name Description
\n New Line It moves the cursor to the start of the next line.
\r Carriage Return It moves the cursor to the start of the current line.
Hexadecimal
\xhh It represents the hexadecimal number.
Number
Out of all these escape sequences, \n and \0 are used the most. In fact, escape sequences like \
f, \a, are not even used by programmers nowadays.
QUESTIONS
Q.1 : What is format specifier in C ? Why do we use format specifiers ?
Q. 2 : What is character and integer format specifier? Explain with an example.
Q. 3: What are the escape sequences in C programming?
Q.4 : Give an example of an escape sequence in C. What is the difference between \n and \r ?
Q.5 : How to use printf and scanf in C programming ? Make a program of addition of 2
numbers.
LECTURE 5:
Keywords, Various Types of Constant
Keywords are the predefined words whose meaning has already been explained
to the C compiler and their meanings cannot be changed.
name, etc. There are only 32 reserved words (keywords) in the C language.
For example:
int money;
Here, int is a keyword that indicates is a variableof money type int (integer).
A list of 32 keywords in the c language is given below:
Thekeywordsarealsocalled‘Reservedwords’.
Keywordsserveas basicbuildingblocksforprogramstatements.
Keywordscanbe usedonlyfortheirintendedpurpose.
Keywordscannotbeusedasuser-defined variables.
Allkeywordsmust bewritteninlowercase.
32keywordsavailableinC.
Literals:-
Constantsrefertofixedvaluesthattheprogram
maynotalterduringitsexecution.Thesefixedv
aluesarealsocalledliterals.
Hencetheliteralsrepresentthefixedvaluesthatcannotbem
odified.
Forexample:1,2.5,’c’etc.Here,1,2.5and'c'areliterals.
TypesofLiterals:-
IntegerLiterals
Anintegerisanumeicliteral(associatedwithnumbers)withoutanyfractionalorexponentialpa
rt
Forexample:1.2,+9.0, -4.5
CharacterLiterals
StringLiterals
1. What is keyword in C?
2. What is constant in C?
3. What are different types of constants in C language?
4. Write a program in C to define a symbolic constant.
5. Write a program in C to declare a constant using const keyword.