You are on page 1of 29

CSIR11 - INTRODUCTION TO

COMPUTER PROGRAMMING

08-01-2023 1
Unit Objectives
• To learn the syntax and semantics for C programming language.
• To develop the C code for simple logic.
• To understand the construct of structure program including
conditionals and iterations

08-01-2023 2
Unit Outcomes
• Knowledge of the syntax and semantics of C programming language
• Ability to code a given logic in C language
• Knowledge in using C language for solving problems

08-01-2023 3
Syllabus – Unit 2
Introduction to C- C character set- Identifiers and Keywords- Data
types- Constants- Variables-Declarations- Expressions- Statements-
Symbolic Constants- Operators- Library Functions-Data input and
output: Single character input and output- Entering input data- Writing
output data- gets and puts functions - Control Statements- Branching:
if-else-looping: while- do-while for; Nested control Structures- switch
statements- Break statements- Continue Statements Comma operator-
goto statements.

08-01-2023 4
Introduction to C
• History - C programming language was developed in 1972 by Dennis
Ritchie at bell laboratories of AT&T (American Telephone &
Telegraph), located in U.S.A.
• It was developed to overcome the problems of previous languages
such as B, BCPL etc. Initially, C language was developed to be used in
UNIX operating system.

08-01-2023 5
Features of C
1).Simple: C is a simple language in the sense that it provides structured approach
(to break the problem into parts), rich set of library functions, data types etc.
2).Machine Independent or Portable: C programs can be executed in many
machines with little bit or no change. But it is not platform-independent.
3).Mid-level programming language: C is also used to do low level programming. It
is used to develop system applications such as kernel, driver etc. It also supports
the feature of high level language. That is why it is known as mid-level language.
4).Structured programming language: C is a structured programming language in
the sense that we can break the program into parts using functions. So, it is easy to
understand and modify.
5).Rich Library: C provides a lot of inbuilt functions that makes the development
fast.
6).Memory Management: It supports the feature of dynamic memory allocation. In
C language, we can free the allocated memory at any time by calling the free()
function

08-01-2023 6
Features of C
7).Speed: The compilation and execution time of C language is fast.
8).Pointer: C provides the feature of pointers. We can directly interact
with the memory by using the pointers. We can use pointers for
memory, structures, functions, array etc.
9).Recursion: In C, we can call the function within the function. It
provides code reusability for every function.
10).Extensible: C language is extensible because it can easily adopt new
features.
11).Case sensitive: Upper case and Lowercase letters are treated
differently.

08-01-2023 7
Steps in Learning C

08-01-2023 8
C Character set
A character denotes any alphabet, digit or special symbol used to represent
information.

08-01-2023 9
Constants, Variables and Keywords
• The alphabets, digits and special symbols when properly combined form constants,
variables and keywords. Let us now understand the meaning of each of them.
• A constant is an entity that doesn’t change, whereas, a variable is an entity that may
change.
• A keyword is a word that carries special meaning.
• Consider the memory locations shown in Figure, Here 3 is stored in a memory
location and a name x is given to it. Then we have assigned a new value 5 to the same
memory location x. This would overwrite the earlier value 3, since a memory location
can hold only one value at a time.

• Since the location whose name is x can hold different values at different times x
is known as a variable (or a variable name). As against this, 3 or 5 do not
change, hence are known as constants.
08-01-2023 10
Constants - C
Constants: Constants in C refer to fixed values that do not change during the execution of a
program. C support several types of constants.

08-01-2023 11
Integer Constants - C
Rules for Constructing Integer Constants:
(a) An integer constant must have at least one digit.
(b) It must not have a decimal point.
(c) It can be either positive or negative.
(d) If no sign precedes an integer constant, it is assumed to be positive.
(e) No commas or blanks are allowed within an integer constant.
(f) The allowable range for integer constants is -2147483648 to +2147483647.

Hint: Truly speaking, the range of an Integer constant depends upon the compiler. For compilers like Visual
Studio, gcc, it is -2147483648 to +214748364, whereas for compilers like Turbo C or Turbo C++ the range is -
32768 to +32767.

