
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
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 Articles
- Difference between strncmp() and strcmp() in C/C++
- Difference between char s[] and char *s in C
- Difference between S Corp and C Corp
- strcmp() in C/C++
- Difference Between PGP and S/MIME
- C/C++ difference's between "int main()" and "int main(void)"
- What's the difference between "STL" and "C++ Standard Library"?
- What is strncmp() Function in C language?
- What's the difference between assignment operator and copy constructor in C++?
- Difference between SAP ERP and SAP S/4 HANA
- 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?
- What's the difference between "!!" and "?" in Kotlin?
- What's the Difference between Skills and Competencies?

Advertisements