给定的任务是从用户那里获取一个整数作为输入,并使用C语言打印该整数。
在下面的程序中,以C语言显示将整数作为用户输入的语法和过程。
脚步:
- 询问时,用户输入一个整数值。
- 该值是通过scanf()方法从用户获取的。 C语言中的scanf()方法按照指定的类型从控制台读取值。
句法:
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.
- 对于整数值,将X替换为int类型。然后,scanf()方法的语法如下:
句法:
scanf("%d", &variableOfIntType);
- 现在,此输入值存储在variableOfIntType中。
- 现在要打印此值,将使用printf()方法。 C语言中的printf()方法在控制台屏幕上打印作为参数传递给它的值。
句法:
printf("%X", variableOfXType);
- 对于整数值,将X替换为int类型。然后,printf()方法的语法如下:
句法:
printf("%d", variableOfIntType);
- 因此,成功读取并打印了整数值。
程序:
C
// C program to take an integer
// as input and print it
#include
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;
}
输出:
Enter the integer: 10
Entered integer is: 10
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。