08-01-2023 12
Real Constants - C
Real constants are often called Floating Point constants. The real constants could be written in two
forms—Fractional form and Exponential form.
Following rules must be observed while constructing real constants expressed in fractional form:
(a) A real constant must have at least one digit.
(b) It must have a decimal point.
(c) It could be either positive or negative.
(d) Default sign is positive.
(e) No commas or blanks are allowed within a real constant.

The exponential form is usually used if the value of the constant is either too small or too large. In exponential form
the real constant is represented in two parts. The part appearing before ‘e’ is called mantissa, whereas the part
following ‘e’ is called exponent. Thus 0.000342 can be written in exponential form as 3.42e-4 (which in normal
arithmetic means 3.42 x 10-4 ).
08-01-2023 13
Real Constants - C
Following rules must be observed while constructing real constants expressed in exponential form:
(a) The mantissa part and the exponential part should be separated by a letter e or E.
(b) The mantissa part may have a positive or negative sign.
(c) Default sign of mantissa part is positive.
(d) The exponent must have at least one digit, which must be a positive or negative integer. Default
sign is positive.
(e) Range of real constants expressed in exponential form is -3.4e38 to 3.4e38.

The exponential form is usually used if the value of the constant is either too small or too large. In exponential form
the real constant is represented in two parts. The part appearing before ‘e’ is called mantissa, whereas the part
following ‘e’ is called exponent. Thus 0.000342 can be written in exponential form as 3.42e-4 (which in normal
arithmetic means 3.42 x 10-4 ).
08-01-2023 14
Character Constants - C

Rules for Constructing Character Constants


(a) A character constant is a single alphabet, a single digit or a single special symbol enclosed
within single inverted commas.
(b) (b) Both the inverted commas should point to the left.
(c) For example, ’A’ is a valid character constant whereas ‘A’ is not.

08-01-2023 15
Variables- C
A particular type of variable can hold only the same type of constant. For example, an integer
variable can hold only an integer constant, a real variable can hold only a real constant and a
character variable can hold only a character constant.
Rules for Constructing Variable Names
(a) A variable name is any combination of 1 to 31 alphabets, digits or underscores. Some compilers
allow variable names whose length could be up to 247 characters. Still, it would be safer to stick
to the rule of 31 characters. Do not create unnecessarily long variable names as it adds to your
typing effort.
(b) The first character in the variable name must be an alphabet or underscore ( _ ).
(c) No commas or blanks are allowed within a variable name.
(d) No special symbol other than an underscore (as in gross_sal) can be used in a variable name.

Hint: It is a good practice to exploit this abundant choice in naming variables by using meaningful
variable names. Thus, if we want to calculate simple interest, it is always advisable to construct
meaningful variable names like prin, roi, noy to represent Principle, Rate of interest and Number of
08-01-2023 16
years rather than using the variables a, b, c.
Variables- C
Naturally, the question follows... how is C able to differentiate between these variables? This is a
rather simple matter. C compiler is able to distinguish between the variable names by making it
compulsory for you to declare the type of any variable name that you wish to use in a program. This
type declaration is done at the beginning of the program. Examples of type declaration statements
are given below.

08-01-2023 17
Keywords - C
Keywords are the words whose meaning has already been explained to the C compiler (or in a broad
sense to the computer). There are only 32 keywords available in C.

Hint: The keywords cannot be used as variable names because if we do so, we are trying to assign a new
meaning to the keyword, which is not allowed. Some C compilers allow you to construct variable names
that exactly resemble the keywords. However, it would be safer not to mix up the variable names and the
keywords.

08-01-2023 18
Structure of C
C program can be viewed as a group of building blocks called functions.
A function (or) subroutine that may include one (or) more statements
to perform a specific task.

08-01-2023 19
First C Program

08-01-2023 20
Form of C Program
Form of a C program indicates how it has to be written/typed. There are
certain rules about the form of a C program that are applicable to all C
programs.
These are as under:
(a)Each instruction in a C program is written as a separate statement.
(b)The statements in a program must appear in the same order in which we
wish them to be executed.
(c) Blank spaces may be inserted between two words to improve the
readability of the statement.
(d)All statements should be in lower case letters.
(e)C has no specific rules for the position at which a statement is to be
written in a given line. That’s why it is often called a free-form language.
(f) Every C statement must end with a semicolon ( ; ). Thus ; acts as a
statement terminator.

