Interview Questions

C - Variables First Round

Just as any other programming language, C supports various types of variables for processing of the required functionality by methods. You might recall that all operations require data to operate upon and this is achieved by variables that hold values.

Data in a programming language is used to represent the problem statement and to achieve a desired output. Variables represent different forms of data representing real time values.

In C, we have the following data types:
Char – 1 byte
Int – 2 bytes
Long – 4 bytes
Float – 4 bytes
Double – 4 bytes
(Assuming a Linux OS, the above size holds good. It could change, depending on the OS)
1 byte means 8 bits of value. 2 bytes mean 16 bits of value and 4 bytes mean 32 bits of value. Each type could also be signed or unsigned to represent –ve or +ve values. Here is an indicative table that represents minimum and maximum values.

char -127 127
unsigned char 0 255
int -32767 32766
unsigned int 0 65535
long -2147483647 2147483647
unsigned long 0 4294967296

As you can see, the signed and unsigned differ in their most significant bit - MSB. Depending on type of declaration, MSB can convey a meaning of sign (+ve or –ve). Let us write a program that displays the size of the variables and assign values and print them.

#include <stdio.h>
void main (void)
{
    char myChar ; /* declaring a char type variable */
    int myInt;
    long myLong;
    unsigned char myU_Char;
    unsigned int myU_Int;
    unsigned long myU_Long;
    myChar = ‘a’; /* value is assigned here. Note singlequote operator for assigning     char value */
    myInt = 500; /* to indicate that it can hold a greater value than char */
    myLong = 100000; /* holds a value greater than int */
    myU_Char = 200; /* char can not hold greater than 127 */
    myU_Int = 50000; /* int can not hold this value */
    myU_Long = 4294967296; /* max. Value is assigned here */
    printf (“myChar\n”); /* it just prints the text. NOTE. We need to specify the     printf how to print this variable */
    printf(%c”, myChar); /* %c is format specifier used to print value of a char variable */
    printf (“myInt\n”);
    printf(%d”, myInt);
    printf(“myLong\n”);
    printf(%ld”, myLong);
    printf (“myU_Char: %u\n”, myU_Char); /* it is possible to combine both text and format specifier and variable name in one statement */
    printf (“myU_Int:%u\n”, myU_Int);
    printf (“myU_Long:%u\n”, myU_Long);
    printf (“size of char: %u\n”, sizeof(char)); /* sizeof() method is a pre-defined system library function just like printf */
    printf (“size of int: %u\n”, sizeof(int));
    printf (“size of long; %u\n”, sizeof(long));
}

The above code is simple and self explanatory. I suggest you try out modifying the values, changing the way printf is used to print values and explore on your own.

If you see the key takeaway would be:
1. Variables hold values based on the type. Values can be stored on variables depending on the type and the sign.
2. Each and every variable needs to be declared FIRST in the program. (Syntactically, immediately below the { of any function.) Values can be assigned at any place in the program to a variable. If value is assigned when it is declared, it is also called as Initialization of a variable.
3. Variables hold data and let the methods operate on them to achieve the desired functionality.
4. Variables have a scope of life between { and } although we will see more of this in later chapters.
5. You can create your own data type using two ways in C – struct and union. We will also see them subsequently in later chapters.