// a code which prints "hello world"
#include<stdio.h>
int main()
{
 (&printf)("hello world");
 return 0;
}
why does the above code work?

#include<stdio.h>
int main()
{
 char str1[]="hello";
 int j=762;
 char str2[]="hello";
 if(str1==str2)
   printf("true %d\n",--j);
 else
   printf("\nfalse %d\n",j--);
 return 0;
}

//the above code prints "false 762", why?
I am new to C programming and I came across this in a programming contest in my college.

>>if(str1==str2)

Can not compare two strings like that. use strcmp() if( strcmp(str1,str2) == 0)

>why does the above code work?
Because there are only two things you can do with a function: take its address and call it. If you're not doing one, you're doing the other, and C basically ignores redundant levels of indirection on a function. This will work too:

#include <stdio.h>

int main ( void )
{
  (********&*&printf) ( "foo\n" );
  return 0;
}

>the above code prints "false 762", why?
Because you're comparing two pointers that have a different address. You don't compare strings that way because the == operator doesn't perform a content comparison on arrays, it performs a pointer comparison of the address of the arrays.

thanks a lot for replying

The problem is in the line:

if(str1==str2) ...

In plain C, you cannot compare two strings directly.
You must call standard library function strcmp() like this:

if (strcmp(str1,str2) == 0 ) // ==0 means comparison successful

Hope helps.

// a code which prints "hello world"
#include<stdio.h>
int main()
{
 (&printf)("hello world");
 return 0;
}
why does the above code work?

#include<stdio.h>
int main()
{
 char str1[]="hello";
 int j=762;
 char str2[]="hello";
 if(str1==str2)
   printf("true %d\n",--j);
 else
   printf("\nfalse %d\n",j--);
 return 0;
}

//the above code prints "false 762", why?
I am new to C programming and I came across this in a programming contest in my college.

>Hope helps.
Congratulations, it only took you seven days longer than Ancient Dragon to discover the same problem and post the same solution. :icon_rolleyes:

commented: turning the gray cookie green. just because. +6

I believe the problem is that the OP is trying to test for logical equivalence on two strings of characters, but since they are actually pointers, their contents can not be compared directly.

>Hope helps.
Congratulations, it only took you seven days longer than Ancient Dragon to discover the same problem and post the same solution. :icon_rolleyes:

Very unpleasant remark from your side.

As you know, I am new, and have just signed in.

This site is to share, and not doing that kind of remarks!

Speaks a lot of who you are!!!

commented: Actually, it is probably you who needs to learn a bit more about how forums work. -4

>As you know, I am new, and have just signed in.
Claiming ignorance only goes so far. I can forgive a failure to check the date on a thread before replying, but not reading any of the replies before adding your own? That's just stupid.

>This site is to share, and not doing that kind of remarks!
If there are ten posts that say the same thing and one post that gives a better answer, the chances of the better answer going unnoticed increase drastically. By sharing redundant information, and thus adding zero value to the thread, you got exactly what you deserved.

>Speaks a lot of who you are!!!
I don't hide the fact that I'm an jerk. I'm really good at it too. Go ahead, ask anybody on Daniweb. I'll wait.

yeah, she is.

she's even a jerk to other jerks.

>I don't hide the fact that I'm an jerk. I'm really good at it too. Go ahead, ask anybody on Daniweb. I'll wait.

I am confused! The dictionary says: Jerk - a dull stupid. fatuous person
Synonym: dork - A stupid, inept, or foolish person.

But I don't see any of those trends.
Are you pulling my leg?

Slang tends not to be described well in dictionaries. This is a bit closer to what I was going for.

Slang tends not to be described well in dictionaries. This is a bit closer to what I was going for.

Ah! Now I am convinced. If it were not because you use other aliases, Narue could be a potential synonym for it. ;)

^^ aha... i think i'm somewhere between Evil Clown and Palooka

^

If it were not because you use other aliases

er, what? :-O

Slang tends not to be described well in dictionaries. This is a bit closer to what I was going for.

Awesome site, Someone (I) should start a thread about it in the Geek's lounge :)

[instantupdate] Click

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.