Dot (.) Operator In C

Dot (.) Operator In C

The dot (.) operator, also known as the direct member access operator, is used for direct member selection when working with variables of type struct and union. It’s a binary operator that allows you to access and extract the values of the members within structures and union

Syntax

variable_name.member;
  • variable_name: This refers to an instance of a structure or a union. It’s a specific variable that represents the entire structure or union.
  • member: This refers to an individual component or field associated with the created structure or union. It’s one of the parts that make up the structure or union and can be accessed using the dot (.) operator.

Example of dot(.) Operator



// 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 is also used to get the members of nested structure.

Syntax with Nested Struct

variable_name.member1.member2;

Example



// 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

In the C language, the dot (.) operator has the highest operator precedence, which means it is evaluated before other operators. Additionally, its associativity is from left to right, meaning that if you have a sequence of dot operators in an expression, they are evaluated from left to right. This ensures that member access is performed in the expected order.

FAQ- Dot (.) Operator In C

Q1. What is a dot operator?

Ans. (dot) operator is used to access class, structure, or union members. The member is specified by a postfix expression, followed by a . (dot) operator, followed by a possibly qualified identifier or a pseudo-destructor name. (A pseudo-destructor is a destructor of a nonclass type.)

Q2. What is the example of dot operator?

Ans. The member access (dot) operator (“.”) is indeed commonly used to access fields or call methods on an object in programming languages like C#. In the code you provided, you create an instance of an object, then use the dot operator to call the ToString() method on that object. This is a fundamental part of object-oriented programming and allows you to interact with objects by accessing their members and invoking their methods.

Q3.Is Dot a character in C?

Ans. Dot is member access operator

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment