473,396 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Reg. strncmp

Hi All,

I have doubt in strncmp.
consider the following prog:

int main()
{

int i;
i = strncmp("Nstring","Astring",6);
printf("%d", i);
}

Here o/p is 13, now if i modify the prog like:

i = strncmp("Nstrisg","Astring",6); O/P is 5 why??

ok ok.. got it !! while typing the question i got the ANSWER.
it print the difference in the character.. Right?
Aug 18 '07 #1
3 1880
JosAH
11,448 Expert 8TB
Hi All,

I have doubt in strncmp.
consider the following prog:

int main()
{

int i;
i = strncmp("Nstring","Astring",6);
printf("%d", i);
}

Here o/p is 13, now if i modify the prog like:

i = strncmp("Nstrisg","Astring",6); O/P is 5 why??

ok ok.. got it !! while typing the question i got the ANSWER.
it print the difference in the character.. Right?
That would clarify the first comparison: 'N'-'A' == 13 but what about the second
comparison then? It seems as if strncmp compares starting at the last character:
's'-'n' == 5 but that isn't a valid way to compare two strings lexicographically.

kind regards,

Jos
Aug 18 '07 #2
That would clarify the first comparison: 'N'-'A' == 13 but what about the second
comparison then? It seems as if strncmp compares starting at the last character:
's'-'n' == 5 but that isn't a valid way to compare two strings lexicographically.

kind regards,

Jos

OOps typing mistake.. Sorry man.. its:
i = strncmp("Nstrisg","Nstring",6); now for i = strncmp("Nstrisg","Astring",6);
Answer is 13 only..
Aug 18 '07 #3
JosAH
11,448 Expert 8TB
OOps typing mistake.. Sorry man.. its:
i = strncmp("Nstrisg","Nstring",6); now for i = strncmp("Nstrisg","Astring",6);
Answer is 13 only..
Ah, ok, that explains it all: the diference of the two characters that are not equal
is the return value of the strncmp function: 'N'-'A' == 13 and 's'-'n' == 5.

kind regards,

Jos
Aug 18 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

26
by: pembed2003 | last post by:
Hi, I have an application where I use the strncmp function extensively to compare 2 string to see if they are the same or not. Does anyone know a faster replacement for strncmp? I notice there is...
9
by: jacob navia | last post by:
What is the result of that? I am reimplementing the C library, and I would like to know. Now I have it to return zero. Is that correct? The standard says: >> The strncmp function compares...
1
by: Bert | last post by:
I have a question about buffer overflows with strcmp or strncmp (strnicmp). It's best described by providing an example Example: There is a string "sBuf" of length 5000. In a for loop from 0 to...
4
by: Preets | last post by:
Hi, Can anybody please tell me the difference between memcmp() and strncmp() functions ?
12
by: Krumble Bunk | last post by:
Hi all, Having some trouble with a seemingly-simple task. Need to have a basic CLI functionality as per: prompt <- \n prompt <- \n promptquit <- should quit as per my snippet, but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.