• retagged by
2,258 views
2 2 votes
#include<stdio.h>
int main()
{
    
    if(*"abc" ==*"abcdef")
    printf("strings are equal");
    else
    printf("not equal");
    return;
}

Can someone please explain this program as output is strings are equal?

4 Answers

4 4 votes
"abc" is a array of characters  hence *"abc " represents the value at starting address of the array which is same for both *"abc" and *"abcdef" .

 

Just print both *"abc" and *"abcdef " in c it will print the ascii value  of a  or a .
1 1 vote

Both are pointers to Strings pointing to the same first element i.e. "a" , It will print 97 , the ASCII value to "a" for both and comparison will result in TRUE . If we change the first element both the values will not match and output will be "not equal".

#include<stdio.h>
#include<conio.h>

main()

{
  
    printf("Value of %d \n ", *"a"); // Will print 97
    printf("Value of %d \n", *"abcd"); // Will print 97
  
    if(*"a"==*"abcd")   // TRUE
      
    printf("strings are equal");
    
    else
    
    printf("not equal");
    
    getch();
    
    return 1 ;
}
 

O/P : 

Value of 97
 Value of 97
strings are equal

0 0 votes

"abc" is a string. *"abc" means pointer to starting adress of "abc", as * is dereferencing *"abc" is 'a'. 

in the same way *"abcdef" is 'a'. so it compares ascii values of both which give condition TRUE . 

0 0 votes

"abc" IS A STRING CONSTANT which returns starting address of the string to a pointer(character pointer)

"abcdef" IS ALSO A STRING CONSTANT which returns starting address of the string to a pointer(character pointer)

Note that every string constant starting address is diffrent with any other string constant

if("abc"=="abcdef") ====> Starting addresses of both string constants are compared and the result is if(0)

but here *("abc") is compared with *("abcdef") therefore character of the addresses will compare it implies that if(1)

Position:
Show:

Related questions

1 1 vote
1 1 answer
140
140 views
khanshahima4 asked 5 days ago
140 views
1. If precedence tells me which operator has priority, and associativity tells me how to handle operators at the same level, then why do I need a separate concept called ...
13 13 votes
1 1 answer
1.2k
1.2k views
GO Classes asked Jul 2
1,243 views
Assume:$\texttt{short = 2 bytes}$, $\texttt{int = 4 bytes}$, $\texttt{char = 1 byte}$$\texttt{int}$ needs $4$-byte alignment and the final structure size is rounded to a ...
16 16 votes
2 2 answers
667
667 views
GO Classes asked Jul 2
667 views
Assume:struct Student { double gpa; }; struct Student alice; struct Student *sptr = &alice;Which of the following correctly assigns $\texttt{4.0}$ to $\texttt{alice.gpa}$...
9 9 votes
1 1 answer
493
493 views
GO Classes asked Jul 2
493 views
Consider the following code idea from the source:typedef struct { double x, y; } Point; void reset(Point p) { p.x = p.y = 0; } int main() { Point a = {12.0, 42.0}; Point ...