C/Console/Console Output String

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

A simple example for printf

<source lang="cpp">

  1. include <stdio.h>

int main(void) {

 printf("This is an example of redirection.\n");
 return 0;

}


      </source>


Displaying a Quotation: the main function

<source lang="cpp"> /*Displaying a Quotation */

  1. include <stdio.h> /* stdio.h is a preprocessor directive */

void main() /* void main() identifes the function main()

                          The entry point of the program.
                       */

{ /* "{" marks the beginning of main() */

 printf("Hi!");  /* This line displays a String */

} /* "}" marks the end of main() */


      </source>


Displaying a string

<source lang="cpp">

  1. include <stdio.h>

void main() {

 printf("The character \0 is used to terminate a string");

}


      </source>


Output a string to stdout: how to use puts

<source lang="cpp">

  1. include <stdio.h>

int main () {

 char string [] = "Hiiiiiiiiiiii!";
 puts (string);

}

      </source>


Output string to console

<source lang="cpp">

  1. include <stdio.h>

int main(void) {

 puts("one");
 puts("two");
 puts("three");
 return 0;

}


      </source>


prints Hello on standard output

<source lang="cpp">

  1. include <stdio.h>

main() {

   printf("Hello \n");

}


      </source>


Print the string in the reverse order

<source lang="cpp">

  1. include <string.h>
  2. include <stdio.h>

void pr_reverse(char *s); int main(void) {

 pr_reverse("I like C");
 return 0;

} void pr_reverse(char *s) {

 register int i;
 for(i = strlen(s) - 1; i >= 0; i--) 
     putchar( s [ i ] );

}


      </source>


Read and output string

<source lang="cpp">

  1. include <stdio.h>

int main(void) {

 char str[80];
 printf("Enter a string: ");
 scanf("%s", str);
 printf("Here"s your string: %s", str);
 return 0;

}


      </source>