Answer: Let’s try to understand following declarations,
int main(void) { int x; /* x is declared as integer */ char initial; /* initial is a character */ int num[10]; /* num is an array of 10 integers */ char msg[10]; /* msg is an array of 10 characters */ return 0; }
Let’s unravel how these declarations are internally interpreted. When we compile the program containing these statements, compiler compiles the program instruction by instruction. When it reaches the first statement,
int x;
it allocates a block of 4 bytes of memory for ‘x’ because it’s of type int on Linux System. It recognises this block by address of its first byte i.e. integer x is located at this address in the memory, though, we name this block as ‘x’.
When compiler reaches the next statement:
char initial;
it allocates just one byte of memory and recognises this as character ‘initial’. Individual variables like ‘x’, initial etc. are also called “Scalar variables”.
Now, when compiler reaches the statement:
int num[10];
it sets aside a block of 40 bytes for 10 integers specified by subscript and creates the name ‘num’ and sets this to the beginning of the block. This declaration is also called as vector i.e. an array of 10 integers. Next statement:
char msg[10];
when compiled, creates an array ‘msg’ of enough size to hold 10 characters.
Remember that compiler allocates memory to a scalar variable according to its type on a given system.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Apply for C Internship
- Apply for Computer Science Internship
- Buy C Books
- Watch Advanced C Programming Videos
- Practice Computer Science MCQs