C/stdio.h/printf

Материал из C\C++ эксперт
Перейти к: навигация, поиск

31 non-null, automatically

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

char   psz2[]   =   "string text.";

printf("%s",psz2); 

}


 </source>


33 non-null, automatically

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

char   psz1[]   =   "this is a test";

printf("%s",psz1); 

}


 </source>


4 decimal places, left-justify

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

double dPi      =   3.14159265;

printf("%-20.4f",dPi); 

}


 </source>


decimal places, right-justify

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

double dPi      =   3.14159265;

printf("%20.4f",dPi); 

}


 </source>


default ivalue width, 4

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

int    ivalue   =   1234;

printf("%d",ivalue); 

}


 </source>


minimum 3 overridden, auto 4

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

int    ivalue   =   1234;

printf("%3d",ivalue); 

}


 </source>


minimum 5 overridden, auto 33

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

char   psz1[]   =   "this is a test";

printf("%5s",psz1); 

}


 </source>


minimum width 1

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

char c = "A";

printf("%c",c); 

}


 </source>


minimum width 10, left-justify

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

int    ivalue   =   1234;

printf("%-d",ivalue); 

}


 </source>


minimum width 10, right-justify

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

int    ivalue   =   1234;

printf("%10d",ivalue); 

}


 </source>


minimum width 19, print all 17

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

char   psz1[]   =   "this is a test";

printf("%19.19s",psz1); 

}


 </source>


minimum width 20, left-justify

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

double dPi      =   3.14159265;

printf("%-20f",dPi); 

}


 </source>


minimum width 20, right-justify

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

double dPi      =   3.14159265;

printf("%20f",dPi); 

}


 </source>


minimum width 38, left-justify

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

char   psz2[]   =   "string text.";

printf("%-38s",psz2); 

}


 </source>


minimum width 38, right-justify

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

char   psz1[]   =   "this is a test";

printf("%38s",psz1); 

}


 </source>


minimum width 5, left-justify

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

char c = "A";

printf("%-5c",c); 

}


 </source>


minimum width 5, right-justify

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

char c = "A";

printf("%5c",c); 

}


 </source>


Output char

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

char c = "A";

printf("%c",c);

}


 </source>


print character with ASCII 90

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

char c = "A";

printf("%c",90); 

}


 </source>


printf %10d, %10.f

<source lang="cpp">

  1. 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">

  1. 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.


  1. 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">

  1. include <stdio.h>

int main( ) {

int    ivalue   =   1234;

printf("%+d",ivalue); 

}


 </source>


printf %ld for long integer number

<source lang="cpp">

  1. 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">

  1. 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">

  1. include <stdio.h>

int main( ) {

int ivalue = 1234;

printf("%o",ivalue); 

}


 </source>


print lower-case hexadecimal

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

int ivalue = 1234;

printf("%x",ivalue); 

}


 </source>


prints 2 chars, left-justify

<source lang="cpp">

  1. 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">

  1. include <stdio.h>

int main( ) {

char   psz1[]   =   "this is a test";

printf("%19.2s",psz1); 

}


 </source>


prints first 2 chars

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

char   psz1[]   =   "this is a test";

printf("%.2s",psz1); 

}


 </source>


print the ASCII code for c

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

char c =   "A";

printf("%d",c); 

}


 </source>


print upper-case hexadecimal

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

int ivalue = 1234;

printf("%X",ivalue); 

}


 </source>


right-justify with leading 0"s

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

double dPi      =   3.14159265;

printf("%020f",dPi); 

}


 </source>


using default number of digits

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

double dPi      =   3.14159265;

printf("%f",dPi); 

}


 </source>


using printf arguments

<source lang="cpp">

  1. 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">

  1. include <stdio.h>

int main( ) {

double dPi      =   3.14159265;

printf("%10.8f",dPi); 

}


 </source>


width 20, 2 to right-justify

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

double dPi      =   3.14159265;

printf("%20.2f",dPi); 

}


 </source>


width 20, scientific notation

<source lang="cpp">

  1. include <stdio.h>

int main( ) {

double dPi      =   3.14159265;

printf("%20.2e",dPi);

}


 </source>