You are on page 1of 13
C Fundamentals fe chapter is concemed with the basic elements used to construct simple C statements, These elements le the C character set, identifiers and keywords, data types, constants, variables and arrays, declarations, expressions and statements. We will see how these basic clements can be combined to form more comprehensive program components, Some of this material is rather detailed and therefore somewhat difficult to absorb, particularly by an inexperienced programmer. Remember, however, that the purpose of this material is to introduce certain basic Concepts and to provide some necessary definitions for the topics that follow in the next few chapters. Therefore, when reading this material for the first time, you need only acquire a general familiarity with the individual topics. A more comprehensive understanding will come later, from repeated references to this material in subsequent chapters. 2.1 THE C CHARACTER SET C uses the uppercase letters A to Z, the lowercase letters a to 2, the digits 0 to 9, and certain special characters as building blocks to form basic program elements (e.g,, constants, variables, operators, expressions, etc.) The special characters are listed below. + . ee ee) i q . . 7 \ i < > ( ) rl 1 { ) i 5 : - (blank space) Most versions of the language also allow certain other characters, such as @ and $, to be included within strings and comments. C uses certain combinations of these characters, such as \b, \n and \t, to represent special conditions such as backspace, newline and horizontal tab, respectively. These character combinations are known as escape sequences, We will discuss escape sequences in Sec. 2.4. For now we simply mention that each escape sequence represents a single character, even though itis written as two or more characters. 2.2 IDENTIFIERS AND KEYWORDS “Identifiers are names that are given to various program elements, such as variables, functions and arrays. Identifiers consist of letters and digits, in any order, except that the i chores a (be a letter.’ Both Uupper- and lowercase letters are permitted, though common usage favors the use of lowercase letters for most iypes of identifiers. Upper- and lowercase letters are not interchangeable (ie., an uppercase letter is nor equivatent to the corresponding lowercase letter.) The underscore character ( _ ) can also be included, and is considered to be a letter. An underscore is often used in the middle of an identifier. An identifier may also begin with an underscore, though this is rarely done in practice, EXAMPLE 2.1 Fhe following names are valid identifiers. x yie sum_t _tenperature names area tax_rate TABLE 4 Scanned with CamScanner History of C ~-€ was originally developed in the 1970s by Dennis Ritchie at Bell Telephone Laboratories, Inc. (now a part of AT&T). It is an outgrowth of two earlier languages, called BCPL and B, which were also developed at Bell Laboratories. C was largely confined to use within Bell Laboratories until 1978, when Brian Kernighan and Ritchie published a definitive description of the language." The Kernighan and Ritchie description is commonly referred to as “K&R C." Following the publication of the K&R description, computer professionals, impressed with C's many desirable features, began to promote the use of the language. By the mid 1980s, the popularity of C had become widespread. Numerous C compilers and interpreters had been written for computers of all sizes, and many commercial application programs had been developed. Moreover, many commercial software products that were originally written in other languages were rewritten in C in order to take advantage of its efficiency and its portability. Early commercial implementations of C differed somewhat from Kernighan and Ritchie's original definition, resulting in minor incompatibilities between different implementations of the language. These differences diminished the portability that the language attempted to provide, Consequently, the American National Standards Institute’ (ANSI committee X3J11) has developed a standardized definition of the C language. Virtually all commercial C compilers and interpreters now adhere to the ANSI standard. Many also provide additional features of their own. In the early 1980s, another high-level programmi juage, called C++, was developed_by. Bjame Stroustrup"** at the Bell Laboratories. and henice"all’standard C features are available with C++. However, C++ is not merely an on of!" Rather, it incorporates several new fundamental concepts that form a basis for object-oriented programming—a new programming paradigm that is of interest to professional programmers. We will not describe C++ in this book, except to mention that a knowledge of C is an excellent starting point for learning C++. This book describes the features of C that are included in the ANSI standard and are supported by commercial C compilers and interpreters. The reader who has mastered this material should have no difficulty in customizing a C program to any particular implementation of the language. Structure of a C Program Every C program consists of one or more modules called functions. One of the functions must be called main. The program will always begin by executing the main function, which may access other functions. Any other function definitions must be defined separately, either ahead of or after main (more about this later, in Chaps. 7 and 8). rian W. Kernighan and Dennis M. Ritchie, The C Programming Language, Prentice-Hall, 1978. ANSI Standard %3.159-1989. American National Standards Institute, 1430 Broadway, New York, ‘ea Kémighan and Dennis M. Ritchie, The C Programming Language, 2d ed, Prentice-Hall, 1988.) ‘Stroustrup, Bjeme, The C++ Programming Language, 24 ed., Addison-Wesley, 1991 NY, 10018, (See also Brian W. Scanned with CamScanner Table 2-1 The ASCII Character Set WW ASCII ASCIT Tie Character Value Character Value Character (sc falue Character o NULL v32 (blank) 64 e = 1 sOH 3 1 1 aN 2 stx 34 3 - (68 8 98 : 4 e0T! 36 $ ea D To ‘ 5 endl 37 % a E 101 5 6 ACK 38 & 70 F aoe 5 7 sel 39 : n o a 3 8 85 | 40 ( 2 a 108 4 9 wry a ) 73 1 105 i 10 uF 42 a 74 J 106 i " vt 43 + 75 k 407 k 12 FF 44 , 76 L 108 L 13 on 45 - 7 M 109 a 14 so 46 . 78 N 110 a 15 st) a7 1 79 ° 11 ° 16 OLE 48 ° 80 P 2 P 17 vet 49 1 81 a 113 q 18 nce 50 2 82 R 114 r 19 ocd 51 3 83 8 118 s 20 pee 52 4 84 1 116 t at aK 53 5 85 u 117 a 22 sy) 54 6 86 v 118 v 23 ere | 55 7 87 Ww 119 w 24 can’ 56 8 88 x 120 x 25 eM 87 9 89 Y 421 y 26 suB 58 : 80 z 122 2 ar esc! 59 i a l 123, { 28 FS 60 < 92 \ 124 1 29 Gs | 61 . 93 1 125, } 30 a8 | 62 > 94 a 126 - a us | 63 2 95 - 127 DEL rhe first 32 characters and the last character are control characters, Usually. they are not displayed. However, some versions of C (some computers) support special graphics characters for these ASCII values. For example, 001 may] represent the character @, 002 may represent @, and so on. Escape Sequences eit onprinting characters) as'well ns the backslash (\) and the apostrophe (°), can be expressed in terms of escape sequences. An escape sequence always begins with a backward slash and is followed by one or no Mee eteates Far seamen ine feed (LF), which is refered to as @ newline in C can be represented 2s \n, ‘Such escape sequences always represent single character, even though they are written in terms of two or more characters, The commonly used escape sequences are ied below. Scanned with CamScanner ey, rt Mg Characise Escape Sequence ASCII Value 007 bell (alert) i os \b backspace horizontal tab \t ooo ne vertical tab Ww on newline (line feed) \n _ 01007 form feed Mt 012 aN carriage retum \r 013 ik \ quotation mark (") 034 \ ) Pe apostrophe (*) \' 039 . YY c ¥ \ question mark (7) \? 063 Ss é s backslash (\) ny 092 a ee null \o 000 KY x No? EXAMPLE 2.12 Shown below are several character constants, expressed in terms of escape sequences An Nt tb used t0 indicate the end of a string (: the character constant ‘0. An escape sequence can also ‘imal escape ¢ however, since the bit patter the character constant directly, character set, EXAMPLE 213 The leter A is represented by the decimal value equivalent to the octal value 101. (The equivalent binary can be expressed as the octal escape sequence "\101", Jn some versions of C, the letter A can also ‘quivalent of the decimal value 65 is 41. (The equi “A* can be expressed as "\x41', or as *\X41" I should be understood that the preferred way 10 represent this character cons Character constant isnot dependent upon its equivalent ASCII represenation 085 in the ASCH character set, This value is bit pattem is 001 000 001.) Hence the characte + constant ‘A’ be expressed as a hexadecimal escape sequence. ‘The hexadecimal valent binary bit pattem is 0100 0001.) Hence the character constant fant is simply A‘. In this form, the Escape sequences can only bi of octal or hexadecimal digits Unpredictable. Usually, however, written for certain special characters, such as those listed above, or in terms If a backslash is followed by any other character, the result may be will simply be ignored. String Constants A string constant consi ists Of any number of ‘Consecutive characters (including none), enclosed quotation marks, Scanned with CamScanner 33 EXAMPLE 2.14 Several sting constants are shovn below, = Washington, D.C, 20005" *270-ap-a4s6+ $19.95" 7 “THE CORRECT ANSWER Is: ST 5 : “Line 1\nlino 2\nline 9° + Note thatthe string constant "Line 1\nLine 2\nLine 3° extend Is over three lines. becaus that arc embedded! within the string, Thus, this string would be displayed as aan imeanaa Line 1 Line 2 Line 9 ‘Also, notice thatthe string ** is a null (empty) string, Sometimes certain special characters (e.g., a backslash or a quotation mark) must be included as a part of astring constant, These characters must be represented in terms oftheir escape sequences. Similarly, certain ronprinting characters (e.g. tab, newline) can be included in a string constant if they are represented in terms of their corresponding escape sequences. EXAMPLE 2.18 The following string constant includes three special characters that are represented by their corresponding escape sequences. "\tTo continue, press the \*RETURN\* key\n" ‘The special characters are \t (horizontal tb). \* (double quotation marks, which appears twice), and \a (newline) ‘The compiler automatically places @ null character (\0) atthe end of every string constant, as the lest charaurer within the string (before the closing double quotation mark). This character i not visible when the string is displayed. However, we can easily examine the individual characters within a string, and test to see se itor pot each character isa null character (we will see how this is done in Chap. 6). Thus, the end of very sting can be readily identified. This is very helpful if the string is scanned on @ character-by-character basis as is required in many applications, Also, in many situations this end-of-sting designation eliminates the need to specify a maximum string length. 15 actually contains 38 characters. This includes five blank EXAMPLE 2.16 The string constant shown in Example 2. ‘masks and newline) represented by escape sequences, and the spaces, four special characters (horizontal tab, two quotation hull character (\0) at the end of the string. Remember that a character constant (eg., ‘A') and the corresponding single-character string constant (-A*) are not equivalent. Also remember that a character constant has an equivalent integer value, whereas Single-character string constant does not have an equivalent integst value and, in fact, consists of two characters — the specified character followed by the nul character (\0). in the ASCII character set. It does not have a integer value of 119 i 13 — the lowercase letter W we actually consists of two character value, EXAMPLE 2.17 The character constant ‘w" hasan i null character at the end. In contrast, the string constant snd the null character \O. This constant does not have a coresponsing intB 2.5 VARIABLES AND ARRAYS A variable is an identifier that is used to represent some SP: e of information within a designated portion of the program. In its simplest form, a variable is an lentifier that is used to represent a single data item; ie., a numerical quantity or a character constant. The data item ust be assigned to the variable at some point in the program, The data item can then be accessed later in the program simply by referring to the ble name. Scanned with CamScanner ewe ee c (C FUNDAMENTALS {cHar.2 u ig be assigned different data items at various places_withi ‘A given variable can if ta.items at_various.places_within-the-program: rapeseed Byte variable ca change ding he execution ofthe programe niaoeas ope mad iated with the variable cannot change. ° EXAMPLE 2.18 lines. ‘AC program contains the followit int a, Di Se chara asst bes at bb ype declarations, which state that 2, b and ¢ are integer variables, and that @ is a char-type resale. Thus a, b and © will each represent an integer-valued quantity, and c will represent a single character. These ‘ype declarations wil aply throughout the program (rare about his in See. 2.6). ‘tre next four Lines cause the following things to happen: the integer quantity 3's assigned to a, 5 is assigned to B and he quantity represented by the sum a + b (i. 8) is assigned toc. The character ‘a" is then assigned to 4. ithe third line within this group, notice thatthe values of the variables @ and P are accessed simply by writing thr variables on the right-hand side of the equal sign. "The last four lines redefine the values assigned to the variables as follows: the integer quently 4 is assigned to ¢ replacing the earlier value, 3; then 2 is assigned to, replacing the ‘earlier value, 5; then the difference between a and {ies 2) is assigned to ¢, replacing the earlier value, 8. Finally, the character ‘wis assigned to 4, replacing the earlit character, ‘a “The array is another kind of variable that is used extensively in C. An array is an identifier, that refers to colecton of dt. tems tat al hae fi Ss Be items must all be of the same.type-(e.8..¢ integers al eharaterst6)” The individual data items are represented by their ‘corresponding array.elemer (ie., the THst data item is represented by the first array element, etc,), The individual array elements 2 distinguished from one another by the value that is assigned to a subscript. The first two lines are EXAMPLE 2.19 Suppose that xis a 10-element array. The first element is referred to as x10), the second as x1}, @ soon. The last element will be x{9] “The subserpt associated with each element is shown in square braces. Thus, the value ofthe subserip) for the £ clement is 0, the value of the subscript for the second element is 1, and so on. For an n-clement array, the subscri always range from 0 to n-1. pets several different ways to_categorize_amrays (©.8+ integer_arrays,character_arrays, © Saye iieoneu msionaLarrays). For now, we will confine our attention to only one type ayer imensional, char-type array (often called a one-dimensional character_atray). This type Tray i Generally used to represent a string. Each array ‘element will represent one character within the stri =e ene array can be thought of as an ordered list of characters. ; value ete may i one-dimensional, there ‘will be a single subscript (sometimes called an index) wh Same individual array elements. If the array contains n elements, the subscript will be an inte aaa Peeatast faites range from 0 to n-1._ Note that an n-character string will require an (n+1)-eler of the null character (\0) that is automatically placed at the end of the string, Scanned with CamScanner Hah 10 Suppose that the string Gal itornia” is to.be-stored-in-a.onesdimensional character array_called : % ey PLE 22 “ t 7 1 ff ir sae costo (characters, Lotter will be an II-element anay. ‘Thus, letter(O) will e aCe SOeTErTTT Wil TEETETEs, ad 30 on, as sumarizedBefow. Note hat the ls Ge, te TUN aay | eer 0 reese he lca whieh ies he end of the string. . Y rement Subscript Array Corresponding Data lem Number = Value Element (Sirine Character) Z i ° retter{0] c Fi 1 retter(t] a 5 2 retter(2] 1 i 3 retter(3} i « 3 4 retter(4} + 6 5 aetter{5} ° t 7 6 aetteri6} r 8 7 aetteri7] a ° 8 aetter(2] 4 : 10 9 letter[9} a : " 10 aetter[10] \0 a this st we ean determine, for example tha the Sth aray element, exter {4}, represents the eter, and so on From snts and their contents are shown schemati ‘The array elemer Cte reese eeeeeneeeteeeeeeeae Subscript 0 An H-element character array 2 We will discuss arrays in much greater detail in Chaps. 9 and 10. AA geeabales 2.6 DECLARATIONS All variables must be declared before ables mus be deci A declaration associat they Garrappesr-in-executable statement “A declaration consists of a data type, foll (Recall that the permissible data types are dis¢ pair of square brackets, containing a positive in the array. lowed by one or more variable names, ending with a semicolon. cussed in Sec. 2.3.) Each array variable must be followed by @ teger which specifies the size (i.e., the number of elements) of EXAMPLE 221 A C program contains the following type declarations. int a, b, ¢; float roott, root2; char flag, text[{80); Mera i ¢ are declared to be integer variables, root and root? are floati iable and text isan 80-clement, char-type aay. Note the square brackets enclos sing the size specification for text Scanned with CamScanner size of the array is explicitly specified, In such situations i Oem such situations itis important, however, thatthe size be spec size be specified char text{10] = "Californias; ters at the end of the string (in this ease, th pecs? the null character) will be lost. Ifthe size is st. Ifthe size is too large, eg., ghar text[(20]-= “Californias; incest aray elements may be essgnedzers, or they may be filled wit meaningless characters, ‘Amray declarations that include the assignment of init . ama (ee Chap. 9) ignment of initial values can only appear in certain places within a in Chap. 8 we shall see that variables can be categor ; fegorized by storage class as well as by data type. The storage class eee ponies of the program within which the variables are recognized. Moreover, the storage class associated with an array determines whether or not the array can be initialized. This is explained in Chap. 9. 2.7 EXPRESSIONS ents-a-single-data item,-such as a number.or.acharacter. The expressi singlé entity, such as a constant, a variable, an array element or a reference to a function-—Itmay also consist of some combinaifon’ oF such entities, interconnected by one or more “operators. The use of éxpressions involving operators is particularly common in C, as in most other programming languages. Expressions can also represent logical conditions that are either tue o false. However, in C the conditions true and false are represented by the integer values 1 and 0, respectively. Hence logical-type expressions really represent numerical quantities. An expressio? EXAMPLE 2.27. Several simple expressions are shown below. atb xey c=atd xey xary ati (4). This expression represents the sum of the values ‘The first expression involves use of the addition operdior assigned tothe variables a and b. The second expression inv represented by y fo be assigned to x. We ha Examples 1.6 through 1.13, 2.25 and 2.26). C includes °° In the third line, the value of the expression (@ * ) features of the first two expressions (addition and assignment). “The fourth expression wil ave the value + auc) see wae oF (16S than or equal to the value of y. Otherwise, the expression will have the valu 0 (false). In this expression, <= 52 relational operator tat compares the values ofthe variables x and y, ' The {ith expression isa est for equality (compare wih the second exPrESAN ee ae ee Thus. the expression will have the value 4 (true) i jo is equa tthe value of. Otherwise, he expression Wil have the value 0 (false) rolves the assignment ope In this case, the expression causes the value cealeeady encountered the use of tis operator in several earlier examples (see ‘onal assignment operators, as discussed in Sec. 3.4. eral a ) is assigned tothe variable c. Note that this combines the zrator (=). ifthe valu Scanned with CamScanner C FUNDAMENTALS 39 jst expression entises the value of the variable 3. as rem ‘Thus, the expression is variable 4 to be increased by y 1 (ie, incremented). Thus, hel spe opertor +4, which indieatesInerementng, i eda unary oper i ane apple). Cinchdes seve ther operas of his Fe, ny oer bras thas only one operand (in this ease, ‘The C Janguage includes many different kit it nt kind: il i 2.8 STATEMENTS wee different classes of statements in C- ‘A statement causes the computer to carry outsome action. There are Tey SE Oe cement compound statements and control statements. expressi “Statement _consists_of_an-expression-followed-by- ‘a-semicolon. ‘The execution of an expression statement causes the expression to be evaluated ewerd EXAMPLE 2.28 Several expression statements are shown below Sle 4 wel (one? a3 nae l ) czar «| on ey. fieasen Loar tis printt(rArea = Sf", area); i Nutl As auses the value of the expression on the right jatements. Each c is an incrementing-tyPe “The first two expression statements ae assignment st of the equat sign to be assigned to the sariable on the left. The third expression statement Statement, which causes the value of 3 to inerette byl “The fourth expression statement causes the printf function to be eval that writes information out of the comPuct (more about this in Sec. 3.6) displayed, followed by the current value ofthe variable area. Thus, ifarea represents the vah generate the message uated, This is a standard C library function In this case, the message Area = will be jue 100., the statement will area = 100. ‘colon. It is simply a mechanism for statement is required. Consequently, itis called @ nt does nothing, since it consists of only @ sem! The last expression statemet tement in places where this type 0 providing an empty expression st ull statement. ual statements enclosed within 2 pair of braces t—} ‘A compound statement consists of seve eson sta ‘The individual -statements-may themse! Ives be expression ‘tatements, compound statements or control tement_provides_a_capal ty_for-embedding-statements-withiin other statements. Thus, the compound. sta statemenfs. Unlike an ‘expression statement, a compound s nt does not end with a semicolon. EXAMPLE 2.29 A typical compound statement is shown below. pi = 3.141593; _ circumference = 2- area = pi * radius * radius: + pi + radius; Scanned with CamScanner AL (FUNDAMENTALS [cHap. 2 “0 pression statements, though itis considered & sis of three assignment-tyPe © rand statement does not end with a semicolon pris particular compound statement consi it appears. Note that the comPo! gle entity within the program in which after the brace. Control statements are used to create spec! il program. features, such as logical tests, loops . and branches. Many control statements require that otfier statements be ‘embedded within them,-2s illustrated in the following, example EXAMPLE 2.30 The following control statement creates «conditional loop in which several actions are executed repeatedly, until some particular condition is saisied while (count <= 9) ( printt(*x =") scant("f", 8x); sun += X} +4count; ) ns four expression statements. The ‘compound which in tum contal ceed the value of n. Note that count ‘This statement contains a compound statement, statement will continue to be exceuted as OnE 1 the value of count does not ¢ sverenses in value during each passthrough the looP. Chapter 6 presents a detailed discussion of control statements. 2.9 SYMBOLIC CONSTANTS mame that substtutes-for.a-sequence of charaeie: ‘The characters may represent a Thus, a symbolic constant allows a name t0 PPS A sgymbolic.constant is @ 2 ; Azavie constant, a character constant OF 2 string constant = When'a program is compiled, each occurrence in place of a numeric constant, @ charcee’ constant aewrsymmbanie constant is replaced by its ‘corresponding character sequence. ‘Symbolic constants are usually ya wed atthe beginning of a program. The symbo) jc constants may.then appear later in place-of-the=n ring eccharecter-constants, etc. that the symbolic constants represent. ‘A symbolic constant js defined by writing adefine ane text where name represents a symbolic name, typically written in uppercase letters, and text represents the seaqtence of Characrers"Wat 15 assorted pine symbolic name. Note that text does not end with 2 seavrolon, since a symbolic constant definition is not a the C statement, Moreover, if text were to end with we would be treated as though it were a part of the numeric constant, character ‘a semicolon, this semicol Constant or string constant that is substituted for the symbolic name. EXAMPLE 2.31 A C program contsins the following symbolic constant definitions. fdefine TAXRATE 0.23 foetine PL/3.141599 psetine ROE 1 pootine FALSE 0 rae Hdetine FRIEND +96 Scanned with CamScanner A header file is a file with extension .h which contzins © function cemerstons— arid macro definitions to be shared between several-source Ses. Thee Se 5° typés of header files: the files that the procremmer writes = comeswitryeurcempin You request to use a header file in your progrem by including = with the © pI sing directive #include, like you have seen inclusion of stdio.h heacer file, which comes along with your compiler. ‘h comes along with your co Including @ header file is equal to copying the content of the header file Sut we do not do it because it will be error-prone and it is not 2 good ides to copy Se content of a header file in the source files, especially if we have mutt files in a program. ‘A simple practice in C or C++ programs is that we keep all the const macros, system wide global variables, and function prototypes in the header Ties and include that header file wherever it is required. Include Syntax Both the user and the system header files are included using the preprocessing directive #include. It has the following two forms: #tinclude filer This form is used for system header files. It searches for a file named “file’ in standard list of system directories. You can prepend directories to this list with the -I option while compiling your source code. include “Ee” This form is used for header files of your own program. It searches for a file named ‘file’ in the directory containing the current file. You cen prepend directories to this list with the -I option while compiling your source code. Include Operation The #include directive works by-directing the C_preprocessor_to_scen_tne specified file _as_in, fore continuing with the rest of the cur The output from the preprocessor contains the output already generated, followed by the output resulting from the included file, followed by the output that comes from the text after the #include directive. For exemple, if you have a header file header.h as follows: Scanned with CamScanner C Programming char “test (void); and a main program called program.c that uses the header file, like this: int x; #include “header.h" int main (void) { puts (test ()); } the compiler will see the same token stream as it would if program.c read. int x; char *test (void); int main (void) { puts (test ()); Once-Only Headers Tf a header file happens to be included twice, the compiler will process its contents twice and it will result in an error. The standard way to prevent this is to enclose the entire real contents of the file in a conditional, like this: #ifndef HEADER_FILE #define HEADER_FILE the entire header file file endif This construct is commonly known as a wrapper #ifndef. When the header is included again, the conditional will be false, because HEADER_FILE is defined. The preprocessor will skip over the entire contents of the file, and the compiler will not see it twice. Scanned with CamScanner Sg pyri | Neo h — elandard fa Sh eatbr et eo) gens gen sv) co cheat) pr kia £69 total Sbniy 50") host) tun ‘ M) 0) ponte wr ©) emo th nu a at v oped Tria tee real — “1h ey Chon — Pc Keb Od Clewi — iy : 4? 19° Scanned with CamScanner peng’? d",soxglet);

You might also like