Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

BIT104 SLM Library - SLM - Unit 02

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Principles of C Programming Unit 2

Unit 2 Introduction to C Programming


Structure:
2.1 Introduction
Objectives
2.2 Features of C and its Basic Structure
Features of C
Basic Structure of C Program
2.3 Simple C program
Compile and Execute C Program
2.4 Constants
Integer Constants
Real Constants
Character Constants
String Constants
Backslash Character Constants
2.5 Keywords
2.6 Integer and Variable
Integers
Variable
2.7 Rules for naming Variables and Assigning Values to Variables
2.8 Summary
2.9 Terminal Questions
2.10 Answers
2.11 Exercises

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

2.2 Features of C and its Basic Structure


2.2.1 Features of C
1. C is characterized by the ability to write very concise source programs
as it has included large number of operators. C is a system
programming language which provides flexibility for writing compilers,
operating systems, and so on.
2. It can also be used for writing the application programs for scientific,
engineering and business applications.
3. It has a relatively small instruction set. It provides a rich set of built-in
functions which enhance the basic instructions.
4. Using C language, users can write additional library functions. Thus, the
features and capabilities of the language can easily be extended by the
user.
5. C compilers are commonly available for computers of all sizes. The
compilers are usually compact, and they generate object programs that
are small and highly efficient when compared with programs compiled
from other high-level languages.

Sikkim Manipal University B2074 Page No.: 26


Principles of C Programming Unit 2

6. C is famous for its portability, meaning that a program written in C for


one computer can be easily loaded to another computer with little or no
changes.
7. C supports various types of data like integers, float point numbers,
characters, and so on.
8. C is a procedure oriented language which is most suited for structured
programming practice.
2.2.2 Basic Structure of C Program
C program can be viewed as a group of building blocks called functions. A
function is a subroutine that may include one or more statements designed
to perform a specific task. To write a C program we first create functions
and then put them together. C program may contain one or more sections
as shown in Fig. 2.1.
The documentation section consists of a set of comment (remarks) lines
giving the name of the program, the author and other details which the
programmer would like to use later. Comments may appear anywhere within
a program, as long as they are placed within the delimiters /* and */
(e.g., /*this is a comment*/). Such comments are helpful in identifying the
program’s principal features or in explaining the underlying logic of various
program features.

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

Figure 2.1: Sections in a C program

Sikkim Manipal University B2074 Page No.: 27


Principles of C Programming Unit 2

The Preprocessor Directive or Link section: It provides instruction to the


compiler to link functions or do the processing prior to the execution of the
program. It is also used to define symbolic constants of the program.
Global Variable Section: There are variables that are used in more than one
function. Such variables are called global variables and are declared in the
global declaration section that is outside of all the functions.
The Main Function Section: The main function section must be present in
every C Program. This section contains two parts, declaration part and
executable part. The declaration part declares all the variables used in the
executable part. There is at least one statement in the executable part.
These two parts must appear between opening and closing braces ({ and }).
The program execution begins at the opening brace and ends at the closing
brace. The closing brace of the main function section is the logical end of
the program. All statements in the declaration and executable parts end with
a semicolon (;).
The subprogram section contains all the user-defined functions that are
called in the main function. User-defined functions are generally placed
immediately after the main function, although they may appear in any order.
All sections, except the main function section may be absent when they are
not required.
Self Assessment Questions
1. Using C language programmers can write their own library functions.
(True/False)
2. C is a ________ level programming language.
3. The documentation section contains a set of __________ lines.
4. Every C program must have one main() function. (True/False)

2.3 Simple C Program


A simple c program given below:
Program 2.1: First C Program to Print “Hello, World!”
#include <stdio.h>
main()
{
printf("Hello, world!\n");
return 0;
}

Sikkim Manipal University B2074 Page No.: 28


Principles of C Programming Unit 2

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;

Sikkim Manipal University B2074 Page No.: 29


Principles of C Programming Unit 2

In general, a function may return a value to its caller, and main is no


