Difference between Array and Variable in C

Question: What is the Difference Between Declaration of an Array and a variable in C?

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’.

advertisement
advertisement

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”.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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:

advertisement
    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.

advertisement
If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.