#include<conio.h>
#include<stdio.h>
main()
{
char name[20];
scanf("%s",&char);
if(char=="name")

  { 
    printf("u entered name");
  }
else
  {
    printf("not name");
  }
}

but it is printing not name when im entering name plzz check for the problem

Edited 5 Years Ago by Narue: Added code tags

I don't think you're ready for pointers and strings just yet. You still don't have a handle on variables. Please take a step back and take a little more time on simpler programs. But since I don't want this post to be completely negative, here's correct code for your program after proactively fixing the lion's share of issues you'd likely encounter along the way:

#include <stdio.h>
#include <string.h>

int main(void)
{
    char name[BUFSIZ];
    
    printf("Enter your name: ");
    fflush(stdout);
    
    if (fgets(name, sizeof name, stdin) != NULL) {
        char *nl = strchr(name, '\n');
        
        if (nl != NULL)
            *nl = '\0';
            
        if (strcmp(name, "name") == 0)
            fputs("You entered 'name'\n", stdout);
        else
            fputs("You did not enter 'name'\n", stdout);
    }
    
    return 0;
}

scanf("%s",&char);

How You can use char as a variable as this is a keyword.
and
For comparing two strings You have to use strcmp function. since name is an array of characters ended with '\0' which is a pointer, Then the pointer will be used for the comparison. So string1 == string2 will be interpreted as &string1[0] == &string2[0] . The same goes for string literals, string1 == "name" will be interpreted as &string1[0] == &"name"[0] .

Follow anand01.
The code will be....

#include<conio.h>
#include<stdio.h>
#include<string.h>
int main()
{
	char name[20];
scanf("%s",&name);
char n[5]="name";
if(strcmp(name,n)==0)

 
    printf("u entered name");
 
else
  
    printf("not name");
  
return 0;
}

Follow anand01.
The code will be....

#include<conio.h>
#include<stdio.h>
#include<string.h>
int main()
{
	char name[20];
scanf("%s",&name);
char n[5]="name";
if(strcmp(name,n)==0)

 
    printf("u entered name");
 
else
  
    printf("not name");
  
return 0;
}

While it's nice to see you correct the missing return statement, the code is still wrong.

scanf("%s",&name);

This is a common mistake. scanf() expects a pointer, so normally you'd prepend your variables with the address-of operator. However, arrays in value context (such as when used as arguments) are already pointers. Thus the address-of operator is redundant, and in fact messes up your type. The %s specifier expects an object of type char*, but now you're passing an object of type char**.

char n[5]="name";

Unless you're compiling as C99 (which I strongly doubt), this line suggests that you're either compiling as C++ or relying on a compiler extension that enables mixed code and declarations. The OP is most certainly not compiling as C99, so as C, the code will fail to compile.

While it's nice to see you correct the missing return statement, the code is still wrong.

Yea you are right. Actually I use Microsoft visual c++-6. It did not show any error on it.

Actually I use Microsoft visual c++-6.

A compiler that's notorious for silent extensions and lack of standard compliance.

It did not show any error on it.

"It works for me" is not synonymous with "it's correct". IIRC, even Visual C++ 6 should toss an error for mixed declarations and code if you properly compile as C. And the type error in scanf() is rarely flagged as even a warning. You simply have to know what you're doing.

This article has been dead for over six months. Start a new discussion instead.