exception. When main returns (that is, reaches its end and stops
functioning), the program is at its end, and the return value from main tells
the operating system (or whatever invoked the program that main is the
main function of) whether it succeeded or not. By convention, a return value
of 0 indicates success.
2.3.1 Compile and Execute C Program:
C program can be compiled and executed across various operating systems
like Windows, Linux, and Mac OS. Various compilers are written for the
purpose. Executing a C program involves a series of following steps.
1. Creating a program
2. Compiling the program
3. Linking the program with functions that are needed from the C library.
4. Executing the program
Linux:
In Linux / UNIX platform, let us look at how to save the source code in a file,
and how to compile and run it. Following are the simple steps:
1. Open a text editor and add the above-mentioned code.
2. Save the file as hello.c
3. Open a command prompt and go to the directory where you saved the
file.
4. Type gcc hello.c and press enter to compile your code.
5. If there are no errors in your code the command prompt will take you to
the next line and would generate a.out executable file.
6. Now, type a.out to execute your program.
7. You will be able to see "Hello World" printed on the screen
$ gcc hello.c
$ ./a.out
Hello, world!
Make sure that gcc compiler is in your path and that you are running it in the
directory containing source file hello.c.
WINDOWS:
Assuming that you are using a Turbo C or Turbo C++ compiler, here are the
steps that you would need to follow to compile and execute your first C
program using command prompt:

Sikkim Manipal University B2074 Page No.: 30


Principles of C Programming Unit 2

1. Start the compiler.


2. Select New from the File menu.
3. Type the program.
4. Save the program using F2 under a proper name (say Hello.c).
5. Use Ctrl + F9 to compile and execute the program.
6. Use Alt + F5 to view the output.
Output on screen will be:
Hello, world!

Self Assessment Questions


5. The information that needs to be passed in when a function is called is
______.
6. The main() function doesn’t return any value. (True/False)

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

Numeric constants Character constants

Integer constants Real constants Single character constants String constants

Figure 2.2: Basic Constants


2.4.1 Integer Constants
An integer constant is an integer valued number. It is refers to a sequence
of digits. Integer constants are divided into three different number systems,
namely decimal, octal and hexadecimal.
Decimal integers consist of a set of digits, 0 through 9, preceded by an
optional – or +.
Examples: -546, 0, 12, 354647, +89

Sikkim Manipal University B2074 Page No.: 31


Principles of C Programming Unit 2

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

/* Integer numbers on 16-bit machine */


main()
{
Printf (“Integer values\n\n”);
Printf (“%d %d %d\n”, 32767, 32767+1, 32767+10);
Printf (“\n”);
Printf (“Long integer values\n\n”);
Printf (“%ld %ld %ld\n”, 32767L, 32767L+1L, 32767L+10L);
}

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.

Sikkim Manipal University B2074 Page No.: 32


Principles of C Programming Unit 2

4. Positive integer constants can be preceded by either positive sign or no


sign.
5. Commas and blank spaces can’t be included with in the constant.
6. The value of constant must not exceed specified minimum and
maximum bound. Generally for 16 bit computer, the integer constant
range is -32768 to 32767.
2.4.2 Real Constants
A real constant consists for a series of digits representing the whole part of
the number, followed by a decimal point, and then a series of the fractional
part. The whole part or the fractional part can be omitted, but both cannot be
omitted. The decimal cannot be omitted. That is, it is possible that the
number may not have digits before the decimal point or after the decimal
point.
Rules for Constructing Real Constants in Fractional Form
1. A real constant must have at least one digit.
2. It must have a decimal point.
3. It could be either positive or negative.
4. Default sign is positive.
5. Commas or blanks are not allowed within a real constant.
Valid Real constants (Fractional): 0.0 , -0.1 , +123.456
A real constant is also called a floating point constant. A real number may
also be expressed in exponential (scientific) notation. The general form is:
mantissa e exponent
The mantissa is either a real number expressed in decimal notation or an
integer. The exponent is an integer number with an optional plus or minus
sign. The letter e separating the mantissa and the exponent can be written
in either lowercase or uppercase.
Examples: 05e4, 62e-2, -7.3E-2
2500000000 may be written as 2.5E9 or 25E8.
In C language, floating point constants are normally represented as double-
precision quantities. However, the suffixes f or F may be used to force
single precision and l or L to extend double-precision further. The range of
real constant expressed in exponential form is 3.4 e+ 38 to 3.4 e -38

Sikkim Manipal University B2074 Page No.: 33


Principles of C Programming Unit 2

2.4.3 Character Constants


