0

In this code , after giving inputs for the first two scanf functions . the output is automatically shown . I learned this form of scanf for string. But it does not wait specifically for it . Any suggestions ?

#include <stdio.h>

int main()
{
    int p;
    int q;
    char kog[50];

    scanf("%d",&p);
    scanf("%d",&q);
    scanf("%50[^ ]s",kog);

    printf("%d %d %s",p,q,kog);

    return 0;
}
3
  • for interactive input read lines. Commented Jan 20, 2017 at 12:58
  • 2
    Your scan set will allow tabs and newlines but not blanks. It is similar to %50s. Also, you must specify a length excluding the null byte, so you need to use 49 and not 50 in the format (or change the array dimension to 51) to be safe. The off-by-one is historical and hence sacrosanct, but unfortunately inconsistent with most other functions that specify a buffer length. If the numbers are on separate lines, you probably want a space before the scan set (" %49[^ ]") to skip the newline left by the prior %d. Commented Jan 20, 2017 at 13:14
  • regarding this line: scanf("%50[^ ]s",kog); the function scanf() for a char string input will always append a NUL byte, so the statement should be: scanf("%49[^ ]s",kog);. Also, the [...] acts to input a string, so the trailing s` should not be there I.E. use: scanf("%49[^ ]",kog); Commented Jan 21, 2017 at 18:01

1 Answer 1

5

What do you mean by:

scanf("%50[^ ]s",kog);

?

If you want to scan till new line is printed, try:

scanf(" %49[^\n]",kog);

And by the way always mention width specifier 1 less than the size of the string. This is to avoid overwriting the terminating null character. So mention 49 instead of 50 as width specifier.

From cppreference:

If width specifier is used, matches only up to width. Always stores a null character in addition to the characters matched (so the argument array must have room for at least width+1 characters).

Also, note that a scan set conversion is complete at the ]. the structure is "%49[…]" and that's all. It is a common misapprehension that the scan set should be followed by s.

And provide a space before % in the scanf() to avoid consuming the new line character entered at the end of previous entry.

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah, i already mentioned a space in the scanf scanf(" %49[^\n]",kog); to avoid consuming the new line. I've added info about it :)
the posted code contains a 'magic' number. I.E. 50. 'magic' numbers are numbers with no basis. Suggest giving the 'magic' numbers meaningful names then using those meaningful names throughout the code.
a much better code sequence would be: #define MAX_STR_LEN 49 .... char kog[ MAX_STR_LEN+1]; ... scanf(" %" MAX_STR_LEN "[^ ]",kog);
Actually, the width needs to be one less than the actual length of the input buffer, so that the call to scanf() does not write beyond the end of the input buffer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.