C - Misc Operators
C - Misc Operators
C - Misc Operators
Besides the main categories of operators (arithmetic, logical, assignment, etc.), C
uses the following operators that are equally important. Let us discuss the operators
classified under this category.
The "&" symbol, already defined in C as the Binary AND Operator copies a bit to
the result if it exists in both operands. The "&" symbol is also defined as the
address−of operator.
The "*" symbol − A well−known arithmetic operator for multiplication, it can also be
used as a dereference operator.
In C, the dot "." symbol is used as the member access operator in connection with a
struct or union type.
C also uses the arrow "→" symbol as an indirection operator, used especially with the
pointer to the struct variable.
https://www.tutorialspoint.com/cprogramming/c_misc_operators.htm 1/11
6/16/24, 11:53 AM C - Misc Operators
The sizeof operator is a compile−time unary operator. It is used to compute the size
of its operand, which may be a data type or a variable. It returns the size in number
of bytes. It can be applied to any data type, float type, or pointer type variables.
sizeof(type or var);
When sizeof() is used with the data types, it simply returns the amount of memory
allocated to that data type. The output can be different on different machines like a
32−bit system can show different output while a 64−bit system can show different
of the same data types.
Example
#include <stdio.h>
int main(){
int a = 16;
return 0;
}
Output
When you run this code, it will produce the following output −
Size of variable a: 4
Size of int data type: 4
Size of char data type: 1
Size of float data type: 4
Size of double data type: 8
Address-of Operator in C
https://www.tutorialspoint.com/cprogramming/c_misc_operators.htm 2/11
6/16/24, 11:53 AM C - Misc Operators
The "&" operator returns the address of an existing variable. We can assign it to a
pointer variable −
int a;
Assuming that the compiler creates the variable at the address 1000 and "x" at the
address 2000, then the address of "a" is stored in "x".
Example
Let us understand this with the help of an example. Here, we have declared an int
variable. Then, we print its value and address −
#include <stdio.h>
int main(){
return 0;
}
Output
https://www.tutorialspoint.com/cprogramming/c_misc_operators.htm 3/11
6/16/24, 11:53 AM C - Misc Operators
type *var;
The name of the variable must be prefixed with an asterisk (*). The data type
indicates it can store the address of which data type. For example −
int *x;
In this case, the variable x is meant to store the address of another int variable.
float *y;
The "y" variable is a pointer that stores the memory location of a float variable.
The "&" operator returns the address of an existing variable. We can assign it to the
pointer variable −
int a;
int *x = &a;
We can see that the address of this variable (any type of variable for that matter) is
an integer. So, if we try to store it in a pointer variable of int type, see what happens
−
The compiler doesn’t accept this, and reports the following error −
initialization of 'int *' from incompatible pointer type 'float *' [-Wincompatible-pointer-types]
It indicates that the type of a variable and the type of its pointer must be the same.
In C, variables have specific data types that define their size and how they store
values. Declaring a pointer with a matching type (e.g., "float *") enforces type
compatibility between the pointer and the data it points to.
Different data types occupy different amounts of memory in C. For example, an int
typically takes 4 bytes, while a float might take 4 or 8 bytes depending on the
system.
Adding or subtracting integers from pointers moves them in memory based on the
size of the data they point to.
https://www.tutorialspoint.com/cprogramming/c_misc_operators.htm 4/11
6/16/24, 11:53 AM C - Misc Operators
Example 1
#include <stdio.h>
int main(){
return 0;
}
Output
var1: 10.550000
address of var1: 6422044
floatptr: 6422044
address of floatptr: 6422032
Example 2
The * operator is called the Dereference operator. It returns the value stored in the
address which is stored in the pointer, i.e., the value of the variable it is pointing to.
Take a look at the following example −
#include <stdio.h>
int main(){
https://www.tutorialspoint.com/cprogramming/c_misc_operators.htm 5/11
6/16/24, 11:53 AM C - Misc Operators
return 0;
}
Output
The term "ternary" implies that the operator has three operands. The ternary
operator is often used to put conditional (if−else) statements in a compact way.
Example
The following C program uses the ? operator to check if the value of a is even or odd.
#include <stdio.h>
int main(){
int a = 10;
(a % 2==0) ? printf("%d is Even\n", a) : printf("%d is Odd\n", a);
https://www.tutorialspoint.com/cprogramming/c_misc_operators.htm 6/11
6/16/24, 11:53 AM C - Misc Operators
return 0;
}
Output
10 is Even
Change the value of "a" to 15 and run the code again. Now you will get the following
output −
15 is Odd
The dot operator is a member selection operator, when used with the struct or
union variable. The dot (.) operator has the highest operator precedence in C
Language and its associativity is from left to right.
var.member;
Here, var is a variable of a certain struct or a union type, and member is one of the
elements defined while creating structure or union.
A new derived data type is defined with struct keyword as following syntax −
struct newtype {
type elem1;
type elem2;
type elem3;
...
...
};
https://www.tutorialspoint.com/cprogramming/c_misc_operators.htm 7/11
6/16/24, 11:53 AM C - Misc Operators
var.elem1;
Example
Let us declare a struct type named book, declare a struct variable. The following
example shows the use of "." operator to access the members in the book structure.
#include <stdio.h>
struct book{
char title[10];
double price;
int pages;
};
int main(){
return 0;
}
Output
Title: Learn C
Price: 675.500000
No of Pages: 325
size of book struct: 32
https://www.tutorialspoint.com/cprogramming/c_misc_operators.htm 8/11
6/16/24, 11:53 AM C - Misc Operators
A new derived data type is defined with a struct keyword as the following syntax −
struct type {
type var1;
type var2;
type var3;
...
...
};
Usually, a struct is declared before the first function is defined in the program, after
the include statements. That way, the derived type can be used for declaring its
variable inside any function.
struct book {
char title[10];
double price;
int pages;
};
The initialization of a struct variable is done by placing value of each element inside
curly brackets.
You can also store the address of a struct variable in the struct pointer variable.
https://www.tutorialspoint.com/cprogramming/c_misc_operators.htm 9/11
6/16/24, 11:53 AM C - Misc Operators
strptr = &b1;
C defines the arrow (→) symbol to be used with struct pointer as indirection operator
(also called struct dereference operator). It helps to access the elements of the
struct variable to which the pointer reference to.
Example
#include <stdio.h>
#include <string.h>
struct book {
char title[10];
double price;
int pages;
};
int main() {
struct book b1 = {"Learn C", 675.50, 325};
struct book *strptr;
strptr = &b1;
printf("Title: %s\n", strptr->title);
printf("Price: %lf\n", strptr->price);
printf("No of Pages: %d\n", strptr->pages);
return 0;
}
Output
Title: Learn C
Price: 675.500000
https://www.tutorialspoint.com/cprogramming/c_misc_operators.htm 10/11
6/16/24, 11:53 AM C - Misc Operators
No of Pages: 325
https://www.tutorialspoint.com/cprogramming/c_misc_operators.htm 11/11