when we pass the string in the sizeof() operator ,it always gives 1 more from the letters present in the string...... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

when we pass the string in the sizeof() operator ,it always gives 1 more from the letters present in the string......

{int a; a=sizeof("ankit"); cout<<a;} //why its giving 6 as answer...

28th Jan 2018, 12:55 PM
Ankit Rathore
Ankit Rathore - avatar
7 Answers
+ 7
i think due to null char(\0) present at end of string.. when a string create compiler set a null char to determine ending string.. and sizeof func also cont that too.. thats why ans 6,,,
28th Jan 2018, 1:04 PM
Scooby
Scooby - avatar
+ 7
null char is not a normal char that print its just a terminal point to determine ending point of any string,
28th Jan 2018, 1:24 PM
Scooby
Scooby - avatar
+ 5
don't say thank you.help to another three people and tell them help other three people,small help do big impact ☺
28th Jan 2018, 1:27 PM
Scooby
Scooby - avatar
+ 3
Every string in c++ is in the memory ended with zero character '\0'. So your string looks like "ankit\0". So its length is 6.
28th Jan 2018, 1:05 PM
michal
+ 2
yes i will definitely do this... the impact will be big
28th Jan 2018, 1:30 PM
Ankit Rathore
Ankit Rathore - avatar
+ 1
thanks... but when we want to print the null character.. it does not printed...and in the place of null character the space prints..and the curser blinks after the space ...
28th Jan 2018, 1:22 PM
Ankit Rathore
Ankit Rathore - avatar
+ 1
👍👍 thanks
28th Jan 2018, 1:26 PM
Ankit Rathore
Ankit Rathore - avatar