将两个日期作为 C 中的 char 数组进行比较

标签 c string comparison

请耐心等待,这是我的第二个 stackoverflow 问题,如果我做错了,请指出正确的方向

我有两个日期作为字符。

我打印出日期:

printf("%s - %s\n",tmpPtr->date, currentDate);

我有一个 总是 执行的 if 语句

if(tmpPtr->date != currentDate) {       // perhaps strcmp(), don't know
  printf("Dates are not equal\n");
}

但这不可能是真的,因为这些是我的结果:

27/12/2015 - 27/12/2015
Dates are not equal
27/12/2015 - 27/12/2015
Dates are not equal
28/12/2015 - 27/12/2015
Dates are not equal
29/12/2015 - 28/12/2015
Dates are not equal
29/12/2015 - 29/12/2015
Dates are not equal
29/12/2015 - 29/12/2015
Dates are not equal
30/12/2015 - 29/12/2015
Dates are not equal
31/12/2015 - 30/12/2015
Dates are not equal
31/12/2015 - 31/12/2015
Dates are not equal

这不可能是真的,因为有些日期是相等的?

我是否正确地比较了字符串?它只是比较内存分配还是类似的东西?

最佳答案

为了比较字符串,像这样使用 strcmp() :

if (strcmp(tmpPtr->date,currentDate) != 0) {      
    printf("Dates are not equal\n");
}

关于将两个日期作为 C 中的 char 数组进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36357245/

相关文章:

c - 如何在函数中打印出大于9?

c# - 无法将类型 'string' 隐式转换为 'System.Windows.Forms.ColumnHeader

java - 检查 2 个数字是否在彼此的 1% 以内

c - 动态分配数组指针

c - 函数终止时出现段错误

c - 如何解决对 `AES_ctr128_encrypt'的 undefined reference

c#正则表达式问题

regex - 提取逗号和引号之间的文本,忽略 perl 中的转义字符

java - 使用 Arrays.sort() 方法对类型对象数组进行排序

vb.net - 搜索特殊字符(例如accute等)的变体时比较字符串