To save content items to your account,
please confirm that you agree to abide by our usage policies.
If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account.
Find out more about saving content to .
To save content items to your Kindle, first ensure no-reply@cambridge.org
is added to your Approved Personal Document E-mail List under your Personal Document Settings
on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part
of your Kindle email address below.
Find out more about saving to your Kindle.
Note you can select to save to either the @free.kindle.com or @kindle.com variations.
‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi.
‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.
In the previous chapter we have learnt to write simple C programs. The statements in the programs are executed sequentially, i.e., one after another as they appeared in the program. However, in practice, the problems are not so simple. We need to take decision whether a particular set of statements will execute or not; instead of one group of statements another group of statements may execute; even group of statements may execute repeatedly for known number of times or until certain condition are met. Thus the order of execution of statements has to be controlled. The statements which control the order of execution is known as Control statement. There are two type of control statement in C. These are:
• Branching and
• Looping
Branching statement decides what actions to take and loop statement decides how many times to execute a certain action. In this chapter, we discuss on several branching statements.
Branching is so called because the program chooses to follow one branch or another. This type of statements is also known as conditional statement as depending on the condition a certain set of statement(s) executes. C has mainly two branching statements – if statement and switch statement.
IF STATEMENT
The most important branching statement is if statement. It handles one way as well as two way decision. The if statement consists of two parts – a test expression and one or more statements or block of statements. The decision is taken based on the expression given in parenthesis with the if statement. If the expression is true then a particular statement or block of statements gets executed.
Initially when computer was invented, people used to communicate with them by on and off switches denoting primitive instructions. Then machine language was introduced, where programmer have to mention instructions as well as operands and addresses in binary format, i.e., using 0 and 1. Both writing and understanding a machine language program is difficult. The functionality of this program is difficult to understand, and a person going through it may not be sure of what can be achieved through the program.
Assembly language program is one in which symbols such as letters, digits and special characters, are used for operation part, address part and other parts of instruction code. Both machine language and assembly language are referred as low level languages as the coding a problem is at the individual instruction level, i.e., for each line of a program written in these languages one and only one machine instruction is executed. Assembly language is considered as a second generation language. Computers based on different processors have got their own assembly languages which depend on the architecture of the processor. An assembler translates a program written in assembly language to machine language code.
Output of an assembler is an object file with extension ‘.obj’. It is the role of linker (another system-software) to make executable .exe file from it. Another system-software called loader, present in operating systems, loads the executable on memory when the program is required to run. The process is illustrated in Fig. 3.1. Process for producing executable file.
Typically, assemblers make two passes over the assembly file, i.e., reads the assembly program twice. (One pass assemblers with more complicated design are also available.)
A flowchart is a type of diagram that represents an algorithm or process. The steps involved in the process are shown as boxes of various kinds. Arrows that connects the boxes depicts the correct order in which the steps should be performed. This diagrammatic representation illustrates a solution to a given problem. As a diagram is equivalent to 1000 words, flowcharts helps people working in a project to understand the problem very easily.
flowcharts are broadly classified into two categories:
1. Program flowchart
2. System flowchart.
Program flowcharts are symbolic or graphical representation of computer programs in terms of flowcharting symbols. They contain the steps of solving a problem unit for a specific result; System flowcharts, on the other hand, contain solution of many problem units together that are closely related to each other and interact with each other to achieve a goal.
In this book, concentration will be on program flowcharts as this book is mainly on a programming language. A program flowchart is an extremely useful tool in program development activity in the following respects:
1. Any error, omission or commission can be easily detected from a program flowchart than it can be from a program.
2. A program flowchart can be followed easily and quickly.
3. It can be referred if program modifications are needed in future.
Follows a program flowchart to ‘find maximum among three numbers’ to give idea about the look of a program flowchart.
‘Computer’ as the name suggests is meant for computation. So knowledge of computer is not possible without the knowledge of various number systems, number formats and their advantages and drawbacks. That is why mathematicians always played a major role in the advancement of computer science.
From pre-historic age when human beings were formed by evolution, numbers are being used to count, label and measure. Initially tally marks were used for counting. Bones, cave pictures from pre-historic age have been discovered with marks cut into them that are similar to tally marks. It is assumed that these tally marks were used for counting elapsed time, such as numbers of days or to keep count of animals. Tally mark was not a positional number system. From tally mark evolved the Roman number system. All these systems have limitations of representing large numbers. Historically it is said that number system with place value was first used in Mesopotamia, from 3400 BC onwards. In India, Aryans used the word SHUNYA in Sanskrit to represent void. This is used as ZERO in mathematics. Greeks were confused about using zero. But eventually this place holder developed today's mostly used positional number system where the value of the number depends on the position of the digits. For example 349= 3*102 + 4*101 + 9*100 as 9 is at position 0, 4 is at position 1 and 3 is at position 2. And obviously the number 1000000 is larger than the number 999999. In fact the value of a number in a positional number system depends on two things, one position and the other is base of that number system.
So far the data types that are used in our programs are primitive or basic data types. However, C language provides the facilities to construct our own data types. These are known as user-defined data types. User-defined data type, also known as derived data type is constructed using primitive data types and/or other user-defined data type.
Already we have seen that array is a collection of homogeneous elements. But in real life, we need to store heterogeneous elements that are highly related to each other and thus demands to store as a single logical unit. For example, to store the information about a student, we need to store roll number which is integer, name which is a string, fees may be of type float and so on. Structure helps us to handle this situation.
STRUCTURE
A structure is a collection of heterogeneous elements. Basically it provides a template to define a set of similar or dissimilar elements under a single name. It helps programmers to group heterogeneous but highly related data elements into a single logical unit. It is a user-defined data type that constructed using primitive data types. To declare a structure, the keyword struct is used. The general form to declare a structure the statement is given below: where struct is a keyword to define a structure. The structure name is called tag and it specifies the name of the structure being defined. Members of the structure may be of same or different data types. They may be of primitive data type or any derived data type.
Until now we have dealt with various operators. All these operators are operated on constants and variables of different data types, i.e., they are operated on byte level. But we are not able to get scope to see within these data types that how they are actually constructed with the individual bits. Also we are not able to set the individual bits within the bytes. If we write int x = 5; we know that the binary equivalent of 5, i.e., 101 will be stored in some memory location named x. As it is an integer variable it occupies 2 bytes of memory. So the total bit pattern of it will be 00000000 00000101. We know it but are never able to test that whether it is the actual bit pattern or not. This is an example but it was not possible for us to check or set the individual bit. But bit level operation is very important when a program interacts directly with the hardware. In this chapter, we will learn about bit by bit manipulation.
Another important feature of C language is it provides a set of operators that operate on individual bits of a variable. They are known as bitwise operators. Apart from hardware interaction these operators help us to save memory and make some computation faster. But these operators can only be applied to integral operands, i.e., integer and character operands, whether signed or unsigned.
We can define language as a mode of communication by which we can express ourselves to others. At different regions of the world, with respect to different geographical diversity, the residence, outfits, food habits and behaviour of the respective residents generally differ a lot. But one thing remains always common. Every individual has his own way of communication; which implies he has a language of his own. In this current digital era, even though human life has advanced immensely in every aspect but to express their heartiest feelings and emotions they have always taken the help of any suitable language (linguistic/non linguistic) since the earliest time. In that ancient age, the language did not sound very polished or flawless to hear and the early men used to apply some weird sounds or signals to communicate. Then, that was the ancient representation of the language. With the evolution of time, the language has also simultaneously evolved with ups and downs and has reached its present form. Naturally language differs from one region to another and exists in different forms and alphabets. Somewhere it is known as English, somewhere Bengali, somewhere Hindi, Spanish, French; and somewhere it is called German. It means in every particular region the language has a particular name. But no individual or group of people or organization have created this present form of language, instead this gradual development is spontaneous and has evolved from the ‘Hoom’, ‘Haam’ like sounds of the ancient era. Hence, this language is called Natural Language. Other than this, another form of language exists, the form of which has been developed by some people.
The C programming language is one of the most, academically as well as industrially, important programming languages in the world. It was unveiled in 1972 and since then, with gradual enhancements and enrichments, it has successfully established itself as a powerful language for programming microcontrollers, operating systems and various commercially significant software packages. Having unique features like block structure, stand alone functions and a rich set of keywords with very few restrictions, it is aptly regarded as the ‘Mother Language’ among programming languages. Hence it is necessary for every programmer to learn and, often, use C.
About the Book
Today, in any corner of the world, there is a necessity to learn computer programming for people to survive in any industry. All students do not possess technical background and therefore find learning C programming difficult. Keeping a strong focus on industrial requirements and the limitations of students from non-technical backgrounds, we have written this new book on C programming to enable students of non-technical as well as technical backgrounds learn this marvelous programming language in a completely new way.
This book is completely different from other popular C programming books available in the market. It teaches programming to someone who wants to learn how to program in C for the first time in his/her life with no programming knowledge at all. In other words, this book on C programming is for absolute beginners of programming but at the same time it outclasses several other C programming books in the market with its coverage. Although targeted at complete beginners, this book covers many advanced concepts in C programming and enables a non-programmer to develop into a competitive C programmer ready to face job interviews on C.
In the previous chapters we have learnt how variables can be declared and initialized, what are the different operators available in C and using these operators how a meaningful expression can be written. Now, we will discuss how the input and output operations are carried out in C.
C language does not have any input or output statement. All the input and output operations are accomplished using some library functions. These are included in stdio.h and conio.h header files. These input and output functions or in short, I/O functions are categorized into three groups. These are:
1. Console I/O functions which deal with keyboard and monitor,
2. Disk I/O functions which deal with hard disk or floppy disk and
3. Port I/O functions which deal with different port.
In this chapter, we will discuss only about some console I/O functions that are used frequently. These console I/O functions can be categorized into formatted I/O functions and unformatted I/O functions.
FORMATTED I/O FUNCTIONS
Formatted input refers to read or input data using keyboard in a particular format and formatted output refers to print or display (i.e., output) data or message in the VDU in a formatted way means when a set of values will be displayed, the sequence of the fields, their widths, spaces among the values, etc. can be specified. The standard library function scanf( ) is used for formatted input operation and printf( ) is used for formatted output operation.
Before we write meaningful programs in C language, let us have a look at the various building block of C language. When we learn some natural language, first we learn about the alphabets, punctuation marks, etc., i.e., the symbols used in that language. Then using these symbols we learn how to construct word, sentence, etc. We follow the same strategy to learn C language. In this chapter, first we will learn about the symbols that can be used in C language. Then, step by step we will learn to construct constants, variables and will also learn about different data types. Constants and variables are the basic data objects that are manipulated in a program. Data type is the type of an object which determines the set of values it can have. These building blocks will be discussed in this chapter.
C CHARACTER SET
A character is a basic element used to construct commands and expressions. It may be any alphabet, digit or special symbol. The valid alphabets, numbers and special symbols allowed in C are followings:
CONSTANTS
A constant is an entity that does not change its value throughout the program. Constant may be of different types – integer constant, real constant, character constant, string constant, etc.
Integer Constants
An integer constant consists of one or more digits but within the range of −32768 to +32767. It is a whole number. So, it does not contain any decimal point. There should not be any spaces, commas or special characters within the digits. An integer constant may be positive or negative. But + sign is optional.