Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8734793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:55:02+00:00 2026-06-13T09:55:02+00:00

I wrote this small piece of code in C to test memcmp() strncmp() strcmp()

  • 0

I wrote this small piece of code in C to test memcmp() strncmp() strcmp() functions in C.

Here is the code that I wrote:

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

int main() {
        char *word1="apple",*word2="atoms";

        if (strncmp(word1,word2,5)==0)
                printf("strncmp result.\n");
        if (memcmp(word1,word2,5)==0)
                printf("memcmp result.\n");
        if (strcmp(word1,word2)==0)
                printf("strcmp result.\n");
}

Can somebody explain me the differences because I am confused with these three functions?

My main problem is that I have a file in which I tokenize its line of it,the problem is that when I tokenize the word “atoms” in the file I have to stop the process of tokenizing.

I first tried strcmp() but unfortunately when it reached to the point where the word “atoms” were placed in the file it didn’t stop and it continued,but when I used either the memcmp() or the strncmp() it stopped and I was happy.

But then I thought,what if there will be a case in which there is one string in which the first 5 letters are a,t,o,m,s and these are being followed by other letters.

Unfortunately,my thoughts were right as I tested it using the above code by initializing word1 to “atomsaaaaa” and word2 to atoms and memcmp() and strncmp() in the if statements returned 0.On the other hand strcmp() it didn’t. It seems that I must use strcmp().

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:55:03+00:00Added an answer on June 13, 2026 at 9:55 am

    In short:

    • strcmp compares null-terminated C strings
    • strncmp compares at most N characters of null-terminated C strings
    • memcmp compares binary byte buffers of N bytes

    So, if you have these strings:

    const char s1[] = "atoms\0\0\0\0";  // extra null bytes at end
    const char s2[] = "atoms\0abc";     // embedded null byte
    const char s3[] = "atomsaaa";
    

    Then these results hold true:

    strcmp(s1, s2) == 0      // strcmp stops at null terminator
    strcmp(s1, s3) != 0      // Strings are different
    strncmp(s1, s3, 5) == 0  // First 5 characters of strings are the same
    memcmp(s1, s3, 5) == 0   // First 5 bytes are the same
    strncmp(s1, s2, 8) == 0  // Strings are the same up through the null terminator
    memcmp(s1, s2, 8) != 0   // First 8 bytes are different
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this small piece of code to test something: #include <stdio.h> int main()
I wrote this program: #include <stdio.h> /*Part B Write a program that: defines an
I've been working on this small piece of code that seems trivial but still,
I've wrote a small piece of code that reads the current battery charge/discharge on
I wrote this small C++ program and built it(Release) #include<iostream> int main(){ std::cout<<Hello World;
I wrote small piece of code which should toggle reporting box on my page.
Here is a small piece of code, the parent process write pipe and child
I am new to jquery . I wrote small piece of code <div id=tablediv><table><tr><td><h2>Select
I'm trying to write a small piece of code that passes a small formula
I have a small piece of code that checks if a computer is alive

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.