In the above code the operator == is used to compare the value of string. But this is not allowed. This is because in C program string is represented as array of characters. So the operate like comparison, assignment and so on cannot be used to manipulate array as whole. So the above code does not work. In order to achieve the string comparison instead of using == C programmers must use function named as strcmp(). So the above code must be coded as:
Code
#include <stdio.h>
#include <string.h>
Copyright GeekInterview.com
In the above strcmp() function compares string s and string 10 and if the function strcmp() gives a matching value it gives value 0 and the printf is executed.
What happens when the following code is executed? #include <stdio.h> main(){ char *s; if (s == "10") { printf("string matches"); } }
In the above code the operator == is used to compare the value of string. But this is not allowed. This is because in C program string is represented as array of characters. So the operate like comparison, assignment and so on cannot be used to manipulate array as whole. So the above code does not work. In order to achieve the string comparison instead of using == C programmers must use function named as strcmp(). So the above code must be coded as:
In the above strcmp() function compares string s and string 10 and if the function strcmp() gives a matching value it gives value 0 and the printf is executed.
Profile Answers by GeekAdmin Questions by GeekAdmin
Questions by GeekAdmin answers by GeekAdmin
Related Answered Questions
Related Open Questions