5 5 votes Consider the following C code fragement - #include<stdio.h> int main() { char t[] = "abcdefghij"; int *p = t; p+=2; char *c = (char *)p; printf("%c", *c); } Which of the following is/are correct? The output of the program in the little-endian system is $\textsf{“ i "}$ (without quotes). The output of the program in the big-endian system is $\textsf{“ i "}$ (without quotes). The output of the program in the big-endian system is $\textsf{“ i "}$ (without quotes). The output of the program in the little-endian system is $\textsf{“ i "}$ (without quotes). Programming in C goclasses-scholarship-test1 goclasses programming programming-in-c array pointers multiple-selects one-mark + – GO Classes 1.7k views answer comment Share Follow Print See 1 comment 1 1 comment reply Sachin Mittal 1 commented Aug 7, 2022 reply Follow flag Concepts about Little Endian and Big Endian https://youtu.be/GtzncSKjEeY?list=PLIPZ2_p3RNHgJFMCeS5c13pREPXcoRkaZ 4 4 replyShare Please log in or register to add a comment.
7 7 votes Note that here, string literal is used to assign the array t[] Since p is an int pointer: and we know that pointer arithmetic works based on the type of pointer p+=2 means p=p+2 and p+2 evaluates to p + 2 * sizeof(*p) here sizeof gives 4 bytes (assuming int occupies 4 bytes in the system) hence, pointer p will jump/skip 8 bytes and will land at the address of “i” (initially it was pointing to the 0th element i.e, ‘a’). And later in the code pointer p is typecasted as character pointer and is being saved as character pointer in *c. And we know that endianness does not apply to arrays. Therefore it doesn't matter if the system uses big endian or the little endian. The output will be the same. Finally, in printf statement *c will fetch the value and %c will print the character ‘i’. Shubhamishere answered Aug 7, 2022 • edited Aug 7, 2022 by Shubhamishere Shubhamishere comment Share Follow See all 2 Comments 2 2 Comments reply Shubhamishere commented Aug 7, 2022 reply Follow flag https://stackoverflow.com/questions/12641908/endianness-of-string-literals-and-usage-of-strings-in-case-statements 2 2 replyShare Jaideep Singh commented Jan 1, 2023 reply Follow flag i think you wanted to write “endianess does not apply to strings array” In array if size of each element is >1B, endianess is applicable. (array won’t be reversed, each element endianess will change) But, in strings it’s only 1B, hence endianess not applicabe on strings. 1 1 replyShare Please log in or register to add a comment.
3 3 votes ENDIANNESS DOES NOT APPLY TO ARRAYS Udhay_Brahmi answered Aug 12, 2022 Udhay_Brahmi comment Share Follow 0 reply Please log in or register to add a comment.
1 1 vote The output of the program in the little-endian system is “i” (without quotes). The output of the program in the big-endian system is “i” (without quotes). Bharat Bhushan answered Aug 7, 2022 Bharat Bhushan comment Share Follow 0 reply Please log in or register to add a comment.