String formatted input / output

Original link: http://www.cnblogs.com/zxj-262410/p/6691221.html

// array of uses mainly scanf and strlen sizeof distinction
#include <stdio.h>
#define for DENSITY 62.4
int main (void)
{
a float weight, Volume;
int size, Letters;
char name [40];

printf("Hi!What's your first name?\n");
scanf("%s",name);
printf("%s,what's your weight in pounds?\n",name);
scanf("%f",&weight);
size = sizeof name;
letters = strlen(name);
volume = weight /DENSITY;
printf("Well,%s,your volume is %2.2f cubic feet.\n",
name,volume);
printf("Also,your first name has %d letters,\n",
letters);
printf("and we have %d bytes to store it in.\n",size);
return 0;

}

 


/ *
After scanf () starts reading input, will be in the first blank character encountered (bank), tabs (tab) or line breaks (newline) at stop reading.
% s only a word and not the entire statement reads as a string, the other will be deposited into a memory buffer
* /


#include <stdio.h>
#define PRAISE "the What A Super Marvelous name!"
int main (void)
{
char name [40];
the while (. 1)
{
the printf ( "What apos your name \ n-?");
Scanf ( "% S", name);
the printf (. "the Hello,% S% S \ n-", name, PRAISE);
}

}

 

 


// strlen () than with sizeof () less a '\ 0'


#include<stdio.h>
#include<string.h>
#define PRAISE "What a super marvelous name!"
int main(void)
{
char name[40];

printf("What's your name?\n");
scanf("%s",name);
printf("Hello,%s. %s\n",name,PRAISE);
printf("Your name of %d letters occupies %d memory cells.\n",
strlen(name),sizeof name);
printf("The phrase of praise has %d letters",
strlen(PRAISE));
printf("and occupies %d memory cells.\n",sizeof PRAISE);
return 0;

}

 

 


/ * Symbolic constant name used must meet the naming variables, you can use uppercase and lowercase letters, numbers, and the underscore character, the first character can not be a number
printf () statement% 1.2f output result rounded to two reserved decimals * /


#include<stdio.h>
#define PI 3.14159
int main(void)
{
float area,circum,radius;

printf("What is the radius of your pizza?\n");
scanf("%f",&radius);
area = PI * radius * radius;
circum = 2.0 * PI * radius;
printf("Your basic pizza parameters are as follow: \n");
printf("circumference = %1.2f,area = %1.2f\n",circum,
area);
return 0;

}

 


// clear the system constants defined constants using the following procedure -------------- limit.h defined and float.h

#include<stdio.h>
#include<limits.h>
#include<float.h>

int main(void)
{
printf("Some number limits for this system: \n");
printf("Biggest int :%d\n",INT_MAX);
// printf("Smallest unsigned long: %lld\n",LLONG_MIN); //我的系统不支持
printf("One byte = %d bits on this system.\n",CHAR_BIT);
printf("Largest double: %e\n",DBL_MAX);
printf("Smallest normal float: %e\n",FLT_MIN);
printf("float precision = %d digits\n",FLT_DIG);
printf("float epsilon = %e\n",FLT_EPSILON);
return 0;
}

 

 

Some discussion about the% g in the group are:

% .2g not altogether two, the situation should be divided, such as an integer, it will print 06, referring to two significant figures, then it can be decided with a median of 2;
and 7.982 prints 7.98, referring to the reservation two decimal digits is determined by the foregoing. point of the foregoing, for example is a 6% 6.2d.

c, there is another provision: if you specify the number of bits less than the actual number of bits, then the field is automatically expand to accommodate the length of the numbers, obviously referring to 3, you specify two, the system automatically turns three. p71


% f is rounded off after the decimal point of significant digits 6
% g automatically select different values or according to% f% e. % e format used in index of less than -4 or greater than or equal accuracy
% g 0 does not display invalid, regardless of the accuracy of how much time it an integer number of significant digits as the format specifier is 3.2% .2g. 33.141434234 with 33% .2g outputs

The first title No, I guess: 0.0000009979 be specified with% f, it first into 0.000001 (because the system of significant digits 6% f - rounded off) after changes to scientific notation, that is 1e-006,
as to why 1.00e-006 is not clear

I made a few pictures of code, you can study at

 


// Use conversion specifier, the second term in () printf may be variable, constant, expression
#include <stdio.h>
#define the PI 3.141593
int main (void)
{
int = Number. 5;
a float Expresso = 13.5;
int cost = 3100;
the printf ( "of The% D CEOs Drank% F CUPS of Expresso \ n-.", Number,
Expresso);
the printf ( "of The value of the PI IS% F \ n-.", the PI);
the printf ( "Farewell! TOO for My Dear Art thou Possessing, \ n-");
the printf ("% C% D \ n-", '$', 2 * cost);
return 0;

}

 

/ * Field width
second conversion specifier% 2d, indicates that it should generate a field width of 2, but since this is a 3-digit integer, the field should be automatically expand to accommodate the length of the digital * /

