You are on page 1of 124

(AMITY CENTER FOR e-LEARNING

)

Computer Programming Using C Language

By:

Ashish Seth Kirti Seth

1

(AMITY CENTER FOR e-LEARNING)

Preface
The question you always might think before learning C language is that why should I learn this language? At present in the market, there are so many languages, using which you can meet most of the industry needs. For the past many years, the solution for the most of the raw needs of the industry has been C language? C is the heart of programming, one who learns this language can command over any language very easily. This book covers more than average discussions of C, It covers the features and functionality of the language in depth. Starting chapter helps the reader to grasp the basic and structure of the language, later chapter deals with the advance features of the language like pointers and file handling. In order to asses your learning the quiz section is appended at the end of each chapter. This assessment system is designed to evaluate the level of knowledge and skills as defined by learning objectives We have tried my best to make the book very useful for the students. Constructive, criticism, and suggestions for the improvement of book are most welcome It is pleasure to acknowledge my sincere gratitude to Prof. Dinesh Khurana (Amity International Business School), Amity University Uttar Pradesh for giving us an opportunity and motivation to write this book. Thanks to our parents who gave us shower of love and believed our ability. we would like to dedicate this work to our lovely daughters baby Verda and baby Vindhya. Finally, we thanks to the Almighty who helped us in disguise in each and every step

Ashish Seth Kirti Seth

2

(AMITY CENTER FOR e-LEARNING)

Index
Chapter No. Title Introduction to C Operators and Expressions Data Input/Output Functions Control Statements Functions Arrays Pointers Structures and Unions Data Files Key to End Chapter Quizzes Bibliography Page No. 1 15 37 48 64 88 106 126 138 150 152

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.

3

(5) Test and debug (find the errors in) the program. (7) Maintain the program.2 Data and Variables 1. This is called the source program. Once you have written the program.1 Introduction C is considered as High Level Languages.2 Decimal Number Variables 1. There are a number of free compilers available on the Internet. C programs are simple text files containing program statements.2. we briefly described the following stages in the development of a computer program: (1) Define the problem.3 Data Types 1. It uses English type statements that are converted to machine statements which are executed by computers. As such. (3) Develop an algorithm (a method) for solving the problem. it is created using a text editor. (Explain how the program works and how to use it). whose task is to convert the statements in the source code to either an intermediate format (called an object file).1 Introduction 1. The source program is compiled by a special program called a compiler.3.3. (4) Write the computer program which implements the algorithm. it must be compiled.4 Form of C Program 1. 4 .1 Reserve words 1.(AMITY CENTER FOR e-LEARNING) Chapter 1 Introduction to C Contents: 1. (2) Analyze the problem. or an executable format which can be run on the computer. (6) Document the program.2.1 Integer Number Variables 1.3.2 Identifiers\ 1.3 Character Variables 1.

we speak of an ‗address‘. For a program to be run. Variable may take different values at different times during execution. We can give a name to a box. • we must not assume that a box contains any value unless we specifically store a value in the box. for instance. Here. we can change the value in a box at any time.(AMITY CENTER FOR e-LEARNING) 1. it must be stored in the computer‘s memory. • the data for an accounting program would include. Thus a variable is a name associated with a particular memory location or. if you wish. given that there may be many data items in memory? Think of memory as a set of boxes (or storage locations). the old one is lost. if we put in a new value. one to hold the side of the square and one to hold the area. We can speak of giving a variable a value. s and a are called variable names. We will call these boxes s and a. we often use variables. etc.2 Data and Variables All computer programs. are written to operate on data. except the most trivial. • the data for a program that teaches Spanish could be an English word that you type in response to a question. television model. ‗address‘ is a variable whose value depends on the person under consideration. But how do we refer to an item of data. In our example. size of population. When data is supplied to a program. Thus we think of memory as a place for holding programs and data. (What are some possible values of these variables?) A variable is a data name that may be used to store a data value. In everyday life. such as 1. respectively. since the values can vary. we will need two boxes. and we will be able to refer to that box by the given name. Each box can hold one item of data. name of school. Important points to remember are: • a box can hold only one value at a time. Variables are a common feature of computer programs. One of the nice things about programming in a high-level language (like C or Java) is that you don‘t have to worry about which memory locations are used to store your data. In particular. for example. one number. among other things. 5 . expenses and income. If we wish. it is a label for the memory location. Other common variables are telephone number. It is very difficult to imagine what programming would be like without them. • the data for a word processing program are the keys pressed while you are typing a letter. or setting a variable to a specific value. we must not assume that the box contains 0. For example: • the data for an action game might be keys pressed or the position of the cursor when the mouse is clicked. For example. that data is also stored in memory. or simply variables. type of car. subject.

2. Thus int is a keyword but Int and INT are not. char.2 Identifiers The C programmer needs to make up names for things such as variables.(AMITY CENTER FOR e-LEARNING) Rules for declaring variable: 1.That is variable total is not the same as toTal or Total. char and while. Uppercase & lowercase are significant . ' $ " # % & ! _ {} [] () < > | +-/*= 1. since only the first eight characters are treated as significant by many compilers. long. float. int can be used only in those places where we need to specify that the type of some item is integer. C's Character Set C does not use. void. every character found on a modern computer keyboard. that is. 4. for. the length should not be normally more than eight characters. Keywords sever as basic building blocks for program statement.while 1. The only characters required by the C Programming Language are as follows: A-Z a -z 0-9 space . A name that he makes up is called a user identifier. case. All keywords have fixed meanings and these meanings can not be changed. do. The variable name should not be a keyword. int. function names and symbolic constants (see next section). goto. nor requires the use of. ANSI standard recognizes a length of 31 characters. Example –break. There are a few simple rules to follow in naming an identifier: 6 . they are usually called reserved words. Keywords are reserved. All keywords are written in lowercase letters only. There are 32 keywords. A keyword has a special meaning in the context of a C program and can be used for that purpose only. you cannot use them as your identifiers. if. They must begin with a letter some systems permit underscore as the first character. : . else.1 Reserve words The C language uses a number of keywords such as int. 2. For example. White space is not allowed. 5.2. However. . As such. 3.

variables that are available to all the program's functions. There are a great many ideas common to programming in any language and C is no exception to this rule. Why? 13 3X %change data-1 my. the rest of the characters should be letters. on the other hand. It demands that you declare the name of each variable that you are going to use and its type. digits or underscores the following are valid identifiers: x x1 x_1 _abc sum Rate averagE the following are not legal identifiers. never use capital letters for variables 1. 7 . So if you haven't programmed before.3 Data Types Now we have to start looking into the details of the C language. In this section we are only going to be discussing local variables. you need to take the rest of this section slowly and keep going over it until it makes sense. C is very fussy about how you create variables and what you store in them. How easy you find the rest of this section will depend on whether you have ever programmed before . If. before you actually try to do anything with it.identifier a(3) C is case sensitive: MyVar and myvar are different identifiers What are good identifiers Careful selection of identifiers makes your program clearer Identifiers should be Short enough to be reasonable to type (single word is norm) Standard abbreviations are fine (but only standard abbreviations) Long enough to be understandable use abbreviations and underscores to separate the words. These are variables that are used within the current program unit (or function) in a later section we will looking at global variables .no matter what the language was. or class. A variable is just a named area of storage that can hold a single value (numeric or character).(AMITY CENTER FOR e-LEARNING) Name of a variable (or any other item you define in program) is called identifier identifier must start with a letter or underscore symbol (_). you have programmed before you'll be wondering what all the fuss is about It's a lot like being able to ride a bike! The first thing you need to know is that you can create variables to store values in.

4e-38 to 3. To declare an int you use the instruction: int variable name. average and so on. A statement of the form a=10.3.a double-precision floating point value. You can think of it as a largish positive or negative whole number: no fractional part is allowed. should be interpreted as take the numerical value 10 and store it in a memory location 8 .2 byte=16bit char --. This is because in each case the 'natural' choice is made for each type of machine.3.valueless special purpose type which we will examine closely in later sections.short for integer.floating point value: ie a number with a fractional part. although it helps if you give them sensible names that give you a hint of what they're being used for . void . C programmers tend to prefer short names! Note: all C's variables must begin with a letter or a "_" (underscore) character.768 to 32767 char -----. total.4 byte=32bit One of the confusing things about the C language is that the range of values and the amount of storage that each of these types takes is not defined. 1. Remember. float .-128 to 127 float -----. You can call variables what you like. To assign a value to our integer variable we would use the following C statement: a=10. char . declares that you want to create an int variable called a.4e+38 Size: int ----.1 Integer Number Variables The first type of variable we need to know about is of class type int . 2 r (that should read as "2 pi r" but that depends upon how your browser has been set-up) would give local variables names of pi and r.names like sum. Ranges of data types: int -------. An int variable can store a value in the range -32768 to +32767.a single character. double .integer: a whole number. The C programming language uses the "=" character for assignment.(AMITY CENTER FOR e-LEARNING) There are five basic data types associated with variables: int . For example. If you are translating a formula then use variable names that reflect the elements used in the formula.-32.1 byte=8bit float --. For example: int a.

number has about seven digits of precision and a range of about 1.3.2 Decimal Number Variables As described above. To declare a variable of type character we use the keyword char. will get mathematicians blowing fuses! This statement should be interpreted as take the current value stored in a memory location associated with the integer variable a. C uses one of two keywords to declare a variable that is to be associated with a decimal number: float and double. The "=" character should not be seen as an equality otherwise writing statements of the form: a=a+10. It very often comes as a surprise to some programmers who learnt a beginner's language such as BASIC that C has no understanding of strings but a string is only an array of characters and C does have a concept of arrays which we shall be meeting later in this course. 1. 9 . A double takes eight bytes to store. To assign a numerical value to our floating point and double precision variables we would use the following C statement: total=0.0.E+303. float A float. Integer variables tend to be used for counting. They are each offer a different level of precision as outlined below.E+36.For example: char c. For example: float total. double sum.A single character stored in one byte. double A double.(AMITY CENTER FOR e-LEARNING) associated with the integer variable a.E-36 to 1. an integer variable has no fractional part. A float takes four bytes to store.3 Character Variables C only has a concept of numbers and characters. 1. or double precision. whereas real numbers are used in arithmetic. add the numerical value 10 to it and then replace this value in the memory location associated with a.3. number has about 13 digits of precision and a range of about 1.E303 to 1.50. . or floating point. sum=12.

