Not able to change the value of the char variable - c

#include<stdio.h>
#include<conio.h>
void main()
{
char c;
int i;
clrscr();
scanf("%c",&c);
scanf("%c",&c);
printf("%c",c);
getch();
}
This program does not takes the value of the char c for the second time however it works fine in the case of the integer variable. Why so?

Add in an extra space before %c:
scanf(" %c",&c);
This is because in the previous scanf, you entered the character, and then a newline \n. Thus, in the first scanf, the character was stored, and in the second scanf the newline was stored.
Alternatively, you can use getchar to store the newline. Add getchar() before the second scanf and after the first scanf.

It's because when you enter the first character, you probably end it with a newline, and that newline is still in the input buffer so the next scanf call will read that newline. So when you print it, it prints a newline.
You can tell scanf to discard leading whitespace by adding a single space before the format: " %c".

Related

The compiler skips the scanf statement for charter input after double. Why does this happen?

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int intvar=0; //variable to store integer input
double decimalvar = 0; //variable to store double input
char string[30]; //arrary to store string
scanf("%d",&intvar); //scan for int input
scanf("%f",&decimalvar); //scan for double input
scanf("%[^\n]",string); //scan for a line input
printf("%d\n",intvar); //print int var
printf("%.1f\n",decimalvar);//print double var
printf("%s",string);//print string
return 0;
}
The program runs successfully, but it only takes two input. After entering the second input, the third scanf statement is skipped and the printf statements are executed. Why does this happen?
scanf("%f",&decimalvar);
should be:
scanf("%lf",&decimalvar);
because you're passing a double not a float.
This is technically undefined behavior, but is not the cause of your problem, which is that a newline is left in the stdin buffer after the scanf calls. Specifiers like d, f, lf consume and ignore that newline. Of course the specifier [^\n] finds that newline and stops reading while leaving some input in the stdin buffer.
To solve the problem add a space at the start of the scanf format string, which will consume the newline, before trying to read the stdin.
scanf(" %[^\n]",string);
After every scanf() statement is executed, a '\n' is left in stdin. When executing scanf("%[^\n]", string);, that '\n' immediately causes a matching failure ("%[^\n]" expects all characters other than '\n'), so scanf() sadly returns.
To fix this, simply add a leading space in the format string to discard all (0 or more) whitespace characters, like this:
scanf(" %[^\n]", string);
A few unrelated tips:
Change scanf("%f", &decimalvar); to scanf("%lf", &decimalvar); to avoid undefined behaviour. See Why does scanf() need "%lf" for doubles, when printf() is okay with just "%f"?
Use fgets(string, sizeof string, stdin) to prevent the potential buffer overflow.
scanf("%[^\n]",string);
should be
scanf("%s",string);

char Input in C by scanf

Help me Please.
I want to know why it happen.
This code is not give right answer:
#include < stdio.h>
int main()
{
char c,ch;
int i;
printf("Welcome buddy!\n\nPlease input first character of your name: ");
scanf("%c",&c);
printf("\nPlease input first character of your lovers name: ");
scanf("%c",&ch);
printf("\nHow many children do you want? ");
scanf("%d",&i);
printf("\n\n%c loves %c and %c want %d children",c,ch,c,i);
return 0;
}
but this code give right answer.
#include < stdio.h>
int main()
{
char c,ch;
int i;
printf("Welcome buddy!\n\nPlease input first character of your name: ");
scanf(" %c",&c);
printf("\nPlease input first character of your lovers name: ");
scanf(" %c",&ch);
printf("\nHow many children do you want? ");
scanf("%d",&i);
printf("\n\n%c loves %c and %c want %d children",c,ch,c,i);
return 0;
}
Why?
and How?
Please help me anyone who know this why it happend.
While you are giving like this, It will not ignore the white spaces.
scanf("%c",&ch);
When you are giving the input to the first scanf then you will give the enter('\n'). It is one character so it will take that as input to the second scanf. So second input will not get input from the user.
scanf(" %c",&ch);
If you give like this, then it will ignore that white space character, then it will ask for the input from the user.
The first program doesn't work properly, because the scanf function when checking for input doesn't remove automatically whitespaces when trying to parse characters.
So in the first program the value of c will be a char and the value of ch will be the '\n' (newline) char.
Using scanf("\n%c", &varname); or scanf(" %c", &varname); will parse the newline inserted while pressing enter.
The scanf function reads data from standard input stream stdin.
int scanf(const char *format, …);
The white-space characters in format, such as blanks and new-line characters, causes scanf to read, but not store, all consecutive white-space characters in the input up to the next character that is not a white-space character.
Now, when you press, by example, "a" and "return", you have two chars in the stdin stream: a and the \n char.
That is why the second call to scanf assign the \n char to ch var.
your scanf() function takes input from stdin. Now when you hit any character from keyboard and hit enter, character entered by you is scanned by scanf() but still enter is present in stdin which will be scanned by scanf() below it. To ignore white spaces you have to use scanf() with " %c".

