Comprehensive Case Study of basic knowledge of C language (1)

Source: Internet
Author: User

1. display information on the screen.

The most common way to display information is to use the library functions printf () and puts ().


A, escape character.


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PGJyPgo8L3A + CjxwPmIs16rSxsu1w/e3 + 6GjPC9wPgo8cD48aW1nIHNyYz0 = "http://www.2cto.com/uploadfile/Collfiles/20140509/20140509091935234.jpg" alt = "\">


The printf () function is located in the Standard C language library. It is the most common way to display information on the screen.


# Include
 
  
Void main () {// display the text on the screen very nice! Printf ("very nice! "); // Display the variable value int number = 10;/* format the string to be formatted and output in full swing, which consists of three parts: literal text: displayed as input. The value of number is an escape sequence, which provides special formatting control and consists of a backslash and a character. The following table lists the hash columns as follows: \ n (line break, which is the starting position to move to the next line) Conversion Description: it consists of a percentile (%) and a character. The following conversion instruction is % d (the conversion instruction tells the printf () function how to interpret the variables to be printed, and % d is in decimal format) */printf ("\ n The value of number is % d", number); // format The string; variable name that contains the value to be printed // printf ("\ a bell"); // zhenling // printf ("\ B Return "); // return the format // printf ("\ f "); // change the printf ("------ ---- \ n"); printf ("\ r "); // press enter/* why does the content of the above two rows disappear after the press enter is added? Because carriage return is the beginning of a row, rather than moving the next row. Solution: printf ("------ demarcation line ---- \ n"); that is, add a line break after the demarcation line. */Printf ("\ t horizontal tab"); // horizontal tab printf ("\ v vertical tab"); // vertical tab printf ("\ Backslash "); // backslash printf ("\? Question mark "); // question mark printf (" \ 'single quotation marks "); // single quotation marks printf (" \ "Double quotation marks"); // double quotation marks printf ("----- end ---"); system ("pause ");}
 
 
A, run result 1 


B. Result 2



2. Write a case based on the cycle and the basic knowledge we have learned.

# Define _ CRT_SECURE_NO_WARNINGS # include
 
  
# Include
  
   
# Include
   
    
Void choice_what (); // define the prototype void goon_or_quit (); // define the prototype void main () {goon_or_quit (); system ("pause ");} // determine and select void choice_what () {int temp = 0; printf ("Select Operation:"); printf ("Bell 1 \ n "); printf ("display date 2 \ n"); printf ("output your information 3 \ n"); scanf ("% d", & temp); switch (temp) {case 1: printf ("\ a is ringing! "); Break; case 2:/* time_t rawtime; // time type struct tm * timeinfo; // time structure time (& rawtime); // get time, in seconds, from January 1, January 1, 1970, stored in rawtime timeinfo = asctime (& rawtime); // localtime (& rawtime); converted to local time, tm time structure asctime () -- converted to standard ASCII time format: Week Month day hour: minute: Second year printf ("\ 007The current date/time is: % s", asctime (timeinfo )); * // system ("date \ n"); system ("@ echo % date % time %"); break; default: printf ("(* ^__ ^ *) Xi ......, We are all good children! ") ;}} Void goon_or_quit () {int mark = 1; // initialize the mark variable and mark do {choice_what (); printf (" continue? \ N "); printf (" Continue 1 \ n "); printf (" Exit 0 \ n "); scanf (" % d ", & mark );} while (mark); exit (0 );}
   
  
 


Running result



The printf () function accepts a series of parameters, each of which should be a conversion specifier of the formatted string.
Printf () Prints formatted Information to the standard output device. When printf () is used, standard input/output must be included.
The header file stdio. h.


Use puts () to display information


# Include
 
  
/* Common conversion specifiers */void main () {char a_char = 'a'; printf ("% c \ n", a_char); int a_int =-9; short a_short =-9; printf ("% d \ n", a_int); printf ("% d \ n", a_short); long a_long =-1_l; printf ("% ld \ n", a_long); float a_float = 100.65f; double a_double = 100.65; printf ("% f \ n", a_float ); printf ("% d \ n", a_double); char a_chars [17] = {"we are good kids! "}; // Note: a Chinese character is 2 bytes ,! 17 = 14 + 3 printf ("% s \ n", a_chars); unsigned int a_uint = 9; unsigned short a_ushort = 9; printf ("% u \ n", a_uint); printf ("% u \ n", a_ushort); unsigned long a_ulong = 10000; printf ("% lu \ n ", a_ulong); printf ("------------------------ Error Type Split line ------------------------- \ n"); printf ("Error Type:"); unsigned short a_ushort_e1 =-9; printf ("% u \ n", a_ushort_e1); printf ("Incorrect conversion descriptor:"); unsigned short a_ushort_e2 =-9; printf ( "% U \ n", a_ushort_e2); puts ("------------------ puts split line -----------------");/* function puts () can also be used to display text messages on the screen, however, it cannot display numeric variables. The puts () function accepts a string parameter, displays the parameter and automatically wraps the line feed. When you only need to print text and do not need to print variables, use puts () instead of printf () */puts ("Use the puts () function to display information :"); puts ("Use the puts () function to display information: \ n"); system ("pause ");}
 


Running result



3. Use the scanf () function to input numerical data.


The most flexible way to read numeric data from the keyboard is to use the library function scanf ().
The scanf () function reads data from the keyboard in the specified format and assigns it to one or more variables. Like printf (), scanf () also
Use a formatted string to describe the input format. The conversion specifiers used for the formatted string are the same as those used for the printf () function.

# Define _ CRT_SECURE_NO_WARNINGS # include
 
  
/* Use scanf to obtain the keyboard input information */void main () {int temp1 = 0; double temp2 = 1.09; // note the conversion instructions here, for the double type, use % lfscanf ("% d % lf", & temp1, & temp2); printf ("% d, % f", temp1, temp2 ); system ("pause ");}
 


Running result



Experience:
Printf () is more powerful than puts (), but has more overhead. Compilation of small, efficient programs, large programs, and precious resources

You must make full use of the advantage of smaller overhead of puts.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.