Scanf prototype int scanf( const char. format. ); The scanf function reads the data from stdin and stores the values into the respective variables. It is defined in header file. Scanf Parameters. Format: Pointer to a null-terminated character string that specifies how to read the input.It consists of format specifiers starting with%. Possible duplicate of Scanf for double not working in Dev C – phuclv Sep 9 '18 at 4:25 Problems with scanf and doubles – phuclv Sep 9 '18 at 4:26 add a comment.
- N/A: i: matches an integer. The format of the number is the same as expected by strtol with the value 0 for the base argument (base is determined by the first characters parsed). U: matches an unsigned decimal integer. The format of the number is the same as expected by strtoul with the value 10 for the base argument.
- Vamos ahora a hablar un poco del uso de scanf para leer valores y algunas dificultades que esto presenta. Uso de scanf para leer valores y cadenas de texto (string) en C. El operador scanf al igual que printf fueron ideados para el lenguaje C (no para C).
Built-in types (also called fundamental types) are specified by the C++ language standard and are built into the compiler. Built-in types are not defined in any header file. Built-in types are divided into three categories: integral, floating point, and void. Integral types are capable of handling whole numbers. Floating point types are capable of specifying values that may have fractional parts.
The void type describes an empty set of values. No variable of type void can be specified — it is used primarily to declare functions that return no values or to declare generic pointers to untyped or arbitrarily typed data. Any expression can be explicitly converted or cast to type void. However, such expressions are restricted to the following uses:
- An expression statement. (For more information, see Expressions.)
- The left operand of the comma operator. (For more information, see Comma Operator.)
- The second or third operand of the conditional operator (
? :
). (For more information, see Expressions with the Conditional Operator.)
The following table explains the restrictions on type sizes in relation to each other. These restrictions are mandated by the C++ standard and are independent of the Microsoft implementation. The absolute size of certain built-in types is not specified in the standard.
C++ Scanf String
Built-in type size restrictions
Category | Type | Contents |
---|---|---|
Integral | char | Type char is an integral type that usually contains members of the basic execution character set — By default, this is ASCII in Microsoft C++. The C++ compiler treats variables of type char, signed char, and unsigned char as having different types. Variables of type char are promoted to int as if they are type signed char by default, unless the /J compilation option is used. In this case, they are treated as type unsigned char and are promoted to int without sign extension. |
bool | Type bool is an integral type that can have one of the two values true or false. Its size is unspecified. | |
short | Type short int (or simply short) is an integral type that is larger than or equal to the size of type char, and shorter than or equal to the size of type int. Objects of type short can be declared as signed short or unsigned short. Signed short is a synonym for short. | |
int | Type int is an integral type that is larger than or equal to the size of type short int, and shorter than or equal to the size of type long. Objects of type int can be declared as signed int or unsigned int. Signed int is a synonym for int. | |
__int8, __int16, __int32, __int64 | Sized integer __int n , where n is the size, in bits, of the integer variable. __int8, __int16, __int32 and __int64 are Microsoft-specific keywords. Not all types are available on all architectures. (__int128 is not supported.) | |
long | Type long (or long int) is an integral type that is larger than or equal to the size of type int. (On Windows long is the same size as int.) Objects of type long can be declared as signed long or unsigned long. Signed long is a synonym for long. | |
long long | Larger than an unsigned long. Objects of type long long can be declared as signed long long or unsigned long long. signed long long is a synonym for long long. | |
wchar_t, __wchar_t | A variable of type wchar_t designates a wide-character or multibyte character type. By default, wchar_t is a native type, but you can use /Zc:wchar_t- to make wchar_t a typedef for unsigned short. The __wchar_t type is a Microsoft-specific synonym for the native wchar_t type. Use the L prefix before a character or string literal to designate the wide-character type. | |
Floating point | float | Type float is the smallest floating point type. |
double | Type double is a floating point type that is larger than or equal to type float, but shorter than or equal to the size of type long double. Microsoft-specific: The representation of long double and double is identical. However, long double and double are separate types. | |
long double | Type long double is a floating point type that is larger than or equal to type double. |
Microsoft Specific
The following table lists the amount of storage required for built-in types in Microsoft C++. In particular, note that long is 4 bytes even on 64-bit operating systems.
Sizes of built-in types
Type | Size |
---|---|
bool, char, unsigned char, signed char, __int8 | 1 byte |
__int16, short, unsigned short, wchar_t, __wchar_t | 2 bytes |
float, __int32, int, unsigned int, long, unsigned long | 4 bytes |
double, __int64, long double, long long | 8 bytes |
END Microsoft Specific
See Data Type Ranges for a summary of the range of values of each type.
For more information about type conversion, see Standard Conversions.
See also
For the input of specific types of variables in the C programming language, you’ll find that the scanf() function comes in handy. It’s not a general-purpose input function, and it has some limitations, but it’s great for testing code or grabbing values.
In a way, you could argue that scanf() is the input version of the printf() function. For example, it uses the same conversion characters (the % placeholder-things). Because of that, scanf() is quite particular about how text is input. Here’s the format:
Scary, huh? Just ignore it for now. Here’s a less frightening version of the format:
In this version, placeholder is a conversion character, and variable is a type of variable that matches the conversion character. Unless it’s a string (char array), the variable is prefixed by the & operator.
The scanf() function is prototyped in the stdio.h header file, so you must include that file when you use the function.
Here are some scanf() examples:
The preceding statement reads an integer value into the variable highscore. This assumes that highscore is an int variable.
The preceding scanf() statement waits for a floating-point value to be input, which is then stored in the temperature variable.
In the preceding line, scanf() accepts the first character input and stores it in the key variable.
The %s placeholder is used to read in text, but only until the first white space character is encountered. So a space or a tab or the Enter key terminates the string. (That sucks.) Also, firstname is a char array, so it doesn’t need the & operator in the scanf() function.
How to read a string with scanf()
One of the most common ways to put the scanf() function to use is to read in a chunk of text from standard input. To meet that end, the %s conversion character is used — just like in printf(), but with input instead of output.
SCANF() SWALLOWS A STRING
Exercise 1: Type the source code from scanf() Swallows a String into a new project, ex0712, in Code::Blocks. Build and run.
Line 5 declares a char array — a string variable — named firstname. The number in the brackets indicates the size of the array, or the total number of characters that can be stored there. The array isn’t assigned a value, so it’s created empty. Basically, the statement at Line 5 sets aside storage for up to 15 characters.
The scanf() function in Line 8 reads a string from standard input and stores it in the firstname array. The %s conversion character directs scanf() to look for a string as input, just as %s is a placeholder for strings in printf()’s output.
Scanf Char C
Exercise 2: Modify the source code from scanf() Swallows a String so that a second string is declared for the person’s last name. Prompt the user for their last name as well, and then display both names by using a single printf() function.
- The number in the brackets (refer to Line 5) gives the size of the char array, or the length of the string, plus one.
- When you create a char array, or string variable, ensure that you create it of a size large enough to hold the text. That size should be the maximum number of characters plus one.
- The reason for increasing the char array size by one is that all strings in C end with a specific termination character. It’s the NULL character, which is written as . The compiler automatically adds the to the end of string values you create in your source code, as well as text read by various text-input functions.You must remember to add room for that character when you set aside storage for string input.
How to read values with scanf()
The scanf() function can do more than read strings. It can read in any value specified by a conversion character.
SCANF() EATS AN INTEGER
In scanf() Eats an Integer, the scanf() function reads in an integer value. The %d conversion character is used, just like printf() — indeed, it’s used in Line 9. That character directs scanf() to look for an int value for variable fav.
Exercise 3: Create a project, ex0714, using the source code shown in scanf() Eats an Integer. Build and run. Test the program by typing various integer values, positive and negative.
Perhaps you’re wondering about the ampersand (&) in the scanf() function. The character is a C operator — specifically, the memory address operator. It’s one of the advanced features in C that’s related to pointers. An ampersand must prefix any variable specified in the scanf() function. The exception is an array, such as the firstname char array in scanf() Eats an Integer.
Try running the program again, but specify a decimal value, such as 41.9, or type text instead of a number.
The reason you see incorrect output is that scanf() is very specific. It fetches only the variable type specified by the conversion character. So if you want a floating-point value, you must specify a float variable and use the appropriate conversion character; %f, in that case.
Scanf C Function
Exercise 4: Modify the source code from scanf() Eats an Integer so that a floating-point number is requested, input, and displayed.
- You don’t need to prefix a char array variable with an ampersand in the scanf() function; when using scanf() to read in a string, just specify the string variable name.
- The scanf() function stops reading text input at the first white space character, space, tab, or Enter key.