You are here:

C/c programming

Advertisement


Question
{ char s[]="hello";char t[]="hello";if(s[]==t[]) printf("true");else  printf("false");}
why the answer is false?

Answer
The code snippet you've given isn't compilable, but I'm going to assume it should, instead, read something like the following:

char s[]="hello";
char t[]="hello";
if(s==t)
   printf("true");
else
   printf("false");

The reason this code prints false is that s and t do not equal one another.  The two variables, s and t, are both arrays.  An array is nothing more than a pointer to an address, so what you've got here is no different than writing the following:

char* s="hello";
char* t="hello";
if(s==t)
   printf("true");
else
   printf("false");

The values of s and t are not "hello", but are instead the memory address that they point to.  If you were to dereference them, then you would be accessing the data that each is pointing to, and that would be equal.  So the following code would, in fact, print "true."

char s[]="hello";
char t[]="hello";
if(*s==*t)
   printf("true");
else
   printf("false");

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joseph Moore

Expertise

I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.

Experience

I've been writing C code for 12 years, both on my own in my spare time and professionally.

Organizations
IGDA

Education/Credentials
Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2012 About.com, a part of The New York Times Company. All rights reserved.