BIT104 SLM Library - SLM - Unit 02
BIT104 SLM Library - SLM - Unit 02
BIT104 SLM Library - SLM - Unit 02
2.1 Introduction
In the previous unit, we have discussed the various basic concepts of
programming and its domains. We discussed program development cycle
with case study. We also discussed about concepts of Algorithm, flowcharts,
program translation and execution process.
C is a general-purpose, structured programming language. C was the
offspring of the ‘Basic Combined Programming Language’ (BPCL) called B,
developed in the 1960’s at Cambridge University. B language was modified
by Dennis Ritchie and was implemented at Bell laboratories in 1972. The
new language was named C. Since it was developed along with the UNIX
operating system, it is strongly associated with UNIX. This operating system,
Sikkim Manipal University B2074 Page No.: 25
Principles of C Programming Unit 2
which was also developed at Bell laboratories, was coded almost entirely
in C.
C is popular language because it is reliable, simple and easy to use. It is
one of the language that has survived more than three decades.
This unit will introduce C programming and its various features, structure of
C program and also describe how the C programs are executed with simple
examples. We will also discuss about constants, variables and keywords
reserved by C language. Finally, we will also discuss about important data
type integer in this unit.
Objectives:
At studying this unit, you should be able to:
discuss the features of C programming language
explain the basic structure of a C program
write simple program in C
construct and use the concept of Constants, Integers and Keywords
discuss the variable and its declaration in C
Documentation section
Preprocessor Directive or Link section
Global declaration section
main() function section
{
Declaration part
Executable part
}
Subprogram section
Function 1
Function 2
.
.
Function n
The first line of the program #include <stdio.h> is a link (i.e. preprocessor)
command, which tells a C compiler to include stdio.h file before going to
actual compilation. This line will appear in almost all the programs we write.
It asks that some definitions having to do with the “Standard I/O Library'' be
included in our program; In program this library is needed to call the library
function printf correctly.
In the second line we are defining a main function. The main function will be
“called” first when the program starts running. The empty pair of
parentheses indicates that our main function accepts no arguments, that is,
there is not any information which needs to be passed in when the function
is called. We can write our own function called user defined function. User
defined functions are generally placed immediately after the main function,
although they may appear in any order.
In C, A list of statements is surrounded by the braces { and }. Here, they
surround the list of statements making up the function main.
The line
printf("Hello, world!\n");
is the first statement in the program. It asks that the function printf be called;
printf is a library function which prints formatted output. The parentheses
surround printf's argument list: the information which is handed to it on
which it should act. The semicolon at the end of the line terminates the
statement.
printf’s first (and, in this case, only) argument is the string which it should
print. The string, enclosed in double quotes (""), consists of the words “Hello,
world!'' followed by a special sequence: \n. In strings, any two-character
sequence beginning with the backslash \ represents a single special
character. The sequence \n represents the “ 'new line'' character, which
prints a carriage return or line feed or whatever it takes to end one line of
output and move down to the next. (This program only prints one line of
output, but it is still important to terminate it.)
The second line in the main function is
return 0;
2.4 Constants
Constants in C refer to fixed values that do not change during the execution
of a program. C supports four types of basic constants as illustrated in
Fig. 2.2.
Constants
An octal integer constant consists of any combination of digits from the set 0
through 7, with a leading 0.
Examples: 045, 0, 0567
A sequence of digits preceded by 0x or 0X is considered as hexadecimal
integer. They may also include alphabets A through F or a through f. The
letters A through F represent numbers 10 through 15.
Examples: 0X6, 0x5B, 0Xbcd, 0X
The largest integer value that can be stored is machine-dependent. It is
32767 on 16-bit machines and 2,147,483,647 on 32-bit machines. It is also
possible to store larger integer constants on these machines by appending
qualifiers such as U, L and UL to the constants.
Examples: 54637U or 54637u (unsigned integer)
65757564345UL or 65757564345ul (unsigned long integer)
7685784L or 7685784l (long integer)
Program 2.2: Program to represent integer constants on a 16-bit
computer
Type and execute the above program and observe the output
All the integer constants have to follow the set of rules given below.
1. Constant must have at least one digit.
2. Must not have decimal point.
3. Constant can be preceded by minus (-) sign, to indicate negative
constants.
2.5 Keywords
Keywords are the words whose meaning is already known to the C compiler.
Keywords are the reserved words of a programming language. All the
keywords have fixed meanings and these meanings cannot be changed.
Keywords serve as basic building blocks for program statements. There are
only 32 keywords available in C. The list of all keywords in ANSI C are listed
in the table 2.2.
Table 2.2: ANSI C Keywords
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
2.6.2 Variable:
A variable is an identifier that may be used to store data value. A value or a
quantity which may vary during the program execution can be called a
variable. Each variable has a specific memory location in memory unit,
where numerical values or characters can be stored. A variable is
represented by a symbolic name. Thus, variable name refers to the location
of the memory in which a particular data can be stored. Variables names
are also called as identifiers since they identify the varying quantities.
For Ex: sum = a+b. In this equation sum, a and b are the identifiers or
variable names representing the numbers stored in the memory locations.
In C, the type of a variable determines what kinds of values it may take on.
The type of object determines the set of values it can have and what
You may wonder why variables must be declared before use. There are two
reasons:
1. It makes things somewhat easier on the compiler; it knows right away
what kind of storage must be allocated and what code to emit to store
and manipulate each variable; it does not have to try to intuit the
programmer's intentions.
2. It forces a bit of discipline on the programmer. You cannot introduce
variables wherever you wish; you must think about them enough to pick
appropriate types for them. (The compiler's error messages to you,
telling you that you apparently forgot to declare a variable, are as often
helpful as they are a nuisance. They are helpful when they tell you that
you misspelled a variable, or forgot to think about exactly how you were
going to use it.)
Most of the time, it is recommended to write one declaration per line. For
most part, the compiler does not care what order declarations are in. You
can order the declarations alphabetically, or in the order that they are used,
or to put related declarations next to each other. Collecting all variables of
the same type together on one line essentially orders declarations by type,
which is not a very useful order (it is considered only slightly more useful
than random order).
A declaration for a variable can also contain an initial value. This initializer
consists of an equal sign and an expression, which is usually a single
constant:
int total = 1;
Self-Assessment Questions
10. The size of the Integers in C language is same in all the machines.
(True/False)
11. A ________ is a place where we can store values.
12. Size of int is _________ bits.
13. Which of the following tells the compiler the name and type of variable
you'll be using in your program?
(a) Declaration
(b) Variables
(c) Integers
(d) Assignments
#include <stdio.h>
/* print addition of two numbers, to illustrate use of int and variable */
main()
{
int sum, a , b; // integer variable declaration
a=10;
b=5;
printf("sum is: %d", sum); // print the result available in sum
return 0;
}
Output:
sum is: 15
Note that printf function used to display the result on screen. We have used
it display on the screen the value contained in sum.
The general form of printf( ) function is,
printf ( "<format string>", <list of variables> ) ;
<format string> can contain,
%f for printing real values
%d for printing integer values
%c for printing character values
Self Assessment Questions
16. In C, variable names are case sensitive. (True/False)
17. A variable name in C consists of letters, numbers and _________.
2.8 Summary
Let us recapitulate important points discussed in this unit:
C is a general-purpose, structured programming language. Its
instructions consist of terms that resemble algebraic expressions,
augmented by certain English keywords such as if, else, for, do and
while.
C is characterized by the ability to write very concise source programs.
2.10 Answers
Self Assessment Questions
1. True
2. High
3. Comment
4. True
5. Arguments
6. False
7. (0, 7)
8. Hexadecimal
9. Characters
10. False
11. Variable
12. 16
13. (a) declaration
14. initializer
15. False
16. True
17. Underscores
Terminal Questions
1. C was developed by Dennis Ritchie and came after B in the year 1972.
(Refer section 2.1 for more details)
2. C language provides various features. (Refer section 2.2 for more
details)
3. Documentation section, Link section, Global declaration section, main()
function section, Subprogram section. (Refer to section 2.2 for more
details)
4. main() is the function that will be “called'' first when our program starts
running.
5. A C program can be compiled and executed across various operating
systems like Windows, Linux, and Mac OS. (Refer to section 2.3 for
more details)
6. The arguments are symbols that represent information being passed
between the function and other parts of the program. They appear in the
function heading.
7. A signed integer uses one bit for sign and remaining bits for the
magnitude of the number, whereas an unsigned integer uses all the bits
to represent magnitude.
8. A declaration consists of the type, the name of the variable, and a
terminating semicolon.
9. Variables (the formal term is “identifiers'') consist of letters, numbers,
and underscores. (Refer to section 2.7 for more details)
2.11 Exercises
1. Which of the following are invalid variable names and why?
a) Total1s
b) $tax
c) name-and-address
d) lrecord
e) file–3
f) name and address
g) 123-45 -6789
2. Which of the following numerical values are valid constants? If it is valid
specify whether it is real, int or character. If not justify answer.
a) 0.12
b) 12.2
c) OXabc123g
d) O178
e) 9.3e12
f) 0.9E0.8
g) 0.8e+5
h) OX34C
3. Identify valid and invalid real constants from the following. Write reasons
for invalid real constants.
a) 0.01
b) 1.5
c) 825.053
d) 1
e) 53e10.3
f) 12E8, 12e8
g) 12e+8
h) 12e-8
i) 0.65E-3
j) 13 E 15
4. Write a program in C to add, subtract, and multiply any 3 numbers.