How are string literals compiled in C? As per my understanding, in test1, the string \"hello\" is put in data segment by compiler and in the 2nd line p is assigned that hard
From the C standard point of view there's no particular requirement about where the literal string will be placed. About the only requirements about the storage of string literals are in C99 6.4.5/5 "String literals":
"hello"
literals in your example may or may not have the same address. You can't count on either behavior.Your understanding is correct, the data of "Hello" will be put in a RO segment, and its relative virtual address will be assigned to the pointers in the testX() functions.
However, those are compiler-specific perspectives, the C standard doesn't care about them.
EDIT: Per test3(), see τεκ's comment.