0% found this document useful (0 votes)
34 views48 pages

Lec 1-5

Uploaded by

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

Lec 1-5

Uploaded by

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

Sushila Devi Bansal College

An Institute of Bansal Group of Institutes


Umaria, A. B. Road, Near Rau, Indore - 453331

Module 3rd sem (CSE/IT/ECE)


(Autumn Season – July-2023 to Dec-2023)
C Programming Programming
Notes and Questions
Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 1
Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

(Lesson 1 to 5)

LECTURE 1 :-

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 2


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

Introduction to Programming,
Introduction to C Programming,
Algorithm

Introduction to Programming, Introduction to C programming, Algorithms.


The base or father of programming languages is ‘ALGOL.’ It was first introduced in 1960.
‘ALGOL’ was used on a large basis in European countries. ‘ALGOL’ introduced the concept
of structured programming to the developer community. In 1967, a new computer
programming language was announced called as ‘BCPL’ which stands for Basic Combined
Programming Language. BCPL was designed and developed by Martin Richards, especially
for writing system software. This was the era of programming languages. Just after three years,
in 1970 a new programming language called ‘B’ was introduced by Ken Thompson that
contained multiple features of ‘BCPL.’ This programming language was created using UNIX
operating system at AT&T and Bell Laboratories. Both the ‘BCPL’ and ‘B’ were system
programming languages.C is a procedural programming language initially developed by
Dennis Ritchie in the year 1972 at Bell Laboratories of AT&T Labs. It was mainly developed
as a system programming language to write the UNIX operating system.In 1972, a great
computer scientist Dennis Ritchie created a new programming language called ‘C’ at the Bell
Laboratories. It was created from ‘ALGOL’, ‘BCPL’ and ‘B’ programming languages. ‘C’

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 3


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

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.

2. It is used for developing system applications.

3. It is widely used for developing desktop applications.

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’.

7. It is used in developing an operating system. Operating systems such as Apple’s OS X,


Microsoft’s Windows, and Symbian are developed using ‘C’ language. It is used for
developing desktop as well as mobile phone’s operating system.

8. It is used for compiler production.

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 4


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

9. It is widely used in IOT applications.

Why learn C Language?


As we studied earlier, ‘C’ is a base language for many programming languages. So, learning
‘C’ as the main language will play an important role while studying other programming
languages. It shares the same concepts such as data types, operators, control statements and
many more. ‘C’ can be used widely in various applications. It is a simple language and
provides faster execution. There are many jobs available for a ‘C’ developer in the current
market.
‘C’ is a structured programming language in which program is divided into various modules.
Each module can be written separately and together it forms a single ‘C’ program. This
structure makes it easy for testing, maintaining and debugging processes.
‘C’ contains 32 keywords, various data types and a set of powerful built-in functions that make
programming very efficient.
Another feature of ‘C’ programming is that it can extend itself. A ‘C’ program contains various
functions which are part of a library. We can add our features and functions to the library. We
can access and use these functions anytime we want in our program. This feature makes it
simple while working with complex programming.
Various compilers are available in the market that can be used for executing programs written
in this language.
It is a highly portable language which means programs written in ‘C’ language can run on
other machines. This feature is essential if we wish to use or execute the code on another
computer.
How C Programming Language Works?
C is a compiled language. A compiler is a special tool that compiles the program and converts
it into the object file which is machine readable. After the compilation process, the linker will
combine different object files and creates a single executable file to run the program. The
following diagram shows the execution of a ‘C’ program

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

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 5


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

 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:

Writing the First Program in C


The following code is one of the simplest C programs that will help us the basic syntax
structure of a C program.
Example:

#include <stdio.h>
intmain() {
inta = 10;
printf("%d", a);
return0;}

Output
10
Let us analyze the structure of our program line by line.

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 6


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

Structure of the C program


After the above discussion, we can formally assess the structure of a C program. By
structure, it is meant that any program can be written in this structure only. Writing a C
program in any other structure will hence lead to a Compilation Error. The structure of a C
program is as follows:
Components of a C Program:

