Interview Questions

Where does constant strings resides in memory in c-programs...

Microsoft Interview Questions and Answers


(Continued from previous question...)

38. Where does constant strings resides in memory in c-programs...

Question:
Where does constant strings resides in memory in c-programs....e.g:
char *ptr="hi Jacline";


maybe an answer:

The string defined by char s[] = "hello world" in C and a C statement like int debug=1 outside the main would be stored in initialized read-write area in the data segment.
The data segment area contains global and static variables used by the program that are initialized. This segment can be further classified into initialized read-only area and initialized read-write area.


maybe an answer3:

The data area contains global and static variables used by the program that are initialized. This segment can be further classified into initialized read-only area and initialized read-write area. For instance the string defined by char s[] = "hello world" in C and a C statement like int debug=1 outside the main would be stored in initialized read-write area. And a C statement like const char* string = "hello world" makes the string literal "hello world" to be stored in initialized read-only area and the character pointer variable string in initialized read-write area. Ex: static int i = 10 will be stored in data segment and global int i = 10 will be stored in data segment

What if This is defined in main().....
e.g.--
int main()
{
char * p="Hi Jacline";
}

(Continued on next question...)

Other Interview Questions