Use of strcmp/strncmp and strcat/strncat functions in C

#include <stdio.h>
#include <string.h>

int demo(void)
{
    
    
    char a[10] = "abc";
    char b[100] = "hello12345657689";
//    strcat(a, b);//将a和b合并为一个字符串,结果放入a
    strncat(a, b, 6); 
    printf("%s\n", a);
    
    return 0;
}


int main(int argc, const char *argv[])
{
    
    
    char a[100] = "abc";
    char b[100] = "abc123";
    
    /* 不能通过这种方式比较两个字符串是否相同 */
    if (a == b)
    {
    
    
        ;
    }
    
    /* 这里只是地址的比较 */
    if(strcmp(a, b) == 0)//比较a和b是否相同
//    if (strncmp(a, b, 3) == 0)
    {
    
    
        printf("相同\n");
    }
    else
    {
    
    
        printf("不相同\n");
    }
    
	puts("*******************************************");
	demo();

    return 0;
}



Test Results

Insert picture description here

Guess you like

Origin blog.csdn.net/zxy131072/article/details/108489012