Mẹo Local and global variables in C
Thủ Thuật Hướng dẫn Local and global variables in C Chi Tiết
Cao Thị Phương Thảo đang tìm kiếm từ khóa Local and global variables in C được Cập Nhật vào lúc : 2022-11-20 09:58:02 . Với phương châm chia sẻ Bí kíp về trong nội dung bài viết một cách Chi Tiết 2022. Nếu sau khi Read nội dung bài viết vẫn ko hiểu thì hoàn toàn có thể lại Comments ở cuối bài để Tác giả lý giải và hướng dẫn lại nha.C Programming
Nội dung chính Show- C ProgrammingParameters and returnGlobal and local variablesWhat is difference between local and global variable in C?What are local and global variables?What is global variable in C with example?What are local variables in C?
Local
These variables only exist inside the specific function that creates them. They are unknown to other functions and to the main program. As such, they are normally implemented using a stack. Local variables cease to exist once the function that created them is completed. They are recreated each time a function is executed or called.
Global
These variables can be accessed (ie known) by any function comprising the program. They are implemented
by associating memory locations with variable names. They do not get recreated if the function is recalled.
DEFINING GLOBAL VARIABLES
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.
#includeAUTOMATIC AND STATIC VARIABLES
C programs have a number of segments (or areas) where data is located. These segments are typically,
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 */ #includeSample 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 the example:
#includeParameters and return
Functions 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 an example where a result is returned:
#includeThe 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.
Void
If you don’t want
to return a result from a function, you can use void return type instead of the int.
So let’s take a look an example of a function that will not return an integer:
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 variables
A 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:
#includeAs you can see two global variables are declared, A and B. These variables can be used in main() and Add().
The local variable answer can only be used in main().
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.
Post a Comment