After reading putchar in c post you will be ready to use putchar function in proper manner.
What is putchar in c programming language?
putchar in c writes a single character (unsigned char type) to standard output device.
putchar is a function used to perform output operation of a single byte.
It is part of standard c buffered IO.
Putchar in c adds a character to the STDOUT
that is bound to the file descriptor of the output console.
Putchar in c is found in standard input output library that is stdio.h.
This function doesnot advances the cursor to the new line.
Prototype
#include<stdio.h>
int putchar(int character);
Parameters of putchar in c
putchar accepts one mandatory character that is the character to be written on to the standard output console.
Retrurn value putchar in c
putchar returns int that is the integer value of the character it wrote to the output console if writing of that character is successful. If the writing operation is unsuccessful then putchar returns EOF
and sets the error to one of the errors listed below:
- EACCES
- EBADF
- EFBIG
- EINTR
- EIO
- ENOMEM
- ENOSPC
- ENXIO
- EPIPE
putchar in c example
#include<stdio.h>
int main(int argc, char const *argv[])
{
char a='s';
putchar(a);
return 0;
}
Code explaination
First we have to include the standard input output header file inorder to use putchar function to perform output operation. That we have done using #include
macro.
Then in the main function I declared a variable of char type and assigned value s by using assignement operator.
After that Iam passing that variable to putchar
functon to output it on to the console.
Output
putchar to print a string
#include<stdio.h>
#include<string.h>
int main(int argc, char const *argv[])
{
char str[15]="Scholar Soul\0";
int i=0;
while(i<strlen(str))
{
putchar(str[i]);
i++;
}
return 0;
}
Code explaination
First we have to include the standard input output header file inorder to use putchar function to perform output operation. That we have done using #include
macro.
Then in the main function I declared a string.
Iam looping through each character by using while loop.After that Iam passing that variable to putchar
functon to output it on to the console.
Output
putchar vs getchar
Putchar writes a single character to stdout. Getchar reads a character from stdin.
Putchar performs output operation. getchar in c performs an input oeration.
Putchar returns the written charater. getchar returns the read value.
Both functions performs single character IO.
putchar | getchar |
int putchar(int); | int getchar(void); |
It writes a single character to output console. | It reads a character from input device. |
It performs output operation. | It performs input operation. |
It returns the written character. | It returns the read character. |
putchar vs puts in c
Putchar performs single character output. puts performs multiple character output.
Putchar returns the character written on to the console. Putchar returns number of characters written on to the console.
Putchar is used to perform single character output operation. puts is used to perform multiple character output operation.
Putchar doesnot advances the cursor to new line. Puts advances the cursor to new line.
putchar in c | puts in c |
It writes a single character to the console. | It writes a string to console. |
Returns the int value of the character written on to the console. | It returns the number of characters written on to the console. |
This function does not advances the cursor to new line. | This function advances the cursor to new line. |
putchar vs putch
Putchar is a standard c library function. Putch is a function for windows os based systems.
Putchar sends the character to the standard output console. Putch sends character to windows console without buffering.
People also ask
What is the use of putchar ()?
putchar in c is a function present in standard input output header which is used to write a single character to the standard output screen.
What is the difference between getchar and putchar?
Putchar function writes a character to output console. Getchar function reads a character from input device.
Putchar function performs standard output operation. Getchar in c performs a standard input operation.
Putchar function returns the written charater as an integer value. Getchar function returns the read value from input console.
Both functions performs single character IO.
What is the return value of Putchar?
On success putchar returns written character as int type. Returns EOF
if encounters error and sets errorno to encountered errors.
Look at the errors listed below:
EACCES
EBADF
EFBIG
EINTR
EIO
ENOMEM
ENOSPC
ENXIO
EPIPE
Is Putchar faster than printf?
putchar in c is much faster than printf. Because printf prints string and putchar prints a single character. Simply putchar prints a character we can easily think that it takes less time to execute. Printf parses the format string, based on the format string it has to make specific modifications on the outputs. Hence putchar is faster than printf.
What is the difference between Putchar and puts?
Putchar writes a single character and puts writes a string. putchar returns the written character and puts function returns int which the no of characters writen into console.