How can I use C to assign a variable to a user?

I am wondering if there is a function that I could use in the standard library. I need another library (BTW, I'm developing for unix).

+3
source share
4 answers

See the function scanf()in stdio.h. A format specifier is required, for example printf(), and variable pointers to store user input in

+3
source

Use scanf()

Format: int scanf ( const char * format, ... );

Read formatted data from stdin. Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format tag within the format string.

Example:

#include <stdio.h>
int main(void)
{
    int n;
    printf("Enter the value to be stored in n: ");
    scanf("%d",&n);
    printf("n= %d",n);
}

However, look at this.

+2
source

' - scanf(); .

, scanf() . - , .

fgets(), ( ). sscanf() . , "12Z", , , , , , . scanf() .

+1

, , C, - Prasoon, , , , .

scanf (const char * format,...); , Prasoon :

scanf("%d",&n);

When using this parameter, "% d" indicates that you are going to read an integer (see wikipedia for a complete list of formats).

The second argument (note that ... indicates that you can send any number of arguments) specify the address of the variable into which you are going to enter the user record.

0
source

Source: https://habr.com/ru/post/1741029/


All Articles