Doubt in my code

Hello, I am getting unexpected output when using & operator with char variable. Why is it so ?

Hey @yashsharma4304
when u print &x with char datatype then instead of printing address it print the string until a \n is encountered

char x ='A';
char y='\0'; //this is allocated directly after x in background (not always true)
cout<< &x <<endl;

when u do this u will see only A getting printed because null is present immediately after A

Also see this

    char x ='A';
    char z='B';
    char y='\0';
    cout<< &x <<endl;

But what is happening right here ?? Bhaiya is not using

char y = β€˜\0’ ;

Why?

As I told u it will print until a NULL char is encounterd
So there must be a null char present there already in memory while he was making this lecture

So do you mean that it is recommended to put a null character by your own. It may be present or may not be present in the memory. And while recording lecture he may get this weird output as he has not put null at the end ?

Yes

I never used this way for printing , this is just for knowledge

1 Like

Ok thanks.

Ok because we don’t use address of operator with char variable.

Yes ,you will only be using & with other data types

1 Like

ok. Thank you for your guide. :slightly_smiling_face:

1 Like