08-01-2023 21
Comments - C Program
Comments are used in a C program to clarify either the purpose of the program or the purpose of some statement in
the program. It is a good practice to begin a program with a comment indicating the purpose of the program, its
author and the date on which the program was written. Here are a few things that you must remember while writing
comments in a C program:
• Comment about the program should be enclosed within /* */.
• Any number of comments can be written at any place in the program.

• The normal language rules do not apply to text written within /* .. */. Thus we can type this text in small case,
capital or a combination. This is because the comments are solely given for the understanding of the programmer
or the fellow programmers and are completely ignored by the compiler.
• Comments cannot be nested. This means one comment cannot be written inside another comment. For example,

• A comment can be split over more than one line, as in,

• ANSI C permits comments to be written in the following way:

08-01-2023 22
Main() - C Program
• main( ) is a function. A function is nothing but a container for a set of statements. In a C program there
can be multiple functions. To begin with, we would concentrate only on those programs which have
only one function. The name of this function has to be main( ), it cannot be anything else. All
statements that belong to main( ) are enclosed within a pair of braces { } as shown below.

• The way functions in a calculator return a value, similarly, functions in C also return a value. main( ) function
always returns an integer value, hence there is an int before main( ). The integer value that we are returning
is 0. 0 indicates success. If for any reason the statements in main( ) fail to do their intended work we can
return a non-zero number from main( ). This would indicate failure.
• Some compilers like Turbo C/C++ even permit us to return nothing from main( ). In such a case we should
precede it with the keyword void. But this is non-standard way of writing the main( ) function.

08-01-2023 23
Variables and Usage
Any variable used in the program must be declared before using it. For example,

In the statement, si = p * n * r / 100 ; * and / are the arithmetic operators. The arithmetic operators
available in C are +, -, * and /. C is very rich in operators. There are as many as 45 operators available in C.
Surprisingly there is no operator for exponentiation.

08-01-2023 24
Printf() and Usage
C does not contain any instruction to display output on the screen. All output to
screen is achieved using readymade library functions. One such function is printf( ).
(a) Once the value of si is calculated it needs to be displayed on the screen. We have
used printf( ) to do so.
(b) For us to be able to use the printf( ) function, it is necessary to use #include at the
beginning of the program. #include is a preprocessor directive. For now, use it
whenever you use printf( ).
(c) The general form of printf( ) function is

08-01-2023 25
Printf() and Usage
Example:

printf( ) can not only print values of variables, it can also print the result of an expression. An expression
is nothing but a valid combination of constants, variables and operators. Thus, 3, 3 + 2, c and a + b * c – d
all are valid expressions. The results of these expressions can be printed as shown below.

08-01-2023 26
Compilation and Execution
• Once you have written the program you need to type it and
instruct the machine to execute it.
• To type your C program you need another program called
Editor.
• Once the program has been typed it needs to be converted to
machine language instructions before the machine can execute
it. To carry out this conversion we need another program called
Compiler.
• Compiler vendors provide an Integrated Development
Environment (IDE) which consists of an Editor as well as the
Compiler.

08-01-2023 27
Receiving Input

Hint: Note the use of ampersand (&) before the variables in the
scanf( ) function is a must. & is an ‘Address of’ operator. It gives
the location number (address) used by the variable in memory.
When we say &a, we are telling scanf( ) at which memory
location should it store the value supplied by the user from the
keyboard.
Note that a blank, a tab or a new line must separate the values
supplied to scanf( ).

08-01-2023 28
Lab 1 - Exercises
1. Ramesh’s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary,
and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary.
2. The distance between two cities (in km.) is input through the keyboard. Write a program to convert
and print this distance in meters, feet, inches and centimeters.
3. If the marks obtained by a student in five different subjects are input through the keyboard, write a
program to find out the aggregate marks and percentage marks obtained by the student. Assume
that the maximum marks that can be obtained by a student in each subject is 100.
4. Temperature of a city in Fahrenheit degrees is input through the keyboard. Write a program to
convert this temperature into Centigrade degrees.
5. The length and breadth of a rectangle and radius of a circle are input through the keyboard. Write a
program to calculate the area and perimeter of the rectangle, and the area and circumference of the
circle.

08-01-2023 29

You might also like