why can't we compare two strings directly in c [duplicate] - c

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.)

Related

strcmp giving wrong outputs [duplicate]

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") ? )

How does arrays bypass its declared length [duplicate]

This question already has answers here:
How dangerous is it to access an array out of bounds?
(12 answers)
Why doesn't my program crash when I write past the end of an array?
(9 answers)
Array index out of bound behavior
(10 answers)
No out of bounds error
(7 answers)
Closed 3 years ago.
I was making practises on the logic of arrays in c and my thought on the array length declaration was unformattable if you declare an array length to be 10 integers, that array could not keep 20 integers in memory but when I tested it I saw that I was completely wrong
int main(){
int i;
int arr[10];
for (i = 0;i<20;i++){
arr[i] = i;
}
for (i = 0;i<20;i++){
printf("%d \n",arr[i]);
}
}
I was expecting to see 10 printed numbers but it prints out 20 could someone explain how is it possible?
C and C++ don't have explicit bounds checking on array sizes. When you read/write past the end of an array, you invoke undefined behavior.
With undefined behavior, your program may crash, it may output strange results, or (as in your case) it could appear to work properly. Also, making a seemingly unrelated change such as adding an unused local variable or adding a printf for debugging can change how UB manifests itself.
Just because a program may crash doesn't mean it will.

how does C compiles "senthil""kumar" in a printf statement? [duplicate]

This question already has answers here:
two strings separated by blank being concatenated automatically
(3 answers)
Closed 8 years ago.
I've doubt regarding the basics of C language while using printf statement.
well this is how my code looks like.
#include<stdio.h>
int main(){
printf("%s %s",("senthil""kumar"),("hello""world"),("stack""overflow");
return 0;}
I've got an output like,
senthilkumar helloworld
but i don't how does this code works.
could u pls help me to figure out how it works...
thanx in advance.
Two consecutive string literals are merged by the compiler.
I.e. the following examples are equivalent:
// 1:
"foo" "bar"
// 2:
"foobar"
// 3:
#define FOO "foo"
FOO "bar"
You do not need all those inner parentheses (also, there's a ) missing at the end).

not able to understand how compiler(gcc) is interpreting the command(c) and giving output of the statement [duplicate]

This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 8 years ago.
Can anyone help me out, I am not getting how gcc is compiling the below statement and printing its output.:-
printf("%d",7["sunderban"]);
C allows to access array's elements in two ways (see Accessing arrays by index[array] in C and C++ and the answers) :
int v[5];
// 1)
v[2] = 33;
// 2)
2[v] = 44;
So, what happens in your case is that you access the 8th element of the string, and it is interpret as int by the printf.

unexpected result from printf character print [duplicate]

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'

Resources