Function returns as void
There are various functions in C which do not return any value or you can say they return void. A function with no return value has the return type as void. For example, void exit (int status);
You may have heard the words datatype and types of datatype.
What is a Datatype?
Data types in c refer to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.
Data types are an important attribute of an identifier in any language because all operations are type checked by compiler for the compatibility.
Data type determines all the possible values that an identifier can have and valid operations that can be applied on it. Illegal operations will not compile by compiler. In a nutshell Datatypes are the Type of Data you store in a variable. for eg ; 1234 is an Value stored in A variable of Int Datatype, JayRyan is a value stored in String B variable of String Datatype
A data type specifies the type of data that we can work with. As C++ is a strongly typed (not truly) language (type is specified before use), type of a variable must be specified before it is used. C++ provides several pre-defined data types.
The types in C can be classified as follows
- Basic Types :- They are arithmetic types and are further classified into: (a) integer types and (b) floating-point types.
- Enumerated types :- They are again arithmetic types and they are used to define variables that can only assign certain discrete integer values throughout the program.
- The type void :- The type specifier void indicates that no value is available.
- Derived types :- They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e) Function types.
The array types and structure types are referred collectively as the aggregate types. The type of a function specifies the type of the function’s return value. We will see the basic types in the following section
Character data type
The char data type is used to work with single characters. An example of storing a character into a variable is as follows:
char ch = ‘s’;
In the above statement, ‘s’ is a character constant. Size of char type is 1 byte which can hold values from -127 to 127 and size of unsigned char is 1 byte which can hold values from 0 to 255. Since 256 values are not enough to represent all the characters across the world, C++ is not suitable for developing cross-language applications.
Integer data types
An integer data type is used to work with numbers that don’t have any fractional parts. Standard data type for working with integers is int. Size of intdata type is generally 4 bytes. There are variations in int data type like shortand long. All of these differ in terms of memory.
To get the exact size of a type or a variable on a particular platform, you can use the sizeof operator. The expressions sizeof(type) yields the storage size of the object or type in bytes.
Another variation in integer types is signed and unsigned. In signed types, the Most Significant Bit (MSB) is reserved by specifying the sign (0 for positive and 1 for negative). In unsigned types, MSB is also used to represent data.
Floating point data types
A floating point type allows us to work with real numbers which have decimal point. Floating point types are float and double. For example to store the value of mathematical PI value, we can declare a variable as follows:
float PI = 3.1415;
The header file float.h defines macros that allow you to use these values and other details about the binary representation of real numbers in your programs. The following example prints the storage space taken by a float type and its range values
For double precision floating point values, we can use double type.
The void Type
The void type specifies that no value is available. It is used in three kinds of situations
- Function returns as voidThere are various functions in C which do not return any value or you can say they return void. A function with no return value has the return type as void. For example, void exit (int status);
- Function arguments as voidThere are various functions in C which do not accept any parameter. A function with no parameter can accept a void. For example, int rand(void);
- Pointers to voidA pointer of type void * represents the address of an object, but not its type. For example, a memory allocation function void *malloc( size_t size ); returns a pointer to void which can be casted to any data type.
Boolean data type
Boolean data type allows us to work with true and false values which are known as boolean values. These values can be used to maintain state, flag, available or unavailable kind of information. C++ in the newer versions had provided support to work with boolean values by including bool data type. Below example demonstrates bool data type:
bool flag = true;
Can we declare variables without data type?
C++ allows programmers to declare variables without specifying the data type. Data type of such variables will be decided by the value that they are initialized to. Such variables should be declared using the keyword auto as shown below:
auto flag = false;
In the above statement the data type of flag variable will be assumed to be bool by the compiler based on the value false.
The typedef keyword can be used to create convenient names (types) for pre-defined data types. Consider the following example which demonstrates the use of typedef keyword:
typedef float average;