Home  >  Article  >  Backend Development  >  What is the function of putchar()

What is the function of putchar()

青灯夜游
青灯夜游Original
2023-01-11 14:44:2118197browse

The function of putchar() is to output a character to the terminal. putchar() is an output function with the syntax "putchar(char)". It can write the character specified by the parameter char (an unsigned character) to the standard output stdout. The output of the putchar() function can be a character, a decimal integer between 0 and 127 (including 0 and 127), or a character variable defined with char.

What is the function of putchar()

The operating environment of this tutorial: windows7 system, c99 version, Dell G3 computer.

putchar() is a function in C language. Its function is to output a character to the terminal.

The putchar() function 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.

The syntax structure is:

int putchar(int char)

The character specified by the parameter char (an unsigned character) can be written to the standard output stdout.

Parameters:

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

Return value

  • This function returns the written character in the form of unsigned char cast to int, Returns EOF if an error occurs.

Explanation

  • 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 "#".

putchar() function uses

Can the putchar() function only output char type data? With such doubts, I conducted verification on the environment. The verification results are quite interesting, so I would like to share them.

When we define a variable a and assign a value from 0 to 255, there is obviously no problem, such as the code:

#include<stdio.h>void main( )
{     int a = 0;  //将0值赋给变量a
     int b = 255;  //将255值赋给变量b
     putchar(a) ; //输出变量a的值
     putchar(b) ; //输出变量b的值
     putchar(&#39;\n&#39;) ;
}

The result is obviously no problem.

But what if the assigned value is a negative number now? For example, the code:

#include<stdio.h>void main( )
{     int a = -4646;  //将-4646值赋给变量a
     int b = -2;  //将-2值赋给变量b
     putchar(a) ; //输出变量a的值
     putchar(&#39;\n&#39;) ;     putchar(b) ; //输出变量b的值
     putchar(&#39;\n&#39;) ;
}

found that the compilation is normal and the results can be output, but the output results are all empty. I am not sure why?

Another situation is, what happens when the assigned value is greater than 255? The code is as follows:

#include<stdio.h>void main( )
{     int a = 592;  //将592值赋给变量a
     int b = 267;  //将267值赋给变量b
     putchar(a) ; //输出变量a的值
     putchar(&#39;\n&#39;) ;     putchar(b) ; //输出变量b的值
     putchar(&#39;\n&#39;) ;
}

The running result is:

What is the function of putchar()

It can be found from the result that the assigned value is truncated into char type data, that is to say, only The lower eight bits of data are taken.

From these examples, the putchar() function does not check the true range of characters to be output.

Conclusion:

When using it, you need to pay attention to whether the range of the output variable is within the range of one character. Only within the range of one character can the correct output be achieved. Otherwise something will definitely go wrong.

Recommended: "c Language Tutorial"

The above is the detailed content of What is the function of putchar(). 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