{"id":2775,"date":"2024-05-10T11:24:52","date_gmt":"2024-05-10T11:24:52","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2775"},"modified":"2024-05-10T11:24:52","modified_gmt":"2024-05-10T11:24:52","slug":"dot-operator-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/dot-operator-in-c\/","title":{"rendered":"Dot (.) Operator In C"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#dot-operator-in-c\">Dot (.) Operator In C<\/a><\/li><li ><a href=\"#dot-operator-with-nested-structures-and-unions\">Dot(.) operator with Nested Structures and Unions<\/a><\/li><li ><a href=\"#operator-precedence-of-dot-operator\">Operator Precedence of dot (.) Operator<\/a><\/li><li ><a href=\"#faq-dot-operator-in-c\">FAQ- Dot (.) Operator In C<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"dot-operator-in-c\">Dot (.) Operator In C<\/h2>\n\n\n\n<p>The dot (.) operator, also known as the direct member access operator, is used for direct member selection when working with variables of type <code>struct<\/code> and <code>union<\/code>. It&#8217;s a binary operator that allows you to access and extract the values of the members within structures and union<\/p>\n\n\n\n<p><strong>Syntax<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>variable_name.member;\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>variable_name<\/strong>: This refers to an instance of a structure or a union. It&#8217;s a specific variable that represents the entire structure or union.<\/li>\n\n\n\n<li><strong>member<\/strong>: This refers to an individual component or field associated with the created structure or union. It&#8217;s one of the parts that make up the structure or union and can be accessed using the dot (.) operator.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-dot-operator\">Example of dot(.) Operator<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n\/\/ C program to demonstrate the use of dot operator \n#include &lt;stdio.h&gt; \n  \nstruct str { \n    int mem; \n}; \n  \nunion un { \n    int mem1; \n    char mem2; \n}; \n  \nint main() \n{ \n    struct str str_name = { 12}; \n    union un un_name; \n  \n    \/\/ accessing union member \n    un_name.mem1 = 9; \n    printf(\"Union Member 1: %d\\n\", un_name.mem1); \n  \n    \/\/ accessing structure member \n    printf(\"Structure Member: %d\", str_name.mem); \n  \n    return 0; \n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Union Member 1: 9\nStructure Member: 12<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"dot-operator-with-nested-structures-and-unions\">Dot(.) operator with Nested Structures and Unions<\/h2>\n\n\n\n<p>The Dot operator is also used to get the members of nested structure.<\/p>\n\n\n\n<p><strong>Syntax with Nested Struct<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>variable_name.member1.member2;\n<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n\/\/ C program to illustrate the use of dot operator for \n\/\/ nested structure \n#include &lt;stdio.h&gt; \n  \nstruct base { \n    struct child { \n        int i; \n    } child; \n}; \n  \nint main() \n{ \n    struct base s_name = { 12 }; \n      \n      \/\/ accessing nested structure member using dot operator \n    printf(\"Nested Structure Variable: %d\", s_name.child.i); \n    return 0; \n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Nested Structure Variable: 12\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"operator-precedence-of-dot-operator\">Operator Precedence of dot (.) Operator<\/h2>\n\n\n\n<p> 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-dot-operator-in-c\">FAQ- Dot (.) Operator In C<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1697629584751\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is a dot operator?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. (dot) operator is\u00a0<strong>used to access class, structure, or union members<\/strong>. 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.)<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1697629594093\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is the example of dot operator?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The member access (dot) operator (&#8220;.&#8221;) 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 <code>ToString()<\/code> 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.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1697629602338\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3.Is Dot a character in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Dot is member access operator<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>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&#8217;s a binary operator that allows you to access and extract the values of the members within structures and union Syntax Example of &#8230; <a title=\"Dot (.) Operator In C\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/dot-operator-in-c\/\" aria-label=\"More on Dot (.) Operator In C\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5387,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[479],"class_list":["post-2775","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-dot-operator-in-c","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2775","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=2775"}],"version-history":[{"count":9,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2775\/revisions"}],"predecessor-version":[{"id":10734,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2775\/revisions\/10734"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5387"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}