In the following code I am not being able to enter value for variable 'b'

#include<stdio.h>
int main(void)
{
char a;
char b;
printf("A is ");
scanf("%c",&a);
printf("B is ");
scanf("%c",&b);
}
The reason is that when you enter first character then after pressing Enter, a newline character (\n) is also passed to the input buffer along with that character. Since scanf("%c",&a); reads a single character at a time, it left behind \n in the buffer for next call of scanf. This \n is read by your second scanf call.
Put a space before %c in scanf to consume that newline character.
scanf(" %c",&a);
Make the scanf like this
scanf(" %c", &b);
when you are after giving the value for first variable, you will give enter. Here %c will take that as input. So avoid this, make the whitespace before the control string. It will skip the whitespace character(newline, tab, space). And ask the input from the user.
You can verify that new line is taken as a input for second variable like this,
printf("%d", b); // You will get the ascii value of new line.
when you give the enter newline placed in the input buffer, then that value will taken by the scanf. So need of scanf ( getting the input) is done. so it doesn't ask the input from you.

Why 2nd scanf doesn't work in my program?

scanf("%d %c",&size,&chara); works but separate scanf for character input does not work. I show these inside the code. Why is that?
void squareCustomFill(int size, char chara);
int main(void) {
int size,i,k;
char chara;
printf("Enter size of square: "); //This works
scanf("%d %c",&size,&chara);
//printf("Enter fill character: "); BUT WHY DOES NOT THIS WORK??
//scanf("%c",&chara);
squareCustomFill(size,chara);
return 0;
}
void squareCustomFill(int size, char chara){
int i,k;
for (k=1;k<=size;k++){
for(i=1;i<=size;i++)
printf("%c",chara);
printf("\n");
}
}
Scanf did not consume the \n character that stayed in the buffer from the first scanf call.
So the second scanf call did.
You have to clear the stdin before reading again or just get rid of the newline.
The second call should be
scanf(" %c",&chara);
^ this space this will read whitespace charaters( what newline also is) until it finds a single char
Yes I believe Armin is correct. scanf will read in whitespace (spacebar, newline, etc.). When you're inputting values if you click the space bar or enter right after the first scanf, the second scanf will read in that value (space, newline, etc.). So you fixed that with scanf("%d %c",&size,&chara) because there is a space between %d and %c. If you want them separate just do what Armin suggested: scanf(" %c",&chara).
Throw a getchar() in between them and slurp up that extraneous newline.

Input taking spaces by default in c

In the below program when am reading input from keyboard its taking only 2 characters instead of 4 and remaining 2 characters its taking spaces by default.
why is it???
program to take char input through pointers/
int c,inc,arrysize;
char *revstring;
printf("enter the size of char arry:");
scanf("%d",&arrysize);
revstring = (char *)malloc(arrysize * sizeof(*revstring));
printf("%d",sizeof(revstring));
printf("enter the array elements:");
for(inc=0;inc<arrysize;inc++)
{
scanf("%c",&revstring[inc]);
}
for(inc =0;inc<arrysize;inc++)
printf("%c",revstring[inc]);
getch();
return 0;
}
scanf reads formatted inputs. When you tape a number, you tape the digits, and then, you press <Enter>. So there is a remaining \n in stdin, which is read in the next scanf. The same applies if you press <Enter> between the characters.
A solution is to consume the characters in the standard input stream after each input, as follow:
#include <stdio.h>
void
clean_stdin (void)
{
int c;
while ((c = getchar ()) != '\n' && c != EOF)
;
}
Another idea is to use fgets to get human inputs. scanf is not suitable for such readings.
Most of the time scanf reads formatted input. For most % formats, scanf will first read and discard any whitespace and then parse the item specified. So with scanf("%d", ... it will accept inputs with initial spaces (or even extra newlines!) with no problems.
One of the exceptions, however, is %c. With %c, scanf reads the very next character, whatever it may be. If that next character is a space or newline, that is what you get.
Depending on what exactly you want, you may be able to just use a blank space in your format string:
scanf(" %c",&revstring[inc]);
The space causes scanf to skip any whitespace in the input, giving you the next non-whitespace character read. However, this will make it impossible to enter a string with spaces in it (the spaces will be ignored). Alternately, you could do scanf(" "); before the loop to skip whitespace once, or scanf("%*[^\n]"); scanf("%*c"); to skip everything up to the next newline, and then skip the newline.

Resources