Arthur Brisbane was a run-of-the-mill newspaperman, no better and no worse than a he receives free publicity in newspa Balaguruswamy OOP with C++. J2P and P2J Ver 1 J2P and P2J Ver 1 Balaguruswamy Programming with C - 2nd Edition - Byron J2P and P2J Ver 1 J2P and P2J Ver 1 . Save As PDF Ebook Programming In Ansi C By E Balaguruswamy 5th.

Ansi C Balaguruswamy Pdf

Language:English, French, German
Genre:Academic & Education
Published (Last):11.04.2016
ePub File Size:20.44 MB
PDF File Size:9.87 MB
Distribution:Free* [*Registration Required]
Uploaded by: ALEEN J2P and P2J Ver 1 J2P and P2J Ver 1 Balaguruswamy OOP with C++. Balaguruswamy OOP with C++. Download it here: Ansi c Balaguruswamy-C - Ebook download as PDF File .pdf) or read book online.

Adding Two Numbers 6 Sample Program 3: Interest Calculation 8 Sample Program 4: Use of Subroutines 10 Sample Program 5: ELSE Statement 5. ELSE Statements 5. Operator 5. Free Altering the Size of a Block: Some Guidelines Bit-level Programming Appendix II: Characters Appendix III: Projects ,? Since C combines the features of high—level language with the elements of the assembler, it is suitable for both systems and applications programming. It is undoubtedly-the most widely used general-purpose language today.

The version that incorporates the new features is now referred to as C The fourth edition of ANSI C has been thoroughly revised and enlarged not only to incorporate the numerous suggestions received both from teachers and students across the country but also to highlight the enhancements and new features added by C The second chapter discusses how to declare the constants, variables and data types.

The third chapter describes the built-in operators and how to build expressions using them. The fourth chapter details the input and output operations. Further, decision making and looping is discussed in Chapter six, which covers while, do and for loops. Arrays and ordered arrangement of data elements are important to any programming language and have been covered in chapters seven and eight.

Strings are also covered in Chapter eight. Chapters nine and ten are on functions, structures and unions. Pointers, perhaps the most difficult part of C to understand, is covered in Chapter eleven in the most user-friendly manner. The above organization would help the students in understanding C better if followed appropriately. Each major feature of the language is treated in depth followed by a complete program example to illustrate its use.

The sample programs are meant to be both simple and educational. Two new projects are added at the end of the book for students to go through and try on their own. Each chapter includes a section at the, beginning to introduce the topic in a proper per- spective.

It also provides a quick loqkyiuto the features that are discussed in the chapter. Wherever necessary, pictiinal dehciifztions of concepth are included to -improve clarity and to facilitate better understanding. The Just Remember section at theend of the chapters lists out helpful hints and possible problem areas. Numerous chapter-end questions and exercises provide ample opportunities to the readers to review the concepts leamed and to practice their applications.

Supplementary Material With this revision we have tried to enhance the online learning center too. The supplemen- tary material would include the following: For the Instructor Cl Solutions to the debugging exercises For the Student D Exclusive project for implementation with code, step-by-step description and user manual El Code for the two projects given in the book D Two mini projects D Reading material on C This book is designed for all those who wish to be C programmers, regardless of their past knowledge and experience in programming.

But this strange sounding language is one of the most popular computer languages today because it is a structured, high-level, machine independent language. It allows software developers to develop programs without worrying about the hardware platforms where they will be implemented.

Although it never became popular in USA, it was widely used in Europe. Subsequently, several languages were announced. C uses many concepts from these languages and added the concept of data types and other powerful features.

This operating system, which was also developed at Bell Laboratories, was coded almost entirely in C. UNIX is one of the most popular network operating systems in use today and the heart of the Internet data superhighway. For many years, C was used mainly in academic environments, but eventually with the release of many C compilers for commercial use and the increasing popularity of UNIX, it began to gain widespread support among computer professionals.

Today, C is running under a variety of operating system and hardware platforms. The rapid growth of C led to the development of different versions of the language that were similar but often incompatible. This posed a serious problem for system developers. They continue to i. The result was the standard for C. This version is usually referred to as C The history andudievelopment of C is illustrated in Fig. We, therefore, discuss all the new features added by C99 in an appendix separately so that the readers who are interested can quickly refer to the new material and use them wherever possible.

