This question already has answers here:
Is this the only return value for strcmp() in C?
(4 answers)
strcmp behaviour in 32-bit and 64-bit systems
(3 answers)
Closed 2 years ago.
So I studied that strcmp returns the difference between the asci value of the two characters being compared. But in my case it is giving a value of -1,0 or 1 only.
#include<stdio.h>
#include<strings.h>
int main()
{
char n1[]="Jerry";
char n2[]="Ferry";
printf("%d",strcmp(n2,n1));
return 0;
}
Ideally it should give -4,but dev cpp is giving an output of -1. Why is that?
strcmp gives the relative difference between 2 strings.
negative if the left item is less than right
0 if the left is the same as the right
positive if the left item is greater than the right
It doesn't give a range on the difference.
(HINT: what value for strcmp( "Hello", "Hfllo") ? )
Related
This question already has answers here:
C/C++ unsigned integer overflow
(4 answers)
Closed 4 years ago.
I was trying to get into c programming, but in a question a get stuck, please explain this.
int main()
{
char c = 255;
c=c+10;
printf("%d",c);
return 0;
}
the output it gave is
> 9
kindly explain this to me.
The maximum value of a char is 255.
By adding 10 to that number you get 265.
Because that value is not a suitable value for a char it will do 265 % 256 resulting 9
That's why your result is 9
This question already has answers here:
What does 1[d=b] mean?
(3 answers)
Closed 5 years ago.
I came across a code as in below
#include <stdlib.h>
int main(){
char a[]="0123456789";
printf("%s\n",a+6[a]-2[a]);
return 0;
}
Output
456789
How does the calculation of a+6[a]-2[a] happens in printf?
Why giving just 6[a] in printf doesn't work?
printf("%s\n",6[a]);
Well, a statement like
a+6[a]-2[a]
can be re-written as
&(a[ a[6] - a[2] ])
which is simply,
use the value of a[6] (type, int) as the index in the first case
use the value of a[2] (type int) as the RHS.
The result, is a pointer, is passed to printf() as an argument to %s conversion specification.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 7 years ago.
#include <stdio.h>
int main ()
{
int a=10;
printf("%d %d %d",a,a++,a);
return 0;
}
The output I am getting is "11 10 11".
I thought the output would be "10 10 11".
why a is incrementing like this?
Because there is no guarantee about the order in which a C compiler evaluates the arguments. The only thing guaranteed (by the standard) is that they are all evaluated before doing the call. Therefore, you should never count on the order of evaluation of the arguments. Just consider it as random.
Hence, in general, avoid using auto-increment if the same variable exists more than once in an argument list.
This question already has answers here:
How do I properly compare strings in C?
(10 answers)
Closed 7 years ago.
why can't we compare two strings in c program directly.For example i have tried the following example
char *str="int";
if(str=="int")
printf("yes");
else
printf("no");
For the above I got output as "no"
I have tried the above code by using the same logic as if for integers
ie
int i=10;
if(i==10)
printf("same");
But when I have modified the above code like the following
if((strcmp(str,"int"))==0)
printf("yes");
I got the output as "yes"
What is the problem in the first stated code?
A "string" in C is just an array of chars. Comparing two arrays with == just compares their addresses, which are different for different arrays. (Literals may or may not be the same, actually, depending on the implementation.)
This question already has answers here:
Accessing arrays by index[array] in C and C++
(2 answers)
Closed 4 years ago.
#include<stdio.h>
main()
{
printf("%c\n",1-3+2["nexus"]);
}
The result is v. How does it turn out?
What does the indentation (square ones) do?
2["nexus"] is probably leading us to 3 element of the nexus considering it as an
array of characters[arr[2]=x in our case]
following is 1-3viz -2 added to x character ie added to to ascii value
and the corresponding element is v
cant exactly figure out what the square identation really mean but according to the oput this is possible way .
2["nexus"] is same as "nexus"[2], which correspond to the 2nd index or the 3rd element of this string. ( Which is 'x' here ).
In equation, 1-3+ 2["nexux"] is like 1-3+'x' that represent 'v'.
Note
When a string is declared like "nexus".
nexus[0] is 'n'
nexus[1] is 'e'
nexus[2] is 'x'
nexus[3] is 'u'
nexus[4] is 's'