which has a very real potential for confusion because a string constant is written between double quotes. or store. You can declare any number of variables of the same type with a single statement. For example. Here is an example program that includes some of the concepts outlined above. Something To Declare Before you can use a variable you have to declare it. printf("Here ").h> main() { int a. printf("answer. c. "). printf("the "). to do this you state its type and then give its name. For example: int a. But for the moment remember that a char variable is 'A' and not "A". declares an integer variable. /* Program#int. average = ( a+b ) / 2 . Later we will be discussing using character strings.. a=10. You have to declare all the variables that you want to use at the start of the program. Later you will discover that exactly where you declare a variable makes a difference. b=6.c Another simple program using int and printf */ #include <stdio. As we have seen above. a character value in a char data type is easy .a character variable is just a symbol enclosed by single quotes. declares three integers: a. printf("is ").. b and c.(AMITY CENTER FOR e-LEARNING) To assign. but for now you should put variable declarations after the opening curly bracket of the main program. printf("\n").b.average. For example. int i. 10 . b. if c is a char variable you can store the letter A in it using the following C statement: c='A' Notice that you can only store a single character in a char variable.

This is just the same as: int i. i=l.(AMITY CENTER FOR e-LEARNING) printf("%d. For more advanced programs the main function will act as a controlling function calling other functions in their turn to do the dirty work! The main function is the first function that is called when your program executes. you cannot have a variable called auto.things will be explained later): pre-processor directives global declarations main() { local variables to function main .average). The form of a C Program All C programs will consist of at least one function. If you are not sure what to use then always use lowercase text in writing your C programs. but the compiler may be able to speed up the operation if you initialise the variable as part of its declaration. Some C compilers store 0 in newly created numeric variables but nothing in the C language compels them to do so. A keyword may not be used for any other purposes. For example: int i=1. } f1() 11 .C. sets the int variable to one as soon as it's created. like UNIX. The only function that has to be present is the function called main. statements associated with function main . uses upper and lowercase text to mean different things. } More On Initialising Variables You can assign an initial value to a variable when you declare it. Note that all keywords are written in lower case . For example. Don't assume that an uninitialised variable has a sensible value stored in it. The layout of C Programs The general form of a C program is as follows (don't worry about what everything means at the moment . but it is usual (when your experience grows) to write a C program that comprises several functions.". C makes use of only 32 keywords which combine with the formal syntax to the form the C programming language.

is to miss off the semicolon. onto the next line. statements associated with function 1 . statements associated with function 2 . Free format also means that you can add as many spaces as you like to improve the look of your programs. A very common mistake made by everyone. () are used in conjunction with function names whereas {} are used as to delimit the C statements that are associated with that function. but you might have missed it! a semicolon (. . without truncation. Also note the semicolon . The C compiler will concatenate the various lines of the program together and then tries to understand them . C is a free format language and long statements can be continued. 12 .) is used to terminate C statements. } .(AMITY CENTER FOR e-LEARNING) { local variables to function 1 . The error message produced by the compiler will relate to a line of you program which could be some distance from the initial mistake. The semicolon informs the C compiler that the end of the statement has been reached. who is new to the C programming language. . etc Note the use of the bracket set () and {}.yes it is there.which it will not be able to do. } f2() { local variables to function f2 .

6 Assignment Operators and Expressions 2.(AMITY CENTER FOR e-LEARNING) Chapter 2 Operators and Expressions Contents: 2. relational and logical operators. In this lesson we will see how arithmetic operators.3 Arithmetic operator 2. A few operators permit only single variable as operand. Table 2-1 presents the set of C operators. Some operators require two operands while others require only one operand. and array.6. The data items on which operators act upon are called operands.8 Miscellaneous Features 2. Table 2-1 C Operators Operator () [] Example f() a[10] Description/Meaning Function call Array reference 13 .1 Conditional Expressions 2. Variables and constants can be used in conjunction with C operators to create more complex expressions.3 Casting C Shorthand Multiple Assignments 2.2 2.8. assignment operators and the conditional operators are used to form expressions.8.7 Bitwise operators 2. unary operators.4 Relational and Logical Operators 2.8.1 2. variables.5 Increment and Decrement operator 2. Most operators allow the individual operands to be expressions.1 Introduction 2.1 Introduction Operators form expressions by joining individual constants. C includes a large number of operators which fall into different categories.2 Precedence of C operators 2.

right-shifted b bits a. 0 otherwise 1 if a >= b.a +a -a *a &a ~a ++a Structure and union member selection Structure and union member selection Value of a Negative of a Reference to object at address a Address of a One's complement of a The value of a after increment The value of a before increment The value of a after decrement The value of a before decrement Size in bytes of object with type t1 Size in bytes of object having the type of expression e a plus b ++ [postfix] a++ . 0 otherwise < > <= >= == != a a a a a a < b > b <= b >= b == b != b 1 if a > b.[postfix] asizeof sizeof sizeof (t1) sizeof e + [binary] ..[prefix] -a .[unary] * [unary] & [unary] ~ ++ [prefix] s->a s.[binary] * [binary] /% a a a a a + * / % b b b b b a minus b a times b a divided by b Remainder of a/b >> << a >> b a << b a. left-shifted b bits 1 if a < b. 0 otherwise 1 if a not equal to b.. 0 otherwise 1 if a equal to b. 0 otherwise 1 if a <= b. + [unary] .(AMITY CENTER FOR e-LEARNING) -> . 0 otherwise 14 .

which assign a value to a variable. which takes three operands and evaluates either the second or third expression. Binary operators. 15 . after b is assigned to it a plus b (assigned to a) = += -= *= /= %= >>= <<= &= |= ^= .e2 a times b (assigned to a) a divided by b (assigned to a) Remainder of a/b (assigned to a) a. The conditional operator (a ternary operator). Unary prefix operators. a && b a || b !a ?: a ? e1 : e2 Expression e2 if a is zero a.(AMITY CENTER FOR e-LEARNING) & [binary] | ^ && || ! a & b a | b a ^ b Bitwise AND of a and b Bitwise OR of a and b Bitwise XOR (exclusive OR) of a and b Logical AND of a and b (yields 0 or 1) Logical OR of a and b (yields 0 or 1) Logical NOT of a (yields 0 or 1) Expression e1 if a is nonzero. The comma operator. left-shifted b bits (assigned to a) a AND b (assigned to a) a OR b (assigned to a) a XOR b (assigned to a) e2 (e1 evaluated first) The C operators fall into the following categories: Postfix operators. right-shifted b bits (assigned to a) a. depending on the evaluation of the first expression. which guarantees left-to-right evaluation of comma-separated expressions. a minus b (assigned to a) a = b a += b a -= b a *= b a /= b a %= b a >>= b a <<= b a &= b a |= b a ^= b e1. which follow a single operand. Assignment operators. which take two operands and perform a variety of arithmetic and logical operations. which precede a single operand.

/* x is assigned 13. Certain operators have higher precedence than others. operators of higher precedence are evaluated before those of lower precedence. Using parenthesis in an expression alters the default precedence.(type) * & sizeof +<< >> < <= > >= == != & ^ | && || ?: . not 20 */ The previous statement is equivalent to the following: x = 7 + (3 * 2). for example.. ++ . the multiplication operator has higher precedence than the addition operator: x = 7 + 3 * 2.(AMITY CENTER FOR e-LEARNING) 2. /* (7 + 3) is evaluated first */ In an unparenthesized expression. This affects how an expression is evaluated.+ .2 Precedence of C Operators Operator precedence determines the grouping of terms in an expression. Consider the following expression: A+B*C The identifiers B and C are multiplied first because the multiplication operator (*) has higher precedence than the addition operator (+). Category Postfix Unary Additive Shift Relational Equality Bitwise AND Bitwise XOR Bitwise OR Logical AND Logical OR Conditional Assignment Comma Operator () [] -> .! ~ ++ . For example: x = (7 + 3) * 2. Associativity Left to right Right to left Left to right Left to right Left to right Left to right Left to right Left to right Left to right Left to right Left to right Left to right Right to left Left to right 16 Multiplicative * / % = += -= *= /= %= >>= <<= &= ^= |= Right to left .

the binary addition. Integer division truncates any fractional part. x = y = z. *. and resolves any ambiguity over the grouping of operators with the same precedence. In the following statement. %) are evaluated from left to right: (A*B)%C Parentheses can always be used to control precedence and associativity within an expression 2. and the modulus operator %. Therefore 17 . The kind of associativity determines the order in which operators from the same row are evaluated in an unparenthesized expression.(AMITY CENTER FOR e-LEARNING) Table 2-2 Precedence of C Operators Associativity relates to precedence. Associativity applies to each row of operators in Table 2-2 and is right-to-left for some rows and left-to-right for others. not 5 */ Other operators associate left-to-right. /. The associativity of the conditional operator is right-to-left on the line. a year is a leap year if it is divisible by 4 but not by 100. and division operators all have left-to-right associativity. the rules of C specify that a * b is evaluated first: y = a * b / c. The assignment operator also associates right-to-left. except that years divisible by 400 are leap years. y = 5. and thus is zero when y divides x exactly. For example. for example: int x = 0 . for example. -. In a more complicated example. The expression x%y produces the remainder when x is divided by y. multiplication. associativity rules specify that b ? c : d is evaluated first in the following example: a ? b ? c : d : e. /* x has the value 3. z = 3. /.3 Arithmetic Operators The binary arithmetic operators are +. subtraction. Consider the following expression: A*B%C This expression is evaluated as follows because the multiplicative operators (*.

(AMITY CENTER FOR e-LEARNING) if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) printf("%d is a leap year\n". which is lower than the precedence of *. Arithmetic operators associate left to right. year). In C. / and %. as is the action taken on overflow or underflow. true is any value other than zero and false is zero. 2.operators have the same precedence. The % operator cannot be applied to a float or double.4 Relational and Logical Operators The relational and logical operators are as follows: Operator &gt &gt= < &lt= == != &amp& Meaning Greater than Greater than or equal to Less than Less than or equal to Equal Not equal logical AND logical OR logical NOT || ! The idea of true and false underlies the concepts of relational and logical operators. year). The direction of truncation for / and the sign of the result for % are machine-dependent for negative operands. All relational and logical operations produce a result of either 1 or 0. else printf("%d is not a leap year\n". 18 . The binary + and . which is in turn lower than unary + and -.

But since the precedence of != is higher than assignment. the numeric value of a relational or logical expression is 1 if the relation is true. Most C programs rely on these properties. and evaluation stops as soon as the truth or falsehood of the result is known. Similarly. ++i) s[i] = c. and both are lower than relational and equality operators. Expressions connected by && or || are evaluated left to right. therefore the call and assignment must occur before the character in c is tested. it would be unfortunate if c were tested against EOF before getchar is called. so the test i < lim-1 must be made first. if this test fails. Before reading a new character it is necessary to check that there is room to store it in the array s. Just below them in precedence are the equality operators: == != Relational operators have lower precedence than arithmetic operators. The precedence of && is higher than that of ||. i < lim-1 && (c=getchar()) != '\n' && c != EOF. parentheses are needed in (c=getchar()) != '\n' to achieve the desired result of assignment to c and then comparison with '\n'. and 0 if the relation is false. More interesting are the logical operators && and ||.(AMITY CENTER FOR e-LEARNING) Although C does not contain an exclusive OR (XOR) operator. By definition. Moreover. as would be expected. we must not go on and read another character. Mostly used relational operators are > >= < <= They all have the same precedence. for (i=0. 19 . a function can easily be created to perform that task. so expressions like i < lim-1 && (c=getchar()) != '\n' && c != EOF need no extra parentheses. so an expression like i < lim-1 is taken as i < (lim-1).

a= 3. while the decrement operator -. a. For example. For example. a++). The decrement operator "--" is used in the same fashion as the increment operator. 20 . Write a loop equivalent to the for loop above without using && or ||.subtracts 1. printf("a=%d a+1=%d\n". printf("a=%d a+1=%d\n". example 2. So the statement example 1. The increment operator "++" increments the named variable. a. A common use of ! is in constructions like if (!valid) rather than if (valid == 0) It's hard to generalize about which form is better. ++a). A statement that uses an increment operator has a value. Exercise." If the increment operator comes after the named variable. then the value of the statement is calculated after the increment occurs. the statement a= 3.(AMITY CENTER FOR e-LEARNING) The unary negation operator ! converts a non-zero operand into 0. will display the text "a=3 a+1=4.5 Increment and Decrement Operators C provides two unusual operators for incrementing and decrementing variables. 2. The increment operator ++ adds 1 to its operand. would display "a=3 a+1=3" but would finish with a set to 4. but more complicated ones can be hard to understand. and a zero operand in 1. if (c == '\n') ++nl. Constructions like !valid read nicely (``if not valid''). We have frequently used ++ to increment variables. the statement "a++" is equivalent to "a= a+1" or "a+= 1".

(AMITY CENTER FOR e-LEARNING)
The unusual aspect is that ++ and -- may be used either as prefix operators (before the variable, as in ++n), or postfix operators (after the variable: n++). In both cases, the effect is to increment n. But the expression ++n increments n before its value is used, while n++ increments n after its value has been used. This means that in a context where the value is being used, not just the effect, ++n and n++ are different. If n is 5, then
x = n++;

sets x to 5, but
x = ++n;

sets x to 6. In both cases, n becomes 6. The increment and decrement operators can only be applied to variables; an expression like (i+j)++ is illegal. In a context where no value is wanted, just the incrementing effect, as in
if (c == '\n') nl++;

prefix and postfix are the same. But there are situations where one or the other is specifically called for. For instance, consider the function squeeze(s,c), which removes all occurrences of the character c from the string s.
/* squeeze: delete all c from s */ void squeeze(char s[], int c) { int i, j; for (i = j = 0; s[i] != '\0'; i++) if (s[i] != c) s[j++] = s[i]; s[j] = '\0'; }

Each time a non-c occurs, it is copied into the current j position, and only then is j incremented to be ready for the next character. This is exactly equivalent to
if (s[i] != c) { s[j] = s[i]; j++; }

As another example, consider the standard function strcat(s,t), which concatenates the string t to the end of string s. strcat assumes that there is enough space in s to hold the combination. As we have written it, strcat returns no value; the standard library version returns a pointer to the resulting string. /* strcat: concatenate t to end of s; s must be big enough */
void strcat(char s[], char t[]) { int i, j; i = j = 0; while (s[i] != '\0') /* find end of s */ i++; while ((s[i++] = t[j++]) != '\0') /* copy t */

21

(AMITY CENTER FOR e-LEARNING)
; }

As each member is copied from t to s, the postfix ++ is applied to both i and j to make sure that they are in position for the next pass through the loop. Exercise. Write an alternative version of squeeze(s1,s2) that deletes each character in s1 that matches any character in the string s2. Exercise. Write the function any(s1,s2), which returns the first location in a string s1 where any character from the string s2 occurs, or -1 if s1 contains no characters from s2. (The standard library function strpbrk does the same job but returns a pointer to the location.)

2.6 Assignment Operators and Expressions
An expression such as i=i+2 in which the variable on the left side is repeated immediately on the right, can be written in the compressed form i += 2 The operator += is called an assignment operator. Most binary operators (operators like + that have a left and right operand) have a corresponding assignment operator op=, where op is one of + - * / % << >> & ^ | If expr1 and expr2 are expressions, then expr1 op= expr2 is equivalent to expr1 = (expr1) op (expr2) except that expr1 is computed only once. Notice the parentheses around expr2: x *= y + 1 means x = x * (y + 1) rather than x=x*y+1 As an example, the function bitcount counts the number of 1-bits in its integer argument.
/* bitcount: count 1 bits in x */ int bitcount(unsigned x) { int b; for (b = 0; x != 0; x >>= 1)

22

(AMITY CENTER FOR e-LEARNING)
if (x & 01) b++; return b; }

Declaring the argument x to be an unsigned ensures that when it is right-shifted, vacated bits will be filled with zeros, not sign bits, regardless of the machine the program is run on. Quite apart from conciseness, assignment operators have the advantage that they correspond better to the way people think. We say ``add 2 to i'' or ``increment i by 2'', not ``take i, add 2, then put the result back in i''. Thus the expression i += 2 is preferable to i = i+2. In addition, for a complicated expression like yyval[yypv[p3+p4] + yypv[p1]] += 2 the assignment operator makes the code easier to understand, since the reader doesn't have to check painstakingly that two long expressions are indeed the same, or to wonder why they're not. And an assignment operator may even help a compiler to produce efficient code. We have already seen that the assignment statement has a value and can occur in expressions; the most common example is while ((c = getchar()) != EOF) ... The other assignment operators (+=, -=, etc.) can also occur in expressions, although this is less frequent. In all such expressions, the type of an assignment expression is the type of its left operand, and the value is the value after the assignment. Exercise In a two's complement number system, x &= (x-1) deletes the rightmost 1-bit in x. Explain why. Use this observation to write a faster version of bitcount.

2.6.1 Conditional Expressions
The statements if (a > b) z = a; else z = b;
23

i++) printf("%6d%c". short. n. if f is a float and n an int. it needed to be able to support assembler-like operations. All other elements are followed by one blank. then the expression expr2 is evaluated. In the expression expr1 ? expr2 : expr3 the expression expr1 is evaluated first. & bitwise AND 24 .(AMITY CENTER FOR e-LEARNING) compute in z the maximum of a and b. If it is non-zero (true). this loop prints n elements of an array. Thus to set z to the maximum of a and b. the type of the result is determined by the conversion rules discussed earlier in this chapter.\n". int. these may only be applied to integral operands. Exercise. /* z = max(a. The conditional expression often leads to succinct code. provides an alternate way to write this and similar constructions. (i%10==9 || i==n-1) ? '\n' : ' '). Another good example is printf("You have %d items%s. i < n. and long. just above assignment. z = (a > b) ? a : b. that is. however. A newline is printed after every tenth element. whether signed or unsigned. and with each line (including the last) terminated by a newline. They are advisable anyway. The conditional expression. char. with a conditional expression instead of if-else. n==1 ? "" : "s").7 Bitwise Operators Since C was designed to take the place of assembly language for most programming tasks. 2. Only one of expr2 and expr3 is evaluated. written with the ternary operator ``?:''. C provides six operators for bit manipulation. 10 per line. Rewrite the function lower. and after the n-th. with each column separated by one blank. Bitwise operations refer to testing. and that is the value. for (i = 0. then the expression (n > 0) ? f : n is of type float regardless of whether n is positive. since the precedence of ?: is very low. Otherwise expr3 is evaluated. For example. a[i]. Parentheses are not necessary around the first expression of a conditional expression. setting or shifting the actual bits in a byte or word (byte and word corresponding to the types char and int and their variants). but it's more compact than the equivalent if-else. b) */ It should be noted that the conditional expression is indeed an expression. This might look tricky. since they make the condition part of the expression easier to see. and it can be used wherever any other expression can be. and that is the value of the conditional expression. For example. which converts upper case letters to lower case. If expr2 and expr3 are of different types.

for example. then x & y is zero while x && y is one. filling vacated bits with zero. which assumes that x is a 16-bit quantity. The bitwise exclusive OR operator ^ sets a one in each bit position where its operands have different bits. if x is 1 and y is 2. it converts each 1-bit into a 0-bit and vice versa. and zero where they are the same. For example x = x & ~077 sets the last six bits of x to zero. which imply left-to-right evaluation of a truth value. for example n = n & 0177. which must be non-negative. x & 0177700. Note that x & ~077 is independent of word length. One must distinguish the bitwise operators & and | from the logical operators && and ||.(AMITY CENTER FOR e-LEARNING) | ^ << >> ~ bitwise inclusive OR bitwise exclusive OR left shift right shift one's complement (unary) The bitwise AND operator & is often used to mask off some set of bits. The shift operators << and >> perform left and right shifts of their left operand by the number of bit positions given by the right operand. that is. For example. since ~077 is a constant expression that can be evaluated at compile time. Thus x << 2 shifts the value of x by two positions. The unary operator ~ yields the one's complement of an integer. The portable form involves no extra cost. Right shifting an unsigned quantity always fits the vacated bits with zero. sets to one in x the bits that are set to one in SET_ON. 25 . The bitwise OR operator | is used to turn bits on: x = x | SET_ON. sets to zero all but the low-order 7 bits of n. this is equivalent to multiplication by 4. and is thus preferable to. Right shifting a signed quantity will fill with bit signs (``arithmetic shift'') on some machines and with 0-bits (``logical shift'') on others.

For example. Clearing a Bit . } The expression x >> (p+1-n) moves the desired field to the right end of the word. int n) { return (x >> (p+1-n)) & ~(~0 << n). We assume that bit position 0 is at the right end and that n and p are sensible positive values.p.n) that returns the (right adjusted) n-bit field of x that begins at position p. For example.4.n) that returns the value of the integer x rotated to the right by n positions. Here is what happens: parity bit set parity bit unset 1 0 0 1 0 1 0 1 0 1 1 1 1 1 1 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 1 1 1 1 1 1 0 0 0 1 0 1 0 1 ch 127 result 26 .n. getbits(x. ~0 is all 1-bits. leaving the others unchanged. Exercise.e. Write a function setbits(x.3) returns the three bits in positions 4. Write a function invert(x..e.p.. Exercise. shifting it left n positions with ~0<<n places zeros in the rightmost n bits.n) that returns x with the n bits that begin at position p inverted (i. leaving the other bits unchanged. Any bit that is 0 in either operand causes the corresponding bit to be set to 0. consider the function getbits(x. complementing that with ~ makes a mask with ones in the rightmost n bits.(AMITY CENTER FOR e-LEARNING) As an illustration of some of the bit operators. the following resets a parity bit to 0: return (ch & 127). 3 and 2. i. the operator may be used to set specific bits to zero. Exercise. Note that the number 127 has bit 8 set to 0.Bitwise AND (&amp) Bitwise ANDing is frequently used for &quotmasking"operations. Write a function rightrot(x.y) that returns x with the n bits that begin at position p set to the rightmost n bits of y. int p. 1 changed into 0 and vice versa). right-adjusted.p. /* getbits: get n bits from position p */ unsigned getbits(unsigned x.

b ^= a. then zeroes will be shifted in. One interesting property of XOR is that any value XOR'd with itself produces 0. If the sign is 1 (negative). e. If the sign is 0 (positive). Any bit which is set to 1 in either operand causes the outcome to be set to 1. An interesting application of XOR is that it can be used to exchange two values without the need for an extra memory location. zeroes are brought in at the other. a ^= b. A shift is not a rotate. what is shifted in on the left depends on the sign of the value being shifted and also on how the operation is implemented on the particular machine. Left shifting has the effect of multiplying the shifted value by two. Swapping Values.: a ^= b.(AMITY CENTER FOR e-LEARNING) Setting a Bit . the latter is known as a logical right shift. The former is known as an arithmetic right shift. 27 . As bits are shifted off one end.Bitwise OR (|) Similarly. the following is 128 | 3: 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 3 in binary 128 in binary result Exclusive OR (^) Exclusive OR (XOR) sets a bit only if the bits being compared are different. the bits shifted off one end are lost. This is useful in assembly programming. For right shifts. the bitwise OR may be used to set a bit.g. For example. 0s. Shift Operators (&lt&lt and &gt&gt) Shift operators move all bits left or right as specified. on some machines 1s will be shifted in and on others. C does not guarantee a defined result if a value is shifted by an amount greater or less than the number of bits in the data item.

The ? Operator The ternary operator ? replaces certain statements of the if-then-else form. A pointer variable is a variable specifically declared to hold a pointer. Pointer Operators. y is made equal to 200. The following operators are used to manipulate pointers: The & Operator The & operator returns the memory address of its operand. If it evaluates to false. allow C functions to modify their calling parameters. 28 . A pointer is the memory address of a variable. y is made equal to 100. Note that two complements in a row return the byte to its original value. In the following: y = x >10 ? 100 : 200 the first expression is evaluated. & can be thought of as "address of". A pointer is of sufficient size to hold an address as defined by the architecture of the computer. and support linked lists and other dynamic data structures. The &amp and * Operators Preamble. If it evaluates to true. For example: m = &count. places the address of count into m.(AMITY CENTER FOR e-LEARNING) One's Complement Operator (~) The One's Complement operator reverses the state of each bit in the specified variable. Pointers: provide a fast means of referencing array elements.

e. e. printf(&quot%d&quot. 29 . the left side of the comma operator is evaluated as void. and -> Operators The .(AMITY CENTER FOR e-LEARNING) The * Operator The * operator is the complement of &amp.: emp_ptr->wage = 123. in bytes. The Comma Operator The comma operator may be used to string together several expressions.g. For example.23. sizeof(int)). The Compile-Time Operator sizeof sizeof is a unary compile time operator which returns the length. which means that y is first assigned the value 3 and then x is assigned the value 4.: employee. in the following extension of the preceding code fragment: q = *count. y + 1). The . It returns the value of the variable at the address which follows it. The . For example. Usage is illustrated in the following: printf(&quot%f&quot. of the variable or parenthesized type specifier that precedes it. The -&gt is used when working with a pointer to the structure or union. * can be thought of as "address". in: x . and -> operators reference individual elements in structures and unions.wage = 123.23. the value of count is placed in q.g. operator is used when working with the actual structure or union.(y = 3. sizeof f).

x = x * 10.141592654.8.1 Casting An expression can be forced to be of a particular type by using a cast. y = (short) x. casting is used to round to the nearest integer: double x. An example of a cast which ensures that an expression evaluates to type float is as follows: x = (float) x / 2.45. // resulting value of y is 5. // resulting value of y is 3. can be written as x *= 10.0 x = 4. For example: x = x + 10.05). y = (short) (x +. a fractional number is truncated to a whole number: double x = 3. y.0 2. // resulting value of y is 4.5). In the following example. y = (short) (x + 0. x = 4.67. and have the same precedence as other unary operators. Casts are often considered to be operators. casts are unary.0 In the following example.8. can be written as x += 10. As an operator. double y. Some further examples using the bitwise operators are as follows: 30 .2 C Shorthand C has a special shorthand which simplifies the coding of certain assignments.(AMITY CENTER FOR e-LEARNING) 2.8 Miscellaneous Features 2.

3.1 getchar 3.1 printf 3.2 putchar 3.(AMITY CENTER FOR e-LEARNING) Operator &gt&gt= &lt&lt=< &amp= ^= |= 2. Chapter 3 Data Input / Output Functions Contents: 3. eg: x = y = z = 0.4.8.3 Multiple Assignments Usage Result in a After Operation a &gt&gt= b a shifted right by b bits a &lt&lt= b a &amp= b a ^= b a |= b a shifted left by b bits a ANDed with b a exclusive ORed with b a ORed with b The assignment of a value to many variables is possible using multiple assignment.4.3 Character Input / Output 3.4 Formatted input /output 3.2 scanf 3.2 Standard input output 3.1 Introduction 3.3.5 whole lines of input and output 31 .

(AMITY CENTER FOR e-LEARNING)
3.5.1 gets 3.5.2 puts

3.1 Introduction
One of the reasons that has prevented many programming languages from becoming widely used for ‗real programming‘ is their poor support for I/O, a subject which has never seemed to excite language designers. C has avoided this problem, oddly enough, by having no I/O at all! The C language approach has always been to do I/O using library functions, which ensures that system designers can provide tailored I/O instead of being forced to change the language itself. As C has evolved a library package known as the ‗Standard I/O Library‘ or stdio, has evolved with it and has proved to be both flexible and portable. This package has now become part of the Standard. The old stdio package relied heavily on the UNIX model of file access, in particular the assumption that there is no distinction between unstructured binary files and files containing readable text. Many operating systems do maintain a distinction between the two, and to ensure that C programs can be written portably to run on both types of file model, the stdio package has been modified. There are changes in this area which affect many existing programs, although strenuous efforts were taken to limit the amount of damage.

3.2 The standard Input Output File.
To perform, input and output to files or the terminal, UNIX supplies a standard package. To use them, this contains most of the functions which will be introduced in this section, along with definitions of the data types required. Your program must include these definitions by adding the line to use these facilities. #include near start of the program file. If you do not do this, the compiler may complain about undefined data types or functions.

32

(AMITY CENTER FOR e-LEARNING)

3.3 Character Input / Output
If you will not press the return key then they‘ll not start reading any input, and they‘ll not print characters on the terminal until there is a whole line to be printed. This is the lowest level of output and input. It provides very precise control, but is usually too fiddly to be useful also. Most computers perform buffering of inputs and outputs. These include:
1. getchar

2. putchar

3.3.1 getchar
getchar always returns the next character of keyboard input as an int. The EOF (end of file) is returned,if there is an error . It is usual to compare this value against EOF before using it. So error conditions will not be handled correctly,if the return value is stored in a char, it will never be equal to EOF. The following program is used to count the number of characters read until an EOF is encountered. EOF can be generated by typing Control - d. #include main() { int ch, i = 0; while((ch = getchar()) != EOF) i ++; printf(‖%d\n‖, i); }

3.3.2 putchar
The putchar() function writes ch to STDOUT. The code putchar( ch );
33

(AMITY CENTER FOR e-LEARNING)
is the same as putc( ch, STDOUT );

The return value of putchar() is the written character, or EOF if there is an error putchar puts its character argument on the standard output (usually the screen). The following example program converts any typed input into capital letters.It applies the function toupper from the character conversion library ctype.h to each character in turn to do this.

#include /* For definition of toupper */ #include /* For definition of getchar, putchar, EOF */ main() { int ch; while((ch = getchar()) != EOF) putchar(toupper(ch)); }

3.4 Formatted Input / Output
They are closest to the facilities offered by Pascal or Fortran, and usually the easiest to use for input and output. The versions offered under C are a little more detailed, offering precise control of layout. These includes following: 1. printf 2. scanf

3.4.1 printf

34

in order.The printf() function prints output to STDOUT. followed by a list of values to be substituted for entries in the control string. with a lowercase "e" scientific notation. The %s means. whichever is shorter octal a string of characters unsigned integer unsigned hexadecimal." The %d indicates that the second argument (an integer) should be placed there.(AMITY CENTER FOR e-LEARNING) This offers more structured output than the putchar. you specify a format string that has text in it.characters that will be printed to the screen. which controls what get printed. with lowercase letters 35 . Its arguments are. Basically. whichever is shorter use %E or %f. with a uppercase "E" floating point use %e or %f. a string. a control string. right here. as well as options to limit the length of the variables and whatnot. The string format consists of two types of items . "insert the first argument. according to format and other arguments passed to printf(). Code ---%c %d %i %e %E %f %g %G %o %s %u %x Format -----character signed integers signed integers scientific notation. and format commands that define how the other arguments to printf() are displayed. as well as "special" characters that map to the other arguments of printf(). There are different %-codes for different variable types.

which has different meanings depending on the format code being used. For example. and %e type specifiers can be preceded with the character '#' to ensure that the decimal point will be present. unless you place a minus sign right after the % sign.4f will display a floating point number with a minimum of 12 characters. The %g. %hd means a short integer). You may modify the %d. The use of the '#' character with the %x type 36 .6f will display a floating number at least 12 digits wide. %-12. %f. If you want to pad with zeros. and pads the output with spaces or zeros to make it long enough. %E. All of printf()'s output is right-justified. even if there are no decimal digits. With %g and %G. and %g type specifiers can have the letter l before them to indicate that a double follows. The %e. 4 decimal places. With %s. %o. %f. the precision modifier determines the maximum number of significant digits displayed. You can use a precision modifier. %i. For example. %u. with six decimal places.g. and %x type specifiers with the letter l and the letter h to specify long and short data types (e. the precision modifer simply acts as a maximum field length. the precision modifier lets you specify the number of decimal places desired. and left justified. %12. to complement the minimum field length that precedes the period. With %e.(AMITY CENTER FOR e-LEARNING) %X %p unsigned hexadecimal. and %f. place a zero before the minimum field width specifier. with uppercase letters a pointer %n the argument shall be a pointer to an integer into which is placed the number of characters written so far %% a '%' sign An integer placed between a % sign and the format command acts as a minimum field width specifier.

…). The prototype for scanf is: int scanf( const char *format. Some people brush off the scanf as a broken function that shouldn‘t be used often. The use of the '#' character with the %o type specifier indicates that the octal value should be displayed with a 0 prefix.(AMITY CENTER FOR e-LEARNING) specifier indicates that the hexidecimal number should be printed with the '0x' prefix. Character strings are an exception to this. 37 . x). Beware though. you are %d years old\n". we give the names of the string variables unmodified by a leading &. int age = 21. or a negative number if an error occurred. Since a string is already a character pointer. Control string entries which match values to be read are preceded by the percentage sign in a similar way to their printf equivalent. printf( "Hello %s. age ). Example: char name[20] = "Bob". However scanf wants to know the address of items to be read.4. Take the example: scanf(‖%d‖. since it is a function which will change that value.The return value of printf() is the number of characters printed.scanf() is used. scanf isn‘t greatest function that C has to offer. OUTPUT: Hello Bob. name. followed by the list of items to be read. Looks similar to printf. Therefore the names of variables are preceded by the & sign. You can also include constant escape sequences in the output string. but doesn‘t completely behave like the printf does. Like printf it has a control string. you are 21 years old 3. To read of data from the keyboard scanf allows formatted.1 scanf To grab things from input.

It returns EOF if there was an error Syntax: #include <stdio. we can read and write whole lines as character strings. args.5 whole lines of input and output Where we are not too interested in the format of our data. continue. This approach allows us to read in a line of input. if (( args = scanf(‖%d‖.1 puts The function puts() writes str to STDOUT. or perhaps we cannot predict its format in advance. This is built in to the function. } else { if (args == 1) printf(‖Read in %d\n‖. . } } 3. or EOF on failure.5. It will then add a newline character. and then use various string handling functions to analyse it at our leisure 3. will output the string pointed to by astring. puts() returns non-negative on success. else break.h> 38 . It returns a nonnegative integer if it was successful. for ( . x). &x)) == 0) { printf(‖Error: not an integer\n‖). Explanation: This function. ) { printf(‖Enter an integer bub: ―).(AMITY CENTER FOR e-LEARNING) The following is the example which shows the use of scanf: int x. puts.

1 4.3 For loop While – do while Break . int main() { puts("Hello World").1.1 Branching 4.(AMITY CENTER FOR e-LEARNING) int puts( char *str ).1.1 4.1 Preliminaries 4. The newline character is translated into a null termination. } 3.1.1.1.1. or NULL if there is an error.2.5.2 If-else Switch-case Looping 4. World" with a newline #include <cstdio> using namespace std. Syntax: #include <stdio.1.2. until a newline or EOF is reached.2 4.1.continue 39 .2 gets The gets() function reads characters from STDIN and loads them into str. //Example outputs "Hello.h> Chapter 4 Control Statements Contents: 4. The return value of gets() is the read-in string.2.1.2 4.

This can be extended with else. The syntax is pretty easy. Programs need to make decisions. Should this statement be executed or that one? Are these parameters correct and so on.1 if statement This is the simplest form of the branching statements. This is done using the if statement. It takes an expression in parenthesis and an statement or block of statements.1. if ( conditional expression ) statement. 40 .1Branching Branching is deciding what actions to take and looping is deciding how many times to take a certain action.1 Preliminaries C provides two styles of flow control: Branching Looping These branching and looping in C can be achieved with the help of following keywords If-Else While For Break / Continue Switch-Case We will discuss each of these one by one to understand their syntax and their application in programming language 4.(AMITY CENTER FOR e-LEARNING) 4. if the expression is true then the statement or block of statements gets executed otherwise these statements are skipped.Branching is so called because the program chooses to follow one branch or another 4.1.1.

then statement-1 is executed. and the else is present.g.. So if the expression is true. if-elseif-else statements take the following form: if (expression) statement. The else clause is optional. } or if (expression) { Block of statements. if it is not equal to zero (e.(AMITY CENTER FOR e-LEARNING) If-Else The if else statement is used to make decisions. else statement2. } 41 . if . or if (expression) { Block of statements. if-else . NOTE: Expression will be assumed to be true if its evaulated values is non-zero. If the if part of the statement did not execute. statement1 is executed. The syntax is: if (expression) statement-1 else statement-2 expression is evaluated. logic true). then statement-2 executes. } else { Block of statements. if ( conditional expression ) statement1. otherwise statement2 is executed.

a += 2. } else { Block of statements. } else { b=0. } Be Careful that you don't write = when you mean == Can you see the problem with this code? if (a=1) { 42 . } else if(expression) { Block of statements. } Here are some examples: if ( a==3 ) b=a. if ( b==0 ) { b=1.(AMITY CENTER FOR e-LEARNING) or if (expression) { Block of statements. a=1.