It is a robust language whose rich set of built-in functions and operators can be used to write any complex program. The C compiler combines the capabilities of an assembly language with the features of a high-level language and therefore it is well suited for writing both system software and business packages. In fact, many of the C compilers available in the market are written in C.

This is due to its variety of data types and powerful operators. For example, a program to increment avariable from 0 to takes about one second in C while it takes more than 50 seconds in an interpreter BASIC. Several standard functions are available which can be used for developing programs. C is highly portable. This means that C programs written for one computer can be run on another with little or no modification.

A proper collection of these modules would make a complete program. This modular structure makes program debugging, testing and maintenance easier. Another important feature of C is its ability to extend itself. A C program is basically a collection of functions that are supported by the C library. We can continuously add our own functions to C library.

4 Exam Hours

With the availability of a large number of functions, the programming task becomes simple. The main is a special function used by the U system to tell the computer where the program starts. Every program must have exactly one main function. Txi case, the closing brace also marks the 'end. The function body contains a set of instructions to perform the given task. In this case, the function body contains three statements out of which only the printf line is an executable statement.

Although comments can appear anywhere, they cannot be nested in C. That means, we cannot have comments inside comments. 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 and operations of a program and serve as an aid to debugging and testing. We shall see the use of comment lines more in the examples that follow.

The concepts of compilation and linking are explained later in this chapter, The printf function causes everything between the starting and the ending quotation marks to be printed out. In this case, the output will be: I see. I remember Note that the print line ends with a semicolon. Every statement in C should end with a semicolon ; mark.

This can be achieved by adding another pr-intf function as shown below: These arguments are simply stnngs of characters to be printed out. This combination is collectively called the newline character A newline character Instructs the computer to go to the next new line. However, note that there is no space between , and I.

For example, the statement - pr1'ntf "I see, n I remember! However, this is not necessary for the functions print]: See Chapter 4 for more on input and output functions. C does make a distinction between uppercase and lowercase letters. In C, everything iswritten in lowercase letters. However, uppercase letters are used for symbolic names; representing constants.

Figure 1. All C programs need a main function. Following forms are allowed. Thekeyword V0 d means that the function does not return any information to the operating system an int means that the function returns aniinteger value to the operating system.

Zf", amount ; This program when executed will produce the following output: It is a good practice to use comment lines in the beginning to give infhrmation such as name of the program, author, date, etc. Comment characters are also used in other lines to indicate line numbers.

The words number and amount are variable names that are used to store numeric data. The numeric data may be either in integer form or in real form. In C, all variables should be declared to tell the compiler what the variable names are and what type of data they hold. The variables must be declared before they are used. Declaration statements must appear at the beginning of the functions as shown in Fig. All declaration statements end with a semicolon; C supports many other data types and they are discussed in detail in Chapter 2.

Ansi c Balaguruswamy-C Language.pdf

A list of keywords is given in Chapter 2. Data is stored in a variable by assigning a data value to it. This is done in lines 8 and The next statement is an output statement that prints the value of number. The print statement. Note that these arguments are Separated by a comma. The newline character n causes the next output to appear on a newline. The second and third lines begin with ltdefine instructions. Whenever a symbolic name is encountered, the compiler substitutes the value associated with the name automatically.

These values remain constant throughout the execution of the program. QMfor a period of 10 years with an initial"; 2 50 investment of Therefore I V: For example, the statement.

They can also be declared as - All computations and printing are accomplished in a while loop. In this case as long as the value of year is less than or equal to the value of PERIOD, the four statements that follow while are executed. Note that these four statements are grouped by braces. The concept and types of loops are discussed in Chapter 6. They are discussed in Chapter 3. The program shown in Fig. The program will print the following output.

The values of a and b are passed on to x and y respectively when the function mul is called. The standard mathematical functions are defined and kept as a part of C math library. If we want to use any of these mathematical functions, we must add an lfinolude instruction in the program. The program calculates cosinevalues for angles 0, 10, 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: Preprocessor directives are placed at the beginning of a program.

A function is a subroutine that may include one or more state- 7 1. To write a C program, we first create functions and then put them together. A C program may contain one or more sections as shown in Fig. The documentation section consists of a set of comment lilies giving the name of the pro.

The link section provides instructions to the compiler to link functions from the system library. There are some variables that are used in more than onelfunction. Such variables are called global variables and are declared in the global declaration section that the functions. Every C program must. This section contains two parts, declaration part and executable part.

