Local and global variables in C
C Programming Show LOCAL AND GLOBAL VARIABLESLocal Global DEFINING GLOBAL VARIABLES /* Demonstrating Global variables */ #include The scope of global variables can be restricted by carefully placing the declaration. They are visible from the declaration until the end of the current source file. #include AUTOMATIC AND STATIC VARIABLES _DATA Static data _BSS Uninitialized static data, zeroed out before call to main() _STACK Automatic data, resides on stack frame, thus local to functions _CONST Constant data, using the ANSI C keyword const The use of the appropriate keyword allows correct placement of the variable onto the desired data segment. /* example program illustrates difference between static and automatic variables */ #include Sample program output Static variables are created and initialized once, on the first call to the function. Subsequent calls to the function do not recreate or re-initialize the static variable. When the function terminates, the variable still exists on the _DATA segment, but cannot be accessed by outside functions. Automatic variables are the opposite. They are created and re-initialized on each entry to the function. They disappear (are de-allocated) when the function terminates. They are created on the _STACK segment. �Copyright B Brown. 1984-1999. All rights reserved. Most languages allow you to create functions of some sort. Functions are used to break up large programs into named sections. You have already been using a function which is the main function. Functions are often used when the same piece of code has to run multiple times. In this case you can put this piece of code in a function and give that function a name. When the piece of code is required you just have to call the function by its name. (So you only have to type the piece of code once). In the example below we declare a function with the name MyPrint. The only thing that this function does is to print the sentence: Printing from a function. If we want to use the function we just have to call MyPrint() and the printf statement will be executed. (Don’t forget to put the round brackets behind the function name when you call it or declare it). Take a look at the example:
Parameters and returnFunctions can accept parameters and can return a result. (C functions can accept an unlimited number of parameters). Where the functions are declared in your program does not matter, as long as a functions name is known to the compiler before it is called. In other words: when there are two functions, i.e. functions A and B, and B must call A, than A has to be declared in front of B. Let’s take a look at an example where a result is returned:
The main() function starts with the declaration of three integers. Then the user can input two whole numbers. These numbers are used as input of function Add(). Input1 is stored in output1 and input2 is stored in output2. The function Add() prints the two numbers onto the screen and will return the result of output1 + output2. The return value is stored in the integer answer. The number stored in answer is then printed onto the screen. VoidIf you don’t want
to return a result from a function, you can use void return type instead of the int.
Note: As you can see there is not an int before our_site() and there is not a return 0; in the function. The function can be called by the following statement: our_site(); Global and local variablesA local variable is a variable that is declared inside a function. A global variable is a variable that is declared outside all functions. A local variable can only be used in the function where it is declared. A global variable can be used in all functions. See the following example:
As you can see two global variables are declared, A and B. These variables can be used in main() and Add(). That’s all for this tutorial. This entry was posted in C Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! or use to share this post with others.What is difference between local and global variable in C?The main difference between Global and local variables is that global variables can be accessed globally in the entire program, whereas local variables can be accessed only within the function or block in which they are defined.
What are local and global variables?Global variables are visible and available to all statements in a setup script that follow its declaration. • A variable is local if it is declared between the function declaration and the keyword begin within that function. Local variables are visible and available only within the function where they are declared.
What is global variable in C with example?The variables that are declared outside the given function are known as global variables. These do not stay limited to a specific function- which means that one can use any given function to not only access but also modify the global variables.
What are local variables in C?Local Variable: A local variable is a type of variable that we declare inside a block or a function, unlike the global variable. Thus, we also have to declare a local variable in c at the beginning of a given block. Example, void function1(){ int x=10; // a local variable.
|