break. The syntax of a switch statement is: switch ( expression) { case a: statement1. switch statement can be slightly more efficient and easier to read. It assigns a value of 1 to a instead of comparing a with 1. a switch statement may be better than an if.1. case d: statement3. statements2. e. switch (character) { case 'B': 43 .] When there are many choices. 4. if (a==1) This is a common mistake amongst beginners. } Notes: Expression should be an int or a char type. break.] statements3. default: statement4. } This will always set b to 9 as (a=1) is always true. case b: statement2.g.] } statements1..1. else statement.2 switch statement The switch statement is much like a nested if . The corrected line should be this. switch( expression ) { case constant-expression1: [case constant-expression2: [case constant-expression3: [default : statements4. Its mostly a matter of preference which you use.(AMITY CENTER FOR e-LEARNING) b =9.

only statement3 is executed.(AMITY CENTER FOR e-LEARNING) case 'b': statement1. case 'd': statement3. all statements from that case on. break.1. default: break. default: statement4. With 'd' though. statement2 and statement1 are executed (in that order). 44 . including those that follow are executed until a break is found. statement3. } If the character is 'B' or 'b' then statement1 is executed. but if the character is 'c' then statement2 and statement3 are both executed. Eg if value ==3. } In this example. statements n. 4. I've exploited 'fall through' so that for any number 1-5. switch (value) { case 3:statement3. Good Practice to use default. perhaps to trap errors. For all other characters statement4 is executed. This is known as 'Fall Through'.2 Looping Loops provide a way to repeat commands and control how many times they are repeated. break. case 2:statement2. The default case can occur anywhere. case 'c': statement2. case 1:statement1. Notes about Switch Each case can have one or more statements without needing to be enclosed by curly brackets. C provides a number of looping way. n-1 down to 1 are executed. When a case matches.

. conditional expression.expr-3) statement OR for ( initial statement. This outputs ten lines The value of i is 1 The value of i is 2 .1 The For Loop There are several ways of doing loops in C. for ( .i) .index++ ) printf("The value of i is %i\n\r". ) mainstatement. The value of i is 10 45 .2. As long as it is true the main statement is always run.expr-2. the loop statement and the main statement are executed. then while the conditional expression is true. Once the expression is false. the loop exits. for (index=1 . This executes the initial statement once.. loop statement ) main statement. The conditional expression is the most important of the three sections. The syntax of a for loop is the following: for (expr-1. Note This is the one place where there is no need to put brackets around a conditional expression.index <= 10.(AMITY CENTER FOR e-LEARNING) 4.1. It is an infinite loop as the conditional expression is always true if absent. This will keep executing main statement for ever. All three sections are optional so the following is allowed.

This cycle repeats until the test condition evaluates to false. One can easily create an infinite loop in C using the while statement: while (1) statement There are two loop statements that use while. } while begins by evaluating expression. The loop exits when expression becomes zero. then statement is evaluated. You could write index = index + 1 or index +=1 but index++ is more concise.2 while and do-while while loop The most basic loop in C is the while loop. Like an If statement. If it is still true the statements get executed again. and the same check is performed.(AMITY CENTER FOR e-LEARNING) index++ is shorthand for adding 1 to index. the test condition is checked again.A while statement is like a repeating if statement. If it is true. If it is false. 46 . Basic syntax of while loop is as follows: while ( expression ) { Single statement or Block of statements. 4. Then the expression is evaluated again.2. if the test condition is true: the statments get executed. The difference is that after the statements have been executed.1. then statement is skipped.

Do While do . the statement is not run. The most important thing to know with while loops is that the control expression is evaluated before the statement is run. printf("Sum of 1 to %i is %i".total) . while is just like a while loop except that the test condition is checked at the end of the loop rather than the start. } Looping doesn't get much simpler than while. So a while loop may never execute a statement. 47 .Less popular than the while statement is the do while statement. This puts the expression at the end. while (expression) statement As long as the expression is true. while (total < 100) { index++. int a =0.a++) . int index =0... This has the effect that the content of the loop are always executed at least once.subtracts one from c. int total =0.index. } c-.(AMITY CENTER FOR e-LEARNING) First is the while statement which has this syntax. Also because c allows a statement to be an expression it is possible to write complicated code like this int c=10. total+=index. If the expression is false. the statement is executed. while (c--) { printf("Value of a is %i".

}while(expression). do if (value[ index] ==999) count++.expr-2.while loop is as follows: do { Single statement or Block of statements. int count =0. A while loop might never execute a statement if the expression is false but a do while will always execute the statement at least once.. index--. Note For and while can be used for same purpose for loop is similar to while.expr-3) statement 48 . it's just written differently. for statements are often used to proccess lists such a range of numbers: The syntax of a for loop is the following: for (expr-1. int index =9. while and do while loops. Break and Continue work equally well with for. while ( index >= 0) ..(AMITY CENTER FOR e-LEARNING) Basic syntax of do. Here is an example of a do while loop.