The declaration part declares all the variablesrused in. There is at least one statement in the executable part. These two parts must appear between the opening andlthe closing braces. 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 ;. All sections, except the main function section may be absent when they are not required.

That is, the C compiler does not care, where on the line we begin typing, While this may be a licence for bad programming, we should try to use this fact to our advantage in , developing readable programs.

Although several alternative styles are possible, we should. First of all, we must develop the habit of writing programs in lowercase letters. C program statements are written in lowercase letters. A proper indentation of braces andbtatements would make a program easierto read and debug.

In this book, each statement is written on a separate line. This is very important for debugging and testing the program. Creating the program; 2.

Linking the program with functions that are needed from the C library; and; 4. Executing the program. These are: The operating sysi tem, which is an interface between the hardware and the user handles the execution of user programs. DOS for nncrocomputers. Source Program Object code Check your system manual. Now the program is ready for compilation. The translation is done alter examining each instruction for its correctness. Thisprogram is known as object code. For example, if the program is using exp function, then the object code of this function should be, brought from the math library of the system and linked to the main program.

Under UNIX, the linking is automatically done if no errors are detected when the cc command is used. If any mistakes in the syntax and semantics of the language are discovered, they are listed out and the compilation process ends right there. The errors should be corrected in the 5 source program with the help of the editor and the compilation is done again.

The command a. During execution, the program may request for some data to be entered through the keyboard. Sometimes the program does not produce the desired results.

Perhaps, something is wrong with the program logic or data. Then it would be necessary to correct the source program or the data. For example, the commands cc —c modl. They can be linked together by the command cc mod1. Maruti Write a program to produce a table showing popularity of various cars in four cities. Tabulation ends when the letter X is read in place of a city code. Enter the letter X to indicate end. Example 8. Every time a character is read.

Write a program to read a series of words from a terminal using scanf function The program shown in Fig. Therefore the index value c-1 gives the position where the null character is to be stored. Note that the string 'Oxford Road' is treated as two words while the string 'Oxford-Road' as one word. Oxford Road.

Think and Grow Rich by Napoleon Hill

When the newline character is read signalling the end of line. When the loop is exited. Enter text. Programming in C is interesting. National Centre for Expert Systems, Hyderabad. The program is shown in Fig. We use a for loop to copy the characters contained inside string2 into the string1.

The loop is terminated when the null character is reached. Note that we are again assigning a null character to the string1. Enter a string? When the field width is less than the length of the string, the entire string is printed. The integer value on the right side of the decimal point specifies the number of characters to be printed.

When the number of characters to be printed is specified as zero, nothing is printed. The minus sign in the specification causes the string to be printed left-justified. CProgramming CProgramming.

The values from 91 to 96 are excluded using an if statement in the for loop. Write a program to concatenate the three parts into one string to be called name. The null character is not copied. The program is given in Fig. In the first loop. Three for loops are used to copy the three strings.

Note that we have stored a space in the fourth cell. At the end, we place a null character to terminate the concatenated string name. Write a program to read two string constants into s1 and s2 and compare whether they are equal or not.

If they are not, join them together. Then copy the contents of s1 to the variable s3. At the end, the program should print the contents of all the three variables and their lengths.

During the first run, the input strings are "New" and "York". These strings are compared by the statement. During the second run, the two strings s1 and s2 are equal, and therefore, they are not joined together. In this case all the three strings contain the same string constant "London". Example 9. A program with three user-defined functions is given in Fig.

Since both the called functions contain no arguments, there are no argument declarations. The printline function, when encountered, prints a line with a length of 35 characters as prescribed in the function. The value function calculates the value of principal amount after a certain period of years and prints the results. The following equation is evaluated repeatedly:.

Most of the program is identical to the program in Fig. The function call value principal. In fact. Output Principal amount? The input prompt and scanf assignment statement have been moved from value function to main. The modified program with function arguments is presented in Fig. On execution of the function call. The variables principal. The function header of value has three formal arguments p. One major change is the movement of the printf statement from value to main.

Also extend the versatility of the function printline by having it to take the length of the line as an argument. The modified program with the proposed changes is presented in Fig.

Programming in ANSI C by Balaguruswamy.pdf

Fig 9. The prototype declaration double power int. The array elements are read from the terminal. Standard deviation of a set of n values is given by n2 S. Use functions to calculate standard deviation and mean. A program to sort an array of integers using the function sort is given in Fig. Its output clearly shows that a function can change the values in an array passed as an argument.

