C Language Data Types

Questions on Data Types in C Language
 

1. What is a Data Type?

A Data Type is a Type of Data. Data Type is a Data Storage Format that can contain a Specific Type or Range of Values. When computer programs store data in variables, each variable must be assigned a specific data type.
Many people believe that a programmer working in C language is provided with a very tiny set of data types but Dennis Ritchie has made available to the C programmers a feature that allows them to derive their own data types from the existing one. This way a programmer can decide which data type is to be used depending on these 2 factors:
•    Economize the memory space.
•    Improve the speed of execution of program.

2. List the different Data Types in C programming language?
In C language, it is compulsory to declare variables with their data type before using them in any statement. Mainly data types are categorized into 3 categories:
•    Primitive Data Types
•    Derived Data Types
•    User Defined Data Types

3. What are the Primitive Data Types in C language?
The primitive data types in c language are the inbuilt data types provided by the c language itself. Thus, all c compilers provide support for these data types. The different primitive data types are,
int
float
double
char
void

4. Define integer data type?
Integer data type is used to store numeric values without any decimal point e.g. 7,-101, 107, etc.  A variable declared as 'int' must contain whole numbers e.g. age is always in number. Integer occupies 2 bytes memory space and its value range limited to -32768 to +32767.
Syntax:
int variable name;
For example: int roll, marks, age;
5. Define Floating point data type?
floating point data type consists of 2 types. They are,
•    float
•    double
float: float data type is used to store numeric values with decimal point. In other words, float data type is used to store real values, e.g. 3.14, 7.67 etc. A variable declared as float must contain decimal values e.g. percentage, price, pi, area etc. may contain real values. Storage size of float data type is 4. This also varies depend upon the processor in the CPU as “int” data type. We can use up-to 6 digits after decimal using float data type. The range for float data type is from IE-38 to IE+38.
For example, 10.456789 can be stored in a variable using float data type.
Syntax:
float variable name;
double: double data type also declares variable that can store floating point numbers but gives precision double than that provided by float data type. Thus, double data type is also referred to as double precision data type. Double data type is also same as float data type which allows up-to 10 digits after decimal. The range for double data type is from 1E–37 to 1E+37.
Syntax
double variable_name;

6. Define character data type?
char (Character) data type is used to store single character, within single quotes e.g. 'a', 'z', 'e' etc. A variable declared as 'char' can store only single character e.g. Yes or No choice requires only 'y' or 'n' as an answer.
Syntax:
char variable name;
For example: char chi='a', cha;

7. Define void data type?
The void type basically means "nothing". A void type cannot hold any values. You cannot declare a variable to be a void, but you can declare a variable to be a pointer to a void. A pointer to a void is used when the pointer may point to various different types. To use the value that a void pointer is pointing to, it must be cast into another type.
Syntax:
void variable_name;

8. What are the user defined data types in C Language?
C Language supports a feature where user can define an identifier that characterizes an existing data type. This User defined data type identifier can later be used to declare variables. In short its purpose is to redefine the name of an existing data type. Different user defined data types are
struct
union
enum
typedef.

9. Define struct data type?
A struct is a user defined data type that stores multiple values of same or different data types under a single name. In memory, the entire structure variable is stored in sequence.
Syntax:
struct <structure name>
{
var1;
var2;
-----
-----
};
Structure name is the name of structure that we have to give for example, if we want to store the details of a student as- name, roll and marks then in structure it will be declared in the following manner:
struct student
{
char name [20];
int roll,
float marks;
};
Now, its variable is declared as: struct
E.g. Struct student s1;
And its variable is accessed using dot (.) operator.
S1.roll, s1.name, s1.marks

10. Define union data type?
A union is a user defined data type that stores multiple values of same or different data types under a single name. In memory, union variables are stored in a common memory location.
Syntax:
union < tag name>
{
var1;
var2;
-----
----
};
Tag name is the name of union, For example, if we want to store details of a student as- name, roll, marks then it will be done in the following manner:
union student
{
char name [20];
int roll,
float marks;
};
Now, the variable is declared as follows:
struct
E.g. Union student s1;
And its variable is accessed by using dot (.) operator.
S1.roll, s1.name, s1.marks
The only difference between Union and Struct is allocation of memory. 'Union' is assigned a memory size equal to the biggest data type used in union declaration whereas 'struct' will occupy the size equal to the sum of all variables sizes.

11. Define enumerated (enum) data type?
An enumeration is a data type similar to a struct or a union. Its members are constants that are written as variables, though they have signed integer values. These constant represent values that can be assigned to corresponding enumeration variables.
Syntax:
enum {value1, value2, _ _ _ _ _ _, value n};
enum is the required keyword;
Tag is the name that identifies enumerations having this composition; value1, value2, - - - - - - , value n represents the individual identifiers that may be assigned to a variable of this type.
For example: enum colours {black, blue, cyan, green, red, yellow};
colour foreground, background;
First line defines enum and second one defines enum variables. Thus, variables foreground and background can be assigned any one of constant black, blue, - - - - , yellow.
In declaration black is assigned 0 value, and blue 1, - - - -, and yellow 5. We can also assign the value accordingly as we want to assign, like:
enum colours {black, blue, cyan=4, green, red, yellow}
Here black=0, blue=1, cyan=4, green=5, red=6, yellow=7

12. Define typedef data type?
The 'typedef' allows the user to define new data-types that are equivalent to existing data types. Once a user defined data type has been established, then new variables, array, structures, etc. can be declared in terms of this new data type.
A new data type is defined as:
Typedef type new-type;
Type refers to an existing data type, New-type refers to the new user-defined data type.
For example: typedef int number
Now we can declare integer variables as: number roll, age, marks;
It is equivalent to: int roll, age, marks;

13. What are the derived data types in C Language?
Derived data types are based on fundamental data types, i.e. a derived data types is represented in the memory as a fundamental data type. Derived data types don't create a new data type; instead, they add some functionality to the basic data types. Two derived data types are - Array & Pointer.

14. Define an array?
An array is a collection of variables of same type i.e. collection of homogeneous data referred by a common name. In memory, array elements are stored in a continuous location.
Syntax:
[ ];
E.g. int a [10];
Char chi [20];
According to the rules of C language, 1st element of array is stored at location a [0], 2nd at a [1] & so on.

15. Define a pointer?
A pointer is a special variable that holds a memory address (location in memory) of another variable.
Syntax:
*
Data type is the type whose address we want to store in the pointer variable.
* is a pointer variable.
'var name' is the name where the variable is to be stored.
For example: if we want to store the address of an 'int' data type variable into a pointer variable, it is done in the following manner:
int a,*b;
b=&a;
In the above case, variable 'b' stores the address of variable 'a'.

0 comments:

Post a Comment