C putchar() function
C library function - putchar()
The putchar() function is used to write a single character to the standard output stream, writes a single character to the standard output stream, stdout.
Syntax:
int putchar(int char)
Parameters:
Name | Description | Required /Optional |
---|---|---|
char | A single character write to o the standard output. | Required |
Return value
- This function returns the character written as an unsigned char cast to an int or EOF on error.
Example 1: putchar() function
#include <stdio.h>
int main () {
char C;
for(C = 'A' ; C <= 'z' ; C++) {
putchar(C);
}
return 0;
}
Output:
ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz
Example 2: putchar() function
Using the getchar() function, the following program reads characters into an array and prints them out using the putchar function once an end-of-file character is encountered.
#include <stdio.h>
int main(void)
{
char text[500];
int C, i, n = 0;
printf("Input some characters:");
printf("\nTo terminate press Ctrl+D on Unix/Linux terminals and Ctrl+Z in Windows console windows:\n");
while ((C = getchar()) != EOF && n < 1000)
text[n++] = C;
printf("Write the said characters to the standard output:\n");
for (i = 0; i < n; ++i)
putchar(text[i]);
putchar('\n');
return 0;
}
Output:
Input some characters: To terminate press Ctrl+D on Unix/Linux terminals and Ctrl+Z in Windows console windows: C programming C Exercises C tutorial ^Z Write the said characters to the standard output: C programming C Exercises C tutorial
C Programming Code Editor:
Contribute your code and comments through Disqus.
Previous C Programming: C putc()
Next C Programming: C puts()
C Programming: Tips of the Day
Reading a string with scanf :
An array "decays" into a pointer to its first element, so scanf("%s", string) is equivalent to scanf("%s", &string[0]). On the other hand, scanf("%s", &string) passes a pointer-to-char[256], but it points to the same place.
Then scanf, when processing the tail of its argument list, will try to pull out a char *. That's the Right Thing when you've passed in string or &string[0], but when you've passed in &string you're depending on something that the language standard doesn't guarantee, namely that the pointers &string and &string[0] -- pointers to objects of different types and sizes that start at the same place -- are represented the same way.
Ref : https://bit.ly/3pdEk6f
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises