Please Explain the !strcmp function from the code.

Please explain !strcmp function in the code i could'nt get the idea .Please explain it . I want to implement it in my programs .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  void Book_Author(){
				char name[40];
				cout<<"Please enter name of Author"<<endl;
				cin>>name;
				for(int i=0;i<=index;i++){
				
						if(!strcmp(name,book[i].author)){
							cout<<"Book id is"<<"="<<book[i].id<<endl;
						cout<<"Book name is"<<"="<<book[i].title<<endl;
						cout<<"Book Author is"<<"="<<book[i].author<<endl;
						}
				
					else
		{
			cout<<"There is no such book in library against this name of Author which you entered."<<endl;
			
		}
if(!strcmp(name,book[i].author))
is the same as
if(strcmp(name,book[i].author) == 0)

Recall that strcmp returns 0 when strings match, and something other than 0 when strings differ.

So !0 is true, and !anythingelse is false.
I want to implement it in my programs .
You should consider <string> instead, as you can do this:
string x = "hello";
string y = "world";
if(x!=y) //this is much, much easier to use and read than strcmp.
cout<< x<<" " << y << endl;

c-strings are good to know so you can read old code and use them when necessary, but usually string is cleaner. strstr's bool result is occasionally better than string's find. Most everything else is equal or favors string.
Last edited on
Registered users can post here. Sign in or register to post.