All variables have three important attributes.
A data typethat is established when the variable is defined, e.g., integer, real, character. Once defined, the type of a C variable cannot be changed.
A name of the variable
A value that can be changed by assigning a new value to the variable. The kind of values a variable can assume depends on its type. For example, an integer variable can only take integer values, e.g., 2, 100, –12.
In C, a variable must be declared before it can be used.
C has five basic data types and they are as follows:
1. character—Keyword used is char
2. Integer—Keyword used is int
3. floating point—Keyword used is fl oat
4. double precision floating point --- Keyword used is double
5. valueless — Keyword used is void
Derived Data types
The data-types that are derived from the primitive or built-in datatypes are referred to as Derived
Data Types. These can be of four types namely:
- Function
- Array
- Pointers
1. Function :
A function is a block of code or program-segment that is defined to perform a specific well-defined
task. A function is generally defined to save the user from writing the same lines of code again and again for the same input. All the lines of code are put together inside a single function and this can be called anywhere required. main() is a default function that is defined in every program of C++.
FunctionType FunctionName(parameters)
{
// function body;
}
2. Array :
An array is a collection of items stored at continuous memory locations. The idea of array is to represent many instances in one variable.
Syntax:
DataType ArrayName[size_of_array];
Ex: int a[5];
Pointer :
Pointers are symbolic representation of addresses. They enable programs to simulate call-by-reference
as well as to create and manipulate dynamic data structures. It’s general declaration in C is:
datatype *var_name;
Ex: int *ptr;
User defined datatypes:
C allows programmers to define their identifier that would represent an existing data type. There are
three such types:
• Structure
• Union
• enum
1. Structure:
It is a package of variables of different types under a single name. This is done to handle data efficiently. "struct" keyword is used to define a structure.
struct [structure tag]
{
member definition;
member definition;
...
member definition;
} [one or more structure variables];
Ex:
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
2. Union:
These allow storing various data types in the same memory location. Programmers can define a union
with different members, but only a single member can contain a value at a given time.
union [union tag] {
member definition;
member definition;
...
member definition;
} [one or more union variables];
Ex:
union Data {
int i;
float f;
char str[20];
} data;
3. enum:
Enumeration is a special data type that consists of integral constants, and each of them is assigned with a specific name. "enum" keyword is used to define the enumerated data type.
Variables of type enum can also be defined. They can be defined in two ways:
// In both of the below cases, "day" is
// defined as the variable of type week.
enum week{Mon, Tue, Wed};
enum week day;
// Or
enum week{Mon, Tue, Wed}day;
0 comments:
Post a Comment