I can't afford to buy an iPhone 4, but I can only get one itouc4. I want to try Ios development. Although monotouch is available, I have to authorize this item for $399 and change it to RMB for several thousand RMB. Forget it, to save money, review C and upgrade it to objective-C.
1.1Run "Hello, world" in your system"Program. Remove some content from the program to see what error messages will be generated.
# Include <stdio. h> // although stdio. H is already included by default, we recommend that you add it to ensureCodeClear structure // The main function always returns Int. Although this int is not required, it is recommended to add it to form a good habit of int main (void) {printf ("hello, world \ n "); System (" pause "); // pause; otherwise, the program will flash through (equivalent to the console in C. read () return 0; // specify the return value. Although this parameter can be omitted, it is recommended that you write it in. The main function also needs to return the value to the running environment}
1.2In an experiment, when the parameter string of the printf function contains \ c (C is a character not listed in the Escape Character Sequence above), observe what happens.
# Include <stdio. h> int main (void) {// Note: We recommend that you use standard escape characters (\ n, \ t, \ B, \ ", \, and so on ), if a non-standard escape character is used, the result becomes unpredictable (for example, \ A, \ f, \ r, \ V, and so on) printf ("\ audible or visual alert. \ A \ n "); printf (" form feed. \ f \ n "); printf (" this escape, \ r, moves the active position to the initial position of the current line. \ n "); printf (" vertical tab \ v is tricky, as its behaviour is unspecified under certain conditions. \ n "); System (" pause "); Return 0 ;}
1.3Modify the temperature conversion program to print a title at the top of the conversion table.
# Include <stdio. h> int main (void) {printf ("% 2.1f", 1.234); float Fahr, Celsius; int lower, upper, step; lower = 0; upper = 300; step = 20; printf ("Fahrenheit Temperature \ t Celsius Temperature \ n"); Fahr = lower; while (Fahr <= upper) {Celsius = (5.0/9) * (Fahr-32.0); // note: the final result of 5/9 is int type, that is, 0. The result of 5.0/9 is float type (INT type 9 is automatically converted to float type) printf ("% 8.0f \ t % 8.1f \ n", Fahr, Celsius); // Note: % 8.1f indicates that the number of decimal places is not counted, the decimal point is 1-bit Fahr + = step;} system ("pause"); Return 0 ;}
1.4Write a program to print the conversion table of the Celsius temperature to the corresponding Fahrenheit temperature.
# Include <stdio. h> int main (void) {float Fahr, Celsius; int lower = 0, upper = 300, step = 20; printf ("Celsius Temperature \ t Fahrenheit Temperature \ n "); celsius = lower; while (Celsius <= upper) {Fahr = (9.0/5.0) * Celsius + 32.0; printf ("% 8.0f \ t % 7.1f \ n", Celsius, fahr); Celsius = Celsius + step;} system ("pause"); Return 0 ;}
1.5Modify the temperature Conversion Program and print the temperature conversion table in reverse order (from 300 degrees to 0 degrees ).
# Include <stdio. h> int main (void) {float Fahr, Celsius; int lower = 0, upper = 300, step = 20; printf ("Celsius Temperature \ t Fahrenheit Temperature \ n "); for (Celsius = upper; Celsius> = lower; Celsius-= step) {Fahr = (9.0/5.0) * Celsius + 32.0; printf ("% 8.0f \ t % 7.1f \ n", Celsius, Fahr);} system ("pause"); Return 0 ;}
1.6Verification expression getchar ()! = Whether the value of EOF is 0 or 1.
# Include <stdio. h> int main (void) {printf ("press a key. CTRL + D or Ctrl + Z stands for EOF \ n"); printf ("expression getchar ()! = EOF value: % d \ n ", getchar ()! = EOF); System ("pause"); Return 0 ;}
1.7Compile a program that prints the EOF value.
# Include <stdio. h> int main (void) {printf ("EOF value is represented as: % d \ n", EOF); System ("pause"); Return 0 ;}
1.8Compile a program for counting the number of spaces, tabs, and line breaks.
# Include <stdio. h> int main (void) {printf ("Enter some characters, CTRL + Z or Ctrl + D to end: \ n"); int C, blanks, tabs, newlines, done, lastchar; C = blanks = tabs = newlines = Done = 0; while (done = 0) {c = getchar (); If (C = '') ++ blanks; If (C = '\ t') ++ tabs; If (C =' \ n') ++ newlines; If (C = EOF) {// according to the explanation in the C answer book, The following processing is to cope with the situation where "a text file may not end with a line break, // However, I cannot try it out in the Windows console application. Because the character is not sent to the while loop even if I press Ctrl + Z at the end Carriage return is also in vain if (lastchar! = '\ N') {++ newlines;} done = 1;} lastchar = C;} printf ("the entered characters contain % d spaces, there are % d tabs and % d linefeeds \ n ", blanks, tabs, newlines); System (" pause "); Return 0 ;}
1.9Write a program that copies the input to the output, and replace multiple consecutive spaces with one space.
Solution 1
# Include <stdio. h> int main (void) {int C; int inspace = 0; // used to determine whether consecutive spaces are encountered (0 NO, 1 Yes) while (C = getchar ())! = EOF) {If (C = '') {// if no consecutive space is encountered (that is, whether the previous character is a space) if (inspace = 0) {inspace = 1; // first assume that the next character is also a space (that is, a consecutive space) putchar (c); // display the first cell}/* because the book has so far, the yet keyword has not yet been mentioned, so we use two non-elegant continuous if statements for processing * // if the next character is not a space if (C! = '') {Inspace = 0; // correct the" consecutive space hypothesis "in the previous if statement, and reset inspace to 0, that is, non-consecutive space putchar (C ); // display the next non-space character} system ("pause"); Return 0 ;}
Solution 2
# Include <stdio. h> int main (void) {int C, PC; // C is used to save the current character, while PC is the first character of previous charactor Pc = EOF; // set the previous character to EOF so that it does not match any character while (C = getchar ())! = EOF) {If (C = '') // if the current character is a space, if (PC! = '') // Continue to judge whether the previous character is a space putchar (c); // if it is not a consecutive space, output this character if (C! = '') // Because the else keyword has not yet been mentioned in the original book, two consecutive if statements are used to process putchar (c); Pc = C; // "Update" the value of the previous character} system ("pause"); Return 0 ;}
Solution 3
# Include <stdio. h> int main (void) {int C; while (C = getchar ())! = EOF) {If (C = '') {putchar (c); While (C = getchar () ='' & C! = EOF) // continue to accept the next character. If the next character is a space and no Terminator is entered, ignore this consecutive space} If (C = EOF) // when an Terminator is encountered, exit break; putchar (c); // note: here is actually the output of the "valid" character in the second while loop} return 0 ;}
1.10Write a program that copies the input to the output, replace the tab with \ t, replace the return character with \ B, and replace the backslash \\. In this way, you can display tabs and rollbacks in a visible manner.
Solution 1
# Include <stdio. h> int main (void) {int C, D; // D is used to determine whether the specified escape character (1 is, 0 is not) while (C = getchar ())! = EOF) {d = 0; If (C = '\') {// outputs two '\' putchar ('\') consecutively ('\\'); putchar ('\'); D = 1;} If (C = '\ t') {// output' \ t' putchar ('\\'); putchar ('T'); D = 1;} If (C = '\ B') {putchar ('\'); putchar ('B '); D = 1 ;}if (D = 0) // if it is not an escape character, the output is (Note: If this judgment is removed, the Escape Character is still output in invisible mode) putchar (c);} system ("pause"); Return 0 ;}
Solution 2
#include
# define esc_char '\' // defines a "character constant" int main (void) {int C; while (C = getchar ())! = EOF) {Switch (c) {Case '\ B': putchar (esc_char); putchar ('B'); break; Case '\ t': putchar (esc_char ); putchar ('T'); break; Case esc_char: putchar (esc_char); break; default: putchar (c); break ;}} system ("pause"); Return 0 ;}