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.
Related
When I run the program below, output give second 1. I debug the program, but I couldn't understand why compiler writing second 1 anytime?
output like this.
#include <stdio.h>
main()
{
int c;
while (c = getchar() != EOF)
printf("%d\n", c);
printf("%d - at EOF\n", c);
}
The condition in the while statement
while (c = getchar() != EOF)
is equivalent to
while (c = ( getchar() != EOF) )
due to the operator precedence. And this logical expression getchar() != EOF always yields 1 if the input was not interrupted.
You need to write
while ( ( c = getchar() ) != EOF)
The Enter key you press to "send" the input from the terminal to the program is added in the input buffer as a newline '\n', which will be read in the second iteration of the loop.
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);
}
When i run the line counting code in the C programming language book, i do not get any output.ie nothing is returned that reflects the number of lines. The code is below:
#include <iostream>
//This program counts lines in its input
//
int main() {
int c, nl;
nl = 0;
while ((c = getchar())!= EOF )
if (c == '\n')
++nl; //nl = nl +1
printf("%d\n", nl);
}
The output is below:
/home/xxx/xxxx/lineCounting1/cmake-build-debug/lineCounting1
line1
line2
line3
line4
^D
Process finished with exit code 0
I use ctrl+D to stop the execution and get an output. But nothing (except ^D is returned). What am i doing wrong?
this is a C program so we have to include stdio.h instead of iostream.
in the main we have declared 2 variables
data type of variable name "c" should be "char" and nl is a counter which is currently set to 0.
getchar() is a library function which takes only the first character of of the string.
we are assigning the value of getchar to the c. and traversing it till end of file (EOF).
if condition is true then we are entering into the while.
there is a if condition inside the while which states that if(c == '\n') then incrementing the counter (nl) and displays the count.
code will be look like this but still it won't work because it is entering into the infinite loop
we have to assign something to the variable c.
`#include<stdio.h>
//This program counts lines in its input
int main()
{
char c;
int nl;
nl = 0;
printf("Enter the character\n");
while((c = getchar())!= EOF )
{
if (c == '\n')
{
++nl; //nl = nl +1
printf("%d\n", nl);
}
}
}`
Here is the function definition of get_line which:-
skip whitespaces in the begining.
stop at first white space.
stop at first newline character and place it in the array.
leave behind character if it does not have space available.
int get_line (char* ch, int n)
{
if ( n <= 0)
return 0;
char c;
while ( isspace(c = getchar()));
int i = 0;
do {
ch[i++] = c;
if ( i == n)
break;
}while ( ! isspace(c = getchar()));
if ( c == '\n')
ch[i++] = '\n';
ch[i] = '\0';
return i;
}
int main()
{
char array[5];
get_line(array, 4);
printf("%s", array);
char c;
while ( (c = getchar()) != '\n'){
printf("\n%c", c);
}
return 0;
}
But when i enter more characters then the size and try to print the remaining character in main using the last while loop, it prints weird characters and not printing the remaining characters in the stream as required by the fourth specification of the function. Please tell me why this is happening?
It looks like you ran afoul of operator precedence.
In this statement:
while ( c = getchar() != '\n'){
The comparison operator != has higher precedence than the assignment operator =. So getchar() != '\n' is evaluated first and will be either 1 or 0 since it is a boolean expression.
Assuming the next character is not a newline, the value will be 1. Then c = 1 is evaluated and the loop continues by printing the value 1 as a char. When a newline is found, getchar() != '\n' evaluates to 0, then c = 0 is evaluated and the loop exits.
You need to add parenthesis here to get the intended order of operations:
while ( (c = getchar()) != '\n'){
WhozCraig pointed out that your problem is the operator precedence. This is what happens:
while ( c = getchar() != '\n'){
printf("\n%c", c);
}
In the condition expression of your while statement, first the experssion getchar() != '\n' is evaluated. Then the result of this is assigned to c. The expression yields the result of the comparission of the call to getchar(), which retrieves the next character, with the constant character value '\n'.
Depending on how the comparisson operator != is implemented, you can get weird characters. Note that Boolean 'true' in C is defined as non-zero and Boolean 'false' as zero. For example, it would be perfectly legal for the compiler to subtract the byte value of \n from the 'getchar() result and test for non-zero. In assembler this would look something like:
call getchar
sub ax, 0x0d ' getchar result in ax; subtract \n from it
mov c, ax ' move result of subtraction to variable c
jz end_loop ' end loop if zero (= equal)
This question already has answers here:
`getchar()` gives the same output as the input string
(5 answers)
Closed 9 years ago.
I wrote a program to read from input character by character and print it to output and here is my code:
#include <stdio.h>
main()
{
int c;
while((c = getchar()) != EOF)
{
printf("%s\n", "log1");
printf("%c\n", c);
printf("%s\n", "log2");
}
}
and this is the result:
a(my input)
log1
a
log2
log1
log2
but it should have this result:
a
log1
a
log2
what's wrong with this program?
you giving input a and newline
a(my input) You are giving a and newline
//this is because of a
log1
a
log2
//this is because of newline
log1
log2
Check for newline and avoid printing Newline.
while((c = getchar()) != EOF)
{
if(c!='\n')
{
printf("%s\n", "log1");
printf("%c\n", c);
printf("%s\n", "log2");
}
}
This is because you while((c = getchar()) != EOF) ends after hitting EOF. This is because when you type something and you hit the enter key then everything is stored in an internal buffer.
Your code stops when getchar doesn't find anything in that buffer.
You may also check out this:- Where does getchar() store the user input?
getchar is running during the second iteration. The problem is that your input was actually "a[enter]", so the second character that getchar read was a newline character, and it printed that.
If you give input of "abc", the things may seem more clear.
while(getchar() != '\n');
bear in mind that the expression in the while loop is executed everytime - so even when the character is '\n' is found, it has already been removed from the stream by the getchar() call.
Place a condition not to print \n (on pressing Enter
while((c = getchar()) != EOF)
{
if(c != '\n')
printf("%s\n", "log1");
printf("%c\n", c);
printf("%s\n", "log2");
}