retagged by
1,249 views
1 votes
1 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?
retagged by

4 Answers

3 votes
3 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 votes
1 votes

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

Related questions

0 votes
0 votes
1 answer
1
Debargha Mitra Roy asked 3 days ago
48 views
#include <stdio.h int main() { int a[3] = {1, 3, 5, 7, 9, 11}; int *ptr = a[0]; ptr += sizeof(int); printf("%d", *ptr); return 0; }(Assume size of int to be $2$ bytes.)T...
0 votes
0 votes
2 answers
3
Debargha Mitra Roy asked Apr 10
103 views
What is the output of the below code?#include <stdio.h void main() { static int var = 5; printf("%d ", var ); if (var) main(); }a. 1 2 3 4 5b. 1c. 5 4 3 2 1d. Error
1 votes
1 votes
1 answer
4
SSR17 asked Feb 29
252 views
#include <stdio.h int main() { int i = -1; int x = (unsigned char)i; printf("%d", x); return 0; }output is 255 , but please explain how