Questions tagged [putchar]

Anything related to C or C++ standard library functions `putchar` (C) or `std::putchar` (C++). These functions are used to write a single character to the standard output stream `stdout`.

Anything related to C or C++ standard library functions putchar (defined in <stdio.h> C standard header) or std::putchar (defined in <cstdio> C++ standard header). These functions are used to write a single character to the standard output stream stdout.

See CPPreference.com:

175 questions
42
votes
9 answers

I'm trying to understand getchar() != EOF

I'm reading The C Programming Language and have understood everything so far. However when I came across the getchar() and putchar(), I failed to understand what is their use, and more specifically, what the following code does. main() { int c; …
Paolo Bernasconi
  • 2,010
  • 11
  • 35
  • 54
23
votes
3 answers

Why putchar, toupper, tolower, etc. take a int instead of a char?

In C, strings are arrays of char (char *) and characters are usually stored in char. I noticed that some functions from the libC are taking as argument integers instead of a char. For instance, let's take the functions toupper() and tolower() that…
Maxime Chéramy
  • 17,761
  • 8
  • 54
  • 75
18
votes
5 answers

putchar() vs printf() - Is there a difference?

I am currently in chapter 1.5.1 File copying and made a program like so: #include /* copy input to output; 1st version */ main() { int c; c = getchar(); while (c != EOF) { putchar(c); c = getchar(); …
user3649506
12
votes
3 answers

Printing the value of EOF

In Kernighan and Ritchie (the C programming language): 'Write a program to print the value of EOF' I wrote: #include main(){ int c; c = getchar(); if ((c = getchar()) == EOF) putchar(c); } but it doesn't output…
bigTree
  • 2,103
  • 6
  • 29
  • 45
10
votes
4 answers

putchar() weird output, why is this happening?

If I type the words "Hello World" into the standard input stream, this program will print out weird box symbols instead of the expected "Hello World" back into standard output. #include int main(void) { // print out all characters…
zxgear
  • 1,168
  • 14
  • 30
9
votes
4 answers

How will you print any character, string or value of a variable without library functions in C?

If for example I should not use standard library functions like printf(), putchar() then how can I print a character to the screen? Is there an easy way of doing it. I dont know much about system calls and if I have to use them then how do I use…
Manoj Doubts
  • 13,579
  • 15
  • 42
  • 45
9
votes
3 answers

Theory Behind getchar() and putchar() Functions

I'm working through "The C Programming Language" by K&R and example 1.5 has stumped me: #include /* copy input to output; 1st version */ int main(int argc, char *argv[]) { int c; while ((c = getchar()) != EOF) …
Raeven
  • 613
  • 2
  • 8
  • 18
8
votes
8 answers

The C Programming Language, Ch.1 Exercise 1.10 (Getchar and Putchar)

I've been working on this for 2 hours and I am stuck... I found the answer online, but that isn't going to help me learn the concept that I'm obviously missing. Prompt: Write a program to copy its input to its output, replacing each tab by \t , each…
user3505236
  • 83
  • 1
  • 6
8
votes
4 answers

what does putchar('0' + num); do?

I am trying to understand how the putchar('0' + r); works. Below, the function takes an integer and transform it to binary. void to_binary(unsigned long n) { int r; r = n % 2; if (n >= 2) to_binary(n / 2); putchar('0' + r); } I…
yaylitzis
  • 5,354
  • 17
  • 62
  • 107
7
votes
2 answers

Is getchar() equivalent to scanf("%c") and putchar() equivalent to printf("%c")?

Is a = getchar() equivalent to scanf("%c",&a);? Is putchar(a) equivalent to printf("%c",a); where a is a char variable?
Gautam Kumar
  • 1,162
  • 3
  • 14
  • 29
7
votes
9 answers

Putchar and Getchar in C

I'm reading K&R's The C Programming Language and have become confused on putchar and getchar. I made a program where you enter 10 chars and the program prints them back out to the screen. #include int main() { int i; int ch; …
CS Student
  • 1,613
  • 6
  • 24
  • 40
6
votes
4 answers

getchar() and putchar()

in the example: #include main() { long nc; nc = 0; while (getchar() != EOF) ++nc; printf("%ld\n", nc); } I don't quite understand it. putchar() would put the character out, but why is it that after EOF it puts…
user379229
  • 95
  • 1
  • 5
5
votes
2 answers

Why doesn't putchar() output the copyright symbol while printf() does?

So I want to print the copyright symbol and putchar() just cuts off the the most significant byte of the character which results in an unprintable character. I am using Ubuntu MATE and the encoding I am using is en_US.UTF-8. Now what I know is that…
5
votes
1 answer

getchar() != EOF

I am running the following program from the C Programming Language book: #include main() { int c; while((c=getchar()) != EOF) putchar(); } Or #include int main(){ int c = getchar(); while(c != EOF){ …
Nishi
  • 53
  • 4
5
votes
1 answer

What is the standard input buffer?

#include int main(void) { int c; c = getchar(); putchar(c); c = getchar(); putchar(c); c = getchar(); putchar(c); return 0; } I want to understand why the function that is called three times working with a…
YaR_
  • 53
  • 1
  • 4
1
2 3
11 12