Dot Operator in c programming

18 Aug 2022 Balmiki Mandal 0 C Programming

Understanding the Dot Operator in C Programming

The dot operator, also known as the member access operator, is used in C programming to access the members of a structure or union. It is a binary operator that takes two operands: the name of the structure or union variable and the name of the member that you want to access.

Syntax for the dot operator is as follows:

C
structure_or_union_variable.member_name;

For example, the following code shows how to use the dot operator to access the name and age members of a structure variable called student:

C programming
struct student {
  char name[20];
  int age;
};

struct student student1;

student1.name = "John Doe";
student1.age = 20;

printf("The student's name is %s and their age is %d.\n", student1.name, student1.age);

This code will print the following output:

The student's name is John Doe and their age is 20.

The dot operator can also be used to access the members of a nested structure. For example, the following code shows how to access the name and age members of the student member of a structure variable called classroom:

C programming
struct student {
  char name[20];
  int age;
};

struct classroom {
  struct student student;
};

struct classroom classroom1;

classroom1.student.name = "John Doe";
classroom1.student.age = 20;

printf("The student's name is %s and their age is %d.\n", classroom1.student.name, classroom1.student.age);

This code will print the same output as the previous example.

 

The dot operator is a powerful tool for accessing the members of structures and unions in C programming. It makes it easy to read and write code that is clear and concise.

Top Resources

Arithmetic operator in c

Assignment operator in c programming

Relational operator in c programming

Logical Operator in c programming

Bitwise operator in c programming

sizeof operator in c programming

Understanding the Comma Operator in C Programming

Dot Operator in c programming

Using && Logical AND Operator in C Programming

Further Reading:

 For further information and examples, Please visit[ C-Programming From Scratch to Advanced 2023-2024]

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.