Input in c language - c

Why I am not able to take 3rd input of character in my c - program??
#include <stdio.h>
int main(){
int a;
scanf("%d",&a);
float f1;
scanf("%f",&f1);
char ch;
scanf("%c",&ch);
printf("Integer %d\nfloat %f\ncharacter %c",a,f1,ch);
}

scanf("%c",&ch);
After entering the float value and pressing enter, the scanf reads that extra newline character \n. To prevent that use this:
scanf(" %c",&ch);
Space before %c reads that extra \n and the required character will be stored in ch.
To demonstrate that please see the code below :
#include <stdio.h>
int main()
{
int a;
scanf("%d", &a);
float f1;
scanf("%f", &f1);
char ch;
scanf("%c", &ch);
printf("Integer %d\nfloat %f\ncharacter %d", a, f1, ch); // changed to %d to print ASCII value
}
The output is :
12
12.3
Integer 12
float 12.300000
character 10
You can see the output here, it prints 10 (ASCII of \n).
Solution :
#include <stdio.h>
int main()
{
int a;
scanf("%d", &a);
float f1;
scanf("%f", &f1);
char ch;
scanf(" %c", &ch);
printf("Integer %d\nfloat %f\ncharacter %c", a, f1, ch);
}
The output :
12
12.3
k
Integer 12
float 12.300000
character k

Related

I am trying to input three characters using scanf in loop and also outside loop in C but none is working properly

Here, I am inputting characters using scanf in for loop but it it takes only one character.
This problem is not happening with integer. Why?
(1) IN LOOP :-
#include <stdio.h>
int main(void) {
char p1, p2, p3, c1, c2;
int i, t;
// t is the number of testcases.
printf("Enter number of testcases : ");
scanf("%d", &t);
for(i = 0; i < t; i++){
printf("Enter three characters : \n");
scanf("%c%c%c", &p1, &p2, &p3);
printf("Again enter characters\n");
scanf("%c%c", &c1, &c2);
printf("\nEnd");
}
return 0;
}
I am able to input only two characters.
OUTPUT :
Enter number of testcases : 2
Enter three characters :
a
Again enter characters
s
End
Enter three characters :
d
f
Again enter characters
g
End
(2) WITHOUT LOOP :-
#include<stdio.h>
int main(){
char p1, p2, p3, c1, c2;
int i, t;
printf("Enter three characters : \n");
scanf("%c%c%c", &p1, &p2, &p3);
getchar();
printf("Again enter characters\n");
scanf("%c%c", &c1, &c2);
printf("\nEnd");
return 0;
}
OUTPUT :
Enter three characters :
a
s
Again enter characters
d
End
Place a space before format specifier in scanf.
#include<stdio.h>
int main(void) {
char p1, p2, p3, c1, c2;
int i, t;
// t is the number of testcases.
printf("Enter number of testcases : ");
scanf("%d", &t);
for(i = 0; i < t; i++){
printf("Enter three characters : \n");
scanf(" %c %c %c", &p1, &p2, &p3);
printf("Again enter characters\n");
scanf(" %c %c", &c1, &c2);
printf("\nEnd");
}
return 0;
}
What is happening is that scanf is looking for 3 characters in stdin to assign to p1, p2 and p3.
After you enter 'a' and 's', stdin has 4 characters.
'a','\n','s','\n'
So, p1 gets 'a', p2 gets '\n' and p3 gets 's'. getchar() removes '\n'
Then, when you enter 'd', c1 gets 'd' and c2 gets '\n'.
If you want to enter one character at a time, then you'll need to include the new lines in your scanf as well.
Do it like this:
scanf("%c\n%c\n%c", &p1, &p2, &p3);
getchar();

getchar following scanf (to read an integer) takes terminating "enter" key of scanf as character

When I have a scanf followed by a getchar, why does the getchar always keep getting the last delimiting character of scanf? How can I stop that? I tried looking into "format specifiers" for scanf, read quite a few things but none solves this.
The code is shown below -
#include <stdio.h>
#include <conio.h>
int main()
{
int a;
char b;
printf ("Enter an integer \n");
scanf_s(" %d", &a);
printf("Enter a character \n");
b = getchar();
printf("The integer you entered is %d \n", a);
printf("The character you entered is %c \n", b);
_getch();
return 0;
}
The output is as below -
Enter an integer
4563
Enter a character
The integer you entered is 4563
The character you entered is
The enter key I press at the end of integer entry is being returned by getchar. The screen does not even stop after printing "Enter a character". What is the correct way to do this ?
Use the scanf(" %c", &b) instead of getchar()
When you put the space befor the %c you clean the buffer
Or you can clean the buffer using this too:
int ch;
while ((ch = getchar()) != '\n' && ch != EOF);
Complete example:
int main(void)
{
printf("Enter an integer \n");
int a;
scanf(" %d", &a);
int ch;
while ((ch = getchar()) != '\n' && ch != EOF) {
}
printf("Enter a character \n");
char b = getchar();
printf("The integer you entered is %d \n", a);
printf("The character you entered is %c \n", b);
_getch();
}
But I think the scanf()

Taking a string input using gets() function in C [duplicate]

