Cómo leer e imprimir un valor entero en C

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:

  1. El usuario ingresa un valor entero cuando se le solicita.
  2. 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.
    
  3. 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);
    
  4. Este valor ingresado ahora se almacena en variableOfIntType .
  5. 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);
    
  6. 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);
    
  7. 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

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *