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.
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
putchar syntax
(1) Function declaration
int putchar(int char)
(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
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); }
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('\n'); } /* draw the middle */ for(i=0; i<4; i++) putchar(VERT); for (j=0; j<10; j++) { putchar(' '); putchar(VERT); putchar('\n'); /* draw the bottom */ putchar(LEFT_BOT); } for(i=0; i<10; i++) { putchar(HORIZ); putchar(RIGHT_BOT); putchar('\n'); return 0; } }
Example 2
#include <stdio.h> int main() { char a,b,c; a='T';b='M';c='D'; putchar(a);putchar(b);putchar(c);putchar('\n'); putchar(a);putchar('\n'); putchar(b);putchar('\n'); putchar(c);putchar('\n'); return 0; }
The output result is:
TMD T M D
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!