A single character constant (or simple character constant) contains a single
character enclosed within a pair of single quote marks.
Examples: ‘6’, ‘X’, ‘;’
Character constants have integer values known as ASCII values. For
example, the statement
printf(“%d”, ‘a’);
would print the number 97, the ASCII value of the letter a. Similarly, the
statement
printf(“%c”, 97);
would print the letter a.
2.4.4 String Constants
A string constant is a sequence of characters enclosed within double quotes.
The characters may be letters, numbers, special characters and blank
space.
Examples: “Hello!”, “1947”, “5+3”
2.4.5 Backslash Character Constants
C supports some special backslash character constants that are used in
output functions. A list of such backslash character constants is given in
Table 2.1. Note that each one of them represents one character, although
they consist of two characters. These character combinations are called
escape sequences.
Table 2.1: Backslash character constants
Constant Meaning
\b back space
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\’ single quote
\” double quote
\\ backslash
\0 null

Sikkim Manipal University B2074 Page No.: 34


Principles of C Programming Unit 2

Self Assessment Questions


7. An octal integer constant consists of any combination of digits from the
set _________ through _________.
8. A sequence of digits preceded by 0x or 0X is considered as
_____________ integer.
9. A string constant is a sequence of __________ enclosed within double
quotes.

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

All keywords must be written in lowercase. Some compilers may use


additional keywords that must be identified from the C manual.

2.6 Integer and Variable


2.6.1 Integers
Integers are whole numbers with a range of values supported by a particular
machine. Generally, integers occupy one word of storage, and since the
word sizes of machines vary (typically, 16 or 32 bits) the size of an integer
that can be stored depends on the computer. If we use 16 bit word length,
the size of the integer value is limited to the range -32768 to +32767 (that is,
-215 to +2 15 -1). A signed integer uses one bit for sign and 15 bits for the

Sikkim Manipal University B2074 Page No.: 35


Principles of C Programming Unit 2

magnitude of the number. Similarly, a 32 bit word length can store an


integer ranging from -2,147,483,648 to 2,147,483,647.
In order to provide some control over the range of numbers and storage
space, C has three classes of integer storage, namely short int, int, and long
int, in both signed and unsigned forms. For example, short int represents
fairly small integer values and requires half the amount of storage as a
regular int number uses. A signed integer uses one bit for sign and 15 bits
for the magnitude of the number, therefore, for a 16 bit machine, the range
of unsigned integer numbers will be from 0 to 65,535.
We declare long and unsigned integers to increase the range of values. The
use of qualifier signed on integers is optional because the default
declaration assumes a signed number. The Table 2.3 shows all the allowed
combinations of basic types and qualifiers and their size and range on a 16-
bit machine.
Table 2.3: Basic types, qualifiers and their size

Type Size (bits) Range


int or signed int 16 -32,768 to 32,767
unsigned int 16 0 to 65535
short int or signed short int 8 -128 to 127
unsigned short int 8 0 to 255
long int or signed long int 32 -2,147,483,648 to 2,147,483,647
unsigned long int 32 0 to 4,294,967,295

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

Sikkim Manipal University B2074 Page No.: 36


Principles of C Programming Unit 2

operations can be performed on it. This is a fairly formal, mathematical


definition of what a type is, but it is traditional (and meaningful). There are
several implications to remember:
1. The “set of values'' is finite. C's int type cannot represent all the integers;
its float type cannot represent all floating-point numbers.
2. When you're using an object (that is, a variable) of some type, you may
have to remember what values it can take on and what operations you
can perform on it. For example, there are several operators which play
with the binary (bit-level) representation of integers, but these operators
are not meaningful for and may not be applied to floating-point operands.
3. When declaring a new variable and picking a type for it, you have to
keep in mind the values and operations you will need.
Now let us see how integers and variables are declared. Assigning an
identifier to a type is called type declaration. Declaration is used to tell the
compiler the name and type of the variable you will use in your program.
The syntax of variable declaration and some examples are given below. In
its simplest form, a declaration consists of the type, the name of the variable,
and a terminating semicolon.
Syntax:
type ‘variable’
For example:
int a;
The above statement declares an integer variable a.
We can also declare several variables of the same type in one declaration,
separating them with commas as shown below.
long int a1, a2;
The placement of declarations is significant. They must either be placed at
the beginning of a function, or at the beginning of a brace-enclosed block of
statements, or outside of any function.
You cannot place them just anywhere (i.e. they cannot be interspersed with
the other statements in your program). Furthermore, the placement of a
declaration, as well as its storage class, controls several things related to its
visibility and lifetime, as we will see later.

Sikkim Manipal University B2074 Page No.: 37


Principles of C Programming Unit 2

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

Sikkim Manipal University B2074 Page No.: 38


Principles of C Programming Unit 2

14. The __________ consists of an equal sign and an expression, which is


