我目前正在学习 C,所以我想制作一个程序,要求用户输入一个字符串并输出输入的字符数,代码编译得很好,当我只输入 1 个字符时,它就可以了,但是当我输入2个或更多字符,无论我输入多少个字符,它总是说只有一个字符,然后就崩溃了。这是我的代码,我不知道出了什么问题。
int main(void)
{
int siz;
char i[] = "";
printf("Enter a string.\n");
scanf("%s", i);
siz = sizeof(i)/sizeof(char);
printf("%d", siz);
getch();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我目前正在学习编程,所以如果有一种方法可以使用相同的 scanf() 函数来完成它,我会很感激,因为我还没有学会如何使用任何其他函数,并且可能不会理解它是如何工作的。
gets()在我的代码中使用时,编译器大喊
warning: the 'gets' function is dangerous and should not be used.`
Run Code Online (Sandbox Code Playgroud)
和
warning: ‘gets’ is deprecated (declared at /usr/include/stdio.h:638)
[-Wdeprecated-declarations]
Run Code Online (Sandbox Code Playgroud)
有什么具体原因吗?
可能重复:
如何使用scanf输入空格?
printf("请输入书名\n"); 的scanf( "%S",BOOKNAME);
我在这样的数据里面: - C编程
但为什么输出这样的数据: - C.
失去编程(字符串)?
为什么
谢谢.
为什么在尝试编译程序时收到此警告?
%lf' expects argument of type 'double', but argument 2 has type 'double *'
我正在使用CodeBlocks IDE,但是这些行提供了大量的信息:
double calculate1 = mealPrice * (double)(percentage)/100;
printf("The tip that you should leave is %lf \n", &calculate1);
Run Code Online (Sandbox Code Playgroud)
我是C编程的新手,仍然在学习东西。
// CS 262, Lab Section <208>
// Lab 2
#include <stdio.h>
#include <stdlib.h>
int main(){
printf("Enter the price of the meal: \n");
double mealPrice = 0;
scanf("%lf\n", &mealPrice);
printf("Now enter the tip percentage: \n");
int percentage = 0;
scanf("%d\n", &percentage);
//Calculates tip amount in double, int, and …Run Code Online (Sandbox Code Playgroud) 之间有什么区别吗?
scanf("%s", c);
Run Code Online (Sandbox Code Playgroud)
和
scanf(" %s", c);
Run Code Online (Sandbox Code Playgroud)
它可以以任何方式影响我的程序吗?提前致谢.