UPDATED:

How To Make A Simple Calculator By C Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... This blog is under construction!!!!
Namecheap.com

Tuesday, December 27, 2016

Scope of validity


In C, you can declare local and global variables. There are therefore two different areas of validity. The scope of validity specifies where a variable exists and is "visible".

A local variable (or constant) is declared within an instruction block, such as a function. So far we have declared only local variables in the main () function. These are only valid in this function. That is, another function (if you had one) can not access these variables. The same applies to constants. For a different function, these variables and constants are non-existent.

It is important that you initialize local variables before using them! The value that local variables have is not predictable! In other words, at the beginning, there is some value in a local variable. This can be 0, but also any other value. And that is rarely desired.

A local variable is only valid (available) when the function in which it was declared is called. If the function is terminated, the variable is no longer available. This is why it is advantageous for local variables if they immediately assign a value to their values ​​when they are declared so that no unpredictable results are obtained. The best way to initialize each variable is 0.

By default, 0 (zero) is used to initialize global variables. Global variables are valid throughout the program, therefore in all functions. Write the declaration as far as possible at the top of the source code to keep the overview.

#include <stdio.h>

Int global_variable; / * Is initialized with 0 and is valid throughout the program * /

Int main ()
{
  Int local_variable; / * Is initialized with an unpredictable value and is only valid in main () * /

  Printf ("local:% d \ n", local_variable); / * Output the value of the local variable * /
  Printf ("Global:% d \ n", global_variable); / * Output the value of the global variable * /

  Return 0;
}

For a detailed explanation of printf (), keep your eyes on our next post. Here, only the following is anticipated: The% d ensures that an integer is output. In this case, the first parameter specified after the text (string). A newline is generated with \ n.

You can declare local variables that have the same name as declared global variables. In this case, you always "see" the local variable, as the following example shows:

#include <stdio.h>

Int i = 10; / * Global variable i with the value 10 * /

Int main ()
{
   Int i = 50; / * Local variable which is also called i /
                 / * I is initialized to 50 * /

   Printf ("value of i:% d \ n", i); / * I output * /

   Return 0;
}


Local Variable

The variables declared inside the function are automatic or local variables.
The local variables exist only inside the function in which it is declared. When the function exits, the local variables are destroyed.
int main() {
    int n; // n is a local varible to main() function
    ... .. ...
}
void func() {
   int n1; // n1 is local to func() fucntion
}

Global Variable

Variables that are declared outside of all functions are known as external variables. External or global variables are accessible to any function.

#include <stdio.h>
void display();

int n = 5;  // global variable

int main()
{
    ++n;     // variable n is not declared in the main() function
    display();
    return 0;
}

void display()
{
    ++n;     // variable n is not declared in the display() function
    printf("n = %d", n);
}

Output
n = 7
Suppose, a global variable is declared in file1. If you try to use that variable in a different file file2, the compiler will complain. To solve this problem, keyword extern is used in file2 to indicate that the external variable is declared in another file.
Test it yourself!

No comments:

Post a Comment

Namecheap.com

Popular Posts