Reading 2 characters in C [duplicate] - c

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).

Related

Why character value is not taking after integer value in c [duplicate]

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

Do while is not taking inputs in the order defines it only works for 1 scanf [duplicate]

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.

scanf is being skipped in C [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 5 years ago.
I tried to run the following program:
int main(){
char a;
char b;
char c;
printf("\nenter a: ");
scanf("%c", &a);
printf("\nenter b: ");
scanf("%c", &b);
printf("\nenter c: ");
scanf("%c", &c);
return 0;
}
upon running the program it prompts you to enter a value for a. once you do, you are prompted to enter a value for b, however you are not allowed to input a value because the program skips the scan and then prompts you again to input a value for c which is not skipped. I can initialize a and c, but not b. and I have no idea why. I read somewhere that using %[^\n] in the scanf, but I tried using it and I don't think I used it correctly because it still wasn't working.
this is the output (with some input examples):
enter a: 1
enter b:
enter c: 1
process returned 0 (0x0)
Instead of "%c", use " %c".
Without the space, scanf does not skip white spaces when the format specifier is %c. That is initially confusing since it will skip white spaces for other format specifiers, such as %d, %f, %s, etc.
When you press enter, that adds a character to the input queue, which is then read into b.
You can either explicitly read a character to ignore it, or you can use (for one alternative) %1s to read a single-character string (which will skip white-space, including the new-line character entered when you press enter.
'\n' becomes the input for variable b after you press enter.
so to take care of this, use getchar() which will take care of '\n'.
int main(){
char a;
char b;
char c;
printf("\nenter a: ");
scanf("%c", &a);
getchar();
printf("\nenter b: ");
scanf("%c", &b);
getchar();
printf("\nenter c: ");
scanf("%c", &c);
return 0;
}

C: Writing a loop to print the alphabet between two characters [duplicate]

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.

Unable to scan char variable after scanning int in C [duplicate]

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.

Resources