C/Console/Printf

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

Demonstrates printing the results of simple equations

       
#include <stdio.h>

int main()
{
    int term = 3 * 5;
    printf("Twice %d is %d\n", term, 2*term);
    printf("Three times %d is %d\n", term, 3*term);
    return (0);
}


Displaying printable characters

#include <stdio.h>
#include <ctype.h>
void main() {
  int i = 0;  /* Loop counter         */
  
  char ch = 0;
  
  for(i = 0 ; i<128 ; i++) {
    ch = (char)i;
    if( ch % 2 == 0)
      printf("\n");
    printf("  %4d    %c",ch,(isgraph(ch) ? ch : " "));
  }
  printf("\n");
}


Displaying printable characters plus whitspace names

#include <stdio.h>
#include <ctype.h>
void main()
{
  int i = 0;                         /* Loop counter         */
  char ch = 0;                       /* Character code value */
  for(i = 0 ; i<128 ; i++)
  {
    ch = (char)i;
    if(ch%2==0)
      printf("\n");
    printf("  %4d",ch);
    if(isgraph(ch))
      printf("               %c",ch);
    else
      switch(ch)
    {
        case "\n":
           printf("         newline",ch);
           break;
        case " ":
           printf("           space",ch);
           break;
        case "\t":
           printf("  horizontal tab",ch);
           break;
        case "\v":
           printf("    vertical tab",ch);
           break;
         case "\f":
           printf("       form feed",ch);
           break;
        default:
           printf("                ");
           break;
    }
  }
  printf("\n");
}


printf usage

#include <stdio.h>
int main()
{
    printf("Hello World\n");
    return (0);
}


Prints 3 characters forward and backward to demonstrate character printing

       
#include <stdio.h>
int main()
{
    char char1;     /* first character */
    char char2;     /* second character */
    char char3;     /* third character */
    char1 = "A";
    char2 = "B";
    char3 = "C";
    printf("%c%c%c reversed is %c%c%c\n",
        char1, char2, char3,
        char3, char2, char1);
    return (0);
}


Return value from printf

#include <stdio.h>
main() {
     int i = 0;
     
     i=printf("abcde"); 
     
     printf("total characters printed %d\n",i); 
}