178 questions
1
vote
0
answers
73
views
C program in CLion not producing expected output, possibly related to I/O buffering
I'm learning C and wrote a simple program in CLion that echoes '1' for every character read until EOF. However, the actual output doesn't match expectations.
Code:
#include <stdio.h>
int main() ...
4
votes
5
answers
171
views
getting a stream of -1 when I pass EOF in an infinite getchar printf loop
I have just started the K&R book. It introduces a loop using getchar() and putchar() to copy characters from input to output until EOF is reached:
main()
{
int c;
while ((c = getchar()) !=...
0
votes
2
answers
139
views
The mechanism of putchar()
#include <stdio.h>
int main(){
int c;
while((c = getchar()) != EOF)){
putchar(c);
}
}
This is the code from R&K's first charpter. I am confused by the behavior of putchar(): ...
0
votes
0
answers
66
views
Why does my printf function not work? Is my while loop not ending? [duplicate]
I am just trying to copy and paste the text input. After which the code must show done.
#include <stdio.h>
/*code to copy all input characters and paste it one by one*/
int main()
{
int c;
...
-1
votes
1
answer
902
views
Suspect code indent for conditional statements
currently I am working on C project and i faced the following error -
8-print_base16.c:11: WARNING: suspect code indent for conditional statements (8, 15)
related with Betty coding style.
This issue ...
1
vote
1
answer
146
views
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'...
0
votes
0
answers
120
views
How can I use putchar to print numbers from 0 to 20 (or more)?
am looking on how to print digits for 0 to 20 and more with puchar
i tried using putchar('0' + 48) is just printing from 0 to 9, finding it hard to do that is there any help please, i have also tried ...
0
votes
1
answer
82
views
What is the difference between these loops?
I am a beginner of C programming.
I am learning C by "The C programming language second edition"(by Brain W.Kerninghan and Dennis M.Ritchie)
In the book I am stack with the exercise 1-10.
&...
2
votes
5
answers
383
views
Why doesn't putchar(1+'0') output 10?
Why does putchar outputs '1' for putchar(1+'0') but not '10' but when only a character argument is passed, like putchar('0'), it outputs it.
With putchar(1+'0'), I expected an output of 10.
4
votes
2
answers
4k
views
What's the difference between putch() and putchar()?
Okay so, I'm pretty new to C.
I've been trying to figure out what exactly is the difference between putch() and putchar()?
I tried googling my answers but all I got was the same copy-pasted-like ...
0
votes
1
answer
66
views
How is the output 15 here? can someone explain it to me? I didn't really understand the use of putc and stdout
int *z;
char *c = "123456789";
z = c;
putc(*(z++),stdout);
putc(*z, stdout);
return 0;
The output is 15 thats for certain but how does this happen.
-3
votes
1
answer
59
views
Im a beginner to C and getting an error with my code: assignment to char from char * makes integer from pointer without a cast. How could I fix this?
Im trying to write a code that gets vowels from a string and replaces them with a "*" before outputting the changed string using the functions of getchar() and putchar().
char input_char;
...
-3
votes
1
answer
312
views
What's the return type and argument of putchar()
I see that return type of putchar() is int,
int putchar(int char)
but
for(i=65;i<91;i++)
putchar(i);
gives output ABC...... How?Shouldn't it return int?
Plus, it turns out that I can use ...
0
votes
1
answer
67
views
Why doesn't my program wait for another input?
I've written a small program to practice getting user input using getchar() and outputting it using putchar() in C.
What I want my program to do:
I want the program to ask the user to enter a char, ...
1
vote
2
answers
200
views
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 ...
0
votes
1
answer
1k
views
How can I store and print a character input?
I am familiar with storing and printing characters using getchar(); and putchar();.
However, when I use it with my entire code, it does not seem to work. In the command window, it will take the ...
0
votes
2
answers
508
views
Questions regarding getchar and putchar in C (K&R)
so I'm learning C by following the book, 'The C Programming Language 2nd Edition' by Dennis Ritchie and Brian Kernighan. In section 1.5.1 File Copying, the following program is shown:
#include <...
0
votes
1
answer
526
views
Do getchar() and putchar() read only one character each time they're called or do they read a stream of characters?
There is something vague about the functionality of getchar(), putchar() in while loop for me.
In the following program that copies its input to its output:
#include <stdio.h>
main()
{
int ...
1
vote
3
answers
287
views
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 ...
0
votes
2
answers
265
views
Why putchar() and getchar() are accepting more than one character when using while loop..? [duplicate]
Here, I have understood why putchar() is printing only the first character that is 'C'
#include <stdio.h>
main(){
int c;
c = getchar();
putchar(c);
}
output : > Cprograming
...
2
votes
5
answers
2k
views
Printing a string of names in different lines
I have recently started learning C, and I made this small piece of code, but it's not really working the way I wanted:
#include <stdio.h>
#include <string.h>
int main()
{
int i;
...
2
votes
3
answers
4k
views
puts(), gets(), getchar(), putchar() function simultaneously use in the program
I have a confusion related to using puts(), gets(), putchar() and getchar() simultaneously use in the code.
When I have run the below code, it is doing all steps:
taking the input, printing the output,...
0
votes
1
answer
458
views
Problem printing out array using putchar function. Getting a punch of symbols as output
#include <stdio.h>
void print_rev(char *s);
int main(void)
{
char *str;
str = "I do not fear computers. I fear the lack of them - Isaac Asimov";
print_rev(str);
...
1
vote
5
answers
1k
views
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 <stdio.h>
#include <stdlib.h>
int ...
0
votes
1
answer
86
views
What are the numbers associated with printing getchar()?
Code:
int main(void) {
int c;
c = getchar();
while (c != EOF) {
putchar(c);
c = getchar();
printf("%d", c);
}
}
I enter a character and that ...
1
vote
2
answers
97
views
C Simple Code Involving getchar() and putchar() Unexpected Output
As I was following an example from a book,
#include <stdio.h>
main()
{
int c;
c = getchar();
while (c != EOF) {
putchar(c)
c = ...
-1
votes
2
answers
477
views
Replace each string of one or more blanks with a single blank
This was already asked, but I made my own program and I don't know why it doesn't work.
int c;
char blank;
while ((c = getchar()) != EOF) {
if (c == ' ') {
putchar(c);
while ((c = ...
1
vote
3
answers
316
views
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 ...
0
votes
1
answer
124
views
Why is putchar() writing a new line in this simple code?
I do not understand why excessive white space is being written to stdout when I run this program.
int main(int argc, char **argv)
{
argv++;
for (int i = 0; i < argc - 1; i++)
{
...
-5
votes
3
answers
3k
views
How to print a symbol `n` times using a `for` loop, in C?
Is it possible to print '#' n times using a for loop in C?
I am used to print(n * '#') in Python.
Is there an equivalent for that in C?
for(i=0; i < h; i++) {
printf("%s\n", i * h);
}
2
votes
1
answer
89
views
I am trying to under eof and getchar ()
In the piece of code given below. Whenever I am inputing a set of characters with a ctrl+z at the end, which should mark EOF for getchar(). It is printing all the characters along with another ...
0
votes
1
answer
658
views
Confusion in the use of putchar() function in C
I am learning C form the book, 'The C programming language'. In that when it was introducing putchar() function using the code as follows:
#include<stdio.h>
/* program to copy its input to its
...
-1
votes
1
answer
231
views
strlen of char array & putchar
I am compiling and running this code:
// hello, world!, without printf
#include <stdio.h>
#include <string.h>
char a[7] = "hello, ";
char b[7] = "world!\n";
void ...
1
vote
1
answer
303
views
Distinguish EOF from error for the Getchar () function [duplicate]
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, ...
1
vote
1
answer
457
views
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 ...
0
votes
0
answers
362
views
How does getchar() in c handle string input
First of all, this is not a problem nor a bug. I just wanted to know how string input from stdin is handled in getchar(). Also, is the same method used for putchar() but just in reverse?
These two ...
0
votes
2
answers
854
views
How can I generate an BACKSPACE in a visual studio debug console?
I'm writing a Windows console mode C program using Visual Studio 2019 community. I want each backspace seen in the input stream to be printed as the literal string "\b" in the output.
How do you ...
0
votes
1
answer
77
views
How do I check if the previous 2 getchars were a space?
My code reads a textfile or input from the terminal and
prints out the message but with each word on a new line
the code below works with the exception of double or more spaces.
Because I use the ...
-6
votes
1
answer
265
views
why the program is not terminating in gcc? [closed]
Why the program listed below is not terminating. I am using gcc compiler in linux.
#include <stdio.h>
int main(void)
{
int c;
printf("Enter characters: ");
while((c = getchar()) !...
1
vote
2
answers
124
views
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 <stdio.h>
main()
{
...
1
vote
3
answers
235
views
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, ...
-1
votes
2
answers
44
views
Nonsensical output stream during the usage of "getchar()" and "EOF"
I have been experimenting with getchar(), putchar() and have been trying to use EOF. Below is the snippet of code I have been experimenting with.
#include <stdio.h>
int main(void)
{
int c;
...
0
votes
2
answers
639
views
Program keeps printing a zero byte?
I am trying to create a program that removes the vowels from a sentence. However, my program keeps failing because it keeps printing zero byte in the string. Would anyone be able to show me where I am ...
0
votes
2
answers
235
views
Could somebody explain the putchar line in this bitwise function to me?
Does anybody know what the 1u does in this function?
The following function takes an integer and prints out its bits.
The line I am trying to figure out is the putchar line.
I see it takes an ...
1
vote
1
answer
53
views
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()
{
...
1
vote
2
answers
230
views
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 <stdio.h>
main()
{
...
0
votes
1
answer
798
views
how the C read the character and Line break when i use loop?
When i try the loop, i find some doubts.
When i press "Enter" first time, it prints two blank lines, at the second time, it prints two more blank lines and execution following statement.
When i press "...
1
vote
1
answer
625
views
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 ...
0
votes
2
answers
87
views
Why output of number of 'K' are more than the input characters?
Isn't the input and output number of characters be the same?
int ch;
while(ch != '\n')
{
ch = getchar();
putchar('K');
}
-3
votes
2
answers
157
views
How does the putchar and whole of the code gets executed? [closed]
I want the dry run of the code for the first 3 iterations to understand.
THe output of the code is : abcdbcdbcdbcdbc........(infinte times)
I know how for loop works and put char also.I did not dry ...