Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Variables in C Language – Local and Global Variables Explained with Examples, Study notes of Programming Languages

This document provides a clear and well-structured explanation of Variables in C Programming, focusing on Local and Global Variables. It includes definitions, syntax, characteristics, code examples, a comparison table, and important differences between the two types of variables. This material is suitable for students studying Computer Science, Engineering, or anyone learning C Language basics in their first or second semester. Topics covered: What is a Variable? Syntax of Variable Declaration Types of Variables in C Detailed explanation of Local and Global Variables Quick quiz for self-evaluation

Typology: Study notes

2022/2023

Available from 04/19/2025

kartikksharma
kartikksharma 🇮🇳

12 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Variables and its Types in C
Language
1. What is a Variable in C?
A variable in C is a named location in memory where data is stored. It acts as a container to
hold values that may change during the execution of a program.
Key Points:
Each variable must have a unique name (called an identifier).
Variables must be declared before use.
They store data of specific types (like int, float, char, etc.).
2. Syntax of Variable Declaration:
data_type variable_name;
Example:
int age;
float price;
char grade;
3. Types of Variables in C
Variables in C can be categorized based on their scope (where they are accessible) and lifetime
(how long they exist in memory). The two most common types are:
Local Variables
pf3
pf4

Partial preview of the text

Download Variables in C Language – Local and Global Variables Explained with Examples and more Study notes Programming Languages in PDF only on Docsity!

Variables and its Types in C

Language

1. What is a Variable in C?

A variable in C is a named location in memory where data is stored. It acts as a container to hold values that may change during the execution of a program.

Key Points:

Each variable must have a unique name (called an identifier).

Variables must be declared before use.

They store data of specific types (like int, float, char, etc.).

2. Syntax of Variable Declaration:

data_type variable_name;

Example:

int age; float price; char grade;

3. Types of Variables in C

Variables in C can be categorized based on their scope (where they are accessible) and lifetime (how long they exist in memory). The two most common types are:

● Local Variables

● Global Variables

4. Local Variables

Definition:

Local variables are declared inside a function or a block (like inside main() or an if block). They are accessible only within that function or block.

Key Characteristics:

Scope : Limited to the function or block.

Lifetime : Exists only while the function is executing.

Automatically destroyed after the function ends.

Must be initialized before use.

Example:

#include <stdio.h>

void display() { int x = 10; // Local variable printf("Value of x: %d\n", x); }

int main() { display(); // printf("%d", x); // Error: x is not accessible here return 0; }

5. Global Variables

Definition:

  1. Can you use a local variable outside the function it is defined in?
  2. What happens if both local and global variables have the same name?