Home > Backend Development > C#.Net Tutorial > What is the usage of strncmp function?

What is the usage of strncmp function?

coldplay.xixi
Release: 2020-08-29 11:21:57
Original
16663 people have browsed it

The usage of strncmp function is to compare the first n characters of string str1 and str2. It is a string comparison function. The code is [int strcmp(char *str1,char * str2, int n)].

What is the usage of strncmp function?

[Related learning recommendations: C language tutorial video]

strncmp Function usage:

Function prototype

int strcmp(char *str1,char * str2,int n)
Copy after login

Function

Compare the first n characters of string str1 and str2 character.

Header file

#include <string.h>
Copy after login

Return value

Return value: Return integer value: When str1str2, the return value is >0.

Other instructions: None at the moment.

Description

(1) str1: the first string to be compared;

(2) str2: the second string to be compared String;

(3) n: n is the number of characters for the comparison between the specified str1 and str2;

Example:

#include<string.h>
#include<stdio.h>
int main(void)
{
    char *str1="Hello,I am sky2098,I liking programing!";
    char *str2="Hello,I am sky2098,gramk has gone。";
    int n=13; //指定比较前13个字符
    int inttemp;
    inttemp=strncmp(str1,str2,n);   //将字符串比较的返回值保存在int型变量inttemp中
    if(inttemp<0)
    {
        printf("strlen(str1) < strlen(str2)");
    }
    else if(inttemp>0)
        {
            printf("strlen(str1) > strlen(str2)");
        }
        else
        {
            printf("strlen(str1) == strlen(str2)");
        }
    return 0;
}
Copy after login
#include<string.h>
#include<stdio.h>
int main(void)
{
    char *str1="Hello,I am sky2098,I liking programing!";
    char *str2="Hello,I am sky2098,gramk has gone。";
    int n=strlen(str2);
    int inttemp;
    inttemp=strncmp(str1,str2,n);   //将字符串比较的返回值保存在int型变量inttemp中
    if(inttemp<0)
    {
        printf("strlen(str1) < strlen(str2)");
    }
    else if(inttemp>0)
        {
            printf("strlen(str1) > strlen(str2)");
        }
        else
        {
            printf("strlen(str1) == strlen(str2)");
        }
    return 0;
}
Copy after login

If you want to know more about programming learning, please pay attention to the php training column!

The above is the detailed content of What is the usage of strncmp function?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template