Here is a neat trick for checking whether two strings are equal
Submitted by: AdministratorHere's a neat trick for checking whether two strings are equal:
if(!strcmp(s1, s2))
Is this good style?
It is not particularly good style, although it is a popular idiom. The test succeeds if the two strings are equal, but the use of ! (``not'') suggests that it tests for inequality.
Another option is to define a macro:
#define Streq(s1, s2) (strcmp((s1), (s2)) == 0)
which you can then use like this:
if(Streq(s1, s2))
Another option (which borders on preprocessor abuse;is to define
#define StrRel(s1, op, s2) (strcmp(s1, s2) op 0)
after which you can say things like
if(StrRel(s1, ==, s2)) ...
if(StrRel(s1, !=, s2)) ...
if(StrRel(s1, >=, s2)) ...
Submitted by: Administrator
if(!strcmp(s1, s2))
Is this good style?
It is not particularly good style, although it is a popular idiom. The test succeeds if the two strings are equal, but the use of ! (``not'') suggests that it tests for inequality.
Another option is to define a macro:
#define Streq(s1, s2) (strcmp((s1), (s2)) == 0)
which you can then use like this:
if(Streq(s1, s2))
Another option (which borders on preprocessor abuse;is to define
#define StrRel(s1, op, s2) (strcmp(s1, s2) op 0)
after which you can say things like
if(StrRel(s1, ==, s2)) ...
if(StrRel(s1, !=, s2)) ...
if(StrRel(s1, >=, s2)) ...
Submitted by: Administrator
Read Online C Programming Job Interview Questions And Answers
Top C Programming Questions
☺ | Why does this code crash? |
☺ | What is wrong with this declaration? |
☺ | How can I read/write structures from/to data files? |
☺ | What does static variable mean in c? |
☺ | How can I increase the allowable number of simultaneously open files? |
Top Coding/Programming Categories
☺ | Python Interview Questions. |
☺ | OOP Interview Questions. |
☺ | Software engineering Interview Questions. |
☺ | PHP Interview Questions. |
☺ | VBA (Visual Basic for Applications) Interview Questions. |