C language string and formatted input and output

Source: Internet
Author: User

1. Preamble procedure

1#include <stdio.h>2#include <string.h>//1 function Prototypes for strlen ()3 #defineDENSITY 62.4//2 preprocessing commands4 intMainvoid)5 {6     floatWeight,volume;7     intsize,letters;8     Charname[ +];//3 Defining an array of length 409 Tenprintf"hi! What ' s your first name?\n"); Onescanf"%s", name); Aprintf"%s,what ' s your weight in pounds?\n", name); -scanf"%f",&weight); -Size=sizeofName//4 Length of array name[] theLetters=strlen (name);//5 calling Function strlen () -volume=weight/DENSITY; -printf"Well,%s,your volume is%2.2f cubic feet.\n", Name,volume);//%2.2f indicates a character width of 2 and is accurate to two digits after the decimal point -printf"Also,your First name has%d letters,\n", letters); +printf"And we have%d bytes to store it in.\n", size); -     return 0; +}

2. About strings

(1) A string is a sequence of one or more characters. such as "I am a student!".

(2) The C language uses null characters to mark the end of a string. The number of cells in the array must be at least 1 more than the number of characters to store.

(3) string and character. The difference between ' x ' and ' X ' (the latter is a string consisting of ' X ' and ' s ').

(4) Sizeof () and strlen () functions.

      • The same string, sizeof () calculates the null character at the end of the flag string, which is 1 larger than strlen ().
      • Strlen () gives the length of the string in characters . sizeof () gives the length of the array (the allocated memory unit).
      • Gets a type large size ~ Gets the size of a specific amount. sizeof (char) and sizeof (name) =sizeof name.
1 sizeof() and strlen ()2#include <stdio.h>3#include <string.h>4 #definePraise "What a Super Marvelous name!"5 intMainvoid)6 {7     Charname[ +];8printf"What ' s your name?\n");9scanf"%s", name);Tenprintf"hello,%s.%s\n", name,praise); Oneprintf"Your name of%d Letrers occupies%d memory cells.\n", Astrlen (name),sizeof(name));//sizeof name -printf"The phrase of praise has%d letters", - strlen (Praise)); theprintf"And occupies%d memory cells.\n",sizeof(praise));//sizeof Praise -     return 0; -}

3. Constants and C-preprocessor

(1) Constants such as 0.015. Float taxrate=0.015. Assign a constant of 0.015 to the variable taxrate, but the program may accidentally change its value.

(2) Two methods of the Const modifier and the # define preprocessing command

      • const int months=12;
      • #define MONTHS +12; (#define TEE ' T ') (#define OOPS "now has it!")

4.printf () function

(1) printf ():("Control description" + variable list) ~ (the variable uses a value, whether the value is a variable, constant, or expression).

(2) printf () conversion specifier:%c--a character,%d--signed decimal integer,%e--floating-point number e notation,%, f--floating-point decimal,%p--pointer,%%--print a%,%s--string ... :

      • Please avoid mismatched conversions.

(3) printf () marker:-(left-aligned), + (Signed), # (...), 0 (for all number formats, with leading 0 padding field widths)

      • Prints the first 8 characters of a string with a field width of 8 characters (%8.8s)
      • Print double quotation marks \ ".... \"
      • Prints a field width in the parameter list for the given octal integer (%*0).
      • %5D (00006)
      • Specify fixed field width (effectively prevent overflow)

(4) Print a longer string with printf ()

A. Use multiple printf () functions;

B. using (\) and enter in a printf ()

C. Using the string Join method ("Hello" "World")

1 printf () prints longer strings2#include <stdio.h>3 intMainvoid)4 {5printf"Here's one to print a");6printf"Long string.\n");//a7printf"Here's another to print a \8 Long string. \ n");//b9printf"Here's the newest to print a"Ten         "Long string.\n");//C One     return 0; A}

(5) The function return value of printf () returns the number of characters printed, and returns 1 if the output is incorrect, often used to check for output errors. To a file rather than a screen)

1 The return value of printf ()2#include <stdio.h>3 intMainvoid)4 {5     intbph2o=212;6     intRV;7  8rv=printf ("%d F is water ' s boiling point.\n", BPH2O);9printf"The printf () function printed%d characters.\n", RV);Ten     return 0; One}

5.SCANF () function

(1) scanf () stops reading at the first blank word spaces, tab, or line break. The ~gets () function can be used to read a string.

(2) Read the value of the variable type plus &, and read the string into a character array without using &.

(3) scanf ("%d,%d", &n,&m) accepts input, {scanf ("%c", &ch) reads the first character encountered in the input}

6. About Modifiers *

      • Use * In the field width section in printf () instead of numbers
      • SCANF () * Causes the function to skip the corresponding output item (scanf ("%*d%*d%d", &n) and the first two skips)

  

1 using variable-width output fields2#include <stdio.h>3 intMainvoid)4 {5 unsigned width,precision;6     intNumber= the;7     Doubleweight=242.5;8 9printf"What field width?\n");Tenscanf"%d",&width); Oneprintf"The number is :%*d:\n", width,number); Aprintf"Now Enter a width and a precision:\n"); -scanf"%d%d",&width,&precision); -printf"weight=%*.*f\n", width,precision,weight); the     return 0; -}

C language string and formatted input and output

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.