• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

strcmp and if statement

 
Ranch Hand
Posts: 104
2
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

To understand strcmp() function, I have written a test program.



Output:

strcmp(indira,ujwal): -12
strcmp(indira,chandan): 6
indira > ujwal
indira > chandan


Expected Output:

strcmp(indira,ujwal): -12
strcmp(indira,chandan): 6
indira < ujwal
indira > chandan


Why is the difference in output for comparison between strings "Indira" and "Ujwal"?
Please clarify.
 
Omkar Shetkar
Ranch Hand
Posts: 104
2
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got the answer!

In the ifstatement, when the conditional expression evaluates to a nonzero value, the
computer will jump to the statements controlled by the ifstatement and execute them
right away. If the expression evaluates to a value of zero, the computer will ignore those
statements controlled by the ifstatement.



Sorry for posting query too early.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed, you should have written something like this instead:

 
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Omkar Shetkar wrote:

Expected Output:

strcmp(indira,ujwal): -12
strcmp(indira,chandan): 6
indira < ujwal
indira > chandan




Why are -12 and 6 the expected outputs? The man page says (vaguely) that the return values are 0, some value greater than zero, or some value less than zero. I somehow remember them as -1, 0 and 1, but I may be getting that from other languages.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The simplest way to compare Strings for ASCIIbetical order is to subtract chars in corresponding positions from each other. Just as in Java®, a C char is a number, only in C it only occupies 8 bits.

U is the 21st letter and I the 9th, so 12 is 21 - 9.
reply
    Bookmark Topic Watch Topic
  • New Topic