Printing ASCII letters, 10 is printed always at the end - c

I'm new to C and ASCII numbers, so I was using a code sample to print ASCII numbers
int main(){
int c;
while ((c = getchar()) != EOF){
printf("%d\n", c);
}
}
Output for "d" was
100
10
Whatever letter I tend to type, the result will be the ASCII code of said letter and 10 at the end as well. I'm not sure where this 10 is coming from.

That's the code for the newline character that ended the input line.
if you want to stop at the end of the line and not include it, add another check.
while ((c = getchar()) != EOF && c != '\n'){
printf("%d\n", c);
}

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 code only prints after ^Z

I'm reading K&R's book on C and i got to this part where the output would be the number of newlines that you input.I wanted to make it so that it prints out each number corresponding to the amount of newlines typed as the lines are being read.This only outputs the value of nl after F6 or CTRL+Z has been pressed(EOF).Could someone explain to me why?
int main(){
int c, nl = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
++nl;
printf("%d\n", nl);
}
You forgot some brackets. Here's what your code does currently:
int main(){
int c, nl = 0;
while ((c = getchar()) != EOF){
if (c == '\n'){
++nl;
}
}
printf("%d\n", nl);
}
Here's what you probably wanted to do based of indentation:
int main(){
int c, nl = 0;
while ((c = getchar()) != EOF){
if (c == '\n'){
++nl;
printf("%d\n", nl);
}
}
}
In C, whitespace is mostly ignored. If you want to run multiple statements together in a block, you need to surround that code with brackets {}
The while loop ends only when the character is an EOF character. EOF is a special character that represents the end of the file the program is reading. Since you are reading from the console, the console itself is the file you are reading from but it as no end. However in your system you can send an EOF character to the console by typing F6 or CTRL+Z
Instead if you want to print the number of lines while typing you should change your code like this:
int main(){
int c, nl = 0;
while ((c = getchar()) != EOF){
if (c == '\n'){
++nl;
printf("%d\n", nl);
}
}
}

c = getchar() != EOF; prints 0 or 1

When i run this code it prints the dos 0 character(space) once, puts a space and prints dos 1 character(smiley face with white eyes and mouth).
int c = 0, b = 1;
printf("%c %c", c, b);
But when i run this code below the result is being printed twice. I either get 2 spaces or 2 smiley faces.
while(c != -1)
{
c = getchar() != EOF;
putchar(c);
}
Edit: My code doesnt have paranthesis so it compares every single entered character with EOF and assigns the result (0 or 1) to c and then prints c as a char. In order to prevent this, we can use paranthesis to force the program to do c = getchar() first and then compare that value with EOF.
while((c = getchar()) != EOF)
Your problem is that a '\n' character is not equal to EOF. The '\n' is being read in as a second character.

K&R C Exercise 1-9 *almost* solved

K&R C Exercise 1-9 states:
Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.
I have nearly solved this exercise, but the code I've written (see below) always prints an extra space before the first nonspace character. So input that looks like this
X(space)(space)X(space)(space)X(space)(space)X
results in output that looks like this
(space)X(space)X(space)X(space)X
#include <stdio.h>
int main()
{
int c; //current input character
int s; //consecutive input space counter
c = getchar();
s = 0;
while ((c = getchar()) != EOF){
if (c == ' '){
++s;
if (s == 1) //uses the counter to print only the
putchar(' '); //first space in each string of spaces
}
else {
putchar(c);
if (s != 0) //resets the space counter when it
s = 0; //encounters a non-space input character
}
}
return 0;
}
Why does my code always print a leading space when I run it?
How can I modify this code to print the first input character first instead of a leading space?
Do not throw away the first char. #David Hoelzer
// Commented out
//c = getchar();
s = 0;
while ((c = getchar()) != EOF){
Also note unbalanced } near return 0;

Resources