赛派号

电动羊肉片机 Difference between Local and Global variables in C

Home » C programming language

Difference between Local and Global variables in C

As we know that variables are the name of memory blocks which are used to store values, in this tutorial we will learn how to declare local and global variables what are their scopes in C language?

Local variables

Before learning about the local variable, we should learn about the function block and function parts. There are two parts of the function block (block means region of the function between curly braces in C)

Declaration part - Region where we declare all variables which are going to be used within the function (this part starts from starting curly brace "{"). Executable part - Other statements except the declarations are the executable statements.

Local variables are the variables which are declared or defined within the declaration part of the function block.

These variables he local scope to that function only, in which they are declared. They cannot be accessed outside of the function.

Global variables

Global variables are the variables which are declared or defined below the header files inclusion section or before the main () function.

These variables he global scope to the program in which they are declared. They can be accessed or modified in any function of the program.

Global variable can also be accessed in another files too (for this, we he to declare these variables as extern in associate header file and header file needs to be included within particular file).

Let's consider the following program

#include /*global variables*/ int a,b; /*function to set values to the global variables*/ void setValues(void) { a=100; b=200; } int main() { /*local variables*/ int x,y; x=10; y=20; setValues(); printf("a=%d, b=%d\n",a,b); printf("x=%d, y=%d\n",x,y); return 0; }

Output

a=100, b=200 x=10, y=20

In this program,Global variables are: a and bLocal variables are: x and y

Here, a and b are using within the function setValues() and main() function because they are global and can be accessed anywhere. While x and y are using within the main() function only because they are local and declared in main() function, so they are accessible only for the main() function i.e. their scope is local for main() function only.

Can we declare local and global variables with the same name?

Yes, we can declare global and local variables with the same name but priority to access the variables are local that means if any function has local variables with the same name of the global variables, local variables will be accessed within that function, but if there are no local variables with the same name as global variables, global variables can be accessed within that function.

Let's consider the following program

#include /*global variables*/ int a,b; /*function to set values to the global variables*/ void setValues(void) { a=100; b=200; } /*function to print global variables*/ void getValues(void) { printf("getValues ()...\n"); printf("a=%d, b=%d\n",a,b); } int main() { /*local variables*/ int a,b; a=10; b=20; setValues(); printf("main ()...\n"); printf("a=%d, b=%d\n",a,b); getValues(); return 0; }

Output

main ()... a=10, b=20 getValues ()... a=100, b=200

In this program,Global variables are: a and b Local variables of main() are: a and b

When we print the value of a and b in main() function, values of local variables 10 and 20 are printing but when we print the value of a and b in getValues() function, values of global variables 100 and 200 are printing.

C Language Tutorial »

Automatic (auto) variables in c language How to access global variables using 'extern'?

Related Tutorials

C language - History, Popularity reasons, Characteristics, Basic structure etc Advantages and Disadvantages of C Programming Language Basics of C language C language character set Why we should use C? Some basic rules of writing a C program Comments in C programming C Tokens Identifier/Variable naming conventions in C language [Rules and Recommendations] Variable Initialization const in c programming Character Constant in C language Octal Literals in C language Hexadecimal (hex) Literals in C language Automatic (auto) variables in c language How to access global variables using 'extern'? Is exit() and return statements are same? How to print float value till number of decimal points using printf? How to print multiple line messages using single printf? Argument index specification in printf Value returned by scanf() function Returned values of printf and scanf in C What do 'lvalue' and 'rvalue' mean Difference between automatic (auto) and static variables in a C language What is Scope in C language? Write difference between local and global variables/Scope C programming errors Advertisement Advertisement

Comments and Discussions!

Load comments ↻

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lsinopec@gmail.com举报,一经查实,本站将立刻删除。

上一篇 没有了

下一篇没有了