为什么 scanf() 总是 return 什么都没有?
Why scanf() always return nothing?
嗨,我需要这方面的帮助:
int t;
t = 1;
char abc[256];
int main() {
scanf("%s", abc);
if(abc == "google") {
printf("%s \n", abc);
system("firefox");
} else
printf("error");
}
它总是return错误,请有人帮忙!
我已经尝试 scanf("%c", &abc);
并且重写了大约 5 次,结果相同。
我是新手,所以这可能是一件非常愚蠢的事情。
不能将两个字符数组的内容与==
进行比较。 ==
将只比较数组的内存地址(在数组到指针衰减之后)。
改用strcmp
:
if (strcmp(abc, "google") == 0) ...
if(abc == "google") {
这与您认为的不同。这将检查指向这两个字符串的 指针 在数值上是否相等。它们永远不会,因为 abc
是在堆栈中分配的,而 "google"
是字符串文字,因此具有静态存储持续时间。
您应该像 ameyCU 指出的那样使用 strcmp
。
一般情况下,不要这样使用scanf
,如果有人传递一个大字符串,你写的代码容易受到缓冲区溢出攻击。
您可能想看看这个关于如何安全使用 scanf
的好文章 post。 How to prevent scanf causing a buffer overflow in C?
常量字符串 "google" 驻留在程序的 .rodata 部分,如果编译时带有所有警告 -Wall,则会收到此警告
google.c:10:12: warning: comparison with string literal results in unspecified behavior [-Waddress]
if(abc == "google") {
此代码等同于
const char* const google_str = "google";
if(abc == google_str)
此处字符串 "google" 和该字符串的地址都是常量。所以你看到你在做指针比较而不是字符串比较。
printf("%p == %p\n", abc, google_str);
此代码将向您显示 abc 位于堆栈中,而 google_str 位于 .rodata 部分。字符串比较应该用
if(0 == strcmp(abc, "google")) {
<code>
if(abc == "google")
</code>
适用于面向对象的语言,如 java 和 dot net
在 c cpp 中,你必须使用 String 的内置库函数,如 strcpy、strcmp
<code> int strcmp(const char *str1, const char *str2) <code>
将 str1 指向的字符串与 str2 指向的字符串进行比较。
因此您将代码修改为
#include <stdio.h>
int t;
t = 1;
char abc[256],a[256];
int main()
{
strcpy(a,"google");
scanf("%s", abc);
if( strcmp("abc","google")==0 ) {
printf("%s \n", abc);
printf("firefox");
} else
printf("error");
return 0;
}
嗨,我需要这方面的帮助:
int t;
t = 1;
char abc[256];
int main() {
scanf("%s", abc);
if(abc == "google") {
printf("%s \n", abc);
system("firefox");
} else
printf("error");
}
它总是return错误,请有人帮忙!
我已经尝试 scanf("%c", &abc);
并且重写了大约 5 次,结果相同。
我是新手,所以这可能是一件非常愚蠢的事情。
不能将两个字符数组的内容与==
进行比较。 ==
将只比较数组的内存地址(在数组到指针衰减之后)。
改用strcmp
:
if (strcmp(abc, "google") == 0) ...
if(abc == "google") {
这与您认为的不同。这将检查指向这两个字符串的 指针 在数值上是否相等。它们永远不会,因为
abc
是在堆栈中分配的,而"google"
是字符串文字,因此具有静态存储持续时间。您应该像 ameyCU 指出的那样使用
strcmp
。一般情况下,不要这样使用
scanf
,如果有人传递一个大字符串,你写的代码容易受到缓冲区溢出攻击。您可能想看看这个关于如何安全使用
scanf
的好文章 post。 How to prevent scanf causing a buffer overflow in C?
常量字符串 "google" 驻留在程序的 .rodata 部分,如果编译时带有所有警告 -Wall,则会收到此警告
google.c:10:12: warning: comparison with string literal results in unspecified behavior [-Waddress]
if(abc == "google") {
此代码等同于
const char* const google_str = "google";
if(abc == google_str)
此处字符串 "google" 和该字符串的地址都是常量。所以你看到你在做指针比较而不是字符串比较。
printf("%p == %p\n", abc, google_str);
此代码将向您显示 abc 位于堆栈中,而 google_str 位于 .rodata 部分。字符串比较应该用
if(0 == strcmp(abc, "google")) {
<code>
if(abc == "google")
</code>
适用于面向对象的语言,如 java 和 dot net 在 c cpp 中,你必须使用 String 的内置库函数,如 strcpy、strcmp
<code> int strcmp(const char *str1, const char *str2) <code>
将 str1 指向的字符串与 str2 指向的字符串进行比较。
因此您将代码修改为
#include <stdio.h>
int t;
t = 1;
char abc[256],a[256];
int main()
{
strcpy(a,"google");
scanf("%s", abc);
if( strcmp("abc","google")==0 ) {
printf("%s \n", abc);
printf("firefox");
} else
printf("error");
return 0;
}