Bifalgorithm post · Computer science one · Basic I/O

How does scanf() read integer, character, and floating-point input in C?

How does scanf() read integer, character, and floating-point input in C? (Computer science one · Basic I/O) — "15.5. Basic I/O The standard I/O library (stdio.…

Ideas in this flowchart

  • La biblioteca de E/S estándar en C facilita la entrada y salida de datos.
  • stdio.h contiene funciones para la entrada y la salida estándar.
  • printf() muestra mensajes y datos en la salida estándar.
  • scanf() obtiene datos desde la entrada estándar.
  • scanf() puede solicitar datos, esperar la tecla Enter y leer el búfer de entrada.
  • El valor leído se convierte al tipo indicado y se almacena en una variable.
  • scanf() utiliza marcadores de formato similares a los de printf().
  • El marcador %d se utiliza para valores de tipo int.
  • Al pasar una variable a scanf(), se requiere colocar & delante de ella.
  • Omitir & puede provocar un error de segmentación por acceso ilegal a memoria.
  • El marcador %c permite leer un carácter y %lf permite leer valores double.
  • Un ejemplo lee un número fraccionario con scanf("%lf", &x).
  • El formato introducido por el usuario debe coincidir con el formato esperado por scanf().
  • Un marcador incorrecto puede producir resultados basura porque la entrada se interpreta incorrectamente.
  • La entrada no numérica cuando se espera un número puede interpretarse como cero.
Feed/

How does scanf() read integer, character, and floating-point input in C?

No cover

Channel

Computer science one

1 page

"15.5. Basic I/O The standard I/O library (stdio.h) contains many functions that facilitate input and output including printf() for standard output and scanf() (scan formatted input) for standard input. The printf() function works exactly as discussed in Section 2.4.3. The scanf() function works using similar placeholders as printf(). To illustrate how it works, consider the following lines of code: 1 int a; 2 printf(\"Please enter a number: \"); 3 scanf(\"%d\", &a); The printf() statement prompts the user for an input. The scanf() then executes and the program waits for the user to enter input. The user is free to start typing. When the user is done, they hit the enter key at which point the program resumes and reads the input from the standard input buffer, converts the value entered by the user into an integer and places the result in the variable a where it can now be used by the remainder of the program. A few points of interest. First, the same placeholder as printf() was used, %d for int values. However, when we “passed” the variable a to scanf() we placed an ampersand, & in front of it. This is passing the variable by reference and we’ll explore that concept further in Chapter 18, but for now just know that when using variables with scanf(), an ampersand is required. Failure to place an ampersand in front of a variable with scanf() will likely result in a segmentation fault (an illegal memory access). You can use the same placeholder, %c with scanf() to read in a single character as well. However, for floating point numbers, in particular double types, the placeholder %lf must be used (which stands for long float, a double precision number). Failure to use the correct placeholder may result in garbage results as the input will be interpreted incorrectly. Another example: 1 double x; 2 printf(\"Please enter a fractional number: \"); 3 scanf(\"%lf\", &x); Another potential problem is that scanf() expects a certain format (thus its name). If we prompt the user for a number but they just start mashing the keyboard giving non-numerical input, we may get incorrect results. scanf() will interpret the input as zero. It may be very difficult to distinguish between the case of a user actually entering"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.