我正在尝试运行以下代码,但在执行过程中,代码不会进入 if 条件。为什么代码在运行时没有进入if条件?我已经标记了问题状况。
在 Windows 10 上运行此程序。线程模型:posix gcc 版本 5.1.0 (tdm64-1)
我尝试过使用三元运算符和带有不同字符串的 if 语句,而 strchr 在这种情况下可以正常工作。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main() {
static char str[] = "hello world";
static char inputTime[] = "12:05:10PM";
char *result = strchr(str, 'w');
long int tempNum = 0;
char *token, tempStr[10], delimit[] = ":";
if (strchr(str, 'w'))
printf("\nFound w");
else
printf("\nDid not find w");
(strchr(inputTime, 'P')) ? printf("\nTrue") : printf("\nFalse");
token = strtok(inputTime, delimit);
if (strchr(inputTime, 'P')) {
printf("Found PM\n");
tempNum = strtol(token, NULL, 10);
if (tempNum != 12)
tempNum += 12;
sprintf(tempStr, "%lu", tempNum);
}
printf("\ntempStr: %s", tempStr);
}
上面的代码给了我这个输出: C:\Users\XX\Documents\Tests\c-programming>a.exe
发现 w
True
tempStr: σ@