multiple character input to char in C - c

#include <stdio.h>
int main(void) {
char ch,ch1;
scanf("%c",&ch);/*input ab here*/
scanf("%c",&ch1);
printf("%c %c",ch,ch1);
return 0;
}
Why this produces a b as output. We don't enter any input for second variable but it still got assigned. Could anyone explain the behavior.
You can check the output here if you want.

We don't enter any input for second variable
That's not true, "%c" in scanf reads one character, after it processes the input a, the "%c" in the next scanf then reads the next input character b.

Because you entered 2 characters in the 1st input while the program expects just 1: the 2nd is pending until the next call to scanf

this may get you rid of the situation
scanf("%c",&ch);/*input ab here*/
fflush(stdin);
scanf("%c",&ch1);
EDIT
Your actual problem is that ch1 is being assigned with the newline character(or the space as tried in IDEONE simulator)
To check that :
enter your values without any separation.

Scanf can do multiple scans:
char a1, a2, a3, a4;
scanf("%c%c%c%c", &a1, &a2, &a3, &a4);

Related

Why my code show a false value of char variable

I write my code in C language but when I write like this
int main()
{
char one,two,three;
int num=0;
scanf("%d",&num);
scanf("%c %c %c",&one,&two,&three);
printf("%c %c %c",one,two,three);
}
When I input chars a b c that shows me
a b
and does not show c
It happens when I input num. If I set a variable of num to be 3 it shows me
a b c
like my input
How to solve this?
When your first "scanf" processes, in addition to whatever number you're inputting you're also putting a newline character "/n" in as well. This is being read by your second "scanf" such that &one is your new line.
To fix this just change this line:
scanf("%c %c %c",&one,&two,&three);
To this:
scanf(" %c %c %c",&one,&two,&three);
The extra space in the second line will prevent the newline from being used by the second "scanf" statement.

How to explain my code with scanf()?

