2
#include <stdio.h>

int main()
{
    int c;
    while ( (c = getchar()) != EOF )
    {
        if (c >= 65 && c <= 90)
            c += 32;
        else if (c >= 97 &&c <= 122)
            c -= 32;
        putchar(c);
    }
    return 0;
}

In the code how is the input interpreted, processed and printed?

For example, if we input abcde we get output ABCDE. As we are processing input character by character, we should get the output character by character but we get output once we press enter. Till we press enter where is the output store.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Siddharth
  • 23
  • 4
  • 4
    The terminal will generally buffer text until you press "enter", at which point the terminal sends the input to the program. – Cornstalks Jul 01 '18 at 15:40
  • 5
    Use [`islower()`](https://port70.net/~nsz/c/c11/n1570.html#7.4.1.7), `isupper()`, `tolower()`, `toupper()`. Don't make non-portable assumptions about the character set. – Andrew Henle Jul 01 '18 at 15:41
  • try this: `for i in {1..10}; do echo $i; done | ./yourExecutable`. Do you understand how it can print them one by one? The buffer is simply flushed by the 'for' command each iteration. Now try writing a C program that flushes the output stream every 3 characters and pipe its output to your program. See how that operates. It'll make sense if it hasn't yet –  Jul 01 '18 at 15:43
  • Until you press the enter key, there is no input, so there is no output to be stored anywhere. – Jonathan Leffler Jul 01 '18 at 16:41
  • It is a poorly readable bit of code better written with `if (c >= 'A' && c <= 'Z') ...; else if (c >= 'a' && c <= 'z') ...` (or simply use the `ctype.h` conversions) You are just stuck with `32` or `0x20` unless you want to use `c ^= (1u << 5)` (which is no better for readability). – David C. Rankin Jul 01 '18 at 22:28
  • @JonathanLeffler Where is the input stored before pressing enter.How is it assigned to int c. – Siddharth Jul 03 '18 at 04:58
  • Until you hit enter, the terminal driver stores the data (it isn't a string at this point — it may never be a string, but that's a separate discussion). The terminal driver may edit the data in response to what you type — backspaces, line kill, word erase, etc. When you hit enter, the terminal driver makes the data (it still isn't a string) available to the code in the standard I/O library, which keeps a copy of what was typed and can then arrange for `getchar()` to return one character (byte) at time, each byte converted to `unsigned char` before being converted to `int`. – Jonathan Leffler Jul 03 '18 at 05:03

3 Answers3

0

what are the functions?

  • getchar() is a function that is used to get a single character from
    the console.

putchar() is a function that is used to return the character written as an unsigned char cast to an int.

#include <stdio.h>
int main()
{
    int c;  
    while ( (c=getchar()) != EOF ){ 
        if (c >= 65 && c <= 90)
            c += 32;
        else if (c >= 97 &&c <= 122)
            c -= 32;
        putchar(c);
    }
  return 0;
}

c is int so when you read character from keyboard it stores it as integer not character. checkout ASCII.

so 65 = A, 90 = Z, 97 = a, 122 = z,

Here is what i understood from the code:

  1. create variable c to store our character.
  2. get character from user and store it to c.
  3. while c is not equal to EOF(end of file)
  4. if c is greater than or equal to 65 and c is less than or equal to 90
  5. add 32 to c
  6. or else if c is greater than 97 and c is less than or equal to 122
  7. minus 32 from c
  8. return c value into character and print it.
Nassir
  • 13
  • 1
  • 6
0

At first, I would appreciate that you have asked such a good question that most of the programmer won't even think about. In this article, I tried best to solve the matter.

Queue: Follows the principal First In First Out. The principal is similar to the priority followed while a queue of people waiting to buy new iPhones.

Input stream behaves like a queue. Consider it as a Scooby Doo. Even what It has eaten is enough for it's day it won't stop eating. If someone offers two tonnes of food in the breakfast, Scooby will eat all the breakfast and still asks for the Lunch.

The input stream is similar to Scooby Doo. Look this code!

 char c=getchar();

Here one character is enough for c but when you run this code. You can type as many characters as you want in the console but no matter what when you press enter your enter: c will assign to the first character you had typed.

But notice that checking for EOF is bad practice because of so many reasons I will list the reasons at the end.

When coming to your Question!. Why is the character not printed one by one?

Studying about stream itself is a big area. So just for your convenience think, that input stream will have some hidden file(Queue) to store whatever character you have typed. Typing character in the stream is like a machine gun firing continuously. There is no option you have to wait for the machine gun to stop firing for doing the counter-attack.

Likewise, while you are typing the character in the stream the file will simply push each character into it. Once you have typed enter or the EOF command 'Cntl+D'.

Then your code will look into the file character by character(Remember the principal first IN). If a condition meets it will stop executing there and it won't care about the next the remaining characters in the file.

In your case, it will look all the characters in the file after the user types enter or EOF command and it will change their cases(upper to lower and vice-versa)

As I promised! Why one should not check for EOF!

  • One reason is that the keystroke that creates an end-of-file condition from the keyboard is not standard, but differs between platforms.

  • Another reason is that terminating the program is best done more explicitly (e.g. with a "quit" command).

  • A third reason is that other error conditions might arise, and this check will not detect them.

So try to avoid it.

Zong
  • 6,160
  • 5
  • 32
  • 46
Naveen
  • 38
  • 8
  • `char c=getchar();` -> `int c = getchar();` – chqrlie Jul 01 '18 at 21:43
  • @chqrlie For simple understanding I used char and obviously char is enough for holding EOF command. – Naveen Jul 02 '18 at 07:48
  • precisely not! `char` is inadequate for storing the return value of `getchar()`. `getchar()` returns an `int` that has the value of the next available byte from the stream as an `unsigned char` or the special negative value `EOF`. `char` cannot store all these different values unambiguously. Testing for `EOF` with a `char` value is either ambiguous (if `char` is signed, `\377` cannot be distinguished from `EOF`) or impossible (is `char` is unsigned, it cannot compare equal to `EOF`. – chqrlie Jul 02 '18 at 21:54
0

stdin and stdout are buffered streams. You can set them to be unbuffered, but that can cause a significant performance hit. And, even if you set them to unbuffered, it still may not be effective from a terminal interface because the terminal often does it's own buffering.

See this for more info: Should I set stdout and stdin to be unbuffered in C?

OregonJim
  • 326
  • 2
  • 10