What happens when the following code is executed? #include <stdio.h> main(){ char *s; if (s == "10") { printf("string matches"); } }

In the above code the operator == is used to compare the value of string. But this is not allowed. This is because in C program string is represented as array of characters. So the operate like comparison, assignment and so on cannot be used to manipulate array as whole. So the above code does not work. In order to achieve the string comparison instead of using == C programmers must use function named as strcmp(). So the above code must be coded as:


Code
  1.  

  2.     #include <stdio.h>

  3.     #include <string.h>

  4.     main()

  5.     {

  6.     char *s;

  7.     if (strcmp(s,10) == 0) {

  8.        printf(string matches”);

  9.     }
Copyright GeekInterview.com

In the above strcmp() function compares string s and string 10 and if the function strcmp() gives a matching value it gives value 0 and the printf is executed.

Questions by GeekAdmin   answers by GeekAdmin

Showing Answers 1 - 20 of 20 Answers

Jerry

  • Jul 26th, 2006
 

The intent is to suggest the use of strcmp...but that was not the question. My feeling is that the compiler thinks we're comparing an address, not the contents of the address, but the address 's' to "10" which the compiler would evaulate to 0x3130 and since the compiler would try to type this short int it's going to depend on the compiler and switches used if this would actually compile.So let's say it compiles, somehow. The answer to your question is the program does nothing; because if the compiler evaluated the constant "10", it would evaluate to 0x00003130 on a 32 bit machine, then 0x00003130 is NOT in the address space of this small model program and hence the comparison fails so the nothing prints.

  Was this answer useful?  Yes

kvsmp2

  • Oct 13th, 2006
 

i do agree with jerry it not the concept of strcmp();

in the above propram u r comparing tha address with 10 ;

i didn't tryout pratically but i think it would give an error message

  Was this answer useful?  Yes

jh_xu

  • Nov 20th, 2006
 

when we say: s = "10"; it is ok, compiler assign the address of string "10" to s;So when we say: if ( s == "10"), the compiler compare the address of string "10" with the value of s; So jerry's answer is partially correct.

  Was this answer useful?  Yes

manish

  • Jan 16th, 2007
 

Here no output will occur.

  Was this answer useful?  Yes

sumalatha

  • Jan 25th, 2007
 

Yes I too agreed jerry.Yaa it is not a matter of strcmp. I think error will occur when we write this ..

  Was this answer useful?  Yes

pavaman

  • Mar 1st, 2007
 

after declaring pointer of type char no value is assigned to it...so by default zero will be compared n that will not match with 10 there for no output

  Was this answer useful?  Yes

Raman Katwal

  • Jun 29th, 2007
 

I agree with you ... It will not generate any kind of output due to the address problem in case of 32 bit machine.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions