Detailed explanation of the usage of putchar

藏色散人
Release: 2020-07-09 13:49:03
Original
15230 people have browsed it

The function of putchar is to write the specified character to the standard output "stdout". Its syntax is "int putchar(int char)". The parameter char represents the character to be written. The character is The corresponding int value is passed.

Detailed explanation of the usage of putchar

The syntax structure of putchar is int putchar(int char), its function is to put the character specified by the parameter char (an unsigned character) Write to the standard output stdout, which is a C library function and is included in the C standard library . The output can be a character, a decimal integer between 0 and 127 (including 0 and 127), or a character variable defined with char.

putchar syntax

(1) Function declaration

int putchar(int char)
Copy after login

(2) Parameter

char-- This is to be written characters entered. The character is passed with its corresponding int value.

(3) Function

Write the character specified by parameter char (an unsigned character) to the standard output stdout. [3]

(4) Description

This function outputs the characters corresponding to the value of the specified expression to the standard output terminal. The expression can be a character type or an integer type, and it can only output one character at a time. For example: "putchar('#')" outputs the character "#".

Application format

The basic format of the putchar function is: putchar(c).

(1) When c is a character enclosed by single quotes (in English), the character is output (note: this character can also be an escape character);

(2 ) When c is a decimal integer between 0 and 127 (including 0 and 127), it will be regarded as the ASCII code of the corresponding character, and the character corresponding to the ASCII code will be output;

(3) When c is a character variable defined in advance with char, output the character pointed to by the variable.

Notes

When using character input/output functions, you must add the header file #include or #include "stdio. h". Moreover, the variable and output result of this function can only be one character.

Function return value

This function returns the written character as an unsigned char cast to int.

(1) When the output is correct, return the unsigned int value that the output character is converted to;

(2) When the output is wrong, return the EOF (End of file) end of file character

if(putchar(c)==EOF)
  {
printf("output error:%m\n");
exit(0);
}
Copy after login

Program Example

Example 1

#include <stdio.h>
/* define some box-drawing characters */
#define LEFT_TOP 0xDA
#define RIGHT_TOP 0xBF
#define HORIZ 0xC4
#define VERT 0xB3
#define LEFT_BOT 0xC0
#define RIGHT_BOT 0xD9
int main(void)
{
char i, j;
/* draw the top of the box */
putchar(LEFT_TOP);
for(i=0; i<10; i++)
{
putchar(HORIZ);
putchar(RIGHT_TOP);
putchar(&#39;\n&#39;);
}
/* draw the middle */
for(i=0; i<4; i++)
putchar(VERT);
for (j=0; j<10; j++)
{
putchar(&#39; &#39;);
putchar(VERT);
putchar(&#39;\n&#39;);
/* draw the bottom */
putchar(LEFT_BOT);
}
for(i=0; i<10; i++)
{
putchar(HORIZ);
putchar(RIGHT_BOT);
putchar(&#39;\n&#39;);
return 0;
}
}
Copy after login

Example 2

#include <stdio.h>
int main()
{
char a,b,c;
a=&#39;T&#39;;b=&#39;M&#39;;c=&#39;D&#39;;
putchar(a);putchar(b);putchar(c);putchar(&#39;\n&#39;);
putchar(a);putchar(&#39;\n&#39;);
putchar(b);putchar(&#39;\n&#39;);
putchar(c);putchar(&#39;\n&#39;);
return 0;
}
Copy after login

The output result is:

TMD
T
M
D
Copy after login

Recommended: "C Language Tutorial"

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template