Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

what will be output of following program ?

0 votes
asked May 2, 2021 by Naksakare Shubham (120 points)
#include

main()

{

   char str[]="S¥065AB";

printf("¥\n%d", sizeof(str));

}

3 Answers

0 votes
answered May 4, 2021 by Hossein Mohtashami (150 points)
the output is :
¥

9

the question is why?
because this character "¥", is presented by two bytes (UTF8)
0 votes
answered May 5, 2021 by Peter Minarik (86,240 points)

Try if yourself

First of all, you can just run it and see for yourself.

Second, if you try to run it, you'll see a compilation error. You're including something, but not telling the compiler what it is. (It's most probably supposed to be stdio.h.

If you fix the include, the program outputs the ¥ sign and the size of the str variable (character array).

Actual result

Now, what the actual result is?

To kill the suspense, the program prints 9.

Why?

Well, the array itself contains elements of a character sequence. The character sequence (I am deliberately not calling it string) contains 7 characters (S¥065AB). However, the array needs to store the terminating zero for the string, so there's the '\0' character as well. So that's now 7 + 1 = 8.
The next important thing is that the ¥ sign seems like to be a Unicode character, so it takes up 2 bytes, not one. Hence 6 bytes (1 byte for each ASCII characters) + 2 bytes (for the single Unicode character) + 1 byte (terminating zero) = 9 bytes
0 votes
answered May 11, 2021 by Jay Kakdiya (140 points)

with this sign we get :¥

9

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...