C/stdio.h/printf
Содержание
- 1 31 non-null, automatically
- 2 33 non-null, automatically
- 3 4 decimal places, left-justify
- 4 decimal places, right-justify
- 5 default ivalue width, 4
- 6 minimum 3 overridden, auto 4
- 7 minimum 5 overridden, auto 33
- 8 minimum width 1
- 9 minimum width 10, left-justify
- 10 minimum width 10, right-justify
- 11 minimum width 19, print all 17
- 12 minimum width 20, left-justify
- 13 minimum width 20, right-justify
- 14 minimum width 38, left-justify
- 15 minimum width 38, right-justify
- 16 minimum width 5, left-justify
- 17 minimum width 5, right-justify
- 18 Output char
- 19 print character with ASCII 90
- 20 printf %10d, %10.f
- 21 printf %d for integer
- 22 printf: display message by format
- 23 printf ivalue with + sign
- 24 printf %ld for long integer number
- 25 printf %s for string
- 26 print ivalue as octal value
- 27 print lower-case hexadecimal
- 28 prints 2 chars, left-justify
- 29 prints 2 chars, right-justify
- 30 prints first 2 chars
- 31 print the ASCII code for c
- 32 print upper-case hexadecimal
- 33 right-justify with leading 0"s
- 34 using default number of digits
- 35 using printf arguments
- 36 width 10, 8 to right of "."
- 37 width 20, 2 to right-justify
- 38 width 20, scientific notation
31 non-null, automatically
<source lang="cpp">
- include <stdio.h>
int main( ) {
char psz2[] = "string text."; printf("%s",psz2);
}
</source>
33 non-null, automatically
<source lang="cpp">
- include <stdio.h>
int main( ) {
char psz1[] = "this is a test"; printf("%s",psz1);
}
</source>
4 decimal places, left-justify
<source lang="cpp">
- include <stdio.h>
int main( ) {
double dPi = 3.14159265; printf("%-20.4f",dPi);
}
</source>
decimal places, right-justify
<source lang="cpp">
- include <stdio.h>
int main( ) {
double dPi = 3.14159265; printf("%20.4f",dPi);
}
</source>
default ivalue width, 4
<source lang="cpp">
- include <stdio.h>
int main( ) {
int ivalue = 1234; printf("%d",ivalue);
}
</source>
minimum 3 overridden, auto 4
<source lang="cpp">
- include <stdio.h>
int main( ) {
int ivalue = 1234; printf("%3d",ivalue);
}
</source>
minimum 5 overridden, auto 33
<source lang="cpp">
- include <stdio.h>
int main( ) {
char psz1[] = "this is a test"; printf("%5s",psz1);
}
</source>
minimum width 1
<source lang="cpp">
- include <stdio.h>
int main( ) {
char c = "A"; printf("%c",c);
}
</source>
minimum width 10, left-justify
<source lang="cpp">
- include <stdio.h>
int main( ) {
int ivalue = 1234; printf("%-d",ivalue);
}
</source>
minimum width 10, right-justify
<source lang="cpp">
- include <stdio.h>
int main( ) {
int ivalue = 1234; printf("%10d",ivalue);
}
</source>
minimum width 19, print all 17
<source lang="cpp">
- include <stdio.h>
int main( ) {
char psz1[] = "this is a test"; printf("%19.19s",psz1);
}
</source>
minimum width 20, left-justify
<source lang="cpp">
- include <stdio.h>
int main( ) {
double dPi = 3.14159265; printf("%-20f",dPi);
}
</source>
minimum width 20, right-justify
<source lang="cpp">
- include <stdio.h>
int main( ) {
double dPi = 3.14159265; printf("%20f",dPi);
}
</source>
minimum width 38, left-justify
<source lang="cpp">
- include <stdio.h>
int main( ) {
char psz2[] = "string text."; printf("%-38s",psz2);
}
</source>
minimum width 38, right-justify
<source lang="cpp">
- include <stdio.h>
int main( ) {
char psz1[] = "this is a test"; printf("%38s",psz1);
}
</source>
minimum width 5, left-justify
<source lang="cpp">
- include <stdio.h>
int main( ) {
char c = "A"; printf("%-5c",c);
}
</source>
minimum width 5, right-justify
<source lang="cpp">
- include <stdio.h>
int main( ) {
char c = "A"; printf("%5c",c);
}
</source>
Output char
<source lang="cpp">
- include <stdio.h>
int main( ) {
char c = "A"; printf("%c",c);
}
</source>
print character with ASCII 90
<source lang="cpp">
- include <stdio.h>
int main( ) {
char c = "A"; printf("%c",90);
}
</source>
printf %10d, %10.f
<source lang="cpp">
- include <stdio.h>
int main() {
int y = 7, z = 35; long longVar = 98456; float floatVar = 8.8; char *phraseFour = "Formatted: "; printf("%s %5d %10d %10.5f\n",phraseFour,y,z,floatVar); return 0;
}
</source>
printf %d for integer
<source lang="cpp">
- include <stdio.h>
int main() {
int x = 5; printf("%d\n",x); return 0;
}
</source>
printf: display message by format
<source lang="cpp">
//Header file: #include <stdio.h> //Declaration: int printf(const char *format, ...); //Return: the number of characters actually printed. A negative value indicates failure. // The printf() Format Specifiers
//Code Format //%a: Hexadecimal output in the form 0xh.hhhhp+d (C99 only). //%A: Hexadecimal output in the form 0Xh.hhhhP+d (C99 only). //%c: Character. //%d: Signed decimal integers. //%i: Signed decimal integers. //%e: Scientific notation (lowercase e). //%E: Scientific notation (uppercase E). //%f: Decimal floating point. //%F: Decimal floating point (C99 only; produces uppercase INF, INFINITY, or NAN when applied to infinity or a value that is not a number. The %f specifier produces lowercase equivalents.) //%g: Uses %e or %f, whichever is shorter. //%G: Uses %E or %F, whichever is shorter. //%o: Unsigned octal. //%s: String of characters. //%u: Unsigned decimal integers. //%x: Unsigned hexadecimal (lowercase letters). //%X: Unsigned hexadecimal (uppercase letters). //%p: Displays a pointer. //%n: The associated argument must be a pointer to an integer. This specifier causes the number of characters written (up to the point at which the %n is encountered) to be stored in that integer. //%%: Prints a percent sign.
- include <stdio.h>
int main(void){
printf("Hi %c %d %s", "c", 10, "there!");
}
/*
Hi c 10 there!*/
</source>
printf ivalue with + sign
<source lang="cpp">
- include <stdio.h>
int main( ) {
int ivalue = 1234; printf("%+d",ivalue);
}
</source>
printf %ld for long integer number
<source lang="cpp">
- include <stdio.h>
int main() {
char *phraseTwo = "Here"s some values: "; char *phraseThree = " and also these: "; int y = 7, z = 35; long longVar = 98456; float floatVar = 8.8; printf("%s %d %d %s %ld %f\n",phraseTwo,y,z,phraseThree,longVar,floatVar); return 0;
}
</source>
printf %s for string
<source lang="cpp">
- include <stdio.h>
int main() {
printf("%s","hello world\n"); char *phrase = "Hello again!\n"; printf("%s",phrase); return 0;
}
</source>
print ivalue as octal value
<source lang="cpp">
- include <stdio.h>
int main( ) {
int ivalue = 1234; printf("%o",ivalue);
}
</source>
print lower-case hexadecimal
<source lang="cpp">
- include <stdio.h>
int main( ) {
int ivalue = 1234; printf("%x",ivalue);
}
</source>
prints 2 chars, left-justify
<source lang="cpp">
- include <stdio.h>
int main( ) {
char psz1[] = "this is a test", psz2[] = "string text."; printf("%-19.2s",psz1);
}
</source>
prints 2 chars, right-justify
<source lang="cpp">
- include <stdio.h>
int main( ) {
char psz1[] = "this is a test"; printf("%19.2s",psz1);
}
</source>
prints first 2 chars
<source lang="cpp">
- include <stdio.h>
int main( ) {
char psz1[] = "this is a test"; printf("%.2s",psz1);
}
</source>
print the ASCII code for c
<source lang="cpp">
- include <stdio.h>
int main( ) {
char c = "A"; printf("%d",c);
}
</source>
print upper-case hexadecimal
<source lang="cpp">
- include <stdio.h>
int main( ) {
int ivalue = 1234; printf("%X",ivalue);
}
</source>
right-justify with leading 0"s
<source lang="cpp">
- include <stdio.h>
int main( ) {
double dPi = 3.14159265; printf("%020f",dPi);
}
</source>
using default number of digits
<source lang="cpp">
- include <stdio.h>
int main( ) {
double dPi = 3.14159265; printf("%f",dPi);
}
</source>
using printf arguments
<source lang="cpp">
- include <stdio.h>
int main( ) {
char psz1[] = "this is a test", psz2[] = "string text."; printf("%*.*s",19,6,psz1);
}
</source>
width 10, 8 to right of "."
<source lang="cpp">
- include <stdio.h>
int main( ) {
double dPi = 3.14159265; printf("%10.8f",dPi);
}
</source>
width 20, 2 to right-justify
<source lang="cpp">
- include <stdio.h>
int main( ) {
double dPi = 3.14159265; printf("%20.2f",dPi);
}
</source>
width 20, scientific notation
<source lang="cpp">
- include <stdio.h>
int main( ) {
double dPi = 3.14159265; printf("%20.2e",dPi);
}
</source>