
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
What are the scope rules to functions in C programming?
Local scope
Local scope specifies that variables defined within the block are visible only in that block and invisible outside the block.
Global scope
Global scope specifies that variables defined outside the block are visible up to end of the program.
Example
#include<stdio.h> int r= 50; /* global area */ main (){ int p = 30; printf (“p=%d, r=%d” p,r); fun (); } fun (){ printf (“r=%d”,r); }
Output
p =30, r = 50 r = 50
Scope rules related to functions
A Function is a block of statements that performs a particular task.
Variables that are declared within the body of a function are called local variables
These variables only exist inside the specific function that creates them. They are unknown to other functions and to the main functions also
The existence of local variables ends when the function completes its specific task and returns to the calling point.
Example
#include<stdio.h> main (){ int a=10, b = 20; printf ("before swapping a=%d, b=%d", a,b); swap (a,b); printf ("after swapping a=%d, b=%d", a,b); } swap (int a, int b){ int c; c=a; a=b; b=c; }
Output
Before swapping a=10, b=20 After swapping a = 10, b=20
Variables declared outside the body of a function are called global variables. These variables are accessible by any of the functions.
Example
#include<stdio.h> int a=10, b = 20; main(){ printf ("before swapping a=%d, b=%d", a,b); swap (); printf ("after swapping a=%d, b=%d", a,b); } swap (){ int c; c=a; a=b; b=c; }
Output
Before swapping a = 10, b =20 After swapping a = 20, b = 10
- Related Articles
- Explain scope rules related to the functions in C language
- What are the local and global scope rules in C language?
- Scope Rules in C
- What are the different types of functions in C Programming?
- What are the different categories of functions in C Programming?
- Explain the scope rules related to the statement blocks in C language
- Functions in C programming
- What are the rules to declare variables in C++?
- The Government Functions and Scope
- What are the rules for naming classes in C#?
- What are the default rules used by the parser for parsing names of built-in functions?
- What are the basic rules for defining variables in C++?
- What are the C library functions?
- Functions in Dart Programming
- What are virtual functions in C#?
