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.