1. Header Files Inclusion – Line 1 [#include <stdio.h>]


The first and foremost component is the inclusion of the Header files in a C program. A header
file is a file with extension .h which contains C function declarations and macro definitions to
be shared between several source files. All lines that start with # are processed by a
preprocessor which is a program invoked by the compiler. In the above example, the
preprocessor copies the preprocessed code of stdio.h to our file. The .h files are called header
files in C.
Some of the C Header files:
 stddef.h – Defines several useful types and macros.
 stdint.h – Defines exact width integer types.
 stdio.h – Defines core input and output functions
 stdlib.h – Defines numeric conversion functions, pseudo-random network generator, and
memory allocation
 string.h – Defines string handling functions
 math.h – Defines common mathematical functions.
2. Main Method Declaration – Line 2 [int main()]
The next part of a C program is to declare the main() function. It is the entry point of a C
program and the execution typically begins with the first line of the main(). The empty
brackets indicate that the main doesn’t take any parameter (See this for more details). The int
that was written before the main indicates the return type of main(). The value returned by the
main indicates the status of program termination. See this post for more details on the return
type.
3. Body of Main Method – Line 3 to Line 6 [enclosed in {}]
The body of a function in the C program refers to statements that are a part of that function. It
can be anything like manipulations, searching, sorting, printing, etc. A pair of curly brackets
define the body of a function. All functions must start and end with curly brackets.
4. Statement – Line 4 [printf(“Hello World”);]
Statements are the instructions given to the compiler. In C, a statement is always terminated by
a semicolon (;). In this particular case, we use printf() function to instruct the compiler to
display “Hello World” text on the screen.
5. Return Statement – Line 5 [return 0;]
The last part of any C function is the return statement. The return statement refers to the return
values from a function. This return statement and return value depend upon the return type of
the function. The return statement in our program returns the value from main(). The returned
value may be used by an operating system to know the termination status of your program. The
value 0 typically means successful termination.

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 7


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

How to Execute the Above Program?


In order to execute the above program, we need to first compile it using a compiler and then we
can run the generated executable. There are online IDEs available for free
like GeeksforGeeksIDE, that can be used to start development in C without installing a
compiler.
1. Windows: There are many free IDEs available for developing programs in C like Code
Blocks and Dev-CPP. IDEs provide us with an environment to develop code, compile it
and finally execute it. We strongly recommend Code Blocks.
2. Linux: GCC compiler comes bundled with Linux which compiles C programs and
generates executables for us to run. Code Blocks can also be used with Linux.
3. macOS: macOS already has a built-in text editor where you can just simply write the code
and save it with a “.c” extension.
Application of C
 Operating systems: C is widely used for developing operating systems such as Unix,
Linux, and Windows.
 Embedded systems: C is a popular language for developing embedded systems such
as microcontrollers, microprocessors, and other electronic devices.
 System software: C is used for developing system software such as device drivers,
compilers, and assemblers.
 Networking: C is widely used for developing networking applications such as web
servers, network protocols, and network drivers.
 Database systems: C is used for developing database systems such as Oracle,
MySQL, and PostgreSQL.
 Gaming: C is often used for developing computer games due to its ability to handle
low-level hardware interactions.
 Artificial Intelligence: C is used for developing artificial intelligence and machine
learning applications such as neural networks and deep learning algorithms.
 Scientific applications: C is used for developing scientific applications such as
simulation software and numerical analysis tools.
 Financial applications: C is used for developing financial applications such as stock
market analysis and trading systems.

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

2. Add numbers using the + operator

3. Display the result

Qualities of a Good Algorithm


 Input and output should be defined precisely.

 Each step in the algorithm should be clear and unambiguous.

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 8


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

 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

An algorithm is a sequence of instructions that are carried out in a predetermined sequence in


order to solve a problem or complete a work. A function is a block of code that can be called
and executed from other parts of the program.A set of instructions ,for resolving an issue or
carrying out a certain activity. In computer science, algorithms are used for a wide range of
operations, from fundamental math to intricate data processing.One of the common algorithms
used in C is the sorting algorithm. A sorting algorithm arranges a collection of items in a
certain order, such as numerically or alphabetically.

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.

Features of the algorithm


It defines several important features of the algorithm, including:
o Inputs: Algorithms must receive inputs that can be represented as values or data.

o Output: The algorithm should produce some output. It can be a consequence of a


problem or a solution designed to solve it.

o Clarity: Algorithms must be precisely defined, using unambiguous instructions that a


computer or other system can follow unambiguously.

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 9


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

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.

o Effectiveness: An algorithm must be effective, meaning that it must be able to produce


a solution to the problem it is designed to solve in a reasonable amount of time.

o Generality: An algorithm must be general, meaning that it can be applied to a wide


range of problems rather than being specific to a single problem.

What is the need for algorithms?


1. Algorithms are necessary for solving complex problems efficiently and effectively.
2. They help to automate processes and make them more reliable, faster, and easier to
perform.
3. Algorithms also enable computers to perform tasks that would be difficult or impossible
for humans to do manually.
4. They are used in various fields such as mathematics, computer science, engineering,
finance, and many others to optimize processes, analyze data, make predictions, and
provide solutions to problems.
Properties of Algorithm:
 It should terminate after a finite time.
 It should produce at least one output.
 It should take zero or more input.
 It should be deterministic means giving the same output for the same input case.
 Every step in the algorithm must be effective i.e. every step should do some work.
Types of Algorithms:
There are several types of algorithms available. Some important algorithms are:
1. Brute Force Algorithm:
It is the simplest approach to a problem. A brute force algorithm is the first approach that
comes to finding when we see a problem.
2. Recursive Algorithm:
A recursive algorithm is based on recursion. In this case, a problem is broken into several
sub-parts and called the same function again and again.
3. Backtracking Algorithm:
The backtracking algorithm builds the solution by searching among all possible solutions.
Using this algorithm, we keep on building the solution following criteria. Whenever a
solution fails we trace back to the failure point build on the next solution and continue this
process till we find the solution or all possible solutions are looked after.
4. Searching Algorithm:
Searching algorithms are the ones that are used for searching elements or groups of elements
from a particular data structure. They can be of different types based on their approach or the
data structure in which the element should be found.

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 10


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

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

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 11


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

As discussed above, to write an algorithm, its prerequisites must be fulfilled.


1. The problem that is to be solved by this algorithm: Add 3 numbers and print their sum.
2. The constraints of the problem that must be considered while solving the problem:
The numbers must contain only digits and no other characters.
3. The input to be taken to solve the problem: The three numbers to be added.
4. The output to be expected when the problem is solved: The sum of the three numbers
taken as the input i.e. a single integer value.
5. The solution to this problem, in the given constraints: The solution consists of adding
the 3 numbers. It can be done with the help of the ‘+’ operator, or bit-wise, or any other
method.
Step 2: Designing the algorithm
Now let’s design the algorithm with the help of the above pre-requisites:
Algorithm to add 3 numbers and print their sum:
1. START
2. Declare 3 integer variables num1, num2, and num3.
3. Take the three numbers, to be added, as inputs in variables num1, num2, and num3
respectively.
4. Declare an integer variable sum to store the resultant sum of the 3 numbers.
5. Add the 3 numbers and store the result in the variable sum.
6. Print the value of the variable sum
7. END
Step 3: Testing the algorithm by implementing it.
To test the algorithm, let’s implement it in C language

Algorithm 1: Add two numbers entered by the user

Step 1: Start

Step 2: Declare variables num1, num2 and sum.

Step 3: Read values num1 and num2.

Step 4: Add num1 and num2 and assign the result to sum.

sum←num1+num2

Step 5: Display sum

Step 6: Stop

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 12


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

Algorithm 2: Find the largest number among three numbers

Step 1: Start

Step 2: Declare variables a,b and c.

Step 3: Read variables a,b and c.

Step 4: If a > b

If a > c

Display a is the largest number.

Else

Display c is the largest number.

Else

If b > c

Display b is the largest number.

Else

Display c is the greatest number.

Step 5: Stop

Algorithm 3: Find Roots of a Quadratic Equation ax2 + bx + c = 0

Step 1: Start

Step 2: Declare variables a, b, c, D, x1, x2, rp and ip;

Step 3: Calculate discriminant

D ← b2-4ac

Step 4: If D ≥ 0

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 13


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

r1 ← (-b+√D)/2a

r2 ← (-b-√D)/2a

Display r1 and r2 as roots.

Else

Calculate real part and imaginary part

rp ← -b/2a

ip ← √(-D)/2a

Display rp+j(ip) and rp-j(ip) as roots

Step 5: Stop

Algorithm 4: Find the factorial of a number

Step 1: Start

Step 2: Declare variables n, factorial and i.

Step 3: Initialize variables

factorial ← 1

i←1

Step 4: Read value of n

Step 5: Repeat the steps until i = n

5.1: factorial ← factorial*i

5.2: i ← i+1

Step 6: Display factorial

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 14


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

Step 7: Stop

Algorithm 5: Check whether a number is prime or not

Step 1: Start

Step 2: Declare variables n, i, flag.

Step 3: Initialize variables

flag ← 1

i←2

Step 4: Read n from the user.

Step 5: Repeat the steps until i=(n/2)

5.1 If remainder of n÷i equals 0

flag ← 0

Go to step 6

5.2 i ← i+1

Step 6: If flag = 0

Display n is not prime

else

Display n is prime

Step 7: Stop

QUESTIONS

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 15


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

1. Why is C called a mid-level programming language


2. What are the features of the C programming language?
3. Describe Algorithm and its need? What are the types of Algorithm?
4. Algorithm to find all the roots of the quadratic equation?
5. What is Algorithm complexity and how to find it

LECTURE 2 :-
Installation of IDE Turbo C++, Basic
Structure of C programming

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 16


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

1) Installation of IDE –Turbo C++

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 17


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

