Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
34 views

Variables in C Programming Language.c

Uploaded by

Atharv Potdar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Variables in C Programming Language.c

Uploaded by

Atharv Potdar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

**Variables in C Programming Language**

A **variable** in C is a named memory location used to store data that can change during program
execution. Variables are a fundamental concept in C, enabling programmers to manipulate data
efficiently. Here's an overview of variables in C, covering their types, declaration, and usage.

---

**1. Characteristics of Variables**

- **Name**: Each variable has a unique name or identifier, adhering to C’s naming rules.

- **Type**: Determines the kind of data a variable can hold (e.g., integer, character).

- **Scope**: Defines where the variable can be accessed within the program.

- **Lifetime**: Specifies how long the variable exists during program execution.

- **Storage Class**: Describes the storage duration and linkage of the variable.

---

**2. Declaring Variables**

In C, variables must be declared before use. The syntax for declaration is:

```c

data_type variable_name;

```

- Example:

```c

int age; // Declares an integer variable

float salary; // Declares a floating-point variable

```

You can also initialize a variable at the time of declaration:


```c

int age = 25; // Declares and initializes the variable

```

---

**3. Data Types of Variables**

Variables in C are categorized based on the data they hold:

**a) Basic Data Types**

- **int**: For integers (e.g., 10, -5).

- **float**: For floating-point numbers (e.g., 3.14, -2.5).

- **double**: For double-precision floating-point numbers.

- **char**: For single characters (e.g., 'A', 'z').

**b) Derived Data Types**

- Arrays

- Pointers

- Structures

- Unions

**c) Enumeration**

Used to define variables with predefined constant values.

**d) Void**

Represents the absence of a data type (used mainly for functions).

---

**4. Variable Initialization**


Variables can be initialized either during or after declaration. Initialization ensures that a variable
holds a defined value before it is used.

```c

int x = 10; // Initialized during declaration

int y;

y = 20; // Initialized after declaration

```

---

**5. Storage Classes**

Variables can belong to different storage classes, affecting their scope and lifetime:

- **Automatic (auto)**: Default for local variables; created and destroyed within a block.

- **Register**: Stored in CPU registers for faster access.

- **Static**: Retains its value between function calls.

- **Extern**: Global variables accessible across multiple files.

---

**6. Scope of Variables**

- **Local Variables**: Declared within a block or function, accessible only inside it.

- **Global Variables**: Declared outside any function, accessible throughout the program.

- **Static Variables**: Retain their value across multiple function calls within the same file.

---

**7. Example Program**

```c

include <stdio.h>
int globalVar = 10; // Global variable

void display() {

static int staticVar = 5; // Static variable

printf("Static Variable: %d\n", staticVar);

staticVar++;

int main() {

int localVar = 20; // Local variable

printf("Global Variable: %d\n", globalVar);

printf("Local Variable: %d\n", localVar);

display();

display(); // The value of staticVar persists between calls

return 0;

```

**Output:**

```

Global Variable: 10

Local Variable: 20

Static Variable: 5

Static Variable: 6

```

---

**8. Rules for Naming Variables**


- Must start with a letter or underscore (_).

- Can only contain letters, digits, and underscores.

- Cannot use reserved keywords (e.g., int, return).

- Case-sensitive (e.g., `age` and `Age` are different).

---

**Conclusion**

Variables are an essential concept in C programming, allowing for data manipulation and efficient
memory usage. By understanding variable types, scope, storage classes, and proper naming
conventions, programmers can create robust and optimized programs.

You might also like