The Wayback Machine - https://web.archive.org/web/20240213201558/https://www.geeksforgeeks.org/dot-operator-in-c/
Open In App
Related Articles

dot (.) Operator in C

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The C dot (.) operator is used for direct member selection via the name of variables of type struct and union. Also known as the direct member access operator, it is a binary operator that helps us to extract the value of members of the structures and unions.

Syntax of Dot Operator

variable_name.member;
  • variable_name: An instance of a structure or a union.
  • member: member associated with the created structure or union.

Example of dot(.) Operator

C

// C program to demonstrate the use of dot operator
#include <stdio.h>
  
struct str {
    int mem;
};
  
union un {
    int mem1;
    char mem2;
};
  
int main()
{
    struct str str_name = { 12};
    union un un_name;
  
    // accessing union member
    un_name.mem1 = 9;
    printf("Union Member 1: %d\n", un_name.mem1);
  
    // accessing structure member
    printf("Structure Member: %d", str_name.mem);
  
    return 0;
}

                    

Output
Union Member 1: 9
Structure Member: 12

dot(.) operator with Nested Structures and Unions

The dot operator can also be used to access the members of nested structures and unions. It can be done in the same way as done for the normal structure.

Syntax with Nested Struct

variable_name.member1.member2;

Example:

C

// C program to illustrate the use of dot operator for
// nested structure
#include <stdio.h>
  
struct base {
    struct child {
        int i;
    } child;
};
  
int main()
{
    struct base s_name = { 12 };
      
      // accessing nested structure member using dot operator
    printf("Nested Structure Variable: %d", s_name.child.i);
    return 0;
}

                    

Output
Nested Structure Variable: 12

Operator Precedence of dot (.) Operator

The dot (.) operator has the highest operator precedence in C Language and its associativity is from left to right. To know more about operator precedence, please refer to the Operator Precedence in C.

Note: dot (.) operator can only be used with structure or unions in C language.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


Last Updated : 05 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads