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
  
#include <stdio.h>
int main( )
{
 char   psz2[]   =   "string text.";
 
 printf("%s",psz2); 
}
33 non-null, automatically
  
#include <stdio.h>
int main( )
{
 char   psz1[]   =   "this is a test";
 
 printf("%s",psz1); 
}
4 decimal places, left-justify
  
#include <stdio.h>
int main( )
{
 double dPi      =   3.14159265;
 
 printf("%-20.4f",dPi); 
}
decimal places, right-justify
  
#include <stdio.h>
int main( )
{
 double dPi      =   3.14159265;
 
 printf("%20.4f",dPi); 
}
default ivalue width, 4
  
#include <stdio.h>
int main( )
{
 int    ivalue   =   1234;
 
 printf("%d",ivalue); 
}
minimum 3 overridden, auto 4
  
#include <stdio.h>
int main( )
{
 int    ivalue   =   1234;
 
 printf("%3d",ivalue); 
}
minimum 5 overridden, auto 33
  
#include <stdio.h>
int main( )
{
 char   psz1[]   =   "this is a test";
 
 printf("%5s",psz1); 
}
minimum width 1
  
#include <stdio.h>
int main( )
{
 char c = "A";
 
 printf("%c",c); 
}
minimum width 10, left-justify
  
#include <stdio.h>
int main( )
{
 int    ivalue   =   1234;
 
 printf("%-d",ivalue); 
}
minimum width 10, right-justify
  
#include <stdio.h>
int main( )
{
 int    ivalue   =   1234;
 
 printf("%10d",ivalue); 
}
minimum width 19, print all 17
  
#include <stdio.h>
int main( )
{
 char   psz1[]   =   "this is a test";
 
 printf("%19.19s",psz1); 
}
minimum width 20, left-justify
  
#include <stdio.h>
int main( )
{
 double dPi      =   3.14159265;
 
 printf("%-20f",dPi); 
}
minimum width 20, right-justify
  
#include <stdio.h>
int main( )
{
 double dPi      =   3.14159265;
 
 printf("%20f",dPi); 
}
minimum width 38, left-justify
  
#include <stdio.h>
int main( )
{
 char   psz2[]   =   "string text.";
 
 printf("%-38s",psz2); 
}
minimum width 38, right-justify
  
#include <stdio.h>
int main( )
{
 char   psz1[]   =   "this is a test";
 
 printf("%38s",psz1); 
}
minimum width 5, left-justify
  
#include <stdio.h>
int main( )
{
 char c = "A";
 
 printf("%-5c",c); 
}
minimum width 5, right-justify
  
#include <stdio.h>
int main( )
{
 char c = "A";
 
 printf("%5c",c); 
}
Output char
  
#include <stdio.h>
int main( )
{
 char c = "A";
 
 printf("%c",c);
 
}
print character with ASCII 90
  
#include <stdio.h>
int main( )
{
 char c = "A";
 
 printf("%c",90); 
}
printf %10d, %10.f
  
#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;
}
printf %d for integer
  
#include <stdio.h>
int main()
{
   int x = 5;
   printf("%d\n",x);
   return 0;
}
printf: display message by format
    
//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!*/
printf ivalue with + sign
  
#include <stdio.h>
int main( )
{
 int    ivalue   =   1234;
 
 printf("%+d",ivalue); 
}
printf %ld for long integer number
  
#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;
}
printf %s for string
  
#include <stdio.h>
int main()
{
   printf("%s","hello world\n");
   char *phrase = "Hello again!\n";
   printf("%s",phrase);
   return 0;
}
print ivalue as octal value
  
#include <stdio.h>
int main( )
{
 int ivalue = 1234;
 
 printf("%o",ivalue); 
}
print lower-case hexadecimal
  
#include <stdio.h>
int main( )
{
 int ivalue = 1234;
 
 printf("%x",ivalue); 
}
prints 2 chars, left-justify
  
#include <stdio.h>
int main( )
{
 char   psz1[]   =   "this is a test",
        psz2[]   =   "string text.";
 
 printf("%-19.2s",psz1); 
}
prints 2 chars, right-justify
  
#include <stdio.h>
int main( )
{
 char   psz1[]   =   "this is a test";
 
 printf("%19.2s",psz1); 
}
prints first 2 chars
  
#include <stdio.h>
int main( )
{
 char   psz1[]   =   "this is a test";
 
 printf("%.2s",psz1); 
}
print the ASCII code for c
  
#include <stdio.h>
int main( )
{
 char c =   "A";
 
 printf("%d",c); 
}
print upper-case hexadecimal
  
#include <stdio.h>
int main( )
{
 int ivalue = 1234;
 
 printf("%X",ivalue); 
}
right-justify with leading 0"s
  
#include <stdio.h>
int main( )
{
 double dPi      =   3.14159265;
 
 printf("%020f",dPi); 
}
using default number of digits
  
#include <stdio.h>
int main( )
{
 double dPi      =   3.14159265;
 
 printf("%f",dPi); 
}
using printf arguments
  
#include <stdio.h>
int main( )
{
 char   psz1[]   =   "this is a test",
        psz2[]   =   "string text.";
 
 
 printf("%*.*s",19,6,psz1); 
}
width 10, 8 to right of "."
  
#include <stdio.h>
int main( )
{
 double dPi      =   3.14159265;
 
 printf("%10.8f",dPi); 
}
width 20, 2 to right-justify
  
#include <stdio.h>
int main( )
{
 double dPi      =   3.14159265;
 
 printf("%20.2f",dPi); 
}
width 20, scientific notation
  
#include <stdio.h>
int main( )
{
 double dPi      =   3.14159265;
 
 printf("%20.2e",dPi);
 
}