I'm a newbie in C programming and I'm learning the scanf now.
I wrote some code and I'm trying to understand it.
Here is the code:
#include <studio.h>
int main(){
int a=1, b=2, c=3;
scanf ("%d%d", &a,&b);
scanf ("%d", &c);
scanf ("%d", &c);
printf ("a=%d\nb=%d\nc=%d\n", a, b, c);
return 0;
}
When I input
11 22 33 44
The output is
a=11
b=22
c=44
This is normal. However, when I input:
11, 22, 33, 44
Output
a=11
b=2
c=3
It looks like the commas stops the input process.
Who can explain to me why is this happening?
Actually, when putting the commas between %d%d (it is like "%d, %d"), and providing input data, the input have to be separated with commas.
Who can give me some suggestions for commas in scanf parameters?
scanf stop reading when find a character in stdin that doesn't matches the format string.
In your example, your scanf expect only character that forms an integer value. Infact, the first scanf stops when it reaches the first comma. When the other scanfs try to read in the stdin, the first character they will find will be the last character the previuos scanf read but didn't use, the comma. This is the reason why they will end soon without read anything.
If you want to use commas or other character in your input over the values you want to memorize, you have to insert them explicity in the format string.
For example with this statment:
scanf("%d, %d", &a, &b);
You are instructing the scanf to read an integer and save it in the variable a, read a comma and forget it, then read another integer and save it in the variable b. If during the parsing the scanf will find something that it is not an integer or a comma they will stop immediately without reads other values(this is the reason why b and c have the earlier value in your program).
you can use commas in scanf like this :
scanf ("%d,%d,%d", &a,&b,&c);
then you input numbers in single line seperated with commas.
11,12,13.
Note that in this scenario scanf expects single line input of numbers seperated with commas. If your input in this case will be 11 12 13 then You will now get the desired output.
Thanks for your guys reply. I tried another code:
#include <stdio.h>
int main(){
int a=1, b=2, c=3;
scanf ("%d, %d", &a,&b);
scanf ("%d", &c);
scanf ("%d", &c);
printf ("a=%d\nb=%d\nc=%d\n", a, b, c);
return 0;
}
When i input
11 22 33 44
output:
a=11
b=2
c=33
how to explain?
To second question ( do not know how to quote here :( )
First scanf expects integers seperated with commas. As you did not enter your inputs like that that scanf took first number 11 and assigned it to a (b is ignored). Second scanf took number 22 and third scanf number 33. Number 44 in the input is redundant. Thats why your output looks like that.
Note that b=2 is from previous part of code int b=2

scanf() function with different format specifier

Below is a program which accepts two character and prints them
#include<stdio.h>
int main()
{
char c1, c2;
printf("\n Enter characters one : ");
scanf(" %c", &c1);
printf("\n Enter character two : ");
scanf("%c", &c2);
printf("\n The the characters are %c and %c ", c1, c2);
return 0;
}
Now one output instance is :-
Enter two characters : a
The the characters are a and
The problem is that I haven't given any space between two format specifier %c
Here I pressed 'a' and then '\n' which gets stored into c1 and c2 respectively. And thus I got output which was not accepted.
I know how to correct this problem.
Now I make the same program for the integers :-
#include<stdio.h>
int main()
{
int a, b;
printf("\n Enter two numbers : ");
scanf("%d%d", &a, &b);
printf("\n The two numbers are %d and %d ", a, b);
return 0;
}
Here we will not found any problem.
I think this time we didn't encounter problem because the second input we give is '\n' or space which is not any integer and thus we get a failure in the reading from the scanf() function so the input buffer is still active and if we press the next input as integer then it gets stored into variable 'b'.
Can you tell me the reason which I thought is correct or not?
Now if it is correct then what will happen if I press again a character. Then also it should not get stored into the variable 'b' but this time 0 gets stored into variable 'b'.
So my question is that what is the reason for proper behavior of the program when I'm trying to make the same program with %d
To answer your question, let's have a look at C11 standard, chapter ยง7.21.6.2
Input white-space characters (as specified by the isspace function) are skipped, unless
the specification includes a [, c, or n specifier.
So, when you have a newline ('\n', which is indeed a white-space character) left in the input buffer,
in case of scanf("%c", &charVar);, the newline is considered as the input, so the second scanf skips asking for the input from user.
in the case of scanf("%d", &intVar); the leftover newline is skipped and it wait for the integer input to appear, so it stops and asks the user for the input.
However, FWIW, in later case, if you input a non-whitespace character and press ENTER, the char input will be considered as an input, causing a matching failure.
Related
[...] If
the input item is not a matching sequence, the execution of the directive fails: this
condition is a matching failure.
It's reading the new line as the newline is a character. Simply changing scanf("%c", &c2); to scanf(" %c", &c2); will make it work.
So in your code:
#include
int main()
{
char c1, c2;
printf("\n Enter characters one : ");
scanf(" %c", &c1);
printf("\n Enter character two : ");
scanf(" %c", &c2);
printf("\n The the characters are %c and %c ", c1, c2);
return 0;
}

Why loop is running once?

#include <stdio.h>
main()
{
int num;
char another="y";
for(;another=="y";)
{
printf("no. is ");
scanf("%d", &num);
printf("sq. of %d is %d", num,num*num);
printf("\nWant to enter another no. : y/n");
scanf("%c", &another);
}
}
I have C code like this. According to me, this should work like: Enter the no and give square. But its nor running in infinite loop. But it is running only once. Why?
I am using GCC4.8.1 compiler on windows 64 bit.
Because on second iteration scanf assign \n to anotherinstead of assigning y.
EXPLANATION: When you press Enter key after typing the input, then one more character goes to the buffer along with the typed input. This character is produced by Enter and is \n. Suppose you typed y and then pressed the Enter key then the buffer would contain y\n, i.e, two characters, y and \n.
When scanf("%d", &num); is executed then it reads the number typed in and leaves behind the \n character in the buffer for next call of scanf. This \n is read by the next scanf call scanf("%c", &another); irrespective of what you have typed in your console.
To eat up this new line char, use a space before %c specifier in scanf.
scanf(" %c", &another);
^Notice the space before %c.
And change
for(;another=="y";) {...} // Remove the double quote.
to
for(;another=='y';) {...} // Single quote is used for `char`s.
The test in the loop is wrong:
another=="y"
this compares the value of another, a single character, with the value of a string literal, which will be reprented as a pointer to the character y. It should be:
another == 'y'
You should have gotten compiler warnings for this, since it's very strange to compare a small integer with a pointer.

Beginner to C: Getting frustrated on a simple program

#include<stdio.h>
int main()
{
char a, b;
scanf("%c", &a);
scanf("%c", &b);
printf("%c %c",a,b);
return 0;
}
When I run this program, I only get output as a & I don't get the prompt to enter 2nd character. Why?
In this line,
scanf("%c", &a);
you are actually taking a %d from the stdin (standard input) but at the time you entered a character from stdin, you also typed ENTER from your keyboard which means that now you have two characters in stdin; the character itself & \n. So, the program took first character as the one you entered & second character as \n.
You need to use
scanf("%c\n", &a);
so that scanf eats the newline (that came by pressing ENTER) too.
As rodrigo suggested, you can use these too.
scanf(" %c", &a); or scanf("%c ", &a);
The way you are thinking that second character is printed is wrong. It's actually being printed but it's \n so your prompt might be coming to the next line.
Your code will work if you enter both characters without using ENTER.
shadyabhi#archlinux /tmp $ ./a.out
qw
q wshadyabhi#archlinux /tmp $
Note, when you used this, the only thing in STDIN was q & w. So, the first scanf ate q & the second one w.
Because when you press the enter key, the resulting newline is read as a separate character into b. Try this instead:
#include<stdio.h>
int main()
{
char a, b;
scanf("%c %c", &a, &b);
printf("%c %c",a,b);
return 0;
}
The %c is a format string which accepts only a single character. I think you pressed Enter key as soon as you pressed an alphabet key. The Enter key is also recognized as a character. So the next variable is taking the enter key which has a value of "\0".
The computer is still printing the character from the second variable but its invisible since nothing is getting printed. If you keenly observe, there will be a new line.
Enter two characters one after the other and you will be getting the right output.

Resources