As soon it is done. The output clearly shows that the value assigned to m in one function does not affect its value in the other functions. When executed. A program with two subprograms function1 and function2 is shown in Fig. When main is active. Because x has been declared 'above' all the functions.

A program to illustrate the properties of global variables is presented in Fig. The local x hides its visibility here. In fun2. Note that variable x is used in all functions but none except fun2.

Structure definition along with the program is shown in Fig. Using this structure. The variable name with a period and the member name is used like an ordinary variable. Goel 10 January M. It also performs member-wise comparison to decide whether two structure variables are identical. Goel 10 January Note that a member name can be any valid C name and can be the same as an existing structure variable name. We have declared a four-member structure.

The linked name total. The grand-total is given by total. We have also declared an array total to keep the subject-totals and the grand-total. The modified program is shown in Fig. You may notice that the use of array name for subjects has simplified in code. Note that both the function update and the formal parameter product are declared as type struct stores. The function mul is of type float because it returns the product of price and quantity. The function update receives a copy of the structure variable item as one of its parameters.

A program to update an item is shown in Fig. The entire structure returned by update can be copied into a structure of identical type. It is done so because the function uses the parameter product to receive the structure variable item and also to return the updated values of item.

XYZ Price: Memory addresses are unsigned integers. The program clearly shows how we can access the value of a variable using a pointer. It also illustrates the order of evaluation of expressions. You may notice that the value of the pointer ptr is and the value it points to is The program and output are shown in Fig. Since incrementing an array pointer causes it to point to the next element.

A program to count the length of a string is shown in Fig. Since a string is always terminated by the null character. When the while loop is terminated.

Note that each character occupies one memory cell byte. The function exchange receives the addresses of the variables x and y and exchanges their contents. The value returned by the function is of type double. With table. During the execution of table.

When table is called in the statement table y. Note that y is not followed by a parameter list. A program to print the function values over a given range of values is shown in Fig. The printing is done by the function table by evaluating the function passed to it by the main. Thus the function y is evaluated over the range 0. Note that the pointer ptr of type struct invent is also used as the loop control index in for loops.

The program highlights all the features discussed above. A program to illustrate the use of a structure pointer to manipulate the elements of an array of structures is shown in Fig.

Output Enter two string constants? It employs the method of bubble sorting described in Case Study 1 in the previous chapter. A program to sort the list of strings in alphabetical order is given in Fig. We enter the input data via the keyboard and the program writes it. A program and the related input and output data are shown in Fig. The end of. This may be control-D in other systems. The contents of DATA file are read. It uses three files simultaneously and therefore we need to define three-file pointers f1.

Notice that when we type The integer values are read from the terminal and are written to the file DATA with the help of the statement putw number. The next step is to open all the three files. DATA for reading. Data is read using the function fscanf from the file stdin.

The data from the file. While reading from a file. When we input filename as TETS. TEST 10 20 Type filename again. The second time. Z We are reading the file twice. The loop is terminated as soon as it crosses it. During the first reading. Since every read causes the position to move forward by one position. This is achieved by the function fseek fp. This statement also tests whether the file pointer has crossed the file boundary or not.

A program employing ftell and fseek functions is shown in Fig. For reading the file from the end. It uses a structure definition to describe each item and a function append to add an item to the file. Note that reading and displaying are done under the control of a while loop. The file is reopened for reading and its contents are displayed. The loop tests the current file position against n and is terminated when they become equal.

On execution. After appending the items. YYY Item number: XXX Item number: Figure The for loop that follows immediately writes the remaining 7 arguments to the file TEXT. YYY The program also illustrates the use of pointer variable for storing and accessing the table values.

It tests for availability of memory space of required size. If it is available. Note that the original contents of the buffer remains same even after modification of the original size. The output illustrates that the original buffer size obtained is modified to contain a larger string. Printing algorithm is as follows. Note that the counting does not include the item — contained in the dummy node.Structure definition along with the program is shown in Fig.

Write a program compute the area of the triangle given the values of a, b and c. Outputs of the program run for two different inputs are also shown in Fig. The program also illustrates the use of pointer variable for storing and accessing the table values. Example 3.

Two new projects are added at the end of the book for students to go through and try on their own. The general format of fscanf is as follows: The values of a and b are passed on to x and y respectively when the function mul is called. The concepts of compilation and linking are explained later in this chapter, The printf function causes everything between the starting and the ending quotation marks to be printed out.