putchar() function in C language with Example

Here, we are going to learn about the putchar() function of library function stdio.h in C language with its syntax, example.
Submitted by Souvik Saha, on February 22, 2019

putchar() function in C

The putchar() function is defined in the <stdio.h> header file.

Prototype:

    int putchar(const char *string);

Parameters: const char *string

Return type: int

Use of function:

In the file handling, through the putchar() function, we take the character to the stream stdout and store them into the specified string array. The prototype of the function putchar() is int putchar(const char *string);

The character which is read is an unsigned char which is converted to an integer value. In the case of file handling, it returns EOF when end-of-file is encountered. If there is an error then it also returns EOF.

putchar() example in C

#include <stdio.h>
#include <stdlib.h>

int main()
{
    //Initialize the character array
    char str[100];
    int i = 0, j = 0;

    printf("Enter the string into the file\n");
    //takes all the characters until enter is pressed
    while ((str[i] = getchar()) != '\n') {
        //increment the index of the character array
        i++;
    }
    //after taking all the character add
    //null pointer at the end of the string
    str[i] = '\0';
    printf("\nThe file content is - ");

    //loop is break when null pointer is encountered
    while (str[j] != '\0') {
        //print the characters
        putchar(str[j]);
        j++;
    }

    return 0;
}

Output

putchar() example in C language

C stdio.h Library Functions Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.