Discussion
What will be the output of the following C code?
char str1[15];
char str2[15];
int mat;
strcpy(str1, "abcdef");
strcpy(str2, "ABCDEF");
mat = strncmp(str1, str2, 4);
if(mat < 0)
printf("str1 is not greater than str2");
else if(mat > 0)
printf("str2 is not greater than str1");
else
printf("both are equal");
Answer: B
Correct answer: str2 is not greater than str1
strncmp compares the first four characters, and the very first pair settles it: 'a' is 97 while 'A' is 65, so the result is positive. The first test mat < 0 therefore fails and the else if (mat > 0) succeeds. Case matters because the comparison is by character code, not alphabetically.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development