




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
C programming solutions and details
Typology: Study notes
1 / 108
This page cannot be seen from the preview
Don't miss anything!





























































































On special offer
MOHAMMAD ABIR REZA
Department of Computer Science & Engineering, Comilla University
MOHAMMAD ABIR REZA (CSE-5TH BATCH) department of Computer science & engineering
ANSI C REVIEW QUESTION SOLUTION
Department of Computer Science & Engineering
Comilla University
MOHAMMAD ABIR REZA (CSE-5TH BATCH) department of Computer science & engineering
ANSI C REVIEW QUESTION SOLUTION
Overview of C
A #define is a preprocessor compiler directive. We often use certain unique constants in a program. These constants may appear repeatedly in a number of places in the program. We face two problems in the subsequent use of such programs. These are 1. problem in modification of the program and 2. problem in understanding the program. Assignment of such constants to a symbolic name frees us from these problems. So if we want to define values to a symbolic constant and to define any statements which uses more frequently in the program we use the #define directive. Whenever a symbolic name is encountered, the compiler substitutes the value associated with the name automatically. To change the value, we have to simply change the definition.
A constant is defined as follows:
#define symbolic-name value of constant
C programs are divided into modules or functions. Some functions are written by users, like us, and many others are stored in the C library. Library functions are grouped category-wise and stored in different files known as header files. If we want to access the functions stored in the library, it is necessary to tell the compiler about the files to be accessed.
This is achieved by using the preprocessor directive #include as follows:
#include
Page 4
Mohammad Abir Reza [CSE- 5TH^ BATCH] Department of Computer Science & Engineering
Filename is the name of the library file that contains the required function definition. Preprocessor directives are placed at the beginning of a program.
The main is a part of a every C program. C permits different forms of main statement. Following forms are allowed.
main()
int main()
void main()
main(void)
void main void()
int main(void)
The empty pair of parentheses indicates that the function has no arguments. This may be explicitly indicated by using the keyword void inside the parentheses. We may also specify the keyword int or void before the word main. The keyword void means that the function does not return any information to the operating system and int means that the function returns an integer value to the operating system. When int is specified, the last statement in the program must be “return 0”.
It is a good practice to use comment lines in the beginning to give information such as name of the program, author, date, etc. Comment characters are also used in other lines to indicate line numbers. The generous use of comments inside a program cannot be overemphasized. Since comments do not affect the execution speed and the size of a compiled program, we should use them liberally in our programs. They help the programmers and other users in understanding the various functions
Page 6
Mohammad Abir Reza [CSE- 5TH^ BATCH] Department of Computer Science & Engineering
main () Function Section
{
Declaration part
Executable part
}
Subprogram Section
Function 1
Function 2
- -
Function n
(User-defined function)
Fig. An overview of a C program
A C program may contain one or more sections:
The documentation section consists of a set of common lines giving the name of the program, the author and other details, which the programmer would like to use later.
The link section provides instructions to the compiler to link functions from the system library.
The definition section defines all symbolic constants.
There are some variables that are used in more than one function. Such variables are called global variables and are declared in the global declaration
MOHAMMAD ABIR REZA (CSE-5TH BATCH) department of Computer science & engineering
ANSI C REVIEW QUESTION SOLUTION
section that is outside of all the functions. This section also declares all the user- defined functions.
Every C program must have one main() function section. 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 the opening and the closing braces. 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.
Creating the program:
Once we load the UNIX operating system into the memory, the computer is ready to receive program. The program must be entered into a file. The file name can consist of letters, digits and special characters, followed by a dot and a letter c. Examples of valid file names are:
hello.c , program.c
The file is created with the help of a text editor , either ed or vi. The command for calling the editor and creating the file is
ed filename
MOHAMMAD ABIR REZA (CSE-5TH BATCH) department of Computer science & engineering
ANSI C REVIEW QUESTION SOLUTION
cc -c mod1.c
cc -c mod2.c
will compile the source files mod1.c and mod2.c into object files mod1.o and mod2.o. They can be linked together by the command
cc mod1.o mod2.o
we may also combine the source files and object files as follows:
cc mod1.c mod2.o
Only mod1.c is compiled and then linked with the object file mod2.o. This approach is useful when one of the multiple source files need to be changed and recompiled or an already existing object files is to be used along with the program to be compiled.
Compiler and preprocessor
.O .O .O Library
a.out
Fig. Compilation of multiple files
Page 10
Mohammad Abir Reza [CSE- 5TH^ BATCH] Department of Computer Science & Engineering
Chapter -
Constants, Variables and Data Types
(a) Any valid printable ANSII character can be used in an identifier. ( False )
(b) All variables must be given a type when they are declared. ( True )
(c) Declarations can appear anywhere in a program. ( False )
(d) ANSI C treats the variable name and Name to be same. ( False )
(e) The underscore can be used anywhere in an identifier. ( True )
(f) The keyword void is a data type in C. ( True )
(g) Floating point data constants, by default, denote float type values.( False )
(h) Like variables, constants have a type. ( True )
(i) Character constants are coded using double quotes. (False )
(j) Initialization is the process of assigning a value to a variable at the time of declaration. ( true )
(k) All static variables are automatically initialized to zero. ( True )
(l) The scanf function can be used to read only one value at a time. ( False )
(a)The keyword ……………..can be used to create a data type identifier. Answer: int (b) …………… is the largest value that an unsigned short int type variable can store.
Page 12
Mohammad Abir Reza [CSE- 5TH^ BATCH] Department of Computer Science & Engineering
Integer Types: 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 (tipically, 16 or 32 bits) the size of an integer that can be stored depends on the computer. If we use a 16 bit word length, the size of the integer value is limited to the range -32768 to +32767 (that is , -2^15 to +2^15 -1). A signed integer uses one bit for sign and 15 bits for the 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. ANSI C defines these types so that they can be organized from the smallest to the largest. For example, short int represents fairly small integer values and requires half the amount of storage as a regular int number uses. Unlike signed integers, unsigned integers use all the bits for the magnitude of the number and are always positive. 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.
Floating point Types:
Floating point (or real) numbers are stored in 32 bits (on all 16 bit and 32 bit machines), with 6 digits of precision. Floating point numbers are defined in C by the keyword float.
Double Types:
When the accuracy provided by a float number is not sufficient, the type double can be used to define the number. A double data type number is not sufficient, the type double can be used to define the number. A double data type number uses 64 bits giving a precision of 14 digits. These are known as double precision numbers. Double type represents the same data type that float represents, but with a greater precision. To extend the precision further, we may use long double which uses 80 bits.
MOHAMMAD ABIR REZA (CSE-5TH BATCH) department of Computer science & engineering
ANSI C REVIEW QUESTION SOLUTION
Character Types:
A single character can be defined as character (char) type data. Characters are usually stored in 8 bits (one byte) of internal storage. The qualifier signed or unsigned may be explicitly applied to char. While unsigned chars have values between 0 and 255, signed chars have values from -128 to 127.
How can we extend the range of values they represents:
We can extend the range of values of integer types by using the qualifier long and unsigned before int.
We can extend the range of values of floating type data by using double data type. To extend the precision further we may use the qualifier long before double.
The qualifier unsigned may be explicitly applied to char to extend the range of character data type.
An unsigned constant will not have any sign means, it will be having positive values only.
for example if we use short int a; the range of a will be from -32,768 to +32,767(means you can enter any no between this range). On the other hand , if we use
unsigned short int a; the range of a will be doubled and it will have only positive values the range will be 0 to +65,535. WHY WE USE IT?
MOHAMMAD ABIR REZA (CSE-5TH BATCH) department of Computer science & engineering
ANSI C REVIEW QUESTION SOLUTION
A variable is a data name that may be used to store a data value. Unlike constants that remains unchanged during the execution of a program. A variable can take different values at different times during execution, just like character, int, float and double.
A variable may be used to store data value. A variable may take different values at different times during execution of a program. Variables need to declare at the beginning of the body but after the main.
Symbolic names are unique constants. These constants may appear in a number of place in the program. Symbolic names need to be defined at the beginning of a program.
Variables are declared at the beginning of the body but after the main. All variables must be declared before they can appear in executable statements. The syntax for declaring a variable is as follow:
Data-type v1,v2,……..vn;
v1, v2,……..vn are the names of variables. For example, valid declarations are
int count; int number,total; float ratio;
Page 16
Mohammad Abir Reza [CSE- 5TH^ BATCH] Department of Computer Science & Engineering
Symbolic names need to be defined at the beginning of a program. A symbolic name constant is defined as follows:
#define SYMBOLIC-NAME value of constant
Valid example of constant definations are:
#define STRENGTH 100 #define PASS MARK 5
Declaration statement of a variable must end with a semicolon. Definition of a symbolic name must not end with a semicolon. A variable may take different values at different times during execution. Symbolic name should not be assigned any other value within the program by using an assignment statement. Symbolic names are not declared for data type. Its data type depends on the type of constant. But we must declare the data type of a variable. Symbolic names are written in CAPITALS to visually distinguish them from the normal variable names, which are written in lowercase letters. This is only a convention not a rule. An instance of an object is created when a variable is declared. Definition of a symbolic name just defines a name that can be used in the program.
The process of giving initial values to variables is called initialization. C allows ordinary variables, structures, unions and arrays to be given initial values in their definitions. There are basically two sorts of initialization: at compile time, and at run time. Example of initialization :
int a=100; char name = ‘x’; float number =75.84 ;
This statement initializes the variable a to 100. External and static variables are initialized to zero by default. Automatic variables that are not initialized explicitly will contain garbage.
Page 18
Mohammad Abir Reza [CSE- 5TH^ BATCH] Department of Computer Science & Engineering
He can put a line like this before the main function in the program: #define DPR double From that point on, he can use DPR in place of double wherever he wants. For example : he can write DPR my_double_variable; instead of double my_double_variable;
Or he can declare DPR as typedef data type. Such as typedef double DPR; now DPR can be later used to declare double type variables as follows: DPR a; (where a is a variable of double data type).
Enumerated is a user-defined data type provided by ANSI standard. It is defined as follows:
enum identifier {member 1, member 2, … member n};
The “identifier” is a user-defined enumerated data type which can be used to declare variables that can have one values enclosed within the braces (known as enumeration constants). These variables are called enumeration variables.
Declaration:
An enumeration is user-defined data type. Its members are constants that are written as identifiers, though they have signed integer values. These constants represent valued that can be assigned to corresponding enumeration variables.
MOHAMMAD ABIR REZA (CSE-5TH BATCH) department of Computer science & engineering
ANSI C REVIEW QUESTION SOLUTION
In general terms, an enumeration may be defined as
enum identifier {member 1, member 2, … member n};
where enum is a required keyword; identifier is a name that identifies enumerations having this composition; and member 1, member 2, … member n represent the individual identifiers that may be assigned to variables of this type. The member names must differ from one another, and they must be distinct from other identifiers whose scope is the same as that of enumeration.
Once the enumeration has been defined, corresponding enumeration variables can declared as
enum identifier v1, v2, … vn;
where enum is a required keyword, identifier is the name that appeared in the enumeration definition, and v1, v2, … vn are enumeration variables of type identifier.
The enumeration definition can be combined with variable declarations, as indicated below.
enum identifier {member 1, member 2, … member n} v1, v2, … vn;
The identifier is optional in this situation.
Advantage: