Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cscope #6003

Merged
merged 8 commits into from
Jan 29, 2025
Merged

Cscope #6003

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions content/c/concepts/scope/scope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
Title: 'Scope'
Description: 'Scope defines the region in a program where a variable can be accessed.'
Subjects:
- 'Computer Science'
- 'Developer Tools'
- 'Web Development'
Tags:
- 'Control Flow'
- 'Memory'
- 'Programming'
- 'Variables'
CatalogContent:
- 'learn-c'
- 'paths/computer-science'
---

In C, [**scope**](https://www.codecademy.com/resources/docs/general/scope) defines the region of a program where a [variable](https://www.codecademy.com/resources/docs/c/variables) can be accessed. Variables in C are categorized into local and global scopes based on their accessibility.

## Local Scope

Variables declared inside a [function](https://www.codecademy.com/resources/docs/c/functions) or block have local scope, meaning they are accessible only within that function or block.

## Syntax for local scope

```pseudo
return_type function_name() {
// Variable with local scope
data_type variable_name = value;

// Code that uses the variable
...
}
```

- `data_type`: Represents the type of the variable.
- `variable_name`: Name of the variable used in the code.
- `value`: Value assigned to the variable.

## Global Scope

Variables declared outside of any function have global scope and can be accessed and modified from any function within the program.

## Syntax for global scope

```pseudo

data_type variable_name = value;
// Global variable (declared outside any function)
return_type function_name() {
// Code that can access the global variable
}
```

## Example

AroParada marked this conversation as resolved.
Show resolved Hide resolved
The example demonstrates variable scope in C, where `globalVar` has global scope (accessible throughout the program), while `localVar` has local scope (accessible only within `myFunction`):

```c
#include <stdio.h>
// Global variable
int globalVar = 100;
void myFunction() {
// Local variable
int localVar = 10;

// Accessing both local and global variables
printf("Local variable inside myFunction: %d\n", localVar);
printf("Global variable inside myFunction: %d\n", globalVar);
}
int main() {
// Accessing the global variable
printf("Global variable inside main: %d\n", globalVar);

// Calling the function which accesses both local and global variables
myFunction();

return 0;
}
```
AroParada marked this conversation as resolved.
Show resolved Hide resolved

The code above produces this output:

```shell
Global variable inside main: 100
Local variable inside myFunction: 10
Global variable inside myFunction: 100
```