ebook img

Exploring c PDF

494 Pages·2016·16.81 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Exploring c

C was developed in 1972 by Dennis Ritchie at Bell Telephone Laboratories ( now AT & T Bell Laboratories ). It is an outgrowth of an earlier language called B, which was 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. This description is often referred as "K & R C". And after this there was no looking back, as more and more computer professionals switched over to C. Today there are C compilers available under every conceivable environment from DOS to Win- dows to Unix, and on all sizes of computers from micros to mainframes. Power, portability, performance and flexibility are pos- sibly the four most important reasons for C's popularity. C has been designed to have a relatively good progamming efficiency as com- pared to Machine oriented languages like Assembly or Machine language, and a relatively good machine efficiency as compared to Problem oriented languages like COBOL and FORTRAN. Variables and Constants in C Simply put, a variable is a tool to reserve space in computer's memory. The reserved space is given a name, which we call a variable The ABC Of C 3 name. Constants are stored in this reserved space. The constants stored in the space reserved by the variable may vary from time to time, hence the name variable. The name once given to a location, however, remains unchanged. Thus in the statement, i = 5 5 is a constant which is being stored in a location which has been given a name i. So, i is a variable just as a, x, note or str are. They all designate memory locations that will hold different values as- signed to these variables from time to time. Depending on the purpose for which you want to utilize memory, C allows you to decide how much memory to allocate to a variable. Accordingly memory comes in three convenient versions: char, int and float, which occupy 1,2 and 4 locations ( bytes ) respectively. A char can store values in the range -128 to +127, whereas an int can store values in the range -32768 to +32767. When do we use float? When we want to store numbers like 3.1415, or 0.0005672 in memory. A float can store values ranging from -3.4e+38to +3.4e+38. Now that is certainly a very impressive range! The memory occupied by each datatype can be found out using an operator called sizeof. For example, sizeof ( i n t ) would yield 2. Similarly, sizeof ( float ) would yield 4. The essence of these datatypes has been captured in Figure 1.1. The chars and ints can also be expressed in hexadecimal and octal notations. For example, 16 can be expressed in hex as 0x10, and as 020 in octal. Similarly, 22 can be expressed in hex as 0x16, and as 026 in octal. A hex number is always preceded by Ox or OX whereas an octal is preceded by 0 ( zero, not o or O ). C doesn't accept floats in hex or octal. With constants out of the way, let us now take a look at the variables. Certain rules have been framed to create variable names. A variable 4 Exploring C name can be any combination of alphabets, digits and an underscore, Figure 1.1 Integer, Character and Float ranges with a proviso that the first character is an alphabet. The length of the variable is a compiler dependent feature. Turbo C accepts a variable name upto 32 characters. The ABC Of C 5 Operations on Data C programs are concise partly because of the large number of operators available with it. There are as many as 45 operators avail- able in C. For starters we will concentrate only on arithmetic and assignment operators..These are shown in the Figure 1.2. Operators are classified as unary, binary or ternary depending on the number of operands they operate upon. For example, + is a binary operator since it operates on two operands that it adds. Figure 1.2 Arithmetic and Assignment Operators Except for % the other operators are as usual. % is read as 'modulus' operator, and it returns the remainder on dividing an int with another. As against this, a / returns the quotient. While evaluating an expres- sion,- C gives priorities to some operators above the others. A unary minus has the highest priority, followed by arithmetic operators and lastly the assignment operator. Within arithmetic operators, *, / and % enjoy a higher priority than + and -. 6 Exploring C Integer and Float Conversions It is important to understand the rules that govern the conversion of floating point and integer values in C. These are mentioned below. (a) An arithmetic operation between an integer and integer always yields an integer result. (b) An arithmetic operation between a float and float always yields a float result. (c) In an arithmetic operation between an integer and float, the integer is first promoted to float and then the operation is carried out. And hence it always yields a float result. (d) On assigning a float to an integer ( using the = operator ) the float is demoted to an integer. (e) On assigning an integer to a float, it is promoted to a float. The following figure illustrates these conversions, demotions and promotions. Assume i to be an int and a to be a float. Operation Result Operation Result i = 5/2 2 a = 5/2 2.0 i = 5.0/2 2 a = 5.0/2 2.5 i = 5/2.0 2 a = 5/2.0 2.5 i = 5.0/2.0 2 a =5.0/2.0 2.5 i = 2 / 5 0 a = 2/5 0.0 i = 2.0/5 0 a = 2.0/5 0.4 i = 2/5.0 0 a = 2/5.0 0.4 i = 2.0/ 5.0 0 a = 2.0/5.0 0.4 Figure 1.3 Integer and Float Conversions The ABC Of C 7 printf() and scanf() printf() is one of the most versatile statements in C. In fact, it is a standard library function used to display the output on the screen. The general form of printf() looks like this... printf ("Format string", list of variables); And here are a few examples... printf ("%c %d %f, name, age, sal); printf ("name = %c age = %d salary = %f, name, age, sal); printf ("name = %c\nage = %d\nsalary = %f, name, age, sal); Assuming the value of name as 'A', that of age as 23, and that of sal as 1500.00, the output of the above printf( )s would be A 23 1500.000000 name = A age = 23 salary = 1500.000000 name=A age = 23 salary = 1500.000000 The first printf() prints the values of the variables name, age and sal. As against this, the second would print messages against each value displayed. In the third printf(), the \n is used to send the cursor to the next line, hence the three values are printed on three different lines. Sometimes the list of variables may be dropped, as in, printf ("Enter values of a and b"); In this case everything enclosed within the pair of double quotes would be printed as it is on the screen. 8 Exploring C Having imbibed those details, now let us switch over to scanf(). scanf( ) is once again a standard library function. It is used to receive values of variables from the keyboard. For example, the following scanf() would let you supply the values of variables a and b. scant ("%d%f,&a,&b); > The general form of scanf() is almost similar to printf( ), except for two important differences: (a) Within the pair of double quotes there should occur only format specifications like %c, %d, %f etc. (b) The variable names must always be preceded by the 'address of operator &. The ABC OfC 9 Exercise [A] Complete the following: (1) C has been developed by working at . in the year while (2) Binary equivalent of 762 is , its octal.equivalent is and its hex equivalent is . (3) Maximum allowable width of a variable in Turbo C is characters. (4) First character in any variable name must always be an a . (6) A character variable can at a time store _ characters). [B] Point out which of the following variable names are invalid: gross-salary INTEREST salary of emp avg. thereisagirlinmysoup [C] Point out which of the following C constants are invalid: 124.567 0001 0xbc40 0Xbc40 0x12.45 Oxfgff (5) C variables are case ( sensitive / insensitive ). 10 Exploring/C .001 -12e-12 [D] What will be the output of the following programs: (1) main() { printf ("%d %d %d %d", 72,072,0x72,0X72); } (2) main() .{ printf("%d%o%x",72,72,72); } (3) main() { char ch; inta; float b; printf ("bytes occupied by ch = %d\n", sizeof (ch)); printf ("bytes occupied by a = %d\n", sizeof (a)); printf ("bytes occupied by b = %d\n", sizeof (b)); } (4) main() { printf ("char occupies %d bytes\n", sizeof (char)); printf ("int occupies %d bytes\n", sizeof (int)); printf ("float occupies %d bytes\n"„sizeof (float)); } main() printf ("bytes occupied by 7' = %d", sizeof ( T ) ) ; printf ("bytes occupied by 7 =i%d", sizeof (7)); The ABC Of C 11 printf ("bytes occupied by 7.0 = %d", sizeof (7.0)); I (6) main() { char ch = 291; printf ("%d %d %c", 32770, ch, ch); } (7) main() { printf ("%d%c\n"); printf ("%d%c\n"); } (8) main() { int a = 33000; float b = 3.4e100; printf("a = %db = %f\n",a,b); printf ("%d %d", sizeof (a), sizeof (b)); } (9) main() { int a, b; a = -3--3; b = -3--(-3); printf("a = %db = %d",a,b); } (10) main() { intx; x = 3*4%5; printf ("x = %d",x); 12 Exploring C } (11) main() { intx; x = 3 + 4-7*8/5%10; printf ("x = %d", x); } (12) main() { intx; x = -3 + 4-7*8/5%10; printf ("x = %d",x)i } (13) main() { intx; x = 4%5 + 6%5; printf (*x = %d",x); } (14) main() { intx; x = -3M%-6/-5; printf ("x = %d",x); } (15) main() { printf ("%d ",4/3); printf ("%d", 4/-3); printf ("%d ",-4/3); printf ("%d",-4/-3); The ABC Of C 13 (16) main() { printf ("%d", 4% 3); printf ("%d ",4%-3); printf ("%d M, -4 % 3); printf ("%d ",-4%-3); (17) main() { f loat a = 5, b = 2; int c; c = a%b; printf ("%d", c); (18) main() { int x; x = 3**4-7 A8; printf ("x = %d",x); (19) main() { int g = 300 * 300/300; printf ("g = %d",g); } (20) main() { floata=1.5; int b = 3; a = b/2 + b*8/b-b + a/3; 14 Exploring C printf ( ua = %f , I a); } (21) main() { int i = 3, a = 4, n; float t = 4.2.; n = a*a/i + i/2*t + 2 + t; printf ("n = %d", n); } (22) main() { int q = 2, d = 3, st; st = q*d/4-12/12+ 12/3*16/d printf ("st = %d", st); } (23) main() { int a, b; a = 5.999999; b = 5.000001; printf {"a = %d b = %d", a, b); } (24) main() { float a; a = 4 / 2 ; printf("%f%f,a,4/2); } (25) main() { printf ("%d %An", 4,4); printf ("%d%f\n", 4.0,4.0); } (26) main() { float a = 4; inti = 2; printf ("%f %d", i/a, i/a); printf ("%d%f,i/a,i / a ) ; (27) main() { printf ("%d Vsizeof (4) / sizeof (2.0)); printf ("%d", sizeof (2.0) / sizeof (4)); } (28) main() { printf ("nn \n\n nn\n"); printf ("nn /n/n nn/n"); } (29) main() { int p, q; scanf ("Enter values of p and q %d %d", &p, &q); printf ("p = %d q = %d", p,q); } (30) main() { int a, b; printf ("Enter values of a and b"); scanf (" %d%d ",&a,&b); printf ("a = %d b = %d", 16 Exploring C } (31) main() { int p, q; printf ("Enter values of p and q"); scant ("%d\n\n%d", &p,&q); printf ("p = %d q = %d", p, q); } (32) main() { int p, q; printf ("Enter values of p and q"); scant ("%d %d\p,q); printf ("p = %dq = %d", p,q); } (33) main() { /* This program attempts to find what happens when integer range I* -32768 to +32767 */ is exceeded */ int a = 330000; float b = 3.4e100; printf("a = %db = %f\n"Ia,b); } (34) main() { printf ("Menu is a list of options you have at a \ particular point in a program. It is just \ like a restaurant menu - everything has a \ misleading name and what you want is never \ available."); }

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.