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
1
vote
1 answer

Why is '0' put in this the putchar function when I want a number printed

I am trying to print numbers using the putchar() function. Here's my code below: int main(void) { int x = 0; for (; x <= 9; x++) { putchar(x + '0'); } } _putchar('\n'); } If run without the " + '0'", no result is…
SJP
  • 31
  • 6
1
vote
2 answers

Confused about "while(getchar() != '\n')"

I knew that getchar() is just a function gets the first character of the line the user entered then the next and so on And if we typed getchar() in a line, at the finishing of the code,it's for let the program wait for user to type any thing and for…
Salahuddin
  • 1,617
  • 3
  • 23
  • 37
1
vote
2 answers

how to print line numbers from multiple files without using fgets

I'm trying to print line numbers in the beginning of the lines without using fgets() yes, it prints line number well when I input multiple files but I want to get result like this. Can you guys help me with this? Now result 1 I'll always remember 2…
1
vote
3 answers

Unexpected input when using getchar(), and unexpected output using putchar()

I am new to c and understand very little of it however as far as i understand it this code should either print the character i enter (it should print it twice), or print the number representation of the bits for my character then my character…
Newbie
  • 31
  • 6
1
vote
5 answers

How to truncate characters over a certain length in C?

I'm working on a C program that takes in an input of lines of text, and return it by printing only 40 characters each. So far, I have this code: #include #include int main() { char input = getchar(); int numChar; …
user14281908
1
vote
2 answers

C Simple Code Involving getchar() and putchar() Unexpected Output

As I was following an example from a book, #include main() { int c; c = getchar(); while (c != EOF) { putchar(c) c = getchar(); } } I thought it would make more sense to…
raincouver
  • 61
  • 7
1
vote
3 answers

putchar() output is a question mark instead entered input stream

I'm trying to output an input stream with alphabet characters in a do-while loop and when the user does EOF(ctrl+d) the loop stops and the output should be the input stream, but the output is just a question mark in a box... What is wrong…
phos
  • 19
  • 4
1
vote
1 answer

Distinguish EOF from error for the Getchar () function

Rewrite the program to distinguish EOF from error for the Getchar () function. In other words, getchar () returns both during the error and at the end of the EOF file, you need to distinguish this, the input should not be via FILE STREAM and handle…
Qualcomm
  • 52
  • 4
1
vote
1 answer

How does putchar() interract with the operating system internally?

Anywhere in the C language, without any libraries, there is no way to print text to the console. printf() could just use language functionality to format a string, and then puts() it, which could work like putchar() in a loop. putchar() is where the…
user13831085
1
vote
2 answers

C- K&R exercise 1-9

looking for exercise 1-9 from the K&R book (Copy input to output. Replace each string of multiple spaces with one single space) I found this code on this site. #include main() { int ch, lch; for(lch = 0; (ch = getchar()) != EOF;…
user13059000
1
vote
3 answers

getchar() != ' ' and getchar() = ' '

I am having trouble understanding what getchar() != ' ' and getchar() = ' ' are doing in my code. Why do there need to be opposites. The user may input extra spaces between the first name and last, and before the first name and after the last…
Nick
  • 25
  • 3
1
vote
3 answers

Very simple C question using getchar() and putchar()

Hello I am teaching myself C and going through the K & R book and I am having some trouble (I am running OS X). This is from section 1.5.1 "File Copying" which is supposed to take a character as input, then output the character. Here is the…
Guillermo Alvarez
  • 1,695
  • 2
  • 18
  • 23
1
vote
1 answer

Multi-character warning while get a char from keyboard in c

According to the operation entered from the keyboard, I want to do 5 operations with the switch structure, but gives an error. I tried also getchar & putchar functions... int main() { char proc; int…
Emre
  • 35
  • 5
1
vote
2 answers

I used the function "putchar()" to print the result of a test, but i got a question mark instead

So like what the title suggest i have tried to use putchar() to print a result of a not equal to test != but the output i got is a question mark. Here is the code: #include main() { int c; c = getchar() != EOF; …
mrnamya98
  • 13
  • 3
1
vote
1 answer

Why is putchar not returning a new line when entered with getchar?

Compiling with Clang/GCC and running on Linux: When running the following code I encountered what was to me unanticipated behavior: when entering a single character ("X") I will be prompted to enter another character ("Y") presumably because the…
NewStack
  • 39
  • 4