What does scanf in c language mean?
scanf() is an input function in C language and belongs to the format input function, which inputs data from the keyboard into the specified variable according to the format specified by the user.
Like the printf function, they are declared in the header file stdio.h, so #include
Extended information:
Function prototype
int scanf(const char * restrict format,...);
Function scanf() is from the standard input stream stdio (standard input device, usually pointing to the keyboard) A general subroutine for reading content, which can read multiple characters in a specified format and save them in variables at corresponding addresses.
The first parameter of the function is the format string, which specifies the format of the input, and parses the input corresponding position information according to the format specifier and stores it in the position pointed to by the corresponding pointer in the variable parameter list. Each pointer must be non-null and correspond to the format characters in the string one by one.
Return value
The scanf function returns the number of successfully read data items. If "end of file" is encountered when reading data, it returns EOF.
For example: scanf("%d %d",&a,&b);
The function return value is int type. If both a and b are read successfully, the return value of scanf is 2;
If only a is read successfully, the return value is 1;
If neither a nor b is read If read successfully, the return value is 0;
If an error or end of file is encountered, the return value is EOF. The end of file is Ctrl z or Ctrl d.
Example: Use the scanf function to input data.
#include <stdio.h>int main(void){ int a,b,c; printf("Give me the value of a,b,c seperated with whitespaces:\n"); scanf("%d%d%d",&a,&b,&c); printf("a=%d,b=%d,c=%d\n",a,b,c); return 0;}
&a,&b,&c in & is the addressing operator, &a represents the address of object a in memory, which is an rvalue. The addresses of variables a, b, c are allocated during the compilation stage (the storage order is determined by the compiler).
Note here: If %d in scanf is written consecutively, such as "%d%d%d", when inputting data, the data cannot be separated by commas, only blank characters (space or tab key or enter key) - "2 (space) 3 (tab) 4" or "2 (tab) 3 (enter) 4" etc. If it is "%d,%d,%d", you need to add "," when entering the data, such as "2,3,4".
For more programming related content, please pay attention to the Programming Introduction column on the php Chinese website!
The above is the detailed content of What does scanf mean in c language?. For more information, please follow other related articles on the PHP Chinese website!