Unit 2 Introduction To C Language
Unit 2 Introduction To C Language
NOTES
2.5 Tokens
2.5.1 2.5.2 2.5.3 2.5.4 The C Character Set Identifiers Keywords Data Types
2.6 Variables
2.6.1 Size of Variables
2.7 Constants
2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 Integer Constants Character Constants Floating Point or Real Numbers Enumeration Constant String Constants Symbolic Constants
Expressions and Statements Summary Key Terms Answers to Check Your Progress Questions and Exercises Further Reading
2.0 INTRODUCTION
In the previous unit, you were introduced to programming. In this unit, you will get indepth understanding of the C language. Dennis Ritchie developed the C language in 1972 under the influence of BCPL and B. The language is, however, rich in data types unlike the other two languages. It is universal. It can be developed and executed in any platform that has a development environment for C. It can run under UNIX, LINUX and even in parallel computers. In this unit, you will also gain knowledge of tokens. There are six classes of tokens in the C programming language, namely keyword, identifier, constant, string literal, operator and punctuators. The unit also discusses arrays, which is
Self-Instructional Material
25
Introduction to C Language
another form of data type. There can be arrays of integers, arrays of characters and arrays of floating-point numbers. An array means a collection of a number of integers or floats or items of the same data type. An array contains data of the same type.
NOTES
Introduction to C Language
NOTES
Design algorithm
Save file
Compile Yes Errors No Object code of the relevant C library functions Get object code
eg. <stdio.h>
Operating systemrelated code Other object files Linker
Execute run or
Check result
END
27
Introduction to C Language
The various steps involved in program development as given in Figure 2.1 are briefly discussed below.
If you are using Turbo C++, you select compile and click. After the compilation process is over, there are two possible outcomes: There are errors in the source code. There are no errors. If there are no errors, you get an object code file with the same filename with .o extension automatically. In case of the above-mentioned example, you will get Ex 2.1.c. If there are errors, the compiler will give error messages. The error messages will give the line numbers in the program along with the type of error. The programmer should note the line numbers and the error messages and then edit the source file. The statements in the line numbers should be checked for errors. Therefore, the programmer should carefully examine the program and correct all the errors and recompile. When there are no errors, the compiler will automatically generate the object code and save it with a .o extension with the file name same as that of the corresponding source code. The object code is in the machine language. However, it is not yet ready for execution. If your program contains more than one source code file, all the source files are to be compiled separately to get the corresponding object code. Assume that you have another source file called px.c. You will get px.o when the compilation is successful.
28 Self-Instructional Material
Introduction to C Language
NOTES
2.3.3 Compilation
The compiler in the UNIX system is called cc. You can compile the above program by typing the following:
ccEx 2.1.C
If there are errors, the program will not compile. The errors will be listed. The errors are to be corrected. On clean compilation, the system generates an executable file called a.out.
2.3.4 Execution
You execute the above-mentioned file a.out and the result will appear on the screen. Whenever a file is compiled as mentioned above, and if there are no errors, the system will produce an executable file with the name a.out. Therefore, the old executable files will be erased. If you want to retain all the executable codes for future use, you have to rename it. From the above discussions, it will be clear that the object file is not available. On clean compilation, the object code produced is converted into an executable code and placed in a.out. Hence, the object file is not available. Suppose, you want to retain the executable code in Ex 2.1.c, you can compile the program as follows:
cc2.1.C
29
Introduction to C Language
Thus, you have to understand the applicable commands for editing, compiling, linking and executing the program in a computer system. Once it is learnt, it will hold well forever in the system.
NOTES
Create a new file in the text editor. Then type in the program exactly as given above. Save this program as Example 2.1. Next, compile the program. The compiler after compilation will give a message. Look at the message box. If the C program was compiled in a C++ compiler, there may be warnings, but you can safely ignore them. However, the errors, if any, should not be ignored. The compiler will give the errors and line numbers. Sometimes, even an experienced programmer will find it difficult to understand the error messages. If you encounter the same difficulty in understanding the error messages, you need not worry. Open the program file again and check whether you have typed it exactly as in the book including the semicolons, quotation marks and brackets and the program is a working program. Keep checking and compiling till the compiler says success, meaning there is no error in the program after compilation. Now, you have to link the object code to get the executable code. When there are no errors, even after linking, you have to execute or run the program. Use the right command for run. On execution, you will get the result as follows: Result of the program
Om Vinayaga
You have succeeded in establishing a communication link with the computer. You can now talk to it regularly by learning new commands and using better communication methods. You have now become familiar with the methodology for learning a programming language. The steps involved are summarized as follows: Type the program in a text editor. Save and give a name to the program. Compile the program. Look for errors and correct them. Link the object code to produce an executable code. Execute/Run the program and analyse the result. The methodology for the preparation of the source code, compilation, linking and execution may vary from system to system. Learn the correct operations of the IDE/system being used for the various steps mentioned above.
30 Self-Instructional Material
Introduction to C Language
NOTES
Thus, both the above programs have carried out the task successfully. However, in the rest of the book, the style given in the previous program will be used. The reader may adopt the style that works in his compiler.
2.5 TOKENS
The Standard defines six classes of tokens in the C programming language as follows: Keyword Identifier Constant String literal Operator Punctuators
Tokens are similar to the atomic elements or building blocks of a program. A C program is constructed using tokens. There are certain other building blocks of a program that do not form part of any of the above. They are as follows: Blanks Horizontal tabs Vertical tabs New line characters Form feed Comments
Self-Instructional Material
31
Introduction to C Language
NOTES
The digits and alphabets are organized sequentially and hence, it is easy to get the ASCII value; for instance, the ASCII value of D is 68, E is 69, 8 is 56, x is 120 and so on. The ASCII table is given in Annexure 1.
2.5.2 Identifiers
Any name is an identifier. Just as the name of a person, street or city helps in the identification of a person or a street or a city, the identifier in the C language assigns names to files, functions, constants, variables, etc. An identifier in the C language is defined as a sequence of alphanumeric characters, i.e., alphabets or digits. The first character of an identifier has to be an alphabet. In the C language, lowercase alphabets and uppercase alphabets are considered to be different. For instance, VAR and var represent different names in the C language. VALID IDENTIFIERS
C1 PROC1 P34 VAR_1 EX1 a bc Ual1 Aa
INVALID IDENTIFIERS
1PROGA 4.3 A-B
32 Self-Instructional Material
Any function name is also an identifier. For instance, printf is the name of the function available with the C language system. The function helps in printing. Therefore, identifiers can be constructed with alphabets (A...Z), (a...z), (0...9). In addition, underscore can also be used in identifiers. Unless otherwise specified, small letters are usually used for identifiers.
Introduction to C Language
NOTES
2.5.3 Keywords
These are also known as reserved words in C. In the first program, int is a reserved word or keyword. They have a specific meaning to the compiler. They should be used for giving specific instructions to the computer. These words cannot be used for any other purpose such as naming a variable. C is a very concise language containing only 32 reserved words and this is one of its strengths. Common statements, such as print, input, etc., are implemented through library functions in C, giving relief to programmers and reducing the size of code as compared to other programming languages. This makes the task of programming simple. Now take a look at the keywords given in Table 2.2. You will use most of them in the book. Their meaning will become clear as you read the book.
Table 2.2 C Keywords auto const double float int short struct unsigned break continue else for long signed switch void case default enum goto register sizeof typedef volatile char do extern if return static union while
33
Introduction to C Language
NOTES
You have to understand how a computer works. Assume that two numbers a and b are to be multiplied. First of all, the two numbers have to be stored in the memory. Then the required calculation has to be performed. The result has also to be stored in the memory. Each number is of a specific data type; for instance, all three of them can be declared to be integers. Each data type occupies a specific size in the memory. What does one mean by size? It is the amount of storage space required; each bit needs one storage space. One byte needs eight storage spaces. If a number is of type integer declared as int, it is stored in 2 bytes. The number depending on its type gets stored in different forms. If a number is of float type, it takes 4 bytes to store it. All characters can be represented according to the ASCII table and hence, 1 byte, i.e., 8 bits are good enough to store a character, which is represented as char. These sizes may vary from computer to computer. The header files <limits.h> and <float.h> contain information about the sizes of the data types. Real numbers can be expressed with single precision or double precision. Double precision means that real numbers can be expressed more precisely. Double precision also means more digits in mantissa. The type float means single precision and double means a double precision real number. Table 2.3 indicates the size of various data types.
Table 2.3 Size of Data Types Data Type char int float double Size 1 byte 2 bytes 4 bytes 8 bytes
Now, let us look at the maximum and minimum magnitudes for the integer data type. They are: INT INT MAX + MIN 32767 32767 maximum value of int minimum value of int
34 Self-Instructional Material
Integer means signed integer. The data type integer occupies 2 bytes or 16 bits. The most significant bit is reserved for sign. It will be 0 for a positive number and 1 for a negative number. Therefore, you can easily calculate how the limits for the integer data types have been arrived at.
The standard has also provided for another type of integer called short int with the same maximum and minimum values.
Introduction to C Language
2.6 VARIABLES
The names of variables and constants are identifiers. The names are made up of alphabets, digits and underscore, but the first character of an identifier must be an alphabet. C allows up to 31 characters for the identifier (names) and therefore, the naming of the variables should be carried out in an easily understandable manner. For example, in the program for the calculation of, Simple interest I = pnr/100, You can declare them with actual names, p = principal, r = rate_of_interest, n = number_of_ years Naturally, programmers may not like typing long names for fear of making mistakes elsewhere in the program apart from being reluctant to increase their typing workload. Meaningful names can, however, be assigned to data types even with few letters. For example,
p = princ; r = intrate; n = years
NOTES
Some compilers may allow names with upto 31 (thirty-one) characters, but may consider the first eight characters for all purposes. Hence, a programmer could coin shorter yet meaningful names, instead of using single alphabets for variable names. One should, however, be careful not to use the reserved words, such as 32 keywords, for variable names as they have a specific meaning to the compiler. If they are used as variable names, then the compiler will get confused. Be careful not to use the reserved words as identifiers. A program to find out the square of integer 5 is given below.
/*Example 2.3*/ /*program to find square of 5*/ #include <stdio.h> int main() { printf(square of %d= %d, 5, 5*5); }
You have now achieved the objective of finding the square of 5. Later on, you may want to find out the square of another number, say 7, for example. You would have to write the same program again replacing 5 by 7 and then compile and run it. This would waste a lot of time. To save time, you can, therefore, write a general-purpose program as follows:
/*Example 2.4*/ /*program to find square of any given number*/ #include <stdio.h>
1. Who developed the C language and when? 2. What are tokens? How many classes of tokens are there? 3. What is an identifier? 4. What do you mean by keywords?
Self-Instructional Material
35
Introduction to C Language
NOTES
printf(Enter the integer whose square is to be found\n); scanf(%d, &num); printf(square of %d= %d, num, num*num); }
Here you define num as an integer variable. When & precedes num, it indicates the memory address of num. At the first printf, the message appears as it is and the cursor goes to the next line because of the new line character \n at the end of the message, but before the closing quotation mark. The next statement is new to you. It is used to receive an integer typed on the console. You can type in any integer number, and the number typed will be stored in the memory at the memory location named num. The purpose of the statement is, therefore, to get the integer (because of the appearance of %d within quotes) and it is stored at the memory address num. The next statement prints the number typed and its square. When you execute the program, the following message appears on the monitor: Enter the integer whose square is to be found. Since you want to find out the square of 25, type:
25
The next time you may want to find out the square of another number, say 121. Then simply run the program and when prompted, type 121 to get the answer. Here, the number whose square has to be found out has been declared as a variable. The variable has a name and is stored at the location pointed to by the variable name. Therefore, the execution of the program for finding out the square of any number is easy with the above modification as in Example 2.4. Variables and constants are fundamental data types. A variable can be assigned only one value at a time, but can change value during program execution. A constant, as the name indicates, cannot be assigned a different value during program execution. For example, PI, if declared as a constant, cannot have its value varied in a given program. If PI has been declared as a constant = 3.14, it cannot be reassigned any other value in the program. Programs may declare a number of constants. Variables are similarly useful for any programming language. If PI has been declared as a variable, then it can be changed in the program to any value. This is one difference between a variable and a constant. Whether an identifier is constant or variable depends on how it is declared. Both variables and constants belong to one of the data types like int, float, etc. The convention in C is to indicate the names of constants by the upper case letters.
PI SIGMA
36 Self-Instructional Material
Variable names are, on the other hand, indicated by the lower case letters:
int a float xy
Introduction to C Language
NOTES
or
sizeof (<expression>)
Therefore, it is obvious that a long double occupies 10 bytes and stores long floatingpoint numbers with double precision. Note that the size of short int will be either equal to or less than the size of an integer variable.
Self-Instructional Material
37
Introduction to C Language
Variables, which require more than 1 byte for storage, will be stored consecutively in the memory.
NOTES
2.7 CONSTANTS
The following are the types of constants: Integer constant Character constant Float constant Enumeration constant String constant Symbolic constant All these types will now be explained:
Unless otherwise specified, integers or long integers will be signed, i.e., the first bit will be reserved for the sign. The long int obviously uses 4 bytes or 32 bits. The magnitudes of long can also be doubled by using an unsigned long integer denoted as unsigned long. However, integers are not suitable for very low values and very large values. This can be overcome by floating point or real numbers. An integer constant may be suffixed by the letter u or U to specify that it is an unsigned integer. Similarly, if the integer is suffixed with l or L, it signifies a long integer. If you specify unsigned long integer you suffix the constant with ul or UL.
38 Self-Instructional Material
The following are the examples of valid and invalid integers: Valid integers
+345 345 345 729u 729U 112345L 112345UL +112345l 112345l positive /* integer */ /* integer */ /* integer */ /* unsigned integer */ /* unsigned integer */ /* Long integer */ /* Unsigned Long integer */ /* Long integer */ /* Long integer - if no sign precedes, it is a number */ /* /* /* /* /* /* /* decimal point not allowed */ no comma allowed */ = blank not allowed */ exceeds the maximum */ unsigned cannot have + */ ( not allowed */ illegal characters */
Introduction to C Language
NOTES
Invalid integers
345.0 112, 345L 112 345UL 112890345L +112 345UL (345l 345s
You have so far considered only decimal numbers. C, however, entertains other types of numbers as well. The octal numbers will be preceded by 0 (zero). The following are the examples of valid and invalid octal numbers: Valid octal number
0346 0547 0120
The C language also supports hexadecimal numbers. Here, since the base is 16, we use alphabets also in the numbers as given in Table 2.4.
Table 2.4
a b c d e f
or or or or or or
A B C D E F
10 11 12 13 14 15
39
Introduction to C Language
The following are the examples of valid and invalid hexadecimal numbers: Valid hexadecimal numbers
0x345 0xA132 0x100 0x20B
Invalid hexadecimal numbers
NOTES
A character constant represents its integer value as defined in the character set of the machine. Therefore, you can add 2 characters. For example, the ASCII values of digit 1 = 49 and C = 67. When you add these values you get code 116 whose equivalent character is t. Now, verify this with the following example:
/* Example 2.6 demonstrates that chars can be treated like integers*/ #include <stdio.h> int main() { const char ALPHA1=1'; char alpha2=C; char alpha3; alpha3=ALPHA1+alpha2;
40 Self-Instructional Material
putchar(alpha3); }
Introduction to C Language
NOTES
Therefore, characters can be treated like integers as well, although they are declared as character variables and constants. Since characters are of type int, you could add them. Characters can also be defined as integers as in the following example:
/* Example 2.7 Demonstrates that a char can also be declared as int*/ #include <stdio.h> int main() { int x; x=1'+C; printf(x as integer=%d\n, x);/*x printed as integer*/ printf(x as character=%c\n, x);/*x printed as character*/ }
Invalid floats
+144 /* no decimal point */ 1,44.0 /* comma not allowed */
Self-Instructional Material
41
Introduction to C Language
Scientific notation: Floating-point numbers can also be expressed in scientific notation. For example, 3.0 E2 is a floating-point number. The value of the number will be equal to 3.0 102 = 300.0 Instead of the upper case E, the lower case e can be used as in
0.453 e + 05, which will be equal to 0.453 105 = 45300
NOTES
There are two parts in the scientific notation of a real number, which are as follows: Mantissa (before E) Exponent (after E) In the scientific form, the following rules are to be observed: The mantissa part can be positive or negative. The exponent must have at least one digit, which can be a positive or negative integer. Remember the exponent cannot be a floating-point number. type float is a single precision number occupying a storage space of 4 bytes. type double represents floating-point numbers of double precision and hence occupies 8 bytes. If you look at the file <float.h> you will find the maximum and minimum floating-point numbers as shown: FLT MAX FLT MIN F or f no suffix L or l 1E 1E + 37 37 float double long double maximum floating point number minimum floating point number
If an integer is suffixed with L or l, then it is a long integer. If a float is suffixed with L or l, then it is a long double floating-point number. Examples Valid floating-point constants
1.0 e 5 123.0 f /* float 11123.05 /* double 23467.34 e 5 l /* long double */ */ */ */ */ */
Introduction to C Language
The values of constants cannot be altered in programs. They can be defined as follows:
const int PRINC = 1000; const float INT_RATE = 0.12 f;
NOTES
The values of PRINC and INT_RATE cannot be changed in the program even by introducing another assignment statement when they are declared as constants using const. The following example verifies this statement:
/*Example 2.8 Demonstrates that constants cannot be changed even with assignment statements. To verify, include statements 7, 8 & 9 in the program by removing the comment specifiers at the beginning of the program statement 7 and the end of statement 9*/ #include<stdio.h> main() { const int PRINC =1000; const float INTST=0.12f; printf(PRINCIPAL=%d INTEREST=%f\n, PRINC, INTST); /*PRINC =2000; INTST=0.24f; printf(PRINCIPAL=%d INTEREST=%f\n, PRINC, INTST);*/ }
Key in the example and execute the program. After successful compilation, you will get the result as given below. Result of the program
PRINCIPAL = 1000; INTEREST = 0.1200
Now include the second part of the program by removing /* and */ at statements 7 and 9, respectively. Earlier this was treated as a comment. Now this part will get included in the program. Now compile it. You will get a compilation error. This is due to your attempt to redefine the constants PRINC and INTST, which is not allowed. Incidentally, the technique of including or excluding a program segment at will using /* and */ is a convenient method for program development.
43
Introduction to C Language
NOTES
Note that there are no semicolons, but only a comma after the members except the last member. There is not even a comma after the last member. There is no conflict between both guardians. The top one is enum and the bottom one is a member of enum guardian. See the similarity between structure, union and enum. The enum variables can be declared as follows:
enum guardian emp1, emp2, emp3;
This is similar to structures and unions. The first part is the declaration of the data type. The second part is the declaration of the variable. The initial values can be assigned in a simpler manner as given below:
emp1 = husband; emp2 = guardian; emp3 = father;
You have to assign only those declared as part of the enum declaration. Assigning constants not declared will cause error. The compiler treats the enumerators given within the braces as constants. The first enumerator father will be treated as 0, the husband as 1 and the guardian as 2. Therefore, it is strictly as per the natural order starting from 0. The enumerated data type is never used alone. It is used in conjunction with other data types. You can write a program using enum and struct. It is shown as follows:
/*Example 2.9 enum within structure*/ #include <stdio.h> main() { enum guardian { father, husband, relative }; struct employee { char *name; float basic; char *birthdate; enum guardian guard; }emp[2]; int i; emp[0].name=RAM; emp[0].basic= 20000.00; emp[0].birthdate= 19/11/1948; emp[0].guard= father; emp[1].name=SITA; emp[1].basic= 12000.00; emp[1].birthdate= 19/11/1958;
44 Self-Instructional Material
emp[1].guard=husband; for(i=0;i<2;i++) { if( emp[i].basic ==12000) { printf(Name:%s\nbirthdate:%s\nguardian: %d\n, emp[i].name, emp[i]. birthdate, emp[i].guard); } } }
Introduction to C Language
NOTES
The program clearly assigns the relationships between the employee and the guardian. enum guardian is the data type, and guard is a variable of this type. However, when you are printing emp [i]. guard, you are printing an integer. Hence 0, 1 or 2 will only be printed for the status, and this is a limitation. This can be overcome by modifying the program. The program modified with a switch statement is given below:
/*Example 2.10 expanding enum*/ #include <stdio.h> main() { enum guardian { father, husband, relative }; struct employee { char *name; float basic; char *birthdate; enum guardian guard; }emp[2]; int i; emp[0].name=RAM; emp[0].basic= 20000.00; emp[0].birthdate= 19/11/1948; emp[0].guard= father; emp[1].name=SITA; emp[1].basic= 12000.00; emp[1].birthdate= 19/11/1958; emp[1].guard=husband;
Self-Instructional Material
45
Introduction to C Language
NOTES
for(i=0;i<2;i++) { if( emp[i].basic ==12000) {printf(Name:%s\nbirthdate:%s\nguardian:, emp[i].name, emp[i]. birthdate); switch(emp[i].guard) { case 0:printf(father\n); break; case 1:printf(husband\n); break; case 2:printf(relative\n); break; } } } }
Even though the conversion of an integer to actual name is additional work, enum is a useful construct since it improves readability in addition to the number of other advantages. For example, you can define boolean as follows:
enum boolean false, true }; {
Here false will contain an integer value 0 and true 1. This can be used to assign values for found in our sorting programs. You can define found as follows after declaring boolean:
enum boolean found;
You have allowed the system to assign integer values to the members of enum, but we can also assign specific values to the various members of enum. When values are assigned, then it takes precedence over what the system assigns. You can define boolean as:
enum boolean { yes = 1, no = 0 };
This is similar to defining using #define. The #define equivalent for this will be:
#define yes 1 #define no 0
46 Self-Instructional Material
Although #define and enum provide a way to associate symbolic names such as boolean with constants, there are differences between them. The differences are: (i) enum can generate values itself unlike #define where you have to specify the replacement constant. (ii) The compilers need not check the validity of what is stored in the enum variable, but the #define replacement constant will be checked for valildity. (iii) It is possible to print out the values of enum variables in symbolic form, but this is not possible with # define. Anyway, either of them can be used depending on the context.
Introduction to C Language
NOTES
You may be surprised about the third string constant, which has no characters. This is called a NULL or empty string and is allowed in C. The string constant can contain blanks, special characters, quotation marks, etc. within the string. In order to distinguish the end of a string constant, the compiler places a null character \0 (back slash followed by zero) at the end of each string before the quotation mark. The null character is not visible when the string appears on the screen. The programmer does not include the null character either. It is the compiler which automatically inserts the null character at the end of every string. Invalid string:
Yoga /* should be enclosed in double quotes */
Which defines INITIAL as 1. The INITIAL type of definition is called symbolic constants. They are not variables and hence, they are not defined as part of the declarations of variables. They are specified on top of the program before the main function. The symbolic constants are to be written in capital or upper case letters. Wherever the symbolic constant names appear in the program, the compiler will replace them with the corresponding replacement constants defined in the # define statement. In this case, 1 will be substituted wherever INITIAL appears in the program. Note that there is no semicolon at the end of the # define statement.
Self-Instructional Material
47
Introduction to C Language
NOTES
The basic data types could be modified and a summary of all modified data types is given below: Basic data type char int Modified signed char unsigned char short unsigned int long long unsigned float double long double No. of bytes occupied 1 1 <=2 2 4 4 8 10
You have read about these modifiers and how their range gets affected by modifying the basic data types.
Although all escape sequences appear to be 2 characters wide, they are represented as a single character with the unique ASCII value.
48 Self-Instructional Material
2.10 ARRAYS
An array is another form of data type. There can be arrays of integers, arrays of characters and arrays of floating-point numbers. What does an array mean? It means a collection of a number of integers or floats or items of the same data type. An array contains data of the same type. For example, A = { 2, 3, 5, 7} Here A is an array of prime numbers. B = { Red, Green, Yellow} B is an array of colours. Thus, an array has a name, which is A in the former case and B in the latter case. How do you give a name to each element? You can use an index with the name of an array to indicate the elements. While in mathematics the first element can be called with an index [1], in C the first element is called with the index [0]. Index and subscript are used interchangeably to indicate the position of the element in an array. Thus,
A A A & [0] = [1] = [2] = A [3] 2 3 5 = 7
Introduction to C Language
NOTES
Similarly,
B [0] = Red B [1] = Green B [2] = Yellow
Array A has four elements and hence, the size of A is 4. Similarly, B has three elements and hence, its size is 3. Therefore, the first element of an array will have a subscript of 0 and the final element will have a subscript of n 1, where n is the size of the array.
Here, you have declared an integer variable known as emp_age with a dimension of 40. A single variable has been declared to store the age of 40 employees. But for this feature, you would have to write 40 lines to declare the same with 40 different names. An array is a variable and hence, must be declared like any other variable. The naming convention is the same as in the case of other variables. The array name cannot be a reserved word and must be unique in the program. An array variable name and another ordinary variable name cannot be identical. Since there is no limit to variable names, do not use similar names for a variable and an array. What distinguishes an array variable from a single variable is the declaration of the dimension within the square brackets.
Self-Instructional Material
49
Introduction to C Language
NOTES
What does variable declaration do? It allots memory space for the variable. If you declare an array variable of type integer and dimension 40, then the computer will allot a memory size of 40 words for the variable contiguously. The last word is important. The elements in an array will be stored in consecutive memory locations. Since an integer needs 2 bytes for storage and if the memory is organized and addressed in terms of bytes, the following allocation would be carried out for Here, an integer variable has been declared, known as emp elements:
emp_age [0] emp_age [1] emp_age [2] 1000 1002 1004
If emp_age [0] is stored at the location with address 1000, emp_age [1] will be at 1002. It is enough for you to indicate the starting address to find the address of any of the elements of the array. The emp_age [2] will be stored at 1004. The formula for finding the address of element is emp_age [n] = starting address + n * 2. If emp_age had been defined as a float variable and if emp_age [0] starts at the location 1000, then emp_age [10] would be found at location 1040. The formula is: address of nth element = starting address + n * (size of variable). The important point to be noted is that if the starting address is known and the type of variable is known, the exact location where an element of the array is stored can be computed. An array has to contain elements of the same type. A float array cannot have integers or characters. A character array is nothing but a string. You must always specify the array size. You would normally expect to get an error message if the array size is exceeded. But, this does not happen in the C language. In the above example, if you try to read the value of emp_age [50] nothing will happen. No error message will be printed. Therefore, it is the responsibility of the programmer not to exceed the array size. Since we have a single subscript, the arrays declared so far are known as one-dimensional arrays. Examples of one-dimensional arrays are as follows:
int emp_age[100]; float mark[100]; char name[25]; /* no space between variable name & dimension */ /* name contains 25 characters */
50 Self-Instructional Material
Statements
A statement is a sentence, which terminates with a semicolon in the C program. You are familiar with the assignment operator which is =. For example, in the statement:
int i = 3; int i is the declaration of the variable; i = 3 is an assignment statement and = is the assignment operator.
Introduction to C Language
NOTES
2.12 SUMMARY
In this unit, you have learned about the C language. C was preceded by two languages called BCPL and B. Though all the three languages BCPL, B and C are procedureoriented languages similar to FORTRAN, the C language removed the lacuna faced by the other two languages, namely BCPL and B. C is a more compact language having few keywords. Simple compilers translate them. They use programs in the standard library for input/output and other interactions with the operating system. During the 1980s, the C language compilers became available for most computer architectures and operating systems. It became a programming tool in PC, which increased its popularity. The C language is rich in data types and universal. It can be developed and executed in any platform that has a development environment for C. It can run under UNIX, LINUX and even in parallel computers. You also gained knowledge of tokens of which there are six classes, namely keyword, identifier, constant, string literal, operator and punctuators. The unit also discussed array, which is another form of data type. Arrays comprise integers, characters and floating-point numbers. An array is a collection of a number of integers or floats or items of the same data type. Simply put, an array contains data of the same type.
51
Introduction to C Language
NOTES
(d) character (e) 0x123 4. State which of the following are valid floating-point constants: (a) 123 E 2 (b) 123.0 e 2 (c) 3350.0001L (d) 267.1 E 2 f (e) 225.12 e 2.5
Introduction to C Language
NOTES
Long-Answer Questions
1. Define data types. 2. What is the difference between floating number and integers? 3. Explain constants. 4. Explain Tokens with the help of examples. 5. What are variables? How are they declared in a C program? 6. Define enumeration constant with the help of C program. 7. What are arrays? How are they declared? Explain with the help of C program.
Self-Instructional Material
53