continue -.2.skip 1 iteration of loop. and expr-3 is an increment or decrement of some manner. For example.3 break and continue statements C provides two commands to control how we loop: break -. i++) printf("%d\n". Here's an example: 49 .(AMITY CENTER FOR e-LEARNING) This is equivalent to the following construct using while: expr-1. printing each number along the way: int i. expr-1 is an assignment. expr-2 is a relational expression. 4. If a condition is met in switch case then execution continues on into the next case clause also if it is not explicitly specified that the execution should exit the switch statement. This is achieved by using break keyword. the following code counts from 0 to 99. for (i= 0.1. } Typically. i).exit form loop or switch. Break Use of the break provides an early exit from a while or a for loop. while (expr-2) { statement expr-3. i < 100.

but this time we'll leave out elements with an index of 4 and 5.index++ ) { if (index==4 || index==5) continue. skipping the rest of the code So lets sum that integer array again. else printf("-1 Found at index %i\n\r". . int value =0. That's a more complicated example. It is initialized to -1 and after the search this value is examined to see if a 999 was found.index < 10. Instead of terminating the loop. break. } } if (markerpos== 999) printf("-1 Not located in array". Continue This does the opposite of break.index++ ) { if (values[index] ==-999 ) { markerpos = index. It searches the 10 element array values looking for a 999 value.(AMITY CENTER FOR e-LEARNING) int markerpos=-1.markerpos) . 50 . for (index=0 . value += numbers[index]. The variable markerpos holds the position if a 999 is found. it immediately loops again.index < 10. for (index=0 .

for( i = 0. i ). You already have seen example of using break statement. int j = 10.value) . i <= j. #include main() { int i. } printf("Hello %d\n". i ++ ) { if( i == 5 ) { continue. } } This will produce following output: Hello 0 Hello 1 Hello 2 Hello 3 Hello 4 51 . Here is an example showing usage of continue statement. You probably won't use continue very often but it's useful on the odd occasion.(AMITY CENTER FOR e-LEARNING) } printf("Value = %i".

(AMITY CENTER FOR e-LEARNING) Hello 6 Hello 7 Hello 8 Hello 9 Hello 10 52 .

2.4 Parts of function 5. it can be executed from as many different points in a C Program as required. We can divide a long C program into small blocks which can perform a certain task.called functions in C . We pass information to the function called arguments specified when the function is called.1 What is a Function? A function is a block of code that has a name and it has a property that it is reusable i. And the function either returns some value to the point it was called from or returns nothing.8 Call by reference 5.(AMITY CENTER FOR e-LEARNING) Chapter 5 Functions Contents: 5.3 Need of function 5.2 Structure of the function 5.5 Types of function 5.2 5. Function groups a number of program statements into a unit and gives it a name.6 Categories of function 5.2. A computer program cannot handle all the tasks by it self. A function is a self contained block of statements that 53 .1 5.3 function header function body function prototype 5. This unit can be invoked from other parts of a program. It neams that a function can be accessed from any location with in a C Program.1 What is a function? 5.2.7 Call by value 5. Instead its requests other program like entities .to get its tasks done. A function is a self contained block of statements that perform a coherent task of same kind The name of the function is unique in a C Program and is Global.e.9 Recursive function 5.

2 Structure of a Function There are two main parts of the function. return ans } Function Header In the first line of the above code int sum(int x. none should be used when calling the function 5. int y) It has three main parts 1. int sum(int x. 3. Function Prototypes The prototype of a function provides the basic information about a function which tells the compiler that the function is used correctly or not. It contains the same information as the function header contains. int //holds the answer that will be returned //calculate the sum //return the answer Function Body What ever is written with in { } in the above example is the body of the function. The name of the function i. If the programmer wants a return value.Almost all programming languages have some equivalent of the function.(AMITY CENTER FOR e-LEARNING) perform a coherent task of same kind. The function header and the function body. Some languages distinguish between functions which return variables and those which don't. sum The parameters of the function enclosed in paranthesis Return value type i.e. You may have met them under the alternative names subroutine or procedure. this is achieved using the return statement. ans = x + y. int y) { int ans = 0. If no return value is required. C assumes that every function will return a value.e. The prototype of the function in the above example would be like 54 . 2.

The only difference between the header and the prototype is the semicolon . In this programming style. If the program is divided into subprograms. 5. the length of the source program can be reduced by using functions at appropriate places. there must the a semicolon at the end of the prototype. A function is a complete and independent program which is used (or invoked) by the main program or other subprograms.(AMITY CENTER FOR e-LEARNING) int sum (int x. the high level logic of the overall problem is solved first while the details of each lower level functions is addressed later. It is easy to locate and isolate a faulty function for further investigation. 4. each subprogram can be written by one or two team members of the team rather than having the whole team to work on the complex program 55 . 2. A program can be used to avoid rewriting the same sequence of code at two or more locations in a program. There are many advantages in using functions in a program they are: 1. A program can be divided into smaller subprograms that can be developed and tested successfully. 5. This factor is critical with microcomputers where memory space is limited. A function may be used by many other programs this means that a c programmer can build on what others have already done. performs calculations and returns the results to the calling program. instead of starting over from scratch. It facilitates top down modular programming. int y). A subprogram receives values called arguments from a calling program. Programming teams does a large percentage of programming. 3. 6. This is especially useful if the code involved is long or complicated.3 Need of functions The basic philosophy of function is divide and conquer by which a complicated tasks are successively divided into simpler and more manageable tasks which can be easily handled.

since they provide a model or blueprint for the function. Void starline ( ).(AMITY CENTER FOR e-LEARNING) 7. The other approach is to define it before it‖s called. and its code easily since the readability is enhanced while using the functions 5. . the functions starline() is declared in the line.‖ a function that looks like this is coming up later in the program.4 Parts of a function Each function used in a program must be Declared : it is like registering the function to make itself known for use Defined : related with actual working or functionality of fuinction Called : to make function in use Function Declaration Just as you can‖t use a variable without first telling the compler what it is. you also can‖t use a funciotns without teling the compiler about it.‖ Calling the Function 56 .) in the Table program. The declaration tells the compiler that at some later point we plan to present a function called starline. There are two ways to do this . The approach we show here ist o declare the funtion before it is called. and the empty parentheses indicate that it takes no arguments. We can understand the flow of program. Function declarations are also called prototypes. we‖ll examine that next. The keyword void specifies that the function has no return value. They tell the compiler. Notice that the functions declarations is terminated with a semicolon It is a complete statement in itself. so it‖s all right if you see references to it before you see the function itself.

return y. The syntax of the call is very similar to that of declaration. Function definition [ data type] function name (argument list) argument declaration. { int y. that is. followed by parentheses.b) int a.b. control is transferred to the function. the last two statements ie. statements. } When the value of y which is the addition of the values of a and b. Executing the call statement causes the function to execute. and then control returns to the statement following the function call. { local variable declarations. [return expression] } Example : mul(a. Each of the three calls look like this: Starline(): This is all we need to call a function name.(AMITY CENTER FOR e-LEARNING) The function is called (or invoked) three times from main (). A semicolon terminates the call. can be combined as 57 . the statement in the function definition are executed. y=a+b. except that the return type is not used. y=a+b.

g the function message mentioned above. The user-defined functions are written by the programmer to carry out various individual tasks The following point must be noted about functions (i) (ii) C program is a collection of one or more functions A function gets called when the function name is followed by a semicolon for e. Function is defined when function name is followed by a pair of braces in which one or more statements may be present for e. 58 . User defined function e. printf ( ). 2. 5.g. scanf ( ) etc. } 3.g. message ( ) { statement 1. main ( ) { message ( ). The library functions are used to carry out a number of commonly used operations or calculations. Library functions Ex.(AMITY CENTER FOR e-LEARNING) return(y) return(a+b). C support the use of library functions and use defined functions.5 Types of Function There are basically two types of functions 1.

(AMITY CENTER FOR e-LEARNING) statement2. } 5. main ( ) { message ( ). Any function can be called from any other function even main ( ) can be called from other functions.g. } 59 . A function can be called any number of times for eg. } message ( ) { printf (‖ \n Hello‖). main ( ). statement 3. main () { message ( ). } 4. message ( ). for e.

main ( ). The order in which the functions are defined in a program and the order in which they get called need not necessarily be same for e. } 6.g.(AMITY CENTER FOR e-LEARNING) message ( ) { printf (‖\n Hello‖). { message 1 ( ). } message 1 ( ) { printf ( ―\n Hello ―). A function can call itself such a process as called ―recursion‖. } message 2 ( ) { printf (‖\n I am learning C‖). 60 . } 7. message 2 ( ).

g main ( ) 61 . control returns to the main( ). for e. argentina ( ) { printf {‖\n I am in argentina‖). The following program code would be wrong. 10. Each function in a program is called in the sequence specified by the function calls in main( ) 14. There is no limit on the number of functions that might be present in a C program. it must be main( ). main ( ) { printf (‖\n I am in main‖). the program ends. since Argentina is being defined inside another function main ( ). In a C program if there are more than one functional present then one of these func-tional must be main( ) because program execution always begins with main( ). 11. 12. then it is necessary to explicitly mention so in the calling functions as well as in the called function. Any C program contains at least one function. Functions declaration and prototypes Any function by default returns an int value. 13. If a program contains only one function. } } 9. when main( ) runs out of function calls. If we desire that a function should return a value other than an int.(AMITY CENTER FOR e-LEARNING) 8. After each function has done its thing. A function can be called from other function. but a function cannot be defined in an-other function.

b = square (a) printf ("\n square of % f is % f". } 62 . printf ("\n Enter any number").000000 Here 6 is not a square of 2.5 square of 2. by default. Y = x * x.5 this happened because any C function. printf ("\n square of % f is % f. b = square (a). b).b). scanf ("\% f". } the sample run of this program is Enter any number 2.(AMITY CENTER FOR e-LEARNING) { float a. always returns an integer value. " a. &a ). printf ("\n Enter any number "). b. float a. return (y). } square (float X) { float y.5 is 6. The following program segment illustrates how to make square ( ) capable of returning a float value.b. a. scanf ("%f" &a). main ( ) { float square ( ).

} /*function to print a message*/ statement1() { printf(―\n Sample subprogram output‖).(AMITY CENTER FOR e-LEARNING) float square (float x) { float y. starline(). Functions with no arguments and no return values: Let us consider the following program /* Program to illustrate a function with no argument and no return values*/ #include main() { staetemtn1(). return ( y). Functions with arguments and return values.6 Categories of functions A function may belong to any one of the following categories: 1. Functions with arguments and no return values. starline(). y= x *x. } 5. statement2(). 3. 2. Functions with no arguments and no return values. 63 .

12. } starline() { int a.12 and 5 are the actual arguments which become the values of the formal arguments inside the called function.a<60. } In the above example there is no data transfer between the calling function and the called function. for (a=1. Both the arguments actual and formal should 64 . 0. r. Similarly when it does not return value the calling function does not receive any data from the called function. n) and assign values 500 to p.5) Would send the values 500.0.(AMITY CENTER FOR e-LEARNING) } statement2() { printf(―\n Sample subprogram output two‖).0. For example the function call value (500. the values 500. printf(―\n‖).a++) printf(―%c‖.12 to r and 5 to n.12 and 5 to the function value (p. When a function has no arguments it does not receive any data from the calling function. Functions with arguments but no return values: The nature of data communication between the calling function and the arguments to the called function and the called function does not return any values to the calling function this shown in example below: Consider the following: Function calls containing appropriate arguments.0.‘*‘). A function that does not return any value cannot be used in an expression it can be used only as independent statement.

b. The values used in actual arguments must be assigned values before the function call is made. the actual arguments may be variable names expressions or constants.a3……an) } function1(f1. When a function call is made only a copy of the values actual arguments is passed to the called function.(AMITY CENTER FOR e-LEARNING) match in number type and order.fn).f3…. the extra actual arguments are discarded. The formal arguments may be valid variable names. The no of formal arguments and actual arguments must be matching to each other suppose if actual arguments are more than the formal arguments.f2. .f2.a2. In both cases no error message will be generated.a3 are actual arguments and f1. The values of actual arguments are assigned to formal arguments on a one to one basis starting with the first argument as shown below: main() { function1(a1. Let us consider the following program /*Program to find the largest of two numbers using function*/ #include main() { int a. { function } here a1. What occurs inside the functions will have no effect on the variables used in the actual argument list.a2. 65 body.f3 are formal arguments. If the number of actual arguments are less than the formal arguments then the unmatched formal arguments are initialized to some garbage values.

&b). else printf(―Largest element=%d‖. } in the above program we could make the calling function to read the data from the terminal and pass it on to the called function. To assure a high degree of portability between programs a function should generally be coded without involving any input output operations.b) } /*Function to find the largest of two numbers*/ largest(int a.&a. largest(a.b). scanf(―%d%d‖. The function call transfers the controls along with copies of the values of the actual arguments of the particular function where the formal arguments are creates and assigned memory space and are given the values of the actual arguments.(AMITY CENTER FOR e-LEARNING) printf(―Enter the two numbers‖). int b) { if(a>b) printf(―Largest element=%d‖.a). Theses shortcomings can be overcome by handing over the result of a function to its calling function where the returned value can be used as required by the program. In the above type of function the following steps are carried out: 1. For example different programs may require different output formats for displaying the results. Functions with arguments and return values: The function of the type Arguments with return values will send arguments from the calling function to the called function and expects the result to be returned back from the called function back to the calling function. 66 . But function foes not return any value.

} The type specifier tells the compiler. There is the necessity in some cases it is important to receive float or character or double data type. The explicit type specifier corresponding to the data type required must be mentioned in the function header. The calling statement is executed normally and return value is thus assigned to the calling function. 67 . 2. To enable a calling function to receive a noninteger value from a called function we can do the two things: 1. returns only the integer part of the sum. The called function must be declared at the start of the body in the calling function.(AMITY CENTER FOR e-LEARNING) 2. the type of data the function is to return. The program given below illustrates the transfer of a floating-point value between functions done in a multiple function program. The return value is passed back to the function call is called function. The general form of the function definition is Type_specifier function_name(argument list) Argument declaration. The called function is executed line by line in normal fashion until the return statement is encountered. This is since we have not specified any return type for the sum. Note that the value return by any function when no format is specified is an integer. This is to tell the calling function the type of data the function is actually returning. { function statement. 3. For example if function does all the calculations by using float values and if the return statement such as return (sum). like any other variable. Return value data type of function: A C function returns a value of type int as the default data type when no other type is specified explicitly.

q) double p.q.add(). This prevents any accidental use of these functions in expressions. { return(a+b). x=12. y=9.345. printf(―%lf\n‖sub(x.b. { return(p-q).82. } double sub(p.y)). Such function calls are called ―calls by 68 .(AMITY CENTER FOR e-LEARNING) main() { float x. printf(―%f\n‖ add(x.y). These declarations clarify to the compiler that the return type of the function add is float and sub is double.y. Void functions: The functions that do not return any values can be explicitly defined as void. 5.7 CALL BY VALUE In the preceding examples we have seen that whenever we called a function we have always passed the values of variables to the called function. } float add(a.b) float a. } We can notice that the functions too are declared along with the variables. double sub(0.

c). int y) { int t. swapy (a. x = y. The example of call by value is shown below. } The output of the above program would be. 69 .(AMITY CENTER FOR e-LEARNING) value‖ by this what it meant is that on calling a function we are passing values of variables to it. In this method the value of each of the actual arguments in the calling function is copied into corresponding formal arguments of the called function.b). x.8 CALL BY REFERENCE In the second method the addresses of actual arguments in the calling function are copied in to formal arguments of the called function. y = t. sum = calsum (a. t = x. x = 20 y = 10 a =10 b =20 5. printf ( "\n x = % d y = % d" . a. The following program illustrates this: main ( ) { int a = 10. This means that using these addresses we would have an access to the actual arguments and hence we would be able to manipulate them the following program illustrates this. f = factr (a). b=20. } swapy (int x.b). printf ("\na = % d b = % d". With this method the changes made to the formal arguments in the called function have no effect on the values of actual argument in the calling function. y). b.

*y = t. t = *x *x = *y. printf ("\n a = %d b= %d".9 Recursive Functions A recursive function is one which calls itself.(AMITY CENTER FOR e-LEARNING) main ( ) { int a = 10. This is another complicated idea which you are unlikely to meet frequently. You may also encounter certain dynamic data structures such as linked lists or binary trees. b =20. } The output of the above program would be a = 20 b =10 5. break. swapv (&a. Recursive functions are useful in evaluating certain types of mathematical function. We shall provide some examples to illustrate recursive functions. Here is a recursive version of the Fibonacci function. int fib(int num) /* Fibonacci value of a number */ { switch(num) { case 0: return(0). Recursion is a very useful way of creating and accessing these structures. } swapr (int **. &b). 70 . a. b). int * y) { int t.

2)). 71 . double power(double val. } } We met another function earlier called power.(AMITY CENTER FOR e-LEARNING) case 1: return(1).0). otherwise your program will never finish. it is returned directly. default: /* Including recursive calls */ return(fib(num .1) + fib(num . unsigned pow) { if(pow == 0) /* pow(x. otherwise the function calls itself. passing a changed version of the input values. The definition of fib is interesting. } Notice that each of these definitions incorporates a test. Care must be taken to define functions which will not call themselves indefinitely.1) * val). Here is an alternative recursive version. break. Consider the effect on program performance of such a function calculating the fibonacci function of a moderate size number. pow . else return(power(val. Where an input value gives a trivial result. 0) returns 1 */ return(1. break. because it calls itself twice when recursion is used.

(AMITY CENTER FOR e-LEARNING) If such a function is to be called many times. The second part of the defintion refers to a cycle (or potential cycle if we use conditional statements) which involves other functions. Don't be frightened by the apparent complexity of recursion. and may lack the elegance of the recursive solution. there are two types of recursive functions. Recursive functions are sometimes the simplest answer to a calculation.-| ^ | | | |---. return.| void A() { A(). it is likely to have an adverse effect on program performance. As the definition specifies. Consider a function which calls itself: we call this type of recursion immediate recursion. One can view this mathematically in a directed call graph. Consider the following directed call graph 72 . This will normally involve the use of a loop. } A() is a recursive function since it directly calls itself. However there is always an alternative nonrecursive solution available too. A -.

Writing Recursive Functions A recursive function has the following general form (it is simply a specification of the general function we have seen many times): ReturnType Function( Pass appropriate arguments ) { if a simple case. 73 . return.(AMITY CENTER FOR e-LEARNING) A ---------> B ^ | | | | | |---. return the simple value // base case / stopping condition else call function with simpler version of problem } For a recursive function to stop calling itself we require some type of stopping condition.C <----| This can be viewed in the following three functions: void C() { A(). } void A() { B(). return. } Recursive functions are an inefficient means of solving problems in terms of run times but are interesting to study nonetheless. return. If it is not the base case. } void B() { C(). then we simplify our computation using the general formula. For our purposes we will only consider immediate recursion since this will cause enough difficulty.

74 .6. . Alternatively. we will quickly realize that we cannot perform such an operation until each and every grade has been entered since it would be quite a tedious task to declare each and every student grade as a variable especially since there may be a very large number.5.1 6. Each element of the set can then be referenced by means of a number called as index number or subscript.4 Initialization of an arrays 6. array is a collection of homogeneous element. which represents not a single value of grade but a entire set of grades. Let's start by looking at a single variable used to store a person's age. 6.2 Need of an Array To understand the need .1 6.6.3 Declaration of Arrays 6.2 initialization of multidimensional array more on multidimensional array 6.(AMITY CENTER FOR e-LEARNING) Chapter 6 Arrays Contents: 6.2 Need of an array 6.5 More operations on array 6. In C we can define variable called grades.3 Create an array Printing an array Copying an array 6.homogeneous means elements of same data type .5.1 Introduction The C language provides a capability that enables the user to define a set of ordered data items known as an array.2 6.5.1 Introduction 6.6 Multidimensional Array 6.Suppose we had a set of grades that we wished to read into the computer and suppose we wished to perform some operations on these grades.

The variable age is created at line (5) as a short.(AMITY CENTER FOR e-LEARNING) 1: #include <stdio. Finally. So height [0] refers to the first element of the array. Now let's keep track of 4 ages instead of just one. In C the array elements index or subscript begins with number zero. such as int float or char and the size indicates the maximum number of elements that can be stored inside the array for ex: float height[50]. age). 75 . Any subscripts 0 to 49 are valid.h> 2: 3: int main() 4: { 5: short age. (If using 4 separate variables is appealing to you. The type specifies the type of the elements that will be contained in the array. In the statement grades [100]=95. The statement assigns the value stored in the 50th index of the array to the variable g.3 Declaration of arrays Like any other variable arrays must be declared before they are used. so if I were equal to 7 when the above statement is executed. then consider keeping track of 93843 ages instead of just 4). (For this reason. then the statement g=grades [I]. Hence the need 6. A value stored into an element in the array simply by specifying the array element on the left hand side of the equals sign. More generally if I is declared to be an integer variable. Declares the height to be an array containing 50 real elements. rather than as referring to the first element). but 4 separate variables have limited appeal. then the value of grades [7] would get assigned to g. The general form of declaration is: type variable-name[50]. As individual array element can be used anywhere that a normal variable with a statement such as G = grade [50]. 7: printf("%d\n". We could create 4 separate variables. 9: } Not much to it. 8: return 0. Will take the value contained in the element number I of the grades array to assign it to g. it is easier to think of it as referring to element number zero. 6: age=23. Rather than using 4 separate variables. we'll use an array. A value is assigned to age. age is printed to the screen.

If low is equal to 1 and high were equal to 9. would reserve enough space for an array called values that could hold up to 10 integers.++i). integer valued expressions can also be inside the brackets to reference a particular element of the array. Refer to the below given picture to conceptualize the reserved storage space. would assign to the variable next_value indexed by evaluating the expression (low+high)/2. then the statement next_value=sorted_data[(low+high)/2]. The ability to represent a collection of related data items by a single array enables us to develop concise and efficient programs. The declaration of an array involves the type of the element that will be contained in the array such as int. So if low and high were defined as integer variables. then the value of sorted_data[5] would be assigned to the next_value and if low were equal to 1 and high were equal to 10 then the value of sorted_data[5] would also be referenced.The declaration int values[10]. The C system needs this latter information in order to determine how much memory space to reserve for the particular array. float. sum = sum + grades [i]. the variable sum will then contain the total of first 100 values of the grades array (Assuming sum were set to zero before the loop was entered) In addition to integer constants.(AMITY CENTER FOR e-LEARNING) The value 95 is stored into the element number 100 of the grades array. Will sequence through the first 100 elements of the array grades (elements 0 to 99) and will add the values of each grade into sum. values[0] values[1] values[2] values[3] 76 . Just as variables arrays must also be declared before they are used. char as well as maximum number of elements that will be stored inside the array. So the for loop for(i=0.i < 100. When the for loop is finished. For example we can very easily sequence through the elements in the array by varying the value of the variable that is used as a subscript into the array.

The initialization of arrays in c suffers two draw backs 1. then only that many elements are initialized. in such cases the compiler allocates enough space for all initialized elements. There is no shortcut method to initialize large number of elements. this approach works fine as long as we initialize every element in the array.4 Initialization of arrays We can initialize the elements in the array in the same way as the ordinary variables when they are declared. 6. The values in the list care separated by commas.(AMITY CENTER FOR e-LEARNING) values[4] values[5] values[6] values[7] values[8] values[9] The array values stored in the memory. for example the statement int number[3]={0. For example the statement int counter[]={1.1. 77 .1. The general form of initialization off arrays is: type array_name[size]={list of values}. Will declare the array size as a array of size 3 and will assign zero to each element if the number of values in the list is less than the number of elements. Will declare the array to contain four elements with initial values 1.1}. In the declaration of an array the size may be omitted.0. 2.0}.There is no convenient way to initialize only selected elements. The remaining elements will be set to zero automatically.

but add the maximum size in square brackets after the name. employee_record employees[1000]. because arrays have a fixed length. like this: int test_answers[10][20]. you will need to define a structure. test_scores[i]).5 More operations on array 6. printf("Test score: %d\n". 78 . and having it require (and waste) a lot of memory. You'll want to strike a balance between having your program able to handle unusually long data sets. a list of test scores will be an array of integers. Step 3 Create the array the same way you would create a single variable. For example. as in these examples: int test_scores[50].(AMITY CENTER FOR e-LEARNING) 6. Step 2 Decide what the largest size your array can ever reach will be. Step 4 Create a second variable that keeps track of how many elements you've added to the array so far. char student_names[50][20].5. Use the Array Step 7 Access the array's elements using the index in square brackets.1 Create an Array in C Step 1 Understand that every element of an array (or any other kind of list) will be the same kind of data. since each test score is an integer. like this: test_scores[12] = 50. char first_name[20]. For more complex data. Step 5 Create an array with multiple dimensions simply making an array of arrays.

12: return 0.h> 2: 79 . The important point to come away with is that simply providing the name of the array in an output statement will not print out the elements of the array. 7: age[1]=34. test_subtotal = subtotal(test_scores). 8: age[2]=65.5. age). 9: age[3]=74. This is what the function definition and call would look like: function subtotal(int test_scores[]).(AMITY CENTER FOR e-LEARNING) Step 8 Pass arrays to functions. But instead of printing out the four short variables. 6: age[0]=23. what appears to be nonsense prints out instead.h> 2: 3: int main() 4: { 5: short age[4].2 Printing arrays Here is the same program with an attempt to print the array to the screen: 1: #include <stdio. if you so choose. 13: } Line (11) is meant to print the 4 ages to the screen. 6. 10: 11: printf("%x\n". How about printing out each of the values separately? Try this: 1: #include <stdio. You don't need to (and should not) specify the size in the function definition.

6: age[0]=23.3 Copying arrays Suppose that after filling our 4 element array with values. 7: age[1]=34. 13: printf("%d\n". 15: } Lines (10) through line (13) produce the output we are expecting. age[3]). Each element in the array must be printed to the screen individually. 10: printf("%d\n". 9: age[3]=74. 6.5. 6: short same_age[4]. There is no single statement in the language that says "print an entire array to the screen". age[2]). 8: age[2]=65. age[1]).(AMITY CENTER FOR e-LEARNING) 3: int main() 4: { 5: short age[4]. 14: return 0. 80 .h> 2: 3: int main() 4: { 5: short age[4]. age[0]). we need to copy that array to another array of 4 short's? Try this: 1: #include <stdio. 11: printf("%d\n". 12: printf("%d\n".

13: 14: printf("%d\n". same_age[1]). 11: 12: same_age=age. same_age[2]). 18: return 0. What happened when you tried to compile the program above? Let's try copying arrays using a technique similar to the technique used to print arrays (that is. 8: age[1]=34. 17: printf("%d\n". 19: } Line (12) tries to copy the age array into the same_age array. 11: age[3]=74. 10: age[2]=65. 9: age[2]=65. 9: age[1]=34. 15: printf("%d\n". 7: 8: age[0]=23. 16: printf("%d\n". same_age[3]).h> 2: 3: int main() 4: { 5: short age[4]. one element at a time): 1: #include <stdio. 12: 81 .(AMITY CENTER FOR e-LEARNING) 7: age[0]=23. same_age[0]). 6: short same_age[4]. 10: age[3]=74.

(AMITY CENTER FOR e-LEARNING)
13: same_age[0]=age[0]; 14: same_age[1]=age[1]; 15: same_age[2]=age[2]; 16: same_age[3]=age[3]; 17: 18: printf("%d\n", same_age[0]); 19: printf("%d\n", same_age[1]); 20: printf("%d\n", same_age[2]); 21: printf("%d\n", same_age[3]); 22: return 0; 23: } This technique for copying arrays works fine. Two arrays are created: age and same_age. Each element of the age array is assigned a value. Then, in order to copy of the four elements in age into the same_age array, we must do it element by element. Like printing arrays, there is no single statement in the language that says "copy an entire array into another array". The array elements must be copied individually. The technique used to copy one array into another is exactly the same as the technique used to copy 4 separate variables into 4 other variables. So what is the advantage to using arrays over separate variables? One significant advantage of an array over separate variables is the name. In our examples, using four separate variables requires 4 unique names. The 4 short variables in our array have the same name, age. The 4 short's in the array are identical except for an index number used to access them. This distinction allows us to shorten our code in a way that would be impossible with 4 variables, each with unique names: 1: #include <stdio.h> 2: 3: int main() 4: { 5: short age[4]; 6: short same_age[4]; 7: int i, j;
82

(AMITY CENTER FOR e-LEARNING)
8: age[0]=23; 9: age[1]=34; 10: age[2]=65; 11: age[3]=74; 12: 13: for(i=0; i<4; i++) 14: 15: 16: for(j=0; j<4; j++) 17: printf("%d\n", same_age[j]); same_age[i]=age[i];

18: return 0; 19: }

Since the only difference between each of the short's in the arrays is their index, a loop and a counter can be used to more easily copy all of the elements. The same technique is used to shorten the code that prints the array to the screen. Even though arrays give us some convenience when managing many variables of the same type, there is little difference between an array and variables declared individually. There is no single statement to copy an array, there is no single statement to print an array. If we want to perform any action on an array, we must repeatedly perform that action on each element in the array.

6.6 Multi dimensional Arrays
Often there is a need to store and manipulate two dimensional data structure such as matrices & tables. Here the array has two subscripts. One subscript denotes the row & the other the column. The declaration of two dimension arrays is as follows: data_type array_name[row_size][column_size]; int m[10][20]

83

(AMITY CENTER FOR e-LEARNING)
Here m is declared as a matrix having 10 rows( numbered from 0 to 9) and 20 columns(numbered 0 through 19). The first element of the matrix is m[0][0] and the last row last column is m[9][19] Elements of multi dimension arrays: A 2 dimensional array marks [4][3] is shown below figure. The first element is given by marks [0][0] contains 35.5 & second element is marks [0][1] and contains 40.5 and so on.

marks [0][0] Marks [0][1] Marks [0][2] 35.5 40.5 45.5

marks [1][0] Marks [1][1] Marks [1][2] 50.5 55.5 60.5

marks [2][0] Marks [2][1] Marks [2][2]

marks [3][0] Marks [3][1] Marks [3][2]

6.6.1 Initialization of multidimensional arrays
Like the one dimension arrays, 2 dimension arrays may be initialized by following their declaration with a list of initial values enclosed in braces Example: int table[2][3]={0,0,01,1,1}; Initializes the elements of first row to zero and second row to 1. The initialization is done row by row. The above statement can be equivalently written as int table[2][3]={{0,0,0},{1,1,1}} By surrounding the elements of each row by braces. C allows arrays of three or more dimensions. The compiler determines the maximum number of dimension. The general form of a multidimensional array declaration is:
84

q.h > #include< conio.j < n.j.i++) for(j=0.h > void main() { int a[10][10].p. Similarly table is a four dimensional array containing 300 elements of floating point type. /* example program to add two matrices & store the results in the 3rd matrix */ #include< stdio.[sn]. Where s is the size of the ith dimension.m.j < q.i < p.j++) scanf(―%d‖. for(i=0.i++) for(j=0. printf(―enter the order of the matrix\n‖). printf(―enter the elements of the matrix a‖). scanf(―%d%d‖. if(m==p && n==q) { printf(―matrix can be added\n‖).n.j++) scanf(―%d‖. for(i=0.. clrscr().c[10][10].i. float table[5][4][5][3].&b[i][j]). printf(―enter the elements of the matrix b‖).b[10][10]. Some examples are: int survey[3][5][12].j < n. printf(―the sum of the matrix a and b is‖).&p.&q).i < m.i < m.i++) for(j=0.&a[i][j]). for(i=0.(AMITY CENTER FOR e-LEARNING) date_type array_name[s1][s2][s3]….j++) 85 . Survey is a 3 dimensional array declared to contain 180 integer elements.

That means that.i++) { for(j=0.j < n.2 More on Multi-Dimensional Arrays consider #define ROWS 5 #define COLS 10 int multi[ROWS][COLS]. the address pointed to by (i.6. multi + row + 1 must increase by value an amount equal to that needed to "point to" the next row. which in this case would be an 86 .i < m.(AMITY CENTER FOR e-LEARNING) c[i][j]=a[i][j]+b[i][j].j++) printf(―%d\t‖.e. Here the arithmetic being used is of a special kind called "pointer arithmetic" is being used. we can access individual elements of the array multi using either: multi[row][col] or *(*(multi + row) + col) To understand more fully what is going on. Since we know the memory layout for 2 dimensional arrays. since we are talking about an integer array. from this we see that X is like a pointer since the expression is de-referenced and we know that col is an integer. we can determine that in the expression multi + row as used above. printf(―\n‖). for(i=0.&a[i][j]). let us replace *(multi + row) with X as in: *(X + col) Now. value of) X + col + 1 must be greater than the address X + col by and amount equal to sizeof(int). } } 6.

Thus. The specific index value for the second dimension. col < COLS. The 2nd dimension of the array 4. } } } And to call this function we would then use: 87 . i. the 2nd dimension. col++) { m_array[row][col] = 1. col. col in this case. The address of the first element of the array. which is returned by the expression multi. 3. row < ROWS. The specific index value for the first dimension. a total of 5 values must be known: 1.. 2. row++) { for (col = 0. one which would set all the elements of the array multi to the value 1. row in this case. consider the problem of designing a function to manipulate the element values of a previously declared array.(AMITY CENTER FOR e-LEARNING) amount equal to COLS * sizeof(int). 5. in this case sizeof(int). to evaluate either expression. the name of the array.e. for (row = 0. i. Because of the equivalence of the two forms of expression. For example. this is true whether we are using the pointer expression as here or the array expression multi[row][col]. The size of the type of the elements of the array. Given all of that. void set_value(int m_array[][COLS]) { int row. the compiler must generate code which takes into consideration the value of COLS. That says that if the expression *(*(multi + row) + col) is to be evaluated correctly at run time.e.

I have not used it here. 88 . Thus. i. often referred to as a pointer to the array. row and col are local variables.e. But. only one value can be passed using a single parameter. out of habit or consistency. The reason is that we need this in the evaluation of m_array[row][col] as has been described.e. within the function we have used the values #defined by ROWS and COLS that set the limits on the for loops. the second dimension must be used as has been shown in the expression for the parameter. as will be seen later. We really don‘t need the first dimension and. the 2nd and 3rd dimension must be specified in the parameter definition. these #defines are just constants as far as the compiler is concerned. The formal parameter definition permits the compiler to determine the characteristics associated with the pointer value that will be passed at run time. That is if we are talking about 3 dimensional arrays. In fact. there is nothing to connect them to the array size within the function.(AMITY CENTER FOR e-LEARNING) set_value(multi). of course. the address of the first element. i. in general all dimensions of higher order than one are needed when dealing with multidimensional arrays. In this case. there are occasions where we would prefer not to define it within the parameter definition. the only way we have of informing the compiler of the 2nd dimension is by explicitly including it in the parameter definition. But. that is the value of multi as noted in the call statement. Now. While the parameter defines the data type (int in this case) and the automatic variables for row and column are defined in the for loops.

#include <stdio.3 Pointers and Strings 7. If you want to know the size of the various types of integers on your system. In C the size of a variable type such as an integer need not be the same on all types of machines.1 Introduction 7. running the following code will give you that information.(AMITY CENTER FOR e-LEARNING) Chapter 7 Pointers Contents: 7. long integers and short integers which you can read up on in any basic text on C. This document assumes the use of a 32 bit system with 4 byte integers. The way the compiler and linker handles this is that it assigns a specific block of memory within the computer to hold the value of that variable. I have found that often the main reason beginners have a problem with pointers is that they have a weak or minimal feeling for variables. (as they are used in C). Further more there is more than one type of integer variable in C.h> 89 .5 Dynamic Allocation of Memory 7.4 Pointers to Arrays 7. The size of that block depends on the range over which the variable is allowed to vary.2 Pointer types and Arrays 7. the value of which can vary. On older 16 bit PCs integers were 2 bytes. on 32 bit PC's the size of an integer variable is 4 bytes. We have integers. The purpose of this chapter is to provide an introduction to pointers and their use to these beginners. For example.1 introduction One of those things beginners in C find difficult is the concept of pointers.A variable in a program is something with a name. Thus we start with a discussion of C variables in general.

Thus: 2 = k. the 2 above. In some languages. On seeing the "int" part of this statement the compiler sets aside 4 bytes of memory (on a PC) to hold the value of the integer. printf("size of a long is %d\n". } When we declare a variable we inform the compiler of two things. the lvalue is the value permitted on the left side of the assignment operator '=' (i.e. Actually. It also sets up a symbol table. i.(AMITY CENTER FOR e-LEARNING) int main() { printf("size of a short is %d\n". In a sense there are two "values" associated with the object k. the value 2 will be placed in that memory location reserved for the storage of the value of k. the address where the result of evaluation of the right side ends up). pronounced "el value") respectively.. sizeof(long)). printf("size of a int is %d\n". the above definition of "lvalue" is somewhat modified for C. In that table it adds the symbol k and the relative address in memory where those 4 bytes were set aside. According to K&R II "An object is a named region of storage. we expect that. is illegal. The rvalue is that which is on the right side of the assignment statement. sizeof(short)). the address of k. at run time when this statement is executed. pronounced "are value") and lvalue (left value. the name of the variable and the type of the variable.e. sizeof(int)). Some texts refer to these two values with the nomenclature rvalue (right value." 90 . Rvalues cannot be used on the left side of the assignment statement. Thus. One is the value of the integer stored there (2 in the above example) and the other the "value" of the memory location. For example. later if we write: k = 2. an lvalue is an expression referring to an object. we declare a variable of type integer with the name k by writing: int k. In C we refer to a variable such as the integer k as an "object".

Such a variable is called a pointer variable (for reasons which hopefully will become clearer a little later). we are using 4 byte integers so all copying of rvalues from one storage location to the other is done by copying 4 bytes. j = 7. ptr is the name of our variable (just as k was the name of our integer variable). On older desk top computers with 64K of memory total. For example. The '*' informs the compiler that we want a pointer variable.line 2 In the above. let's say that we have a reason for wanting a variable designed to hold an lvalue (an address). the compiler interprets the j in line 1 as the address of the variable j (its lvalue) and creates code to copy the value 7 to that address. k = j. The actual size required is not too important so long as we have a way of informing the compiler that what we want to store is an address. now consider: int j.(AMITY CENTER FOR e-LEARNING) However. k = 2. In line 2. however.e. As we become more familiar with pointers we will go into more detail on this.line 1 <-. Computers with more memory would require more bytes to hold an address. refers to the type of data stored at the address we will be storing in our pointer. Okay. In C when we define a pointer variable we do so by preceding its name with an asterisk. That is. in this case 7. here the j refers to the value stored at the memory location set aside for j. we would be copying 2 bytes. k. consider the variable declaration: int *ptr. <-. to set aside however many bytes is required to store an address in memory. The int says that we intend to use our pointer variable to store the 91 . the definition originally cited above is sufficient. In C we also give our pointer a type which. Had we been using two byte integers. the j is interpreted as its rvalue (since it is on the right hand side of the assignment operator '='). in this case. the address of any point in memory can be contained in 2 bytes. i. at this point. The size required to hold such a value depends on the system. In all of these examples. the 7 is copied to the address designated by the lvalue of k. Now. So.

However. and copies that to the contents of our pointer ptr. That macro goes under the name NULL. we did not give k a value. Similarly.. To make the source code compatible between various compilers on various systems. note that when we wrote int k. What the & operator does is retrieve the lvalue (address) of k. back to using our new variable ptr. will copy 7 to the address pointed to by ptr. when we use the '*' this way we are referring to the value of that which ptr is pointing to. guarantees that the pointer has become a null pointer. If this definition is made outside of any function ANSI compliant compilers will initialize it to zero. In this case. even though k is on the right hand side of the assignment operator '='. ptr has no value. just as one can test for an integer value of zero. Now. a macro is used to represent a null pointer. 92 .*ptr). there is only one more operator we need to discuss. that is we haven't stored an address in it in the above declaration. Bear with us now. But. That is. as in if(k == 0). Suppose now that we want to store in ptr the address of our integer variable k. we could write: printf("%d\n". Similarly. The actual bit pattern used for a null pointer may or may not evaluate to zero since it depends on the specific system on which the code is developed. Thus if ptr "points to" (contains the address of) k.(AMITY CENTER FOR e-LEARNING) address of an integer. The "dereferencing operator" is the asterisk and it is used as follows: *ptr = 7. setting the value of a pointer using the NULL macro. To do this we use the unary & operator and write: ptr = &k. A pointer initialized in this manner is called a "null" pointer. Similarly. not the value of the pointer itself. ptr is said to "point to" k. Such a pointer is said to "point to" an integer. again if the declaration is outside of any function. as with an assignment statement such as ptr = NULL. we can test for a null pointer using if (ptr == NULL). Thus. it is initialized to a value guaranteed in such a way that it is guaranteed to not point to any C object or function. the above statement will set the value of k to 7. to print to the screen the integer value stored at the address pointed to by ptr.

(AMITY CENTER FOR e-LEARNING) One way to see how all this stuff fits together would be to run the following program and then review the code and the output carefully. once ptr "points to" something.h> int j.2 Pointer types and Arrays Okay. *ptr). int main(void) { j = 1. j. k. One reason for doing this is so that later. the compiler will know how many bytes to copy into that memory location pointed to by ptr. #include <stdio. (void *)&j). if we write: *ptr = 2. If ptr was declared as pointing to an integer. But. printf("\n"). 40 bytes of memory are set aside 93 . Let us consider why we need to identify the type of variable that a pointer points to. let's move on. Similarly for floats and doubles the appropriate number will be copied. return 0. That is. int *ptr. (void *)&ptr). printf("The value of the integer pointed to by ptr is %d\n". as in: int *ptr. consider a block in memory consisting if ten integers in a row. k. defining the type that the pointer points to permits a number of other interesting ways a compiler can interpret code. ptr. (void *)&k). 4 bytes would be copied. For example. printf("j has the value %d and is stored at %p\n". } 7. ptr = &k. printf("k has the value %d and is stored at %p\n". printf("ptr has the value %p and is stored at %p\n". k = 2.

we could alternatively access them via a pointer as follows: int *ptr. What happens when we write: ptr + 1. Now.or post-.23. a term which we will come back to later. But.e. The following code illustrates this: 94 . Here we have an array containing 6 integers.e. incrementing a pointer using the unary ++ operator. In C it is referred to as addition using "pointer arithmetic". let's say we point our integer pointer ptr at the first of these integers. Similarly. an array of integers. increments the address it stores by the amount sizeof(type) where "type" is the type of the object pointed to.4.e. This is obviously not the same kind of "addition" that we normally think of. ptr = &my_array[0]. its value is an address) and that it points to an integer (its current address.(AMITY CENTER FOR e-LEARNING) to hold 10 integers. so the pointer "points to" the next integer. /* point our pointer at the first integer in our array */ And then we could print out our array either using the array notation or by dereferencing our pointer. doubles. by definition. 4 for an integer). this brings up an interesting relationship between arrays and pointers. either pre. 100. Similarly. it adds 4 to ptr instead of 1. Consider the following: int my_array[] = {1. Since a block of 10 integers located contiguously in memory is. i. since ++ptr and ptr++ are both equivalent to ptr + 1 (though the point in the program when ptr is incremented may be different). using my_array[0] through my_array[5]. were the ptr declared as a pointer to a short. Furthermore lets say that integer is located at memory location 100 (decimal). Because the compiler "knows" this is a pointer (i.-5. or even user defined data types such as structures. The same goes for other data types such as floats. at memory location 104. We refer to each of these integers by means of a subscript to my_array.17. is the address of an integer). (i.100}. it would add 2 to it instead of 1.

23. we first added i to it and then dereferenced the new pointer. i++) { printf("my_array[%d] = %d ".i.17. thus in our code where we wrote: ptr = &my_array[0]. then change it to: printf("ptr + %d = %d\n". and try once more. Each time try and predict the outcome and carefully look at the actual outcome. *(++ptr))..i.e. *ptr++). Also observe how we dereferenced our pointer in line B. we can write: 95 /* point our pointer to the first element of the array */ /*<-. i < 6. ptr = &my_array[0].i. *(ptr + i)). int *ptr. } return 0.A */ printf("ptr + %d = %d\n".4.. /*<-. for (i = 0. printf("\n\n"). the standard states that wherever we might use &var_name[0] we can replace that with var_name. Change line B to read: printf("ptr + %d = %d\n". int main(void) { int i.100}. and run it again.(AMITY CENTER FOR e-LEARNING) #include <stdio. } Compile and run the above program and carefully note lines A and B and that the program prints out the same values in either case.h> int my_array[] = {1.-5.i. In C. i.my_array[i]).B */ .

to achieve the same result. For example. This raises an interesting problem. Some writers will refer to an array's name as a constant pointer. What do we mean by that? Well. let's delve a little further into the difference between the names ptr and my_array as used above. Modify the example program above by changing ptr = &my_array[0]. When used on the left side of the assignment operator. to understand the term "constant" in this sense. the compiler interprets it as the 96 . the location at which the first element of my_array will be stored cannot be changed once my_array[] has been declared. This leads many texts to state that the name of an array is a pointer. Now. we cannot write my_array = ptr. Since my_array is a named region of storage.(AMITY CENTER FOR e-LEARNING) ptr = my_array. I prefer to mentally think "the name of the array is the address of first element in the array". and run it again to verify the results are identical. an lvalue is an expression referring to an object". Earlier when discussing the term "lvalue" I cited K&R-2 where it stated: "An object is a named region of storage. to ptr = my_array. When we declare a variable we set aside a spot in memory to hold the value of the appropriate type. let's go back to our definition of the term "variable". why is my_array in the above assignment statement not an lvalue? To resolve this problem. The reason is that while ptr is a variable. my_array is a constant. That is. while we can write ptr = my_array. Many beginners (including myself when I was learning) have a tendency to become confused by thinking of it as a pointer. Once that is done the name of the variable can be interpreted in one of two ways. some refer to my_array as an "unmodifiable lvalue".

while i is a variable and then occupies space in the data portion of memory. Finally it illustrates how and when pointers can and should be passed to functions. Pascal. But. when used on the right side of the assignment operator. tells the compiler to create code which at run time will look at memory location &i to determine the value to be moved to k. 7. let's now consider the simplest of constants. Similarly. With that in mind. code created by i = 2. This is not necessarily true in other languages. you would probably never write in an actual program. But in C it does not. To start off our discussion we will write some code which. Fortran and various other languages. k. 2 is a constant and. Consider. while writing something like k = i. it simply uses this address as a constant in the code segment and there is no referencing of the data segment beyond that. while preferred for illustrative purposes. simply puts the 2 in the code and there is no referencing of the data segment. since my_array is a constant. it is imbedded directly in the code segment of memory. strings are arrays of characters. in the above. It also makes it easy to illustrate how some of the standard C string functions can be implemented. That is. for example: 97 . once the compiler establishes where the array itself is to be stored. it "knows" the address of my_array[0] and on seeing: ptr = my_array. but 2 is not an object. In C a string is an array of characters terminated with a binary zero character (written as '\0'). Here. the name of a variable is interpreted to mean the contents stored at that memory address set aside to hold the value of that variable. as such. both k and i are objects. a string has its own data type. In BASIC.(AMITY CENTER FOR e-LEARNING) memory location to which to move that value resulting from evaluation of the right side of the assignment operator. That is. i = 2. as in: int i. instead of setting aside memory in the data segment. In C.3 Pointers and Strings The study of strings is useful to further tie in the relationship between pointers and arrays.

puts(pA). a string is an array of characters terminated with the nul character. /* point pA at string A */ /* show what pA is pointing to */ 98 . char *pB. NULL. my_string[1] = 'e'.}. The nul refers to a zero as defined by the escape sequence '\0'. one might write: char my_string[40] = {'T'. '\0'. C permits two alternate ways of achieving the same thing. instead of the single quotes as was done in the previous #include <stdio. char strB[80]. my_string[2] = 'd': my_string[3] = '\0'. By definition. the end result is a string in that it is an array of characters terminated with a nul character. is the name of the macro used to initialize null pointers. When the double quotes are used. First.h> char strA[80] = "A string to be used for demonstration purposes". /* a pointer to type character */ /* another pointer to type character */ puts(strA). That is it occupies one byte of memory. int main(void) { char *pA. in C. my_string[0] = 'T'. on the other hand. NULL is #defined in a header file in your C compiler.(AMITY CENTER FOR e-LEARNING) char my_string[40]. Be aware that "nul" is not the same as "NULL". So. But this also takes more typing than is convenient. 'd'. 'e'. Since writing the above code would be very time consuming. While one would never build a string like this. C permits: char my_string[40] = "Ted". nul may not be #defined at all. /* show string A */ pA = strA.

do the following: Line B states: copy the character pointed to by pA to the space pointed to by pB. Line A states: While the character pointed to by pA (i. *pA) is not a nul character (i. we declare two character pointers and show the string on the screen.(AMITY CENTER FOR e-LEARNING) pB = strB. by means of the assignment statement we copy the address of strA[0] into our variable pA. Then. we are passing the same address. ignore the const. we are passing the address of strA[0]. simply. when we write puts(pA). or. as we have seen. Thus when we write puts(strA). The parameter passed to puts() is a pointer. follow the code down to the while() statement on line A. strA has the first 42 characters initialized to the string in quotes. Now. Consider here that the function prototype for puts() is: int puts(const char *s). /* point pB at string B */ /* move down one line on the screen */ while(*pA != '\0') /* line A (see text) */ { *pB++ = *pA++. /* line B (see text) */ } *pB = '\0'. moving into the code. We then "point" the pointer pA at strA. then increment pA so it points to the next character and pB so it points to the next space. /* line C (see text) */ /* show strB on screen */ 99 . For the moment. they are initialized to all '\0's first. putchar('\n'). return 0. That is. since we have set pA = strA. an address. puts(strB). Given that.e. } In the above we start out by defining two character arrays of 80 characters each. We now use puts() to show that which is pointed to by pA on the screen. the terminating '\0'). and the value of a pointer is the address to which it points. that is the value of a pointer (since all parameters in C are passed by value). Since these are globally defined.e. Similarly.

} *p = '\0'. by definition a string in C must be nul terminated. i. } 100 . pA and pB and single stepping through the program. while (*source != '\0') { *p++ = *source++. pA now points to the terminating nul character and the loop ends. It is very educational to run this program with your debugger while watching strA. After playing with the above until you have a good understanding of what is happening. So. And. initialize it also with something like: strB[80] = "12345678901234567890123456789012345678901234567890" where the number of digits used is greater than the length of strA and then repeat the single stepping procedure while watching the above variables. Of course. what the above program illustrates is a simple way of copying a string.(AMITY CENTER FOR e-LEARNING) When we have copied the last character.e. we have not copied the nul character. the "const" used as a parameter modifier informs the user that the function will not modify the string pointed to by s. Give these things a try! Getting back to the prototype for puts() for a moment. we add the nul character with line C. It is even more educational if instead of simply defining strB[] as has been done above. It might look like: char *my_strcpy(char *destination. return destination. it will treat that string as a constant. char *source) { char *p = destination. we can proceed to creating our own replacement for the standard strcpy() that comes with C. strB. However.

if used on the first character of the above example string the 'T' would be incremented to a 'U'. Try it and see. Now. const char *source). puts(strB). and thus in the previous program we could write: int main(void) { my_strcpy(strB.(AMITY CENTER FOR e-LEARNING) In this case. Recall again that a string is nothing more than an array of characters. I have followed the practice used in the standard routine of returning a pointer to the destination. we would not be dealing with strings and hence the end of the array would not be marked with a special value like the nul 101 . Were we to write (*ptr)++ we would increment. First off. It happens to be an array of characters but the technique could be applied to an array of integers. i. with the last character being a '\0'. which would normally change the first character of the string to an X. The const modifier should cause your compiler to catch this as an error. within the function you can add a statement which attempts to change the contents of that which is pointed to by source.e. the function is designed to accept the values of two character pointers. let's consider some of the things the above examples have shown us. doubles. What we have done above is deal with copying an array. Again. to include the "const" modifier as shown. addresses. Then. but that which the pointer points to! i. You can prove this by modifying the function above. You can write some simple example code to illustrate this. not the pointer. strA). In those cases. } I have deviated slightly from the form used in standard C which would have the prototype: char *my_strcpy(char *destination. This has to do with the precedence of the operators. etc. and its prototype.e. consider the fact that *ptr++ is to be interpreted as returning the value pointed to by ptr and then incrementing the pointer value. Here the "const" modifier is used to assure the user that the function will not modify the contents pointed to by the source pointer. however. such as: *source = 'X'.

Within the function any manipulation of the value passed can in no way effect the original integer. e.e. The array itself does not get passed.g. 7. 102 . For example. with arrays and pointers we can pass the address of the variable and hence manipulate the values of the original variables. to a function. i. can be "pointed at" any type of data object. we could copy an array of positive integers by marking the end with a negative integer. depending on what we are doing). if we have an array of 5000 integers that we want to manipulate with a function. the type of the pointer variable must match the type of the first element of the array. including arrays. we need only pass to that function the address of the array (and any auxiliary information such as nbr above. we can use a pointer as a formal parameter of a function which is designed to manipulate an array. only its address is sent. the whole array is not copied and put on the stack before calling the function. something like the following prototype might indicate: void int_copy(int *ptrA. say an integer. that given an array of integers we could point an integer pointer at that array using: int *ptr. In addition.e. i. /* point our pointer at the first integer in our array */ As we stated there.g. of course. e.4 Pointers to Arrays Pointers. it is more usual that when we write a function to copy an array of items other than strings we pass the function the number of items to be copied as well as the address of the array. ptr = &my_array[0]. int nbr). To review. get its value and put it on the stack. This is different from passing. On the other hand. But. int *ptrB. For example.(AMITY CENTER FOR e-LEARNING) character. This permits using functions to manipulate large arrays. While that was evident when we discussed program 3.1. We could implement a version that relied on a special value to identify the end. it is important to expand on how we do this when it comes to multi-dimensional arrays. where nbr is the number of integers to be copied. When we pass an integer we make a copy of the integer. You might want to play with this idea and create an array of integers and see if you can write the function int_copy() and make it work.

in either case. the word byte has replaced that which would normally be the name of our unsigned char. the rule for using typedef is that the new name for the data type is the name used in the definition of the data type. which would tend to inform others who might use this function that the function is designed to manipulate the elements of an array. Thus in: typedef int Array[10]. Hence byte b[10]. typedef assigns a new name to a specified data type. Because *p1d points to the same type as arr2d. As stated in the last chapter. independent of which notation is used in the function prototype or definition. void a_func(int *p). Also note that Array *p1d. the first element of a 2 dimensional array of integers is a one dimensional array of integers. makes arr2d an array of 5 arrays of 10 integers each. causes the name byte to mean type unsigned char. assigning the address of the two dimensional array arr2d to p1d. the 103 . Note that if the array notation is used. C interprets a 2 dimensional array as an array of one dimensional arrays. i. Some programmers might prefer to write the function prototype as: void a_func(int p[]). 5.(AMITY CENTER FOR e-LEARNING) Given: int array[3] = {1. We now turn to the problem of the 2 dimensional array. would be an array of unsigned characters. For example: typedef unsigned char byte. And a pointer to a two dimensional array of integers must be a pointer to that data type. 7}. That is. One way of accomplishing this is through the use of the keyword "typedef". declares my_arr as an array of 10 integers and Array arr2d[5]. Note that in the typedef declaration. Array my_arr.e. makes p1d a pointer to an array of 10 integers. only the address to the first element. That being the case. there is no need to pass the actual dimension of the array since we are not passing the whole array. Array becomes a data type for an array of 10 integers. what actually gets passed is the value of a pointer to the first element of the array. Of course.

7. Since the data type we use for our pointer is an array of 10 integers we would expect that incrementing p1d by 1 would change its value by 10*sizeof(int). is the proper declaration. 104 . Or it permits using a section of memory for the storage of an array of integers at one point in time. which it does. You can prove this to yourself by writing and running a simple short program.5 Dynamic Allocation of Memory There are times when it is convenient to allocate memory at run time using malloc(). calloc(). or p1d = arr2d. for example. with the ANSI compiler it is void.e. i. the allocating function (such as malloc(). p1d here is a pointer to an array of 10 integers just as it was under the declaration using the Array type. It turns out that this can be done and that int (*p1d)[10]. etc. until run time. such as the storage of an array of structures. and then when that memory is no longer needed it can be freed up for other uses.) returns a pointer. Using this approach permits postponing the decision on the size of the memory block need to store an array. With the older compiler the type of the returned pointer is char. while using typedef makes things clearer for the reader and easier on the programmer. i. p1d = &arr2d[0]. Now. which would make p1d the name of an array of 10 pointers to type int.(AMITY CENTER FOR e-LEARNING) pointer to a one dimensional array of 10 integers is acceptable. it is not really necessary. calloc(). are both correct. sizeof(*p1d) is 20. That is. When memory is allocated. or other allocation functions. The type of this pointer depends on whether you are using an older K&R compiler or the newer ANSI type compiler. Note that this is different from int *p1d[10]. What we need is a way of declaring a pointer like p1d without the need of the typedef keyword.e.

The array dimension can be determined at run time and is not needed at compile time. to allocate space for 10 integers we might write: int *iptr. Even with a reasonably good understanding of pointers and arrays. k++) iptr[k] = 2. the 10 above could be a variable read in from a data file or keyboard. } If you are using an ANSI compliant compiler. and you want to allocate memory for an array of integers you will have to cast the char pointer returned to an integer pointer. wherever possible. or calculated based on some need. to set the values of all elements to 2. Now. Because of the equivalence between array and pointer notation.. iptr = (int *)malloc(10 * sizeof(int)). That is. at run time. we would like to be able to access elements of such arrays using array notation. In general. ERROR ROUTINE GOES HERE . Whether we need to know the higher dimensions depends on how we go about writing the code. the (int *) cast shown above is not needed. one can use the array notation. malloc() returns a void pointer and since a void pointer can be assigned to a pointer variable of any object type. we never need to know the first dimension at compile time. For example. if (iptr == NULL) { . one could write: int k. one place the newcomer to C is likely to stumble at first is in the dynamic allocation of multi-dimensional arrays. when dynamically allocating a one dimensional array its dimension can be determined at run time.(AMITY CENTER FOR e-LEARNING) If you are using an older compiler.. when using dynamic allocation of higher order arrays. once iptr has been assigned as above. for (k = 0. Depending on the application we may or may not know both dimensions at compile time. For example. k < 10. Here I will discuss various methods of dynamically allocating room for 2 dimensional arrays of integers. 105 . This leads to a variety of ways to go about our task. As we have seen. not pointer notation.

(AMITY CENTER FOR e-LEARNING) Chapter 8 Structures and Union Contents: 8.3 Union 8. When we require using a collection of different data items of different data types we can use a structure. We can summarize structure as Structure is a method of packing the data of different types.1 8.2. If we need to use a collection of different data type items it is not possible using an array.2. A structure is used as a method of handling a group of related data items of different data types. When we require using a collection of different data items of different data types in that situation we can use a structure.4 More on structures and Union 8. Structure is a method of packing data of different types. A structure is a convenient method of handling a group of related data items of different data types.2.1 What is structure? 8.2 8.2 Functions and Structures 8.3 Passing structure to elements to functions Passing entire function to functions Arrays of structure 8.1 What is a Structure? Arrays are used to store large set of data and manipulate them but the disadvantage is that all the elements stored in an array are to be of the same data type. Syntax of Using Structure structure definition: general format: struct tag_name { 106 .

declares the book1. Each member may belong to same or different data type. To holds the details of four fields namely title. int pages. book3 as variables of type struct lib_books each declaration has four elements of the structure lib_books. book2. These are the members of the structures. book2. 8. The tag name can be used to define the objects that have the tag names structure. int pages.struct lib_books. book2.(AMITY CENTER FOR e-LEARNING) data type member1. book3. The structure we just declared is not a variable by itself but a template for the structure.2 Functions and structures 107 . float price. float price. book3. }.the keyword struct declares a structure. The complete structure declaration might look like this The complete structure declaration might look like this struct lib_books { char title[20]. }. char author[15]. book1. } Example of Using Structure: struct lib_books { char title[20]. We can declare the structure variables using the tag name any where in the program. author pages and price. struct lib_books book1. For example the statement. char author[15]. data type member2.

emp1.‖operator‖. } /* function to display structure variables*/ display(e_no.(AMITY CENTER FOR e-LEARNING) We can pass structures as arguments to functions.e_name). Unlike array names however.7500.e_name) int e_no. emp_id and name have been declared as integer and character array. # include < stdio. float salary.emp_id. /* pass only emp_id and name to display function*/ display(emp1.name).emp_id.00}. which always point to the start of the array.emp1.name). we don‘t effect its corresponding argument. in the declaration of structure type.‖sampath‖.4. when we change structure parameter inside a function.1 Passing structure to elements to functions A structure may be passed into a function as individual member or a separate variable. structure names are not pointers.e_name. A program example to display the contents of a structure passing the individual elements to a function is shown below. we are sending the emp_id and name to function display(0). As a result. static struct emp1={125.e_no. }. 8. { printf(―%d%s‖. char name[25]. char department[10].h > void main() { int emp_id. it can be immediately realized that 108 . When we call the function display() using display(emp1.

In such cases we may pass whole structure to a function as shown below: # include stdio.(AMITY CENTER FOR e-LEARNING) to pass individual elements would become more tedious as the number of structure elements go on increasing a better way would be to pass the entire structure variable at a time. } /*function to pass entire structure variable*/ 109 . void main() { static struct employee emp1= { 12. float salary. }. /*sending entire employee structure*/ display(emp1). 8. char name[25]. 7500.2.h> { int emp_id.2 Passing entire function to functions In case of structures having to having numerous structure elements passing these individual elements would be a tedious task.00 }. ―computer‖. char department[10]. ―sadanand‖.

%f‖.empf.2. #include< stdio.empf. char address[20].%s.3 Arrays of structure It is possible to define a array of structures for example if we are maintaining information of all the students in the college and if 100 students are studying in the college. char combination[3]. We can define an array of structures as shown in the following example: structure information { int id_no. Remember that each element is a structure that must be assigned corresponding initial values as illustrated below. empf. int age. char name[20].h > { struct info { int id_no. 110 . } student[100]. } 8.name. We need to use an array than single variables.empid.department. An array of structures can be assigned initial values just as any other array can.empf.salary).(AMITY CENTER FOR e-LEARNING) display(empf) struct employee empf { printf(―%d%s.

int year. printf(―\n Student information‖).&std[I]. char address[20]. In such structures the declaration of the embedded structure must appear before the declarations of other structures.n. int I. int month. ‖. printf(― Enter Id_no. } struct info std[100].4 Structure within a structure A structure may be defined as a member of another structure.address.I< n.id_no. }.id_no.age). printf(―Enter the number of students‖).std[I].&n). } 8.std[I].name.combination.std[I].I < n.std[I]. struct date { int day.address.age). scanf(―%d‖. int age.(AMITY CENTER FOR e-LEARNING) char name[20]. char combination[3].name address combination age\m‖).I++) scanf(%d%s%s%s%d‖.&std[I].std[I].I++) printf(―%d%s%s%s%d\n‖.2.std[I].name. for (I=0.std[I].combination. for(I=0.std[I]. struct student { 111 .

3 Union However the members that we compose a union all share the same storage area within the computers memory where as each member within a structure is assigned its own unique storage area. }oldstudent. newstudent. } code 112 . char combination[3]. Thus unions are used to observe memory. They are useful for the application involving multiple members.(AMITY CENTER FOR e-LEARNING) int id_no. int age. the sturucture student constains another structure date as its one of its members. char address[20]. structure date def. float p. structure date doa. Important points about a structure 1)A structure allocates the total size of all elements in it. Unions like structure contain members whose individual data types may differ from one another also. 8. char c. Manipulations of one member will not affect the values of any of the others in any way unless they are operated on in code to do so. 2) Members inside a structure are always stored in separate memory locations throughout the lifetime and scope of the entire structure. char name[20]. Where values need not be assigned to all the members at any time. Like structures union can be declared using the keyword union as follows: union item { int m.

member or union. This presents the programmer with extra responsibility when using a union to determine that the logic will properly store all values used in the union through a correct lifetime and scope. When a different number is assigned to a new value the new value supercedes the previous members value.In effect. 2) The union will store one and only one actual value for one element at a time. An example program is shown below.h> /* Declares union */ 113 . 8. 3) They both can have their size correctly determined as maximum size in bytes by use of the sizeof() operator.) operator to address a member from the object name. the first stored value is lost. each member have their own memory space Similarities between structure and union: 1) They both can accept the dot (.h> #include <stdlib. If another element is stored before the first is retrieved. one block is used by all the member of the union but in case of structure.a union creates a storage location that can be used by one of its members at a time. Important points about a union: 1) A union only allocates as much memory as its largest element (member) requires. union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members. as struct. I hope it helps you understand. #include <stdio. 2. 2) They both use brace delimited declarations to create the template for the data object. In union.4 More on Structures and Union The difference between structure and union: 1. Unions may be used in all the places where a structure is allowed. Both accept tagname and name as well as explicit initialization as options.member.(AMITY CENTER FOR e-LEARNING) The notation for accessing a union member that is nested inside a structure remains the same as for the nested structure.

}. union one U. float three.(AMITY CENTER FOR e-LEARNING) union one{ char one. /* Outputs object sizes to screen.three = 678. */ struct two S.two = 3645. U.32. /* Loads values into S and U.two = 3645. as below. int two. S. float three. }.\n\n". 114 . int main(void) { /* Uses tag names to create structure S and union U. as structure. sizeof(S)). */ S. U. printf("%d is the size of U.one = 'A'. int two. /* Declares structure */ struct two{ char one.\n".three = 678. sizeof(U)). S. as union. U.32.one = 'A'. */ printf("%d is the size of S.

which ensures that system designers can provide tailored I/O instead of being forced to change the language itself.As C has evolved.2 Reading from a stream using fgetc 9.4 Closing files 9.1 Introduction One of the reasons that has prevented many programming languages from becoming widely used for ‗real programming‘ is their poor support for I/O.3 Writing to a stream using fputc 9. has evolved with it and has proved to be both flexible and portable.The editor which you use to enter your program and save it.2. oddly enough.4. This package has now become part of the Standard We frequently use files for storing information which can be processed by our programs.1 Text streams 9.h header file 9.4. In order to store information permanently and retrieve it we need to use files Files are not only used for data.1 Introduction 9. 115 .2 Binary streams 9.(AMITY CENTER FOR e-LEARNING) Chapter 9 Data Files Contents: 9. by having no I/O at all! The C language approach has always been to do I/O using library functions.4.1 Opening named files 9.2.4. simply manipulate files for you. C has avoided this problem.3 Stdio. a subject which has never seemed to excite language designers. Our programs are also stored in files.2 File Types 9.4 Oprations on file 9. a library package known as the ‗Standard I/O Library‘ or stdio.

under the same implementation. portable. a common requirement is to translate the ‗\n‘ line-terminator into the sequence ‗\r\n‘ on output. an implementation-defined number of NUL characters may be appended to a binary stream.1 Text streams The Standard specifies what is meant by the term text stream. Some implementations may strip the leading space from lines consisting only of a space followed by a newline. no newline character is immediately preceded by space characters and the last character is a newline. such as the contents of structures or arrays in binary form. Binary streams A binary stream is a sequence of characters that can be used to record a program's internal data.It is guaranteed that. A line is a sequence of zero or more characters terminated by a newline character. within a program. are manipulated as text streams and binary streams once they have been opened for I/O. The stdio package does not permit operations on the contents of files ‗directly‘.2.2.2. and not. and do the reverse on input. Data read in from a binary stream will always compare equal to data written out earlier to the same stream. in general. but only by viewing them as streams. text files and binary files. Opening a text stream in update mode may result in a binary stream in some implementations.Writing on a text stream may cause some implementations to truncate the file at that point—any data beyond the last byte of the current write being discarded. or strip trailing spaces at the end of a line! An implementation must support text files with lines containing at least 254 characters. this is because on some implementations text files and binary files are the same. 9. Other translations may also be necessary. It is implementation defined whether the last line written to a text file must terminate with a newline character.(AMITY CENTER FOR e-LEARNING) 9.2 File Types There are two types of file. including the terminating newline.The contents of binary files are exceedingly machine specific. if the last character written to a text file is a newline. It is quite possible that the actual representation of lines in the external environment is different from this and there may be transformations of the data stream on the way in and out of the program. which essentially considers a file to contain lines of text. Data read in from a text stream is guaranteed to compare equal to the data that was earlier written out to the file if the data consists only of complete lines of printable characters and the control characters horizontal-tab and newline. 116 . 9. it will read back as the same. In some circumstances. which.

Read from the file. Every file you open has its own file pointer variable.e. stdin stdout stderr Predefined objects of type (FILE *) referring to the standard input. SEEK_CUR SEEK_END SEEK_SET Integral constant expressions used to control the actions of fseek. Write to the file. When you open a file you must also specify what you wish to do with it i. 9. These streams are automatically open when a program starts execution. This is accomplished by using a variable called a file pointer. but simply manipulate pointers to them. . Because you may use a number of different files in your program.3 The stdio. EOF A negative integral constant expression. that there is no more input. It is not safe to copy these objects within the program.e. you must specify when reading or writing which file you wish to use. then you must specify which file or files you wish to use. As you know.4 Operations on file The primary difference between manipulating files and doing terminal I/O is that we must specify in our programs which files we wish to use. An integral constant expression whose value is at least 256. Specifying the file you wish to use is referred to as opening the file. BUFSIZ The size of the buffer used by the setbuf function. When you wish to write to a file you specify the file by using its file pointer variable. sometimes their addresses may be ‗magic‘. fpos_t A type of object that can be used to record unique values of a stream's file position indicator. If you wish to use a file in your programs.h header file To provide support for streams of the various kinds.(AMITY CENTER FOR e-LEARNING) 9. The <stdio. Users of stdio never need to know the contents of these objects. or both. together with the following macro and type declarations: FILE The type of an object used to contain stream control information.You declare these file pointer variables as follows: 117 . indicating the end-of-file condition on a stream i.h> header file contains the various declarations necessary for the functions. output and error streams respectively. you can have many files on your disk. a number of functions and macros exist.

4. 9. Thus by checking the file pointer returned by fopen.You should note that a file pointer is simply a variable like an integer or character.g. you can determine if the file was opened correctly and take appropriate action e.g. fp = fopen( ―prog. fp2.c for reading and associates the file pointer fp with the file. The pathname argument is the name of the file to open. It checks if the file can be opened appropriately. *fp3.c‖. Note that if you only want to write data to a file. and so on. You may use any name you wish. When we wish to access this file for I/O. or truncate it to zero length (losing its previous contents) if it did exist The function fopen is one of the Standard Library functions and returns a file pointer which you use to refer to the file you have opened e. write mode for writing data. The variables fp1. It does not point to a file or the data in a file.(AMITY CENTER FOR e-LEARNING) FILE *fopen(). Constants such as FILE. fopen will create the file if it does not already exist. we use the file pointer variable fp to refer to it.1 Opening named files Named files are opened by a call to the fopen function.Files can be opened in a variety of modes.h> FILE *fopen(const char *pathname.h> contains declarations for the Standard I/O library and should always be included at the very beginning of C programs using files. it returns a NULL pointer. *fp1. your program will crash!! The fopen function was designed to cope with this eventuality. *fp2. EOF and NULL are defined in <stdio. If the file cannot be opened. ―r‖) .you need one file pointer for each file you intend to use. fp3 are file pointers. It is simply used to indicate which file your I/O operation refers to. The above statement opens a file called prog. or some program-specific filename.h>. If you attempt to read from an non-existent file. const char *mode). whose declaration is this: #include <stdio. 118 . such as read mode for reading data. A file number is used in the Basic language and a unit number is used in Fortran for the same purpose. The file <stdio. such as that returned from tmpnam. You can have up to about 20 files open in your program .

The function exit() is a special function which terminates your program immediately. /*Terminate program: Commit suicide !!*/ } The above code fragment show how a program might check if a file could be opened appropriately.Alternatively. exit(0) mean that you wish to indicate that your program terminated successfully whereas a nonzero value means that your program is terminating due to an error condition. } In this code fragment. 9.2 Reading from a stream using fgetc The fgetc function is used to read a character from a stream. fname ). exit(1) . you could prompt the user to enter the filename again. if ( fp == NULL) { printf(―Cannot open %s for reading \n‖. filename ). ―r‖) . 119 . and try to open it again: fp = fopen (fname. ―r‖) . we keep reading filenames from the user until a valid existing filename is entered. printf(―\n\nEnter filename :‖ ). gets( fname ). fp = fopen (fname.(AMITY CENTER FOR e-LEARNING) fp = fopen (filename.4. while ( fp == NULL) { printf(―Cannot open %s for reading \n‖. ―r‖) .

fgetc returns the next byte or character from the stream (depending on whether the file is "binary" or "text". *fp. c = getc( fp ) . takes no arguments. 120 .h>. (The specific type of error can be determined by calling ferror or feof with the file pointer. /* file. behaves in almost the same way as fgetc.) The standard macro getc.4.h> void main() { FILE *fopen().3 Writing to a stream using fputc The fputc function is used to write a character to a stream.fp) is similar to putchar(c). as discussed under fopen above). c = getc ( fp ).fp). If successful. while ( c != EOF ) { putchar( c ).The standard function getchar. If unsuccessful.h>. FILE *fp). int fputc(int c. int c . writes the character c into file referenced by fp. ―r‖ ). and is equivalent to getc(stdin).c‖. also defined in <stdio.(AMITY CENTER FOR e-LEARNING) int fgetc(FILE *fp). also defined in <stdio. except that — being a macro — it may evaluate its arguments more than once. 9. The routine getc(fp) is similar to getchar() and putc(c.c: Display contents of a file on screen */ #include <stdio. Thus the statement c = getc(fp). fp = fopen( ―prog. fgetc returns EOF. reads the next character from the file referenced by fp and the statement putc(c.

If a buffer had been automatically allocated for the stream. int c . } In this program. so getc returns EOF a special value to indicate that the end of file has been reached. Any unwritten data buffered for stream is flushed out and any unread data is thrown away. char filename[40] .c for reading.e. until the end of the file is reached 9. we open the file prog. We could reuse the file pointer fp by opening another file.Zero is returned on success. If the file is empty.(AMITY CENTER FOR e-LEARNING) } fclose( fp ). /* Prompt user for filename and display file on screen */ #include <stdio. we are at the end. *fp. 121 . #include <stdio. (Normally -1 is used for EOF) The while loop simply keeps reading characters from the file and displaying them. indicate that we are finished processing this file.h> void main() { FILE *fopen(). EOF if any error occurs The function fclose is used to close the file i. This file must exist for this program to work.4 Closing files An open file is closed using fclose. The file is then closed.4.h> int fclose(FILE *stream). it is freed. We then read a character from the file.

Example usage The following C program opens a binary file called myfile. c = getc( fp ) . to the fopen function.c‖ can be used so can a character array such as filename.h> #include <stdlib. and then closes the file. ―r‖). while ( c != EOF ) { putchar(c). anywhere a string constant such as ―prog.(AMITY CENTER FOR e-LEARNING) printf(―Enter file to be displayed: ―). fp = fopen( filename. In general.h> int main(void) { 122 . gets( filename ) . They do not check whether the files to be used exist or not. reads five bytes from it. } In this program. we pass the name of the file to be opened which is stored in the array called filename. (Note the reverse is not true). c = getc ( fp ). } fclose( fp ).The above programs suffer a major limitation. #include <stdio.

/* initialized to zeroes */ int i. buffer[0]. fclose(fp). buffer[i++] = rc) .\n".. stderr). } for (i = 0. buffer[3]. buffer[4]). rc. FILE *fp = fopen("myfile". return EXIT_SUCCESS. if (fp == NULL) { perror("Failed to open file \"myfile\""). } else fputs("There was an error reading the file.. if (i == 5) { puts("The bytes read were. return EXIT_FAILURE. "rb"). printf("%x %x %x %x %x\n". } 123 .(AMITY CENTER FOR e-LEARNING) char buffer[5] = {0}. (rc = getc(fp)) != EOF && i < 5. buffer[1]."). buffer[2].

Kernigham & Ritchie.W. B. E Balagurusamy . PHI 4. The C Programming Language. TMH 124 . Programming in ANSI C. Gottrified. BPB 3. Programming in C . Y Kanetkar . Let Us C. TMH 2.(AMITY CENTER FOR e-LEARNING) BIBLIOGRAPHY 1.