Output for the following C code is confusing me [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
This is my code
#include <stdio.h>
main()
{
int c;
c=getchar();
while(c!=EOF)
{
int x;
x=(c!=EOF);
printf("%d",x);
putchar(c);
c=getchar();
}
}
Output (When I enter A):
A
1A1
Why is it not 1A only. Why is it repeating 1.Like first the program will take the value of c from getchar. Then it will go inside the loop. When condition will be true it will print the value of x and then value of c. Then again it should ask me for an input. Instead it is displaying another 1 then asking for the input. Kindly help.

It is because you entered A and <enter>. The <enter> produces the second 1.
You can press CTRL + D on unix or CTRL + Z on Windows to close the stdin of the program instead of pressing <enter>. This will give you the expected output.
With the <enter>: https://ideone.com/lWJ3Xz
and without: https://ideone.com/QsXiYz

a minimum debug effort and you will know yourself.
printf("FEOF=%d, C=0x%x\n",x,c);

Related

I'm a beginner and I'm creating a C program to print numbers from 0 to n using while loop where n is input from user [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
I'm a beginner and I'm creating a C program to print numbers from 0 to n using while loop where n is input from user.
//program to print numbers 0 to n where n is input from user
#include<stdio.h>
int main()
{
int i=0,num;
printf("Enter number: ");
scanf("%d",&num);
while(i<=num)
{
printf('%d',i);
i++;
}
return 0;
}
Im getting error saying expected const char
I tried to get solution over several websites
since im new to this language I'm facing trouble in such simple code
I tried running this code on several online compilers but everywhere I get the same issue
In line 11 in the printf statement you have used single quotes - '%d' which does is giving you problems here, change it to a "%d". Hope that helps.

Using %c in scanf and assigning the value to an int variable in C? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
int main()
{
int i= 0;
printf("i value is %d\n",i);
scanf("%c", &i); // I am giving an input of 255 here
printf("i after scan %d\n",i); // This prints 50. How???
return 0;
}
Can someone explain how does the printf statement give 50? I have a little-endian machine.
Your program won't even compile as I is undeclared. I am assuming that it is a typo. Since you are scanning %c it will read only one character which is 2 from 255. Now 2 has ascii code of 50 which is being printed.

Why does this loop execute only 4 times? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am beginner in c programming.I just want to know why this loop is not working properly.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
char x[8];
char t;
for (i = 0; i < 8; i++) {
scanf("%c", &t);
x[i] = t;
}
return 0;
}
Because when any input given from keyboard we need to press enter to confirm completion of input. This enter stay in buffer and if next input is char or string, stores enter in string or char var and do not wait to input that char or string. In this case, first input given at execution it stores char in X[0] and enter in x[1] and so on. So executes loop 8 time but it seems to 4 time because it ask input only four times. To check that put one printf in loop
it executes 8 times.
Whenever you press enter to submit, you are entering a whitespace character that is consuming one of your loop iterations.

My c program is not giving any result.pls help everyone [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
i have written a program which is not giving proper result.
main()
{
int i=1,n,s=1;
printf("enter the value of n");
scanf("%d",&n);
while(i<=n)
{
s=s*i;
i++;
if (i==n+1)
{
break;
}
}
printf("factorial of n=",s);
}
it is giving the result as shown in the picture below.
Your problem is in this line:
printf("factorial of n=",s);
This outputs factorial of n=, but it does not simply concatenate the value of s, and there is no placeholder for s, so you actually have too many parameters.
You need a placeholder for the int output:
printf("factorial of n=%d",s);
Without it, your program exits with an error (status 15, when 0 would be normal).
Also, (as Vlad pointed out in his answer) the if (i==n+1) { ... } block is redundant, as your while loop will already exit when i > n.
Write
printf("factorial of n=%d\n",s);
^^
And this code snippet
if (i==n+1)
{
break;
}
is redundant and may be removed.
You could write the loop simpler. For example
while ( n > 1 ) s *= n--;
without a need to use one more variable i.

Receiving strange number from fscanf - int conversion? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am very new to C and having a problem here. I am attempting to pass a file numbers.in through the below script. numbers.in contains 2 lines as follow:
12,34,56789
123456,789,0123
I am attempting to recognize the comma delineation.
#include <stdio.h>
void main(int argc, char *argv[])
{
int p ,n ,x ; //Converted ints.
while ( fscanf(stdin,"%d,%d,%d\n",&p,&n,&x) == 3 );
{
printf("got the sequence (%d,%d,%d)\n",x,p,n);
}
}
I am running the script like:
./a.out < numbers.in
Currently my script returns completely different numbers! What am I doing wrong here? Is the file sending them as characters so I need to somehow convert to ints? (I tried saving as chars and then later converting chars to ints and also got strange numbers - but different strange numbers!)
SOLVED, bad semicolon usage >_<
while ( fscanf(stdin,"%d,%d,%d\n",&p,&n,&x) == 3 ); <-- remove this semicolon

Resources