New to coding an need help figuring out why certain portions aren't working as they should be [duplicate] - c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *method1(void)
{
static char a[4];
scanf("%s\n", a);
return a;
}
int main(void)
{
char *h = method1();
printf("%s\n", h);
return 0;
}
When I run the code above, the prompt is asking me twice for input (I only use scanf once in the code). Why is that?
(I entered 'jo'; it asked for more input, so I entered 'jo' again. Then it only printed out 'jo' once.)

From my scanf manual page
White space (such as blanks, tabs, or newlines) in the format string match any amount of white space, including none, in the input. Everything else matches only itself.
Thus with scanf ("%s\n", a) it will scan for a string followed by optional white space. Since after the first newline more whitespace may follow, scanf is not done after the first newline and looks what's next. You will notice that you can enter any number of newlines (or tabs or spaces) and scanf will still wait for more.
However, when you enter the second string, the sequence of whitespace is delimited and scanning stops.
Use scanf ("%s", a) to not scan trailing whitespace.

you have to remove the \n from the string format of the scanf. It should be
scanf("%s",a);
EDIT: Explanation
the %s means that the scanf reads the input character till it gets a delimiter which should be a white space like space or tab or new line(\n) so the first enter is get as a delimiter for the "%s" and adding the "\n" to the string format "%s\n" means that the scanf will wait 2 newlines the first newline is related to the delimiter of the "%s" and the second newline is related to the\n of the string format.

Remove \n from the scanf format and give an input and it displays the output based on the given output once.

you can use either of these to avoid the mentioned problem :
scanf("%s",a);
or
scanf("\n%s",a);

Try this: Don't use \n on scanf, it won't ask you twice and sometimes it might show an error
Your code: scanf("%s\n", a);
Try this on scanf: scanf("%s", a);

use gets() or fgets() instead...alternatively use scanf("%[^\n]s",a);

Don't use the escape sequence in scanf stdio function
scanf ("%s", a);

Related

Why does the 2nd iteration not execute the expected printf in the loop and even takes one extra input to show the final output? [duplicate]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *method1(void)
{
static char a[4];
scanf("%s\n", a);
return a;
}
int main(void)
{
char *h = method1();
printf("%s\n", h);
return 0;
}
When I run the code above, the prompt is asking me twice for input (I only use scanf once in the code). Why is that?
(I entered 'jo'; it asked for more input, so I entered 'jo' again. Then it only printed out 'jo' once.)
From my scanf manual page
White space (such as blanks, tabs, or newlines) in the format string match any amount of white space, including none, in the input. Everything else matches only itself.
Thus with scanf ("%s\n", a) it will scan for a string followed by optional white space. Since after the first newline more whitespace may follow, scanf is not done after the first newline and looks what's next. You will notice that you can enter any number of newlines (or tabs or spaces) and scanf will still wait for more.
However, when you enter the second string, the sequence of whitespace is delimited and scanning stops.
Use scanf ("%s", a) to not scan trailing whitespace.
you have to remove the \n from the string format of the scanf. It should be
scanf("%s",a);
EDIT: Explanation
the %s means that the scanf reads the input character till it gets a delimiter which should be a white space like space or tab or new line(\n) so the first enter is get as a delimiter for the "%s" and adding the "\n" to the string format "%s\n" means that the scanf will wait 2 newlines the first newline is related to the delimiter of the "%s" and the second newline is related to the\n of the string format.
Remove \n from the scanf format and give an input and it displays the output based on the given output once.
you can use either of these to avoid the mentioned problem :
scanf("%s",a);
or
scanf("\n%s",a);
Try this: Don't use \n on scanf, it won't ask you twice and sometimes it might show an error
Your code: scanf("%s\n", a);
Try this on scanf: scanf("%s", a);
use gets() or fgets() instead...alternatively use scanf("%[^\n]s",a);
Don't use the escape sequence in scanf stdio function
scanf ("%s", a);

The entered values ​are not applied [duplicate]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *method1(void)
{
static char a[4];
scanf("%s\n", a);
return a;
}
int main(void)
{
char *h = method1();
printf("%s\n", h);
return 0;
}
When I run the code above, the prompt is asking me twice for input (I only use scanf once in the code). Why is that?
(I entered 'jo'; it asked for more input, so I entered 'jo' again. Then it only printed out 'jo' once.)
From my scanf manual page
White space (such as blanks, tabs, or newlines) in the format string match any amount of white space, including none, in the input. Everything else matches only itself.
Thus with scanf ("%s\n", a) it will scan for a string followed by optional white space. Since after the first newline more whitespace may follow, scanf is not done after the first newline and looks what's next. You will notice that you can enter any number of newlines (or tabs or spaces) and scanf will still wait for more.
However, when you enter the second string, the sequence of whitespace is delimited and scanning stops.
Use scanf ("%s", a) to not scan trailing whitespace.
you have to remove the \n from the string format of the scanf. It should be
scanf("%s",a);
EDIT: Explanation
the %s means that the scanf reads the input character till it gets a delimiter which should be a white space like space or tab or new line(\n) so the first enter is get as a delimiter for the "%s" and adding the "\n" to the string format "%s\n" means that the scanf will wait 2 newlines the first newline is related to the delimiter of the "%s" and the second newline is related to the\n of the string format.
Remove \n from the scanf format and give an input and it displays the output based on the given output once.
you can use either of these to avoid the mentioned problem :
scanf("%s",a);
or
scanf("\n%s",a);
Try this: Don't use \n on scanf, it won't ask you twice and sometimes it might show an error
Your code: scanf("%s\n", a);
Try this on scanf: scanf("%s", a);
use gets() or fgets() instead...alternatively use scanf("%[^\n]s",a);
Don't use the escape sequence in scanf stdio function
scanf ("%s", a);