I'm pretty new to C, and I have a problem with inputing data to the program.
My code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
int a;
char b[20];
printf("Input your ID: ");
scanf("%d", &a);
printf("Input your name: ");
gets(b);
printf("---------");
printf("Name: %s", b);
system("pause");
return 0;
}
It allows to input ID, but it just skips the rest of the input. If I change the order like this:
printf("Input your name: ");
gets(b);
printf("Input your ID: ");
scanf("%d", &a);
It will work. Although, I CANNOT change order and I need it just as-is. Can someone help me ? Maybe I need to use some other functions. Thanks!
Try:
scanf("%d\n", &a);
gets only reads the '\n' that scanf leaves in. Also, you should use fgets not gets: http://www.cplusplus.com/reference/clibrary/cstdio/fgets/ to avoid possible buffer overflows.
Edit:
if the above doesn't work, try:
...
scanf("%d", &a);
getc(stdin);
...
scanf doesn't consume the newline and is thus a natural enemy of fgets. Don't put them together without a good hack. Both of these options will work:
// Option 1 - eat the newline
scanf("%d", &a);
getchar(); // reads the newline character
// Option 2 - use fgets, then scan what was read
char tmp[50];
fgets(tmp, 50, stdin);
sscanf(tmp, "%d", &a);
// note that you might have read too many characters at this point and
// must interprete them, too
scanf will not consume \n so it will be taken by the gets which follows the scanf. flush the input stream after scanf like this.
#include <stdlib.h>
#include <string.h>
int main(void) {
int a;
char b[20];
printf("Input your ID: ");
scanf("%d", &a);
fflush(stdin);
printf("Input your name: ");
gets(b);
printf("---------");
printf("Name: %s", b);
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
int a;
char b[20];
printf("Input your ID: ");
scanf("%d", &a);
getchar();
printf("Input your name: ");
gets(b);
printf("---------");
printf("Name: %s", b);
return 0;
}
Note:
If you use the scanf first and the fgets second, it will give problem only. It will not read the second character for the gets function.
If you press enter, after give the input for scanf, that enter character will be consider as a input f or fgets.
you should do this way.
fgetc(stdin);
scanf("%c",&c);
if(c!='y')
{
break;
}
fgetc(stdin);
to read input from scanf after reading through gets.
scanf("%d", &a); can't read the return, because %d accepts only decimal integer. So you add a \n at the beginning of the next scanf to ignore the last \n inside the buffer.
Then, scanf("\n%s", b); now can reads the string without problem, but scanf stops to read when find a white space. So, change the %s to %[^\n]. It means: "read everthing but \n"
scanf("\n%[^\n]", b);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
int a;
char b[20];
printf("Input your ID: ");
scanf("%d", &a);
printf("Input your name: ");
scanf("\n%[^\n]", b);
//first \n says to ignore last 'return'
//%[^\n] read until find a 'return'
printf("---------\n");
printf("Name: %s\n\n", b);
system("pause");
return 0;
}
The scanf function removes whitespace automatically before trying to parse things other than characters. %c, %n, %[] are exceptions that do not remove leading whitespace.gets is reading the newline left by previous scanf. Catch the newline usinggetchar();
scanf("%d", &a);
getchar(); // catches the newline character omitted by scanf("%d")
gets(b);
https://wpollock.com/CPlus/PrintfRef.htm
Just use 2 gets() functions
When you want to use gets() after a scanf(), you make sure that you use 2 of the gets() functions and for the above case write your code like:
int main(void) {
int a;
char b[20];
printf("Input your ID: ");
scanf("%d", &a);
//the change is here*********************
printf("Input your name: ");
gets(b);
gets(b);
//the change is here*********************
printf("---------");
printf("Name: %s", b);
system("pause");
return 0;
}

Why does this scanf in a while loop work?

I can't understand why this does exactly what I want. The part where I used two scanf's in the loop confuses me. I compiled it using devcpp.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int dend, dsor, q, r;
char c;
while(c!='n')
{
printf("enter dividend: ");
scanf("%d", &dend);
printf("enter divisor: ");
scanf("%d", &dsor);
q=dend/dsor;
r=dend%dsor;
printf("quotient is %d\n", q);
printf("remainder is %d\n", r);
scanf("%c", &c);
printf("continue? (y/n)\n");
scanf("%c", &c);
}
system("PAUSE");
return 0;
}
FWIW, your code invokes undefined behavior. In the part
char c;
while(c!='n')
c is an uninitialized local variable with automatic storage and you're trying to use the value of c while it is indeterminate.
That said, first scanf("%c", &c); is used to eat up the newline present in the input buffer due to the press of enter key after previous input. You can read about it in details in another post.

How can I insert 2 other value in scanf one time

I have an incorrect number in numB.
How can I insert 2 other value in scanf one time?
Or it can't do it?
#include <stdio.h>
main()
{
char a,b;
int numA,numB;
printf("A : ");
scanf("%c%d",&a,&numA);
printf("B : ");
scanf("%c%d",&b,&numB);
printf("\n\n%d %d",numA,numB);
}
result
A : S 67
B : D 56
67 1684370524
The problem is that the value read into b is the newline character (Enter key) that you pressed after typing in 67. Then reading numB fails because it tries to interpret D as numB.
If you had typed S 67 D 56 <Enter> (i.e. without the Enter in the middle) then you would get the right output.
To fix this, one way is to change your format string to " %c%d". The space means that it will consume any whitespace before trying to read a character.
You get a newline from after the first number in b, and then an undefined value in numB. Use " %c%d" to avoid this problem; it skips white space.
#include <stdio.h>
int main(void)
{
char a, b;
int numA, numB;
printf("A : ");
scanf(" %c%d", &a, &numA);
printf("B : ");
scanf(" %c%d", &b, &numB);
printf("\n%d %d\n", numA, numB);
return 0;
}
Try this out and see if it works:
#include <stdio.h>
void main(){
char a,b,e;
int c,d;
printf("A: ");
scanf("%c %d",&a,&c);
e=getchar();
printf("B: ");
scanf("%c %d",&b,&d);
printf("%d %d",c,d);
}

Resources