---
title: "Global vs local; & and * operators"
subject: "Computer Programming"
chapter: 3
key: "semester_1/computer_programming:global-local-ampersand-star"
source: https://www.bctengineer.com/q/semester_1/computer_programming/global-local-ampersand-star
site: BCT Engineering
---

# Global vs local; & and * operators

Computer Programming · Chapter 3: Introduction to ‘C’ programming

- Asked in: 2073 Chaitra, 4+4 marks
- Video: [Local vs global variables in C](https://www.youtube.com/watch?v=nasTgF78CUc) — CodeVault
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/global-local-ampersand-star
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-3

## Question

What are the differences between global and local function, variables and data types, & (Ampersand) operator and * operators used in c-programming language? Explain with example.

## Short answer

Global variables, functions, and data types declared outside all functions are accessible throughout the program; local ones are confined to their block. The & operator gives a variable’s memory address; the * operator dereferences a pointer to access the pointed value.

## Answer

#### Global versus local

| Point | Global | Local |
|---|---|---|
| Variable | Declared outside all functions; visible to all functions below it | Declared inside a function or block; visible only there |
| Lifetime of variable | Whole program | Only while the function/block runs |
| Default value | 0 | Garbage (undefined) |
| Function | Ordinary function, can be called from any file/function (external linkage) | `static` function, usable only inside its own file |
| Data type | Structure or `typedef` declared outside functions can be used by all functions | Declared inside a function, usable only there |
| Memory | Data segment | Stack |

#### `&` and `*` operators
| `&` (address-of) | `*` (indirection) |
|---|---|
| Gives the memory address of a variable | Gives the value stored at an address (and is used to declare a pointer) |
| `p = &x;` | `y = *p;` |
| Used with `scanf("%d", &x)` | Used to read or change a value through a pointer |

```c
#include <stdio.h>
int g = 100;                    /* global variable */
void show(void)
{
    int g2 = 5;                 /* local variable */
    printf("g = %d, g2 = %d\n", g, g2);
}
int main(void)
{
    int x = 20, *p;
    p = &x;                     /* & gives address of x */
    *p = *p + 5;                /* * gives value at the address */
    g = g + 1;
    show();
    printf("x = %d, *p = %d\n", x, *p);
    return 0;
}
```
Output:
```text
g = 101, g2 = 5
x = 25, *p = 25
```

`g` is global, so `show()` can use it; `g2` is local to `show()`. `p = &x` stores the address of `x`, and `*p = *p + 5` changes `x` through the pointer.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is the default value of a global variable versus a local one?
   A. Global 0, local garbage
   B. Global garbage, local 0
   C. Both 0
   D. Both garbage
   Answer: A. Globals default to 0; locals are uninitialised. (Key point: Default value)

2. Which statement is correct?
   A. Local variables live for the whole program
   B. Global variables live for the whole program
   C. Both exist only in main
   D. Global variables are on the stack
   Answer: B. Globals last the whole program and sit in the data segment; locals live on the stack while the function runs. (Key point: Lifetime)

3. What does & give?
   A. Value stored at an address
   B. Memory address of a variable
   C. Size of a variable
   D. Remainder
   Answer: B. & is the address-of operator. (Key point: & operator)

4. In *p = *p + 5; with p = &x, x = 20, what is x afterwards?
   A. 20
   B. 25
   C. 5
   D. Address changes
   Answer: B. *p is the value at the address, so x becomes 25. (Key point: * operator)

## Related in Computer Programming

- [Operators; conditional operator](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aoperator-conditional) — Computer Programming, chapter 3
- [Define programming language](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Adefine-programming-language) — Computer Programming, chapter 1, asked 3×
- [Data types and operators in C](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Adata-types-operators-precedence) — Computer Programming, chapter 3
- [Define operator, data type, constant, variable](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Adefine-operator-datatype-constant-variable) — Computer Programming, chapter 3
- [I/O functions used with data files](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-io-functions) — Computer Programming, chapter 10, asked 2×
- [Conditional operator vs if-else; digit sum](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aconditional-operator-vs-ifelse) — Computer Programming, chapter 5

## Related in other subjects

- [8086 assembly language operators](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3A8086-operators) — Microprocessor, chapter 3, asked 3×
- [Global unicast, link local, site local, multicast](https://www.bctengineer.com/question.md?key=semester_8%2Finternet_and_intranet%3Aipv6-address-types) — Internet and Intranet, chapter 2
- [Binary operator member function one argument](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Abinary-member-one-argument) — Object Oriented Programming, chapter 5
- [Continuous vs discrete variables, return period formulae](https://www.bctengineer.com/question.md?key=semester_5%2Fengineering_hydrology%3Acontinuous-discrete-variables-return-period) — Engineering Hydrology, chapter 6