Scanf reads wrong value

I’m new to C programming language, I have wrote a simple code that reads two “char” values and prints them on the screen but the second one got empty value for a strange reason. What’s going wrong with my code?
Char c;
Scanf(“%c”,&c);
Printf(“Value:%c”,c);
Scanf(“%c”,&c);
Printf(“Value:%c”,c);
Output:
Value:g
Value:
(This is a comment, but comments are hard to format)
There's nothing wrong with your code (other than the failure to check the value returned by scanf and deal with errors or incorrect input). Consider:
#include <stdio.h>
int
main(void)
{
char c;
scanf("%c",&c);
printf("Value:%c",c);
scanf("%c",&c);
printf("Value:%c",c);
return 0;
}
$ gcc a.c
$ printf 'abc' | ./a.out
Value:aValue:b
Perhaps what's "wrong" is that you have newlines in your input. (eg, you are entering data interactively and forgetting that when you hit "return" a newline is inserted into the input stream.)
If your goal was to read two "interesting" characters, and if you don't think that whitespace characters like space and newline are "interesting", you've been tripped up by number six out of the seventeen things about scanf that are designed to trip up the unwary: %c does read whitespace characters.
If you want scanf to skip over whitespace characters, so that %c will read the next, non-whitespace or "interesting" character, simply include a space character in the format string before the %c:
char c;
scanf(" %c", &c);
printf("Value: %c\n",c);
scanf(" %c", &c);
printf("Value: %c\n",c);
In a scanf format string, the presence of a whitespace character indicates that you want scanf to skip over all whitespace at that point in the input.
Normally you don't have to worry about skipping whitespace with scanf, because most of the other format specifiers -- %d, %f, %s, etc. -- automatically skip over any whitespace, if necessary, before they start parsing their input. But %c is special: someone thought you might want to use it to read whitespace characters, so it doesn't skip them, so if you don't want to read them, you have to skip them yourself, with that space character in the format string first.

Multiple function:something wrong with scanf [duplicate]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *method1(void)
{
static char a[4];
scanf("%s\n", a);
return a;
}
int main(void)
{
char *h = method1();
printf("%s\n", h);
return 0;
}
When I run the code above, the prompt is asking me twice for input (I only use scanf once in the code). Why is that?
(I entered 'jo'; it asked for more input, so I entered 'jo' again. Then it only printed out 'jo' once.)
From my scanf manual page
White space (such as blanks, tabs, or newlines) in the format string match any amount of white space, including none, in the input. Everything else matches only itself.
Thus with scanf ("%s\n", a) it will scan for a string followed by optional white space. Since after the first newline more whitespace may follow, scanf is not done after the first newline and looks what's next. You will notice that you can enter any number of newlines (or tabs or spaces) and scanf will still wait for more.
However, when you enter the second string, the sequence of whitespace is delimited and scanning stops.
Use scanf ("%s", a) to not scan trailing whitespace.
you have to remove the \n from the string format of the scanf. It should be
scanf("%s",a);
EDIT: Explanation
the %s means that the scanf reads the input character till it gets a delimiter which should be a white space like space or tab or new line(\n) so the first enter is get as a delimiter for the "%s" and adding the "\n" to the string format "%s\n" means that the scanf will wait 2 newlines the first newline is related to the delimiter of the "%s" and the second newline is related to the\n of the string format.
Remove \n from the scanf format and give an input and it displays the output based on the given output once.
you can use either of these to avoid the mentioned problem :
scanf("%s",a);
or
scanf("\n%s",a);
Try this: Don't use \n on scanf, it won't ask you twice and sometimes it might show an error
Your code: scanf("%s\n", a);
Try this on scanf: scanf("%s", a);
use gets() or fgets() instead...alternatively use scanf("%[^\n]s",a);
Don't use the escape sequence in scanf stdio function
scanf ("%s", a);

Why does scanf ask twice for input when there's a newline at the end of the format string?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *method1(void)
{
static char a[4];
scanf("%s\n", a);
return a;
}
int main(void)
{
char *h = method1();
printf("%s\n", h);
return 0;
}
When I run the code above, the prompt is asking me twice for input (I only use scanf once in the code). Why is that?
(I entered 'jo'; it asked for more input, so I entered 'jo' again. Then it only printed out 'jo' once.)
From my scanf manual page
White space (such as blanks, tabs, or newlines) in the format string match any amount of white space, including none, in the input. Everything else matches only itself.
Thus with scanf ("%s\n", a) it will scan for a string followed by optional white space. Since after the first newline more whitespace may follow, scanf is not done after the first newline and looks what's next. You will notice that you can enter any number of newlines (or tabs or spaces) and scanf will still wait for more.
However, when you enter the second string, the sequence of whitespace is delimited and scanning stops.
Use scanf ("%s", a) to not scan trailing whitespace.
you have to remove the \n from the string format of the scanf. It should be
scanf("%s",a);
EDIT: Explanation
the %s means that the scanf reads the input character till it gets a delimiter which should be a white space like space or tab or new line(\n) so the first enter is get as a delimiter for the "%s" and adding the "\n" to the string format "%s\n" means that the scanf will wait 2 newlines the first newline is related to the delimiter of the "%s" and the second newline is related to the\n of the string format.
Remove \n from the scanf format and give an input and it displays the output based on the given output once.
you can use either of these to avoid the mentioned problem :
scanf("%s",a);
or
scanf("\n%s",a);
Try this: Don't use \n on scanf, it won't ask you twice and sometimes it might show an error
Your code: scanf("%s\n", a);
Try this on scanf: scanf("%s", a);
use gets() or fgets() instead...alternatively use scanf("%[^\n]s",a);
Don't use the escape sequence in scanf stdio function
scanf ("%s", a);

Resources