This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 2 years ago.
So this is my c code
#include <stdio.h>
void main()
{
int a;
char b;
printf("Enter input \n");
scanf("%d", &a);
printf("Enter char input \n");
scanf("%c", &b);
printf("%d,%c", a, b);
}
And I'm getting output as
Enter input
3
Enter char input
3,
Can you explain me why its not taking character value the second time. This program works well if i take charcter input first.
you are entering space or enter button after integer input, So b is assigned that space/enter.
add getchar(); before input b
Related
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 3 years ago.
My program should accept 2 int value as the number and the position at which it should be added. After that it should ask wheather you want to insert more Y/N? But my program dosen't take the input of char instead takes a garbage value and keeps on asking for numbers only.
I have tried using seperate scanf for each input.
#include<stdio.h>
#include<conio.h>
void main()
{
int x,n;
char opt;
clrscr();
do{
printf("\nEnter the no. to be added and the position:");
scanf("%d%d",&x,&n);
printf("Do you want to insert more elements Y/N?");
scanf("%c",&opt);
printf("opt=%c x=%d n=%d",opt,x,n);
}while(opt!='N'&&opt!='n');
}
My o/p should be the value of each variables instead I get a garbage value in opt and the loop continues as it isn't 'n'.
The new line character is being read into your variables.
Change this line:
scanf("%c",&opt);
To this:
scanf(" %c",&opt);
The space consumes the new line character. This is only necessary for your " %c" format specifier. The "%d" does not require the space.
This question already has answers here:
Scanf skips every other while loop in C
(10 answers)
Closed 5 years ago.
#include<stdio.h>
int main()
{
char a,b;
printf("Enter Character 1 ");
scanf("%c",&a);
printf("Enter Character 2");
scanf("%c",&b);
printf("%c%c",a,b);
}
find output of program. It is not taking 2 characters.
When you enter the first character;
The character Ascii value is placed in the variable A .
Then you press enter and the ascii value of enter thats 13 is placed in the second variable.
So B should be 13.
In order to run this program use flushall() library function..
int main() {
char a,b;
printf("Enter Character 1 ");
scanf("%c",&a);
flushall();
printf("Enter Character 2");
scanf("%c",&b);
printf("%c%c",a,b);
return 0;
}
You could also use the standard Library function scanf()
Write something like:
Scanf(" %c",&variablename);
The space before the %c tells the compiler to skip any whitespace (if any).
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
C skipping one command of a function? [duplicate]
(2 answers)
Closed 6 years ago.
I've been given the pretty simple task of writing a program that will take two characters and then print the letters inbetween them using a for() loop.
Here's my code:
#include <stdio.h>
int main() {
char a, b;
printf("\nEnter the first character: ");
scanf("%c", &a);
printf("\nEnter the second character: ");
scanf("%c", &b);
for(char i = a; i <= b; i++) {
printf("%c ", i);
}
return 0;
}
When I run it, I am prompted to enter the first character correctly but when I press enter it only runs the next printf() and then terminates.
No errors or warnings or anything on compilation. Another similar question I found that was apparently solved does not work for me either.
Thanks in advance.
You have to consume the \n in stdin left by first scanf.
Fastest fix
scanf(" %c", &b);
The space before %c tells to scanf to ignore all whitespaces before to read the char.
If I read your code correctly, by pressing enter, you would enter the second character, which would most probably (depending on the environment) start with a numeric value of 13, which would be smaller than any letter, so the loop's body is executed only once.
This question already has answers here:
scanf won't ask for input the second time [duplicate]
(5 answers)
Packing two characters to an integer
(2 answers)
Closed 6 years ago.
Here is the code
//WAP in Co convert a binary or octal to a decimal number depending on user choice.
#include <stdio.h>
#include <math.h>
int conversion(int number,int base);
int main(void){
int number,base,decimal;
char choice;
printf("Enter the number:");
scanf("%d",&number);
printf("Enter 'b' for binary or 'o' for octal:");
scanf("%c",&choice); //Problem occuring here
if(choice=='b')
base=2;
base=8;
decimal=conversion(number,base);
printf("Decimal number:%d",decimal);
return 0;
}
int conversion(int number,int base){
int reminder;
int decimal=1;
int i=0;
while(number>=0){
reminder=number%base;
decimal=(decimal*reminder)*pow(base,i);
number/=10;
i++;
}
return decimal;
}
You have two scanf() and after the one some character is left out to read. you can use getchar() before the next scaf() it should take the next input
printf("Enter 'b' for binary or 'o' for octal:");
getchar();
scanf("%c",&choice);
Inside the conversion function you have while loop with a condition causes it go in infinite loop.Dividing a positive number by 10 minimum you can get is 0. use the below code:
while(number>0)
Also, if condition you have after the second input is useless. since you assign 8 to base anyway.
This question already has answers here:
My program skip getting input data? [duplicate]
(7 answers)
Closed 8 years ago.
I am having problem scanning char variable , my code is
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int a;
float b;
char c;
printf("Enter value for int variable \n");
scanf("%d",&a);
printf("Enter value for float variable \n");
scanf("%f",&b);
printf("Enter value for char variable \n");
scanf("%c",&c); //scanning is automatically skipped !!!
getch();
}
Please tell me , why is this happening and what can i do to solve it !
because of the stored enter key press [considered as character input]. use one getch(); before the third scanf().
alternatively, use (scanf(" %c",&c);) [mind the space before %c] which will get rid of any number of whitespace [buffered] character present before actual input.