2) Basic Structure of A Program In C

C program follows a particular structure. So first, let us see the structure of a C program.

1 #include<stdio.h> Header

2 int main() Main

3 {

4 printf(“Hello World”); Statement

5 return 0; Return

6 {

Here is a detailed description of the various program components:

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 18


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

Header File- Line 1 (#include<stdio.h>)

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.

Main Declaration- Line 2 (int main() )

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.

Body – Line 3 to 6 ({})

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.

Statement- Line 4 (printf(“Hello World”);)

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".

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 19


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

Return Statement- Line 5 (return 0;))

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

The successful execution of a C language program is dependent upon the availability of an


exclusive compiler designed to compile codes written in C. A compiler is a piece of software
used to create and run programs. There exist two distinct methodologies by which one may
install a compiler for the C programming language on any personal computing device, such as
a desktop computer or laptop. They are-

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.

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 20


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

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.

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 21


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

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.

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 22


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

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.

5. Finally, the program output is shown in the output window.

#include <stdio.h>
int main()
{
// printf() displays the string inside quotation

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 23


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

printf("Hello, World!");
return 0;
}

Output

Hello, World!

How "Hello, World!" program works?

 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

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 24


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

LECTURE 3 :-
Input Functions/Output Functions:
Printf(), scanf()

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 25


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

C Input and Output - printf()/scanf(), and more.

 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:

 Standard Input (stdin)


 Standard Output (stdout)
 Standard input or stdin is used for taking input.
 Standard output or stdout is used for giving output.
 The functions used for standard input and output are present in the stdio.h header file.
 Hence, to use those functions, we need to include the stdio.h header file in our
program, as shown below.

#include <stdio.h>

Functions Used for Input and Output

C language offers us several built-in functions for performing input/output operations. The
following are the functions used for standard input and output:

1. printf() function - Show Output


2. scanf() function - Take Input
3. getchar() and putchar() function
4. gets() and puts() function

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.

1. The printf() function

 The printf() function is the most used function in the C language.


 This function is defined in the stdio.h header file and is used to show output on the
console (standard output).

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 26


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

Following is how the printf() function is defined in the C stdio.h library.

int printf(const char *format, ...);

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.

printf() Code Examples

Let's start with a simple example.

1. Print a sentence

Let's print a simple sentence using the printf() function.

#include <stdio.h>

int main() {

// using printf()

printf("Welcome to bansal");

return 0;

Welcome to bansal

This one is a very common code example.

To understand the complete code and structure of a basic C language program, check Hello
World Program in C.

2. Print an Integer value

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 27


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

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

printf("Value of x is: %d", x);

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.

3. Print a Character value

The %c format specifier is used to print character variable values using the printf() function.

#include <stdio.h>

int main() {

// using printf()

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 28


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

char gender = 'M';

printf("John's Gender is: %c", gender);

return 0;

John's Gender is: M

4. Print a Float and a Double value

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

float num1 = 15.50;

double num2 = 15556522.0978678;

printf("Value of num1 is: %f \n", num1);

printf("Value of num2 is: %lf", num2);

return 0;

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 29


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

Value of num1 is: 15.500000

Value of num2 is: 15556522.097868

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.

5. Print multiple outputs

We can use a single printf() function to display values of multiple variables.

#include <stdio.h>

int main() {

// using printf() for multiple outputs

int day = 20;

int month = 11;

int year = 2021;

printf("The date is: %d-%d-%d", day, month, year);

return 0;

The date is: 20-11-2021

As you can see in the code example above, we can do the formatting and print values of
multiple variables using the printf() function.

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 30


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

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.

Here is a list of all the format specifiers.

Datatype Format Specifier

Int %d, %i

Char %c

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 31


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

Datatype Format Specifier

Float %f

Double %lf

short int %hd

unsigned int %u

long int %li

long long int %lli

unsigned long int %lu

unsigned long long int %llu

signed char %c

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 32


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

Datatype Format Specifier

unsigned char %c

long double %Lf

2. The scanf() function

When we want to take input from the user, we use the scanf() function and store the input value
into a variable.

Following is how the scanf() function is defined in the C stdio.h library.

int scanf(const char *format, ...);

 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.

Here is the syntax for scanf():

scanf("%x", &variable);

where, %x is the format specifier.

 Using the format specifier, we tell the compiler what type of data to expect from the
user.

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 33


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

 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.

scanf() Code Examples

Let's start with a simple example.

1. Input Integer value

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;

printf("Please enter a number: ");

scanf("%d", &user_input);

printf("You entered: %d", user_input);

return 0;

Please enter a number: 7

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.

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 34


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

 In the above code example, we have used %d format specifier to inform


the scanf() function that user input will be of type int.
 And we have also used & symbol before the name of the variable,
because &user_input refers to the address of the user_input variable where the input
value will be stored.

2. Input Float value

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;

printf("Please enter a decimal number: ");

scanf("%f", &user_input);

printf("You entered: %f", user_input);

return 0;

Please enter a decimal number: 7.007

You entered: 7.007

 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.

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 35


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

3. Input Character value

Let's see how we can take a simple character input from the user.

#include <stdio.h>

int main() {

// using scanf()

char gender;

printf("Please enter your gender (M, F or O): ");

scanf("%c", &gender);

printf("Your gender: %c", gender);

return 0;

Please enter your gender (M, F or O): M

Your gender: M

4. Take Multiple Inputs from the User

In the below code example, we are taking multiple inputs from the user and saving them into
different variables.

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 36


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

#include <stdio.h>

int main() {

// using scanf() for multiple inputs

char gender;

int age;

printf("Enter your age and then gender(M, F or O): ");

scanf("%d %c", &age, &gender);

printf("You entered: %d and %c", age, gender);

return 0;

Enter your age and then gender(M, F or O): 32 M

You entered: 32 and M

Return Value of printf() & scanf()

 The printf() function returns the number of characters printed by it,


 and scanf() function returns the number of characters read by it.

int i = printf("bansal");

printf("Value of i is: %d", i);

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 37


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

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.

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 38


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

LECTURE 4 :-
Discussion on Format Specifiers &
Escape Sequence

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 39


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

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.

The Most Commonly Used Format Specifiers in C

The Most commonly used format specifiers are given below:

Format Specifiers Type of Output

%d or %i A decimal integer or signed integer

%c Signed character

%f Signed float

%e A floating-point number

%s A string or sequence of character

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 40


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

%lf Double

%Lf Long double

%o Octal integer

%u Short unsigned integer

%ld Long decimal integer

%x Hexadecimal integer

%p Print memory address in the hexadecimal form

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

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 41


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

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.

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 42


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

Escape
Sequence Name Description

\a Alarm or Beep It is used to generate a bell sound in the C program.

\b Backspace It is used to move the cursor backward.

It is used to move the cursor to the start of the next


\f Form Feed
logical page.

\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.

It inserts some whitespace to the left of the cursor and


\t Horizontal Tab
moves the cursor accordingly.

\v Vertical Tab It is used to insert vertical space.

\\ Backlash Use to insert backslash character.

\’ Single Quote It is used to display a single quotation mark.

\” Double Quote It is used to display double quotation marks.

\? Question Mark It is used to double quotation marks.

\ooo Octal Number It is used to represent an octal number.

Hexadecimal
\xhh It represents the hexadecimal number.
Number

\0 NULL It represents the NLL character.

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 ?

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 43


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

Q.5 : How to use printf and scanf in C programming ? Make a program of addition of 2
numbers.

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 44


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

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.

 A keyword is a reserved word. You cannot use it as a variable name, constant

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 45


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

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.

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 46


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

TypesofLiterals:-

IntegerLiterals

Anintegerisanumeicliteral(associatedwithnumbers)withoutanyfractionalorexponentialpa
rt

For example: 1,2,3,4 e.t.c


Floating-pointLiterals
Afloating-
pointliteralisanumericliteralthathaseitherafractionalformor
anexponentform.Examplesoffloatliteralindecimalformare:

Forexample:1.2,+9.0, -4.5

CharacterLiterals

A character literal is created by enclosing a single character inside single quotationmarks.


Forexample:‘a’,’m’ ,’F’,’2’ ,'a','}'etc.

StringLiterals

A string literal is a sequence of characters enclosed in double-quote marks.


Forexample:“Hello”

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 47


Sushila Devi Bansal College
An Institute of Bansal Group of Institutes
Umaria, A. B. Road, Near Rau, Indore - 453331

Questions on Keywords, Various Types of Constants

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.

Website: https://sdbc.ac.in Contacts: +91 9669662255, 9669660055, E-Mail ID: [email protected] 48

You might also like