this post was submitted on 01 May 2026
1 points (100.0% liked)

techsupport

3135 readers
6 users here now

The Lemmy community will help you with your tech problems and questions about anything here. Do not be shy, we will try to help you.

If something works or if you find a solution to your problem let us know it will be greatly apreciated.

Rules: instance rules + stay on topic

Partnered communities:

You Should Know

Reddit

Software gore

Recommendations

founded 2 years ago
MODERATORS
 

Goal: a program that takes a char as input and outputs which char has been inputted and the humber of line(s). The below code makes the program output the inputted char twice: once - I assume - as a result of how getchar() works and once as a result of printf(). Is there any way to do this more elegantly, as in, not displaying the inputted char twice? If possible, keep to while, without suggesting for.

PS: the repetition of getchar() is just my ugly way of removing the trailing '\n'.

#include <stdio.h>  

int main(void)  
{  
        int c, nl;  
        c = 0;  
        nl = 0;  

        while ((c = getchar()) != EOF) {  
                getchar();  
                printf("%c, %d\n", c, nl);  
                ++nl;  
        }  
}  
no comments (yet)
sorted by: hot top controversial new old
there doesn't seem to be anything here