Home  >  Article  >  Backend Development  >  Examples of usage of putchar in C language

Examples of usage of putchar in C language

下次还敢
下次还敢Original
2024-05-02 19:27:16407browse

The putchar() function is used to write a single character to the standard output device. The usage steps are as follows: include the <stdio.h> header file. Define an integer variable representing the character to be written. Use the putchar() function to print characters to the console.

Examples of usage of putchar in C language

putchar() function is used in C language

putchar() function is used in C language Function that writes a single character to the standard output device (usually the console). The syntax is as follows:

<code class="c">int putchar(int character);</code>

Among them:

  • character: The character to be written, expressed as an integer.

Usage Example

The following example demonstrates how to use the putchar() function to print characters to the console:

<code class="c">#include <stdio.h>

int main() {
    char character = 'A';
    putchar(character);
    return 0;
}</code>

In this example:

  • First, include the <stdio.h> header file, which contains the declaration of the putchar() function.
  • Then, define a character variable character and initialize it to 'A'.
  • Finally, use the putchar() function to print characters to the console.

Running this program will output the letter 'A' to the console.

Other examples

The following are more examples showing the use of the putchar() function:

  • Print newline characters :

    <code class="c">putchar('\n');</code>
  • Print multiple characters:

    <code class="c">char string[] = "Hello, world!";
    for (int i = 0; i < strlen(string); i++) {
      putchar(string[i]);
    }</code>
  • Print without printing newlines:

    <code class="c">putchar('A');
    putchar('B');</code>

Note:

  • putchar() function returns the number of characters written.
  • If the write operation is successful, the number of characters is returned; if the operation fails, EOF (-1) is returned.
  • The putchar() function does not automatically flush the output buffer. To output characters immediately, you can use the fflush() function.

The above is the detailed content of Examples of usage of putchar in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn