not able to understand the role of getchar and putchar here - c

#include <stdio.h>
#include <stdlib.h>
int main()
{
int c;
c = getchar();
while (c != EOF) {
putchar(c);
}
return 0;
}
when I compile and give input ABC and then press enter, the never ending loop starts like AAAAAAAAA....
And now look at this code below
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c;
c = getchar();
while (c != EOF) {
putchar(c);
c = getchar (); // added this single line
}
return 0;
}
In this program, when I input ABC, the output is ABC.
Can anyone please explain why it is not showing just a single A as output?

Look at the below code you mentioned
int main(void){
int c;
c = getchar();
while (c != EOF) {
putchar(c);
}
return 0;
}
When c = getchar(); executes & if you provided input as ABC at runtime & press ENTER(\n), that time c holds first character A.
Next come to loop, your condition is c!=EOF i.e A!=EOF which always true & it will print A infinitely because you are not asking second time input so c holds A.
correct version of above code is
int main(void){
int c;
while ( (c = getchar())!=EOF) { /* to stop press ctrl+d */
putchar(c);
}
return 0;
}
case 2 :- Now looks at second code
int main(void){
int c;
c = getchar();
while (c != EOF) { /*condition is true */
putchar(c);
c = getchar ();/*After printing ABC, it will wait for second input like DEF, unlike case-1 */
}
return 0;
}
Can anyone please explain why it is not showing just a single A as output ? Why it should prints only A, it prints whatever input you given like ABC & so on. Just note that getchar() works with buffered input i.e when you press ENTER getchar() will read upto that & when there is nothing left to read getchar() returns EOF.

Related

Simple C code contains ((c = getchar()) != EOF)

I run the following simple C code.
int main()
{
int c;
while ((c = getchar()) != EOF)
{
putchar(c);
printf("%d\n", c);
}
return 0;
}
The output of code when I enter character A as input from keyboard is as follow:
>A
>A65
>
>10
>
Why does this code print the number 10 after each inner while loop?
Think about what you're doing when the program is asking for input.
Are you just hitting A?
No, you're hitting AEnter, and hitting the Enter key results in a newline, so your while loop is actually doing this:
1. Enter loop
2. getchar() waits for input from stdin
2. You enter 'A', hit 'Enter'
3. c is assigned '65' (integral value of 'A')
4. putchar(c) and then printf("%d\n"), so you see 'A65'
5. getchar() immediately gets next character from stdin, which is linefeed
6. c is assigned '10' (integral value of '\n')
7. putchar(c) and printf("%d\n"), so you see a newline followed by 10.
You can make your code ignore the linefeeds/non-printable characters if you want (this depends on your locale), using isprint:
#include <stdio.h>
#include <ctype.h>
int main() {
for(int c; (c = getchar()) != EOF;) {
if (!isprint(c)) {
printf("Ignoring character with value %d\n", c);
} else {
putchar(c);
printf("%d\n", c);
}
}
return 0;
}
And if I run this:
root#6f67da78fe9a:~# gcc -o chartest source.c
root#6f67da78fe9a:~# ./chartest
A
A65
Ignoring character with value 10

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 character is returned with putchar(c), however, if I print that character I get a code back? example:
0 = 48010
1 = 49110
2 = 50210
etc...
what are these numbers? is this where the character is stored in memory or something?
cheers
So I believe I figured out the problem by introducing newlines
int main(void) {
int c;
c = getchar();
while (c != EOF) {
putchar(c);
printf("\n");
c = getchar();
printf("%d\n", c);
}
}
When I enter 1 this returns the following in the terminal window.
49
1
10
What's happening is I'm returning the ASII character code for 1 - > 49
returning the value I entered with getchar()
and returning 10 which is the linefeed value i.e. the enter command.
Right? without the newlines it was just concatenating them all, making it confusing.

C: Print on previous line after newline check

I am a beginner in C and I would like to know if there is a way where I can print my character count on the same line as a putchar() function without going to a newline.
#include <stdio.h>
int main(void) {
int c, i = 0;
while ((c = getchar()) != EOF) {
i++;
if(putchar(c) == '\n'){
printf(":%d\n", i - 1);
i = 0;
}
}
return 0;
}
For example if run this I get:
Input
This is the first line.
Output
This is the first line.
:23
Is there a way to which I can have the output look like this?
This is the first line.:23
Simple: since putchar outputs at the very point it is called, just don't output yet:
if (c == '\n') {
// omit newline here so that no empty lines are printed
printf(":%d", i - 1);
i = 0;
}
putchar(c);

Character Count in C

The folllowing C Program is to calculate the character Count .
#include <stdio.h >
int main()
{
int nc = 0;
while (getchar() != EOF)
{
++nc;
printf("%d\n", nc);
}
return 0;
}
When I Enter a character , for example 'y' in the terminal , The output returns as follows
1
2
How does this calculation happens and why 2 is in the output?
I suppose you didn't know but when you press enter you just insert a newline character or '\n'. If you want to get the correct result ignore the newline character or just decrease the nc by one.
#include <stdio.h>
int main()
{
int nc = 0;
while (getchar() != EOF)
{
++nc;
printf("Character count is:%d\n", nc - 1);
}
return 0;
}
Even better code:
#include <stdio.h>
int main()
{
int nc = 0;
for(;;)
{
do
++nc;
while (getchar() != '\n');
printf("Character count is:%d\n", nc - 1);
nc = 0;
}
}
The updated code will reset your counter back to 0.
You ENTERed "a character". A y and a new line character. That's 2.
Because you entered two character. One is y and another one is \n(newline) character.
Hence you get the output 1 and 2.
If you want to count only the visible bytes, you can use the isprint function, which returns whether a byte is printable or the space character. It goes like this:
#include <ctype.h>
#include <stdio.h>
int main()
{
int nc = 0;
int ch;
while((ch = getchar()) != EOF)
{
if (isprint(ch) && ch != ' ')
++nc;
printf("Character count after reading '%c' is %d.\n",ch, nc);
}
return 0;
}
Note that since in C, a char is not a Unicode character but often just a byte, this program counts some characters as 2 or more bytes, for example emojis, Cyrillic letters, Chinese ideographs.
When you hit enter it's considered a character

Counting input characters in C

I have just started learning C, been reading a C textbook by Keringhan and Ritchie. There was this example in the textbook, counting characters from user input. Here's the code:
#include <stdio.h>
main()
{
long nc;
nc = 0;
while(getchar() != EOF) {
if (getchar() != 'q')
++nc;
else
break;
}
printf("%ld\n", nc);
}
The problem is, when I execute the code, if I input only one character per line, when I input "q" to break, it doesn't do so. I have to type some word per line, only after that it will break the loop. Also, it only counts the half of the characters of the word. I.e. if I input
a
b
russia
it will only print '5' as final result.
Could you please explain to me why is this happening?
This works, but only when you finish off with an Enter. So, this will count the characters until the first "q" appears. That is just how getchar() and getc(stdin) work.
#include <stdio.h>
int main() {
char c = 0;
long count = 0;
short int count_linebreak = 1; // or 0
while((c = getchar()) != EOF) {
if(c != 'q' && (count_linebreak || (!count_linebreak && c != '\n'))) {
++count;
}else if(c == 'q') {
printf("Quit\n");
break;
}
}
printf("Count: %ld\n",count);
return 0;
}
A StackOverflow question about reading stdin before enter
C read stdin buffer before it is submit

Resources