What is modifier?
Modifier are keywords in C language, which by which meaning of basic data type can change. The modifiers specify how much memory should be allocated to the variable.
The modifiers in the c language help in making the primary data types much more specific.
Data types are prefixed with the basic data types to modify the memory allocated to the variable.
For example: we all know that storage space of integer data type is 4 byte for 32-bit processor.
If we want to increase, the range we can use the modifier called long int. space allocated for long int is 8 byte.
If we want to decrease the range then we can use short int, space allocated to short int is 2 byte.
There are four data modifiers for all data types used in c.
Four modifiers are
- Short
- long
- unsigned
- signed
Short and long modifiers cause an effect on the value range of any given data type and unsigned and signed modifiers present the signed + or – value in any given data type.
Long : long modifier used increase the size of the data type with 2 more bytes. For example we have int data type with size 2 byte then if we are using long with int then it can occupy 4 byte of memory.
Short: short keyword is used to allocate the fixed memory space to the existing data type.
Unsigned: this keyword used make the value of the data to the positive value.
Signed: this keywords accepts both positive or negative value.
Ex: long int, short int
Unsigned int, signed int, etc. are valid data types in c.
Derived data types in c
Derived data types in c are derived out of the fundamental data type.
The derived data type will not create new data types but it would add various new functionalities to the existing one.
Derived data types in c are
*arrays –the array refers to a sequence of a finite number of data items from the same data type sharing one common name.
The declaration of the array is:
Variable type variable name [sequence of elements];
Ex: int A[10];
Here, 10 means this array A can have 10 integer elements.
Ex: char B [10];
This array B can have 10 character elements.
Initialization of array: if an array is described inside a function, the elements will have a garbage value .in the case of static; it is initialized automatically to 0.
Types of arrays:
There are two types of arrays:
- One-dimensional arrays: a one-dimensional array is a kind of linear array.it involves a single sub-scripting.
Syntax:
Data type arraname[size];
Ex: int a[10];
- Multi-dimensional array: an array involving two or more subscripts [] [] is known as a multi-dimensional array.
Syntax for two-dimensional array:
Data type arrayname [row_size][column_size];
Ex: int a[4][4];
The syntax for three-dimensional array:
Data type arrayname[size1][size2][size3];
Ex: int a[5][5][5];
Advantages of Array
- it is a better version of storing the data of the same size and the same type
- It enables us to collect the number of elements in it.
- Arrays have a safer cache positioning that improves performance.
- Arrays can represent multiple data items of the same type using a single name.
Disadvantages of array
- In an array, it is essential to identify the number of elements that you need to store.
- It is a static structure. It means that in an array, in an array, it fixes the memory size.
- When it comes to insertion and deletion, it is a bit difficult because the elements are stored sequentially, and shifting operation is expensive.
- Functions – a function refers to the self-contained block of single or multiple statements it has its own specified name.
Declaration of function:
Return _type_of function (parameter list)
{
Function body
}
Whenever a function does not return values, you must declare it in the form of a function-returning void.
Ex:
int add ()
{
Int x=10, y=10,Z;
Z=x+y;
Return Z;
}
Types of functions
The c programming language has functions that are
- User-defined functions –these are the type of functions that we can create using the c programmers so that we can make use of it multiple times. The function reduces the complexity of any big problem.
- Library functions-these are the functions whose declaration occurs in the header files of c, such as gets (), puts (), scanf ().etc.
Advantages of using functions in c:
- When we make use of the function then we can easily avoid the rewriting of the same code/logic repeatedly multiple times in any program.
- The calling of function may appear as many numbers of times as we want in any program.
- One of the primary achievements of the c functions is reusability.
FAQs
What are data type modifiers in C?
Data type modifiers are keywords in C that you can use to alter the properties of a basic data type. This alteration helps tailor data storage to specific needs within an application. The main data type modifiers include signed, unsigned, short, long, and long long. These modifiers can be used with integer type variables to adjust their storage sizes and ranges.
How does the signed and unsigned modifier affect an integer data type?
In C, integers are signed by default, meaning they can hold both positive and negative values. Using the unsigned modifier with an integer data type limits it to hold only non-negative values, effectively doubling the upper limit of the value it can store, compared to its signed counterpart. For example, an unsigned int can hold values from 0 to 65535, whereas a signed int typically holds values from -32768 to 32767.
What is the difference between short, long, and long long?
These modifiers adjust the size and range of integer data types. Short typically signifies a smaller integer, while long and long long represent larger integers. The exact size depends on the system, but commonly, short is 16 bits, long is 32 bits, and long long is 64 bits. Using these modifiers allows more fine-tuned control over memory usage and range requirements.
Can type modifiers be combined? For example, can we use both unsigned and long together?
Yes, modifiers can be combined to specify an integer’s type precisely. For example, unsigned long int is a common combination that defines a variable which can store a large range of positive integers, using more bytes of memory than a standard unsigned int. This is particularly useful for applications requiring a wide range that exceeds typical integer limits.
Are there any data type modifiers for floating-point types?
The primary modifiers for floating-point types are float, double, and long double. These are not exactly modifiers but are rather considered distinct types. However, they serve a similar purpose by providing different ranges and precisions for decimal numbers. Double provides more precision and a larger range than float, while long double offers even more precision and range than double, depending on the compiler and the system.