这是C代码,我用gcc编译
char *a="a";
char *d="d";
printf("%d\n", strcmp("a", "d"));
printf("%d\n", strcmp(a, "d"));
printf("%d\n", strcmp(a, d));
Run Code Online (Sandbox Code Playgroud)
当我用-O输出编译时
-1
-3
-1
Run Code Online (Sandbox Code Playgroud)
当我编译没有-O那么输出是
-1
-3
-3
Run Code Online (Sandbox Code Playgroud)
为什么输出不同,代码是strcmp什么?
我是编程的新手,目前正在学习C.我已经在这个问题上工作了一个星期了,我似乎无法理解逻辑.这直接来自我正在使用的书:
构建一个使用字符串数组来存储以下名称的程序:
- "佛罗里达"
- "俄勒冈"
- "Califoria"
- "格鲁吉亚"
使用前面的字符串数组,编写自己的
sort()函数,使用该strcmp()函数按字母顺序显示每个状态的名称.
所以,让我说我有:
char *statesArray[4] = {"Florida", "Oregon", "California", "Georgia"};
Run Code Online (Sandbox Code Playgroud)
我应该嵌套for循环strcmp(string[x], string[y])...吗?我已经入侵并砍掉了.我无法绕过解决这个问题所需的算法,甚至有效.帮助很多赞赏!!!
我正在尝试检查从stdin读取的行是否以"login:"开头,但strcmp似乎不起作用.
char s1[20], s2[20];
fgets(s1, 20, stdin);
strncpy(s2,s1,6);
strcmp(s2, "login:");
if( strcmp(s2, "login:") == 0)
printf("s2 = \"login:\"\n");
else
printf("s2 != \"login:\"\n");
Run Code Online (Sandbox Code Playgroud)
我不关心"login:"之后会发生什么,我只是想确定命令是如何给出的.我究竟做错了什么?
下面是Matlab控制台的输出.两个字符串都是相同的:'@TBMA3'.然而Matlab的strcmp函数0在比较时会返回.为什么?
K>> str='@TBMA3'
str =
@TBMA3
K>> method.fhandle
ans =
@TBMA3
K>> strcmp(method.fhandle, str)
ans =
0
Run Code Online (Sandbox Code Playgroud) 我的印象是比较运算符没有为C风格的字符串定义,这就是为什么我们使用类似的东西strcmp().因此,以下代码在C和C++中是非法的:
if("foo" == "foo"){
printf("The C-style comparison worked.\n");
}
if("foo" == "bob"){
printf("The C-style comparison produced the incorrect answer.\n");
} else {
printf("The C-style comparison worked, strings were not equal.\n");
}
Run Code Online (Sandbox Code Playgroud)
但我在使用GCC和VS 2015的两个Codeblock中测试了它,编译为C和C++.两者都允许代码并产生正确的输出.
比较C风格的字符串是否合法?或者它是否是允许此代码工作的非标准编译器扩展?
如果这是合法的,那么为什么人们strcmp()在C中使用?
我用C编写了这段代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main()
{
char string1[20];
char string2[20];
strcpy(string1, "Heloooo");
strcpy(string2, "Helloo");
printf("%d", strcmp(string1, string2));
return(0);
}
Run Code Online (Sandbox Code Playgroud)
控制台打印值是1还是字符和字符ASCII值之间的差值,即111?在这个网站上写道,这应该给出111,但当我在我的笔记本电脑上运行时,它显示1.为什么?o\0
我可以比较#definevarible和char *strcmp如下.
#include<stdio.h>
#include<string.h>
#define var "hello"
int main()
{
char *p ="hello";
if(strcmp(p,var)==0)
printf("same\n");
else
printf("not same\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如上例所示#define,是否存在任何风险comapre char *?
我有以下代码,它应该作为命令获取用户输入,然后检查命令是否是预定义的.但是,对于输入的任何命令,输出是" 您要求帮助 ".我认为问题可能与我将用户输入字符串与设置字符串进行比较的方式有关,但我仍然需要帮助解决问题.
char command[10];
char set[10];
char set1[10];
strcpy(set, "help");
strcpy(set1, "thanks");
int a = 0;
while (a != 1)//the program should not terminate.
{
printf("Type command: ")
scanf("%s", command);
if (strcmp(set, command))
{
printf("You asked for help");
}
else if (strcmp(set1, command))
{
printf("You said thanks!");
}
else
{
printf("use either help or thanks command");
}
}
Run Code Online (Sandbox Code Playgroud) 我想知道为什么strcmp()在同一个函数中多次使用会返回不同的值.以下是该计划.第一种情况我知道为什么它打印-6.但在第二种情况下,为什么打印-1?
#include<stdio.h>
#include<string.h>
int main()
{
char a[10] = "aa";
char b[10] = "ag";
printf("%d\n",strcmp(a, b));
printf("%d\n",strcmp("aa","ag"));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它产生的输出低于
[sxxxx@bhlingxxx test]$ gcc -Wall t51.c
[sxxxx@bhlingxxx test]$ ./a.out
-6
-1
Run Code Online (Sandbox Code Playgroud)
为什么第二个strcmp()-1 的输出?这是在这里玩的编译器吗?如果是这样,它的确切优化是什么?
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int main()
{
string x;
cin>>x;
if(strcmp(&x.at(0), "M") == 0)
{
cout<<"midget ";
}
else if(strcmp(&x.at(0), "J") == 0)
{
cout<<"junior ";
}
else if(strcmp(&x.at(0), "S") == 0)
{
cout<<"senior ";
}
else
{
cout<<"invalid code";
}
if(strcmp(&x.at(1), "B") == 0)
{
cout<<"boys";
}
else
{
cout<<"girls";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我已经使用上面的代码来比较MB应该返回"侏儒男孩",但它继续下降到其他并返回"无效的codeboys".不知何故第二个条件正常.我的诊断告诉我,在第一次比较它返回66.我猜这是"M"的ASCII代码.但是我现在如何解决我的问题呢?