Complementary task for topic: 1

M Nemeth · 2023-08-29 15:21:04.603220'

scanf: integer I.

scanf: integer I.

Task: Scan and display two integers:
Write a C program that uses scanf() to scan two integers from the user and printf() to display the entered integers.

Hint: scanf() works like printf(), but you need a & sign before the variable.
Later you will understand that is needed because the scanf needs a place to STORE the variable, not the value of the variable, & sign is an operator that gives us the memory address. This address is needed to be passed to scanf() so scanf can SAVE the read value there. (and so the value can change)

Solution
#include 

int main() {
    int num1, num2;
    
    printf("Enter the first integer: ");
    scanf("%d", &num1);
    
    printf("Enter the second integer: ");
    scanf("%d", &num2);
    
    printf("You entered: %d and %d\n", num1, num2);
    
    return 0;
}



Explanation
In this example, the program uses scanf() to scan two integers entered by the user. The %d format specifier is used to indicate that the input should be interpreted as an integer. The entered values are stored in the variables num1 and num2. Then, the program uses printf() to display the entered integers using the %d format specifier.
< < previous    next > >