- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain scope rules related to the functions in C language
Scope rules are related to the following factors −
- Accessibility of a variables.
- Period of existence of a variable.
- Boundary of usage of variables.
Scope rules related to functions are as follows
Function which is a self-contained block that performs a particular task.
Variables that are declared within the function body 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 too.
The existence of local variables ends when the function completes its specific task and returns to the calling point.
Example 1
Following is the C program for scope rules related to functions −
#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
The output is stated below −
Before swapping a=10, b=20 After swapping a = 10, b=20
Variables that are declared outside the function body are called global variables.
These variables are accessible by any of the functions.
Example 2
Here is another C program for scope rules related to functions −
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
The output is stated below −
Before swapping a = 10, b =20 After swapping a = 20, b = 10
- Related Articles
- Explain the scope rules related to the statement blocks in C language
- What are the scope rules to functions in C programming?
- What are the local and global scope rules in C language?
- Scope Rules in C
- Explain scope of a variable in C language.
- Explain important functions in math.h library functions using C language
- Explain the functions putw() and getw() in C language
- Explain variable declaration and rules of variables in C language
- Explain fgetc() and fputc() functions in C language
- Explain unformatted input and output functions in C language
- Explain putc() and getc() functions of files in C language
- The Government Functions and Scope
- Explain the Scope and Scope Chain in JavaScript
- What are the predefined functions in C language?
- Write a structure in local scope program using C language
