
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C/C++ difference's between strncmp() and strcmp.
strncmp() and strcmp compares two strings using ASCII character comparison. strncmp takes one additional parameter as number to characters upto which a string is to be compared. It is very useful as if a string is not valid, then strcmp will not be able to complete its operation. strcmp searches for end character ('/0') at string end to finish its operation. strncmp uses no. of characters to end its operation and thus is safe.
Example
#include <stdio.h> int main() { char str1[] = "TutorialsPoint"; char str2[] = "Tutorials"; // Compare strings with strncmp() int result1 = strncmp(str1, str2, 9); if(result1 == 0){ printf("str1 == str2 upto 9 characters!\n"); } // Compare strings using strcmp() int result2 = strcmp(str1, str2); if(result2 == 0){ printf("str1 == str2!\n"); } else { if(result2 > 0){ printf("str1 > str2!\n"); } else { printf("str1 < str2!\n"); } } return 0; }
Output
str1 == str2 upto 9 characters! str1 > str2!
- Related Questions & Answers
- Difference between strncmp() and strcmp() in C/C++
- Difference between char s[] and char *s in C
- What's the difference between __PRETTY_FUNCTION__, __FUNCTION__, __func__ in C/C++?
- What's the difference between Tkinter's Tk and Toplevel classes?
- What's the difference between assignment operator and copy constructor in C++?
- Difference between 'struct' and 'typedef struct' in C++?
- C/C++ difference's between "int main()" and "int main(void)"
- What's the difference between sizeof and alignof?
- What's the difference between window.location and document.location?
- What's the difference between Matplotlib.pyplot and Matplotlib.figure?
- strcmp() in C/C++
- Difference between 'struct' and 'typedef struct' in C++ program?
- What's the difference between "STL" and "C++ Standard Library"?
- Difference between C and C++.
- Difference Between C# and C++
Advertisements