scanf() function doesnt work program in c [GNU/LINUX] [duplicate] - c

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 5 years ago.
Today i have been coding a came across with this code:
Code
#include <stdio.h>
main() {
char letra1;
char letra2;
printf("Primera letra: ");
scanf("%c", &letra1);
printf("Segunda letra: ");
scanf("%c", &letra2);
}
When I execute the code the first scanf() executes well but the second even didn't execute and close the program and I don't know why.
Execution
> ej3
Primera letra: A
Segunda letra:
Thanks for your time guys.

You should use a scanf format string " %c" to skip any pending whitespace characters, including the newline the user typed after the first letter. As posted, the second scanf() reads the \n that is pending in the input stream, so it does not wait for user input.

Related

C program won't use printf after scanf [duplicate]

This question already has answers here:
What is the effect of trailing white space in a scanf() format string?
(4 answers)
Why does printf not flush after the call unless a newline is in the format string?
(10 answers)
Closed 2 days ago.
I am new to C and am writing a very simple C program which just provides a input and repeats it back to you. The code shows the first print f which is a $ and lets you input something but will not print the text back to you. Here is the code:
char input[50];
printf("$");
scanf("%s\n", input);
printf("\n %s", input);
I thought it might have been a compile issue but nothing changes. I use make token which is the name of the file.
Remove the "\n" in the scanf() format string. The trailing "\n" instructs scanf() to match any number of white space characters and it will only know when it's done when encountering a non-white space character. Meanwhile you expect it to return after reading the first "\n". Consider using fgets() instead.
#include <stdio.h>
int main() {
char input[50];
printf("$");
scanf("%s", input);
printf("\n %s", input);
}
and example session:
$abc
abc

Is it possible to use scanf() and getchar() in the same program to get input? [duplicate]

This question already has answers here:
The program doesn't stop on scanf("%c", &ch) line, why? [duplicate]
(2 answers)
Closed 1 year ago.
I'm struggling on a question proving scanf() and getchar() can both retrieve a character from the input.
However, when I try to put them inside the same program, only the first function is running properly. The latter is discarded completely.
#include <stdio.h>
char letter;
int main()
{
printf("I'm waiting for a character: ");
letter = getchar();
printf("\nNo, %c is not the character I want.\nTry again.\n\n",letter);
printf("I'm waiting for a different character: ");
scanf("%c",&letter);
printf("Yes, %c is the one I'm thinking of!\n",letter);
return(0);
}
output
I have tried switching the places of those two functions but it is of no use.
Can someone help me find the issue and provide a way to fix this? The only requirement is that the program takes input twice, once by the getchar() function and once via scanf()
The second read attempt just reads whitespace (the end of line character, since you pressed enter after the first letter). Simply replace it with this:
scanf(" %c", &letter);
The space before % will tell scanf to read the next non-whitespace character.

In this super easy program written in C won't let me read a variable of type "char"? [duplicate]

This question already has answers here:
can not read character from user [duplicate]
(2 answers)
scanf() leaves the newline character in the buffer
(7 answers)
Closed 4 years ago.
This easy code in C won't work, and I don't get it why. If I read only "n", or only "ch" separately, it works, otherwise if I try to read them both, it won't let me read "ch". What happens and how could I make it work?
#include <stdio.h>
int main()
{
int n;
char ch;
printf("n=");
scanf("%d",&n);
printf("ch="); //when i press Build and Run it won't let me read "ch"??? why?
scanf("%c",&ch);
return 0;
}
When you read in a number using %d, a newline is left in the input buffer. When you then read a character with %c, it reads that newline immediately so you don't get prompted for more input.
Unlike the %d format specifier, which discards any leading whitespace, the %c format specifier does not.
Add a leading space before %c to consume any leftover whitespace:
scanf(" %c",&ch);

fgets will be ignored probably due to other scanf's [duplicate]

This question already has answers here:
Why scanf("%d", [...]) does not consume '\n'? while scanf("%c") does?
(4 answers)
scanf() leaves the newline character in the buffer
(7 answers)
Closed 6 years ago.
The program won't compile fgets in the teste1 function. Or at least it isn't working properly, it won't let me type the string, the program will end right after it prints "Nome do cliente".
If I disable the other scanf's in the function, it will run without any issue.
How do I make fgets to work?
#include <stdio.h>
void teste1(){
char teste[50];
printf("Nome do cliente\n");
fgets(teste,50,stdin);
}
void teste2(){
teste1();
}
void teste3(){
int opc1,opc2;
printf("\nSeleccione a área desejada\n1- Clientes\n2- Concessionários\n3- Carros de demonstração\n");
scanf("%d",&opc1);
printf("\nSeleccione a área desejada\n1- Inserir\n2- Alterar\n3- Remover\n4- Consultar\n");
scanf("%d",&opc2);
teste2();
}
int main()
{
teste3();
}
The Enter key you pressed for the last scanf input will be left in the input buffer as a newline. The fgets function will read this newline and think it is finished.

why second scanf is skipped when twice scanf is used with input as character? [duplicate]

This question already has answers here:
abnormal behavior of scanf [duplicate]
(3 answers)
Closed 8 years ago.
Today I encountered with problem that when I use twice scanf which accept character as input then second scanf skipped.
I tried to figure out I came to the conclusion that when we press enter key after first scanf the second scanf is skipped because enter key is take as input in the second scanf.
Can some please explain what is the exact reason with it?
int main()
{
char ch;
int num;
scanf("%d",&num);
scanf("%c",&ch);//This is skipped but its accept input when space as scanf(" %c",&ch)
}
scanf("%c", &ch);
It will read '\n' from the previous scanf since you are inputting a number and pressing enter, if you don't write it like scanf(" %c", &ch);. That way it will ignore '\n' and wait until you enter a valid char.

Resources