{"id":1836,"date":"2024-05-10T06:50:47","date_gmt":"2024-05-10T06:50:47","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=1836"},"modified":"2024-05-10T06:50:47","modified_gmt":"2024-05-10T06:50:47","slug":"scope-rules-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/scope-rules-in-c\/","title":{"rendered":"Scope Rules 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=\"#scope-rules-in-c\">Scope Rules In C<\/a><\/li><li ><a href=\"#what-is-the-scope-of-variable-in-c\">What is the Scope Of Variable in C<\/a><\/li><li ><a href=\"#types-of-scope-rules-in-c\">Types of Scope Rules in C<\/a><ul><li ><a href=\"#1-global-scope-in-c\">1. Global Scope in C<\/a><\/li><li ><a href=\"#2-local-scope-in-c\">2. Local Scope in C<\/a><\/li><\/ul><\/li><li ><a href=\"#faq-scope-rules-in-c\">FAQ &#8211; Scope Rules In C<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"scope-rules-in-c\">Scope Rules In C<\/h2>\n\n\n\n<p>Scope rules in C are like the rules that guide how and where you can use things in programming. They determine where you can work with different pieces of information, like numbers or words, in your program. Whether you&#8217;re just starting to learn programming or you&#8217;re already experienced, understanding these rules is crucial for writing good code in C. In this explanation, we&#8217;ll make it easier to understand how these scope rules work in C and how they affect your code. So, let&#8217;s dive into the world of C scope rules together!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-scope-of-variable-in-c\">What is the Scope Of Variable in C<\/h2>\n\n\n\n<p>In C programming, the scope of a variable refers to the specific part of the program where that variable is declared, defined, and used. Once we go outside this specific part, the variable becomes inaccessible, as if it doesn&#8217;t exist.<\/p>\n\n\n\n<p>Think of scope as the area where you can &#8220;see&#8221; and use a variable.<\/p>\n\n\n\n<p>In C, you can only work with a variable within its scope, and you can&#8217;t use it outside of that scope. It&#8217;s important to note that in C, all identifiers follow lexical (or static) scoping rules.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate the scope of a variable\n#include &lt;stdio.h&gt;\n \nint main()\n{\n    \/\/ Scope of this variable is within main() function\n    \/\/ only.\n    int var = 34;\n \n    printf(\"%d\", var);\n    return 0;\n}\n \n\/\/ function where we try to access the var defined in main()\nvoid func() { printf(\"%d\", var); }<\/code><\/pre>\n\n\n\n<p><strong>Output <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>solution.c: In function 'func':\nsolution.c:15:28: error: 'var' undeclared (first use in this function)\n void func() { printf(\"%d\", var); }<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"types-of-scope-rules-in-c\">Types of Scope Rules in C<\/h2>\n\n\n\n<p>C Scope Rules are divided into 2 categories :<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Global Scope <\/li>\n\n\n\n<li>Local Scope <\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-global-scope-in-c\">1. Global Scope in C<\/h3>\n\n\n\n<p>The global scope in programming refers to the area outside of any specific block or function. Variables that are declared in this global scope are known as global variables. These global variables can be seen and used in any part of the program.<\/p>\n\n\n\n<p>You can also think of the global scope as the file scope because the scope of an identifier begins at the start of the file where it&#8217;s defined and continues until the end of that file.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate the global scope\n#include &lt;stdio.h&gt;\n  \n\/\/ variable declared in global scope\nint global = 5;\n  \n\/\/ global variable accessed from\n\/\/ within a function\nvoid display()\n{\n    printf(\"%d\\n\", global);\n}\n  \n\/\/ main function\nint main()\n{\n    printf(\"Before change within main: \");\n    display();\n  \n    \/\/ changing value of global\n    \/\/ variable from main function\n    printf(\"After change within main: \");\n    global = 10;\n    display();\n}<\/code><\/pre>\n\n\n\n<p><strong>Output <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Before change within main: 5\nAfter change within main: 10<\/code><\/pre>\n\n\n\n<p><strong>Linkage of Variables in Global Scope<\/strong><\/p>\n\n\n\n<p>Global variables in C have external linkage by default, which means they can be accessed in another C source file. To access these global variables from another source file, you need to use the <code>extern<\/code> keyword to declare them. This allows you to use the global variables defined in one source file in other source files as well, promoting code modularity and reusability.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<p><strong>file1.c<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ filename: file1.c\n \nint a;\n \nint main(void)\n{\n   a = 2;\n}<\/code><\/pre>\n\n\n\n<p><strong>file2.c<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ filename: file2.c\n\/\/ When this file is linked with file1.c, functions\n\/\/ of this file can access a\n \nextern int a;\n \nint myfun()\n{\n   printf(\"%d\", a);\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n2<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-local-scope-in-c\">2. Local Scope in C<\/h3>\n\n\n\n<p>The local scope in programming is the area enclosed by curly braces <code>{}<\/code> within a block or a function. Variables declared within this local scope are known as local variables. These local variables are only visible within the block they are declared in and any nested blocks inside that block. Local scope is also referred to as block scope because it&#8217;s tied to the specific block where the variables are declared.<\/p>\n\n\n\n<p>Additionally, local variables have internal linkage by default, meaning they are only accessible within the file or function where they are defined and cannot be accessed from other source files.<\/p>\n\n\n\n<p><strong>Example <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate the local scope\n#include &lt;stdio.h&gt;\n \n\/\/ Driver Code\nint main()\n{\n    {\n        int x = 10, y = 20;\n        {\n            \/\/ The outer block contains\n            \/\/ declaration of x and\n            \/\/ y, so following statement\n            \/\/ is valid and prints\n            \/\/ 10 and 20\n            printf(\"x = %d, y = %d\\n\", x, y);\n            {\n                \/\/ y is declared again,\n                \/\/ so outer block y is\n                \/\/ not accessible in this block\n                int y = 40;\n \n                \/\/ Changes the outer block\n                \/\/ variable x to 11\n                x++;\n \n                \/\/ Changes this block's\n                \/\/ variable y to 41\n                y++;\n \n                printf(\"x = %d, y = %d\\n\", x, y);\n            }\n \n            \/\/ This statement accesses\n            \/\/ only outer block's\n            \/\/ variables\n            printf(\"x = %d, y = %d\\n\", x, y);\n        }\n    }\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p>Output <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 10, y = 20\nx = 11, y = 41\nx = 11, y = 20<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-scope-rules-in-c\">FAQ &#8211; Scope Rules 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-1694589358548\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What are the 4 scopes of C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. This concept is also known as &#8220;lexical scope.&#8221; In programming, there are four main types of scope: function scope, file scope, block scope, and function prototype scope.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1694589366845\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is the scope of the storage class in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In the C language, we use storage classes to specify key attributes of a variable, such as its visibility, lifetime, initial value, and memory location. These storage classes help define how long a variable exists and where it can be accessed in a C program. <\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1694589377778\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is the scope of a structure in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In C programming, the scope of a type defines where it can be used in the source code. When defining a structure, you have two forms: one with a name and another without. The named form allows broader use throughout the program, while the unnamed form has limited scope, typically within the same block or function.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Scope Rules In C Scope rules in C are like the rules that guide how and where you can use things in programming. They determine where you can work with different pieces of information, like numbers or words, in your program. Whether you&#8217;re just starting to learn programming or you&#8217;re already experienced, understanding these rules &#8230; <a title=\"Scope Rules In C\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/scope-rules-in-c\/\" aria-label=\"More on Scope Rules In C\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":1839,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[308],"class_list":["post-1836","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-scope-rules-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\/1836","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=1836"}],"version-history":[{"count":12,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/1836\/revisions"}],"predecessor-version":[{"id":10603,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/1836\/revisions\/10603"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/1839"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=1836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=1836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=1836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}