usually a single constant.
15. A single declaration statement can contain variables of different types.
(True/False)

2.7 Rules for Naming Variables and Assigning Values to


Variables
Within limits, you can give your variables and functions any names you want.
These names formally called “identifiers'' and consist of letters, numbers,
and underscores.
Rules to be followed for constructing the Variable names (identifiers) are:
1. They must begin with a letter and underscore is considered a letter.
2. It must consist of single letter or sequence of letters, digits or
underscore character.
3. Uppercase and lowercase are significant. For ex: Sum, SUM and sum
are three distinct variables.
4. Keywords are not allowed in variable names. (The words such as int
and for which are part of the syntax of the language)
5. Special characters except the underscore are not allowed.
6. White space is also not allowed.
7. There is no restriction on length of variable name in theory, but
extremely long ones get tedious to type after a while, and the compiler is
not required to keep track of extremely long ones perfectly. (What this
means is that if you were to name a variable, say,
supercalafragalisticespialidocious, the compiler might get lazy and
pretend that you'd named it super- calafragalisticespialidocio, such that
if you later misspelled it super-calafragalisticespialidociouz, the compiler
would not catch your mistake. Nor would the compiler necessarily be
able to tell the difference if for some perverse reason you deliberately
declared a second variable named supercalafragalisticespialidociouz.)
Now let us see how you can assign values to variables. The assignment
operator = assigns a value to a variable. For example,
Num = 1;
sets Num to 1, and
a = b;

Sikkim Manipal University B2074 Page No.: 39


Principles of C Programming Unit 2

sets a to whatever b's value is. The expression


inc = inc + 1;
is, as we have mentioned elsewhere, the standard programming idiom for
increasing a variable's value by 1. This expression takes inc's old value,
adds 1 to it, and stores it back into inc. (C provides several “shortcut''
operators for modifying variables in this and similar ways, which we'll meet
later.)
We have called the = sign the “assignment operator'' and referred to
“assignment expressions'' because, in fact, = is an operator just like + or -.
C does not have “assignment statements''; instead, an assignment like a = b
is an expression and can be used wherever any expression can appear.
Since it is an expression, the assignment a = b has a value, namely, the
same value that's assigned to a. This value can then be used in a larger
expression; for example, we might write
c = a = b;
Which is equivalent to?
c = (a = b);
and assigns b’s value to both a and c. (The assignment operator, therefore,
groups from right to left.) Later we will see other circumstances in which it
can be useful to use the value of an assignment expression.
It is usually a matter of style whether you initialize a variable with an
initializer in its declaration or with an assignment expression near where you
first use it. That is, there is no particular difference between
int a = 10;
and
int a;
/* later... */
a = 10;
Now let us write one program that shows the addition of two integer
numbers.

Sikkim Manipal University B2074 Page No.: 40


Principles of C Programming Unit 2

Program 2.3: Print addition of two numbers

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

Sikkim Manipal University B2074 Page No.: 41


Principles of C Programming Unit 2

 Every C program consists of one or more functions, one of which must


be called main. The program will always begin by executing the main
function. Additional function definitions may precede or follow the main.
 Integers are whole numbers with a range of values supported by a
particular machine. Generally, integers occupy one word of storage, and
since the word sizes of machines vary (typically, 16 or 32 bits) the size
of an integer that can be stored depends on the computer.
 Keywords are reserved words of programming language. Keywords
cannot be used as identifiers.
 A variable (also called an object) is a place where you can store a value.
A declaration tells the compiler the name and type of a variable you will
be using in your program. The assignment operator = assigns a value to
a variable.

2.9 Terminal Questions


1. Explain the history of C language.
2. Write down features of C language.
3. What are the major components of a C program?
4. What significance is attached to the function main?
5. Write down different steps of compiling and executing a C program.
6. What are arguments? Where do arguments appear within a C program?
7. Distinguish between signed and unsigned integers.
8. What are the components of declaration statement?
9. State the rules for naming a variable in C.

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

Sikkim Manipal University B2074 Page No.: 42


Principles of C Programming Unit 2

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

Sikkim Manipal University B2074 Page No.: 43


Principles of C Programming Unit 2

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.

Reference and further reading:


1. Balaguruswami , “Programming In Ansi C 5E” Tata McGraw-Hill
2. Ajay Mittal (2010), “Programming In C: A Practical Approach”, Published
by Pearson education.

Sikkim Manipal University B2074 Page No.: 44

You might also like