The following is a compilation of miscellaneous operators utilized in the C programming language:
| Operator | Description |
|---|---|
| * (Asterisk) | It is a pointer thaLogic Practices to a particular variable, such as ptr, a will be known as the pointer variables. |
| Sizeof () | It is used to return the size of the variable. |
| Dot Operator (.) | The dot (.) operator is commonly used to access a member from the code. |
| Ampersand (&) | It is the miscellaneous operator that is used to return the memory address of a particular variable. |
| Ternary Operator (? :) | It is used for evaluating the condition. For example, if the condition is true ?, then return value t : else return value f. |
| Arrow Operator (->) | This operator is used to access members of a struct variable with a pointer. For example, p -> member ;. |
Therefore, these are a few of the miscellaneous operators that we use in C programming. Now, let us look at each of the mentioned miscellaneous operators with examples as well to understand the concept in a better way.
Dereference Miscellaneous Operator (*)
In C programming, the * symbol is referred to as the dereference operator. This operator is frequently employed with pointers to retrieve or modify the data located at the specific memory location stored in the pointer.
Syntax:
It has the following syntax:
< data type > * variable name;
Here, the variable functions as a pointer variable, designed to store the memory location of a variable of the same data type. The '&' operator is employed to retrieve the memory address of the current variable.
C Dereference Operator Example
Let's consider an example to illustrate the dereference operator in the C programming language.
Example
#include <stdio.h>
int main( ){
int x = 5;
int *p = &x;
printf("Value of x is : %d ", x );
printf("\nAddress of x is : %d\n ", &x );
printf("\nValue of p is : %d", p );
printf("\nAddress of p is : %d\n", &p );
return 0;
}
Output:
Value of x is : 5
Address of x is : 587258716
Value of p is : 587258716
Address of p is : 587258704
Explanation:
In this instance, we have declared an integer variable 'x' and assigned it a value of 5. Subsequently, we initialized a pointer variable '*p' and assigned it the address of variable 'x' using the '&x' syntax. Following this, the program showcases the value stored in variable 'x', the memory address of 'x', the value stored in pointer 'p', and the memory address of pointer 'p'.
Sizeof Operator
In C programming, a unary operator is frequently employed to determine the byte size of a variable or data type, which can vary based on the system architecture and compiler being used.
C sizeOf Operator Example
Let's consider a scenario to demonstrate the sizeOf operator in the C language.
Example
#include <stdio.h>
int main(){ //main function
printf("The size of int is : %d \n", sizeof(int));
printf("The size of float data type : %d\n", sizeof(float));
printf("The size of double data type : %d\n", sizeof(double));
printf("The size of long data type : %d\n", sizeof(long));
return 0;
}
Output:
The size of int is : 4
The size of float data type : 4
The size of double data type : 8
The size of long data type : 8
Explanation:
In this instance, we are examining the stdio.h header file. Within the main function, we displayed the sizes of integers, floats, longs, and doubles. The program concluded by returning 0 as the final instruction. The output reveals that integers occupy 4 bytes, floats take up 4 bytes, doubles require 8 bytes, and longs also use 8 bytes.
Dot(.) Operator
In C programming, the dot (.) operator is frequently employed with a struct or union variable, referred to as the member selection operator. This operator has left-to-right associativity and holds the highest precedence among operators in the C language.
C Dot (.) Operator Example
Let's consider a scenario to demonstrate the dot (.) operator in the C programming language.
Example
#include <stdio.h>
struct car {
char modelName[10];
double price;
int seatCount;
};
int main( ){ //main function
struct car obj = {"Maruti", 564.67, 4};
printf("Model Name is : %s \n", obj.modelName );
printf("Price is : %lf \n", obj.price );
printf("Seat Count is : %d \n", obj.seatCount );
printf("The size of the car struct is : %d", sizeof( struct car ) );
return 0;
}
Output:
Model Name is : Maruti
Price is : 564.670000
Seat Count is : 4
The size of the car struct is : 32
Explanation:
In this instance, we've utilized a struct named 'car' where we've declared variables like modelName, price, and seatCount. Within the main function, an instance of the car struct is instantiated with the assigned values. Subsequently, the model name, price, and seat count of the car are displayed. Lastly, the size of the car struct, which was determined to be 32, is also outputted.
Addressof (&) Operator
In C programming, the address-of operator (&) is frequently utilized to obtain the memory location of a current variable, which can then be stored in a pointer variable.
C addressof (&) Operator Example
Let's consider an example to demonstrate the functionality of the addressof(&) operator in the C programming language.
Example
#include <stdio.h>
int main( ) { //main function
int x = 5;
printf("The memory address of x is : %d \n", &x );
printf("The value of x is : %d", x );
return 0;
}
Output:
The memory address of x is : -2045373092
The value of x is : 5
Explanation:
In this instance, we declare an integer variable 'x' and assign it the value of 5. Subsequently, we display the memory location of the variable 'x' by utilizing the address operator. Following this, we output the content of the 'x' variable. The resulting output showcases the memory address value for the 'x' variable. Additionally, we exhibit the value of 'x', which is determined to be 5.
Ternary Operator
In the ternary operation, we employ '?' as the ternary operator, also recognized as the conditional operator. This operator functions based on an if-else condition. When the specified expression evaluates to true, the initial statement is executed, associated with 'true'. Conversely, if the expression evaluates to false, the second statement linked with 'false' is executed.
C Ternary Operator Example
Let's consider a scenario to demonstrate the Ternary Operator in the C programming language.
Example
#include <stdio.h>
int main( ){
int x = 5;
(x + 5 == 10) ? printf("Yes, it is correct\n", x) : printf("Oops! Sorry\n", x);
return 0;
}
Output:
Yes, it is correct
Explanation:
In this instance, we start by assigning the integer variable 'x' a value of '5'. Following this, we employ the ternary operator with the initial condition being 'x + 5 == 10'. This condition is assessed first. If the condition evaluates to true, the second expression "Yes, it is correct" will be displayed. Otherwise, the third expression "Oops ! Sorry" will be output instead.
Arrow Operator in C
The Arrow operator (->) is employed with the structure pointer to retrieve the elements within the struct variable that the pointer is pointing to.
C Arrow Operator Example
Let's consider an example to demonstrate the Arrow Operator in the C programming language.
Example
#include <stdio.h>
#include <string.h>
struct car {
char modelName[15];
double price;
int seatCount;
};
int main( ) {
struct car obj = { "Maruti", 3555535, 4 };
struct car *p;
p = &obj;
printf("The model name of the car is : %s\n", p->modelName );
printf("The price of the car is : %lf \n", p->price );
printf("The seat count of the car is : %d\n", p->seatCount );
return 0;
}
Output:
The model name of the car is : Maruti
The price of the car is : 3555535.000000
The seat count of the car is : 4
Explanation:
In this instance, we are including the string.h header file within the code. We declare a structure named car with attributes such as the model name, price, and number of seats. Within the main function, an instance of the structure is instantiated, and a pointer is employed to retrieve its attributes using p->member, displaying the details of the car.
Conclusion
In summary, various operators in C, such as sizeof, the comma operator, pointer operators (* and &), the conditional (?:) operator, and structure member access operators (. and ->), provide additional versatility to the programming language. These operators are frequently employed in tasks like memory allocation, conditional logic, struct manipulation, and writing efficient code. Mastery of these operators enables the development of well-structured, efficient, and robust C programs.
Miscellaneous Operators FAQs
Yes, the 'sizeof' operator in C can be applied to both data types and variables to determine their sizes in bytes.
Yes, the 'sizeof' operator can be applied to both data types and variables.
For example,
sizeof( int ); // size of int type
sizeof( x ); // size of variable x's type
No, the comma operator in C is not equivalent to separating statements with semicolons.
No, the comma operator assesses the expressions and provides the outcome of the final expression within a solitary statement, while semicolons delimit numerous statements.
The miscellaneous operator in C, also known as the comma operator, has the lowest precedence among all C operators.
Among the miscellaneous operators, the precedence exists from highest to lowest that is generally used in C:
- 'sizeof' operator
- '&' address operator and '*' pointer dereference
- '?' ternary conditional operator
- ',' comma operator
The disparity lies in the context in which the comma is utilized. The comma operator in C is used to separate expressions within a statement, while the comma used in function arguments serves to separate multiple parameters being passed to a function.
The primary contrast between the comma operator and the comma lies in their functionality. The comma operator assesses multiple expressions and yields the final one, while commas within function arguments serve to delimit distinct parameters without resulting in a value.
5) Is it possible to utilize the 'sizeof' operator with dynamically allocated memory in C programming?
In C programming, the 'sizeof' operator provides the size of the pointer type rather than the size of allocated memory. To determine the size of dynamically allocated memory, manual tracking is necessary.
For example,
int *p = malloc(10 * sizeof(int));
printf("%zu \n", sizeof(p));
It will display the size of the pointer.