My prob' is that when I type 'Amanda' when the program asks for my name, it returns Hi. I want it to return Howdy when I input 'Amanda', and hello for all other strings.

This is my program:

#include <stdio.h>

int main()
	int		nothing;
	char	        namn[20];
	printf("What's your name?\n");
	scanf("%s", name);
	if(name == "Amanda")
	scanf("%d", &nothing);
	return 0;
7 Years
Discussion Span
Last Post by simpatar

I'm a total rookie at C so that doesnt make any sense to me :P


You must realize that in C strings are not first class citizens. There's no support for them in the language. There's no operator to compare them. There's strictly speaking no such thing as a string.
What is there is a memory region occupied by bytes, and a pointer to that region. You may think of it as an address of the region.

The comparison operator '==' compares just those addresses.

Now notice that in your program there are two distinct memory regions: one called "name", and another one, where lives the string literal "Amanda". They are distinctly different; they appear at the different addresses. Of course the comparison fails.

What you really want to do is to test that those regions contain the same data. For that, you need to compare them byte by byte. You may do it manually (I bet you know what the for loop is), or call a library function such as strcmp, which exists specifically for that purpose.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.