#include<stdio.h>
#define PAGES 931
int main()
{
printf("*%d*\n",PAGES);
printf("*%2d*\n",PAGES);
printf("*%10d*\n",PAGES);
printf("*%-10d*\n",PAGES);
return 0;


}

 

Some combination //float.c-- floating point

#include<stdio.h>
int main(void)
{
const double RENT = 3852.99;

printf("*%f*\n",RENT);
printf("*%e*\n",RENT);
printf("*%4.2f*\n",RENT);
printf("*%3.1f*\n",RENT);
printf("*%10.3f*\n",RENT);
printf("*%10.3e*\n",RENT);
printf("*%+4.2f*\n",RENT);
printf("*%010.2f*\n",RENT);
return 0;
}

 


/ *
Second row specifier spaces to the value before generating a leading space (a leading space without generating negative values), which will enable the effective bits of the same positive and negative values with the same width of the print output, The results will look comfortable.
The third row of leading zeros produce enough to fill the required minimum number (here, 3); 0 flag will be used to fill the entire character width with a leading zero; Finally, if the flag is 0 and precision specifiers occur simultaneously 0 flag will be ignored
* /

#include<stdio.h>
int main(void)
{
printf("%x %X %#x\n",31,31,31);
printf("**%d**% d**% d**\n",42,42,-42);
printf("**%5d**%5.3d**%05d**%05.3d**\n",6,6,6,6);
return 0;

}

 

// the format string - .5 format specifier tells printf () prints only five characters
#include <stdio.h>
#define Blurb "Imitation of Authentic!"
Int main (void)
{
the printf ( "/ 2S% / \ n-", Blurb);
the printf (" / 24S% / \ n-", Blurb);
the printf (" / 24.5s% / \ n-", Blurb);
the printf (" /% - 24.5s / \ n- ", Blurb);
return 0;
}

 


Float mismatched conversion
/ *
value n1 ~ n4 of the first on the stack, when they put the type byte, long four-byte, double octet. % Ld four bytes and then read the instructions, and a description of the time symbol does not read on the
float when the print will be used as parameters into * Double /



#include <stdio.h>

int main(void)
{
float n1 = 3.0;
double n2 = 3.0;
long n3 = 2000000000;
long n4 = 1234567890;

printf("%.1e %.1e %.1e %.1e\n",n1,n2,n3,n4);
printf("%ld %ld\n",n3,n4);
printf("%ld %ld %ld %ld\n",n1,n2,n3,n4);
return 0;

}

 

printf function returns the value
/ *
printf () returns the number of characters printed, if the output error, returns a negative * /


#include <stdio.h>
int main (void)
{
int bph2o = 212;
int RV;

RV the printf = ( "% D F. IS apos Water Boiling Point \ n-.", bph2o);
the printf ( "of The the printf () function D Printed characters% \ n-.",
RV);

return 0;
}

 

 

//打印较长的字符串--三种方法
#include<stdio.h>
int main(void)
{
printf("Here's one way to print a ");
printf("long string.\n");
printf("Here's another way to print a \
long string.\n");
printf("Here's the newest way to print a"
"long string.\n");
return 0;

}

 

Few notes and use & scanf () under what circumstances
/ *
If fewer parameters, then, will go wrong.
scanf () with spaces (line feed, tabs, spaces) to determine how the input into several fields, except,% c. double use% lf.
char array without using &
scanf ( "% D, D%", & n-, & m); 88,121 form of use, the format specifier spaces running input means skipping any space before the next entry
scanf ( "% d% d", & n, & m); and scanf ( "% d% d" , & n, & m); behavior is the same. % c will be different, scanf ( "% c", & ch); first non-blank character reads encountered


* /


#include<stdio.h>
int main(void)
{
int age;
float assets;
char pet[30];

printf("Enter your age,assets,and favourite pet.\n");
scanf("%d %f",&age,&assets);
scanf("%s",pet);
printf("%d $%.2f %s\n",age,assets,pet);
return 0;

}

 


// The Output field of variable width

#include<stdio.h>
int main(void)
{
unsigned width,precision;
int number = 256;
double weight = 242.5;

printf("What field width?\n");
scanf("%d",&width);

printf("The number is: %*d: \n",width,number);
printf("Now enter a width and a precision: \n");
scanf("%d %d",&width,&precision);

printf("Weight = %*.*f\n",width,precision,weight);
return 0;

}

 


/ *
Skip the first inputs of two integers - copy program will give the following third integer n
, if the program needs to read the file in a specific column (the file data are arranged in a uniform row), then the function will be very Useful * /

#include <stdio.h>
int main (void)
{
int n-;

the printf ( "Please Enter Three integers: \ n-");
Scanf ( "% D% * D * D%", & n-);
the printf ( "Integer Last of the WAS% D \ n-", n-);
return 0;
}
Tip:% 9d aligned output format width, and a desired number specified width or less similarly small field width will make positive number Suitable.

 

Reproduced in: https: //www.cnblogs.com/zxj-262410/p/6691221.html

Guess you like

Origin blog.csdn.net/weixin_30410999/article/details/94790227