Home > Back-end >  Use scanf with defined value
Use scanf with defined value

Time:06-04

I have an array of characters in the size of SIZE

#define SIZE 50
int main(){
    char str[SIZE];
    return 0;
}

I want to read a sentence into it up to SIZE characters, using scanf.

I know I can do something such as

scanf("Ps", str);

but I can't find a way to do it with the defined value of SIZE (something like

scanf("%SIZEs", str);

I don't want to read character by character until '\0' or '\n' or SIZE -1 (for '\0') characters found.

How can I do so? Thanks!

CodePudding user response:

If the length is a define like in your code, you might want to do something like this:

#define _STRINGIFY(x) #x
#define STRINGIFY(x) _STRINGIFY(x)
[...]
scanf("%" STRINGIFY(SIZE) "s", str);

The #x in the macro definition actually surrounds the parameter of the macro with quotes, so this would result in:

scanf("%" "50" "s", str);

in your case. The extra indirection is necessary for the preprocessor to substitute WIDTH with "50" and not with "WIDTH".

CodePudding user response:

Something line this should work:

char format[10];
sprintf(format, "%%%ds", SIZE);
scanf(format, str);

Explanation:

%% - escaped percent sign
%d - Format specifier for SIZE
s  - The trailing s.

Results in format containing Ps

CodePudding user response:

I want to read a sentence into it up to SIZE characters, using scanf.

Using "%s" will fail to read a sentence like "Hello World!" as "%s" does not save white-spaces.


I don't want to read character by character until '\0' or '\n' or SIZE -1 (for '\0') characters found.

  1. Instead of character by character, use fgets(): fgets(str, sizeof str, stdin); (Additional code needed to consume extra characters.)

  2. If still wanting to use scanf() to read to end of line, see following.
    Be sure the format width parameter is 1 less than the buffer size. @Oka

Example:

#define STRINGIFY_HELPER(x) #x
#define STRINGIFY(x) STRINGIFY_HELPER(x)
#define BUFFER_STRING_LENGTH_MAX 50
#define BUFFER_SIZE (BUFFER_STRING_LENGTH_MAX   1)

char str[BUFFER_SIZE];
str[0] = '\0'; // In case input begins with a '\n'
// Also consume extra non-'\n' characters.         v----v 
scanf("%" STRINGIFY(BUFFER_STRING_LENGTH_MAX) "[\n]%*[\n]", str);
// Consume 1 '\n'
scanf("%*1[\n]");

More robust code would also check the return values of scanf().

  • Related