La tarea dada es tomar un entero como entrada del usuario e imprimir ese entero en lenguaje C.
En el programa a continuación, la sintaxis y los procedimientos para tomar el número entero como entrada del usuario se muestran en lenguaje C.
Pasos:
- El usuario ingresa un valor entero cuando se le solicita.
- Este valor se toma del usuario con la ayuda del método scanf() . El método scanf(), en C, lee el valor de la consola según el tipo especificado.
Sintaxis:
scanf("%X", &variableOfXType); where %X is the format specifier in C It is a way to tell the compiler what type of data is in a variable and & is the address operator in C, which tells the compiler to change the real value of this variable, stored at this address in the memory.
- Para un valor entero, la X se reemplaza con el tipo int. La sintaxis del método scanf() se convierte en la siguiente:
Sintaxis:
scanf("%d", &variableOfIntType);
- Este valor ingresado ahora se almacena en variableOfIntType .
- Ahora, para imprimir este valor, se usa el método printf() . El método printf(), en C, imprime el valor que se le pasa como parámetro, en la pantalla de la consola.
Sintaxis:
printf("%X", variableOfXType);
- Para un valor entero, la X se reemplaza con el tipo int. La sintaxis del método printf() se convierte en la siguiente:
Sintaxis:
printf("%d", variableOfIntType);
- Por lo tanto, el valor entero se lee e imprime con éxito.
Programa:
C
// C program to take an integer // as input and print it #include <stdio.h> int main() { // Declare the variables int num; // Input the integer printf("Enter the integer: "); scanf("%d", &num); // Display the integer printf("Entered integer is: %d", num); return 0; }
Producción:
Enter the integer: 10 Entered integer is: 10
Publicación traducida automáticamente
Artículo escrito por RishabhPrabhu y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA