Chapter 3: Variables and Data Types

Introduction to Variables

Variables are used to store data that can be manipulated by the program. In C, a variable must be declared before it can be used.

Declaring Variables

To declare a variable in C, you specify its type followed by its name:

int age;
float salary;
char grade;

Initializing Variables

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

int age = 25;
float salary = 50000.50;
char grade = 'A';

Basic Data Types

C provides several fundamental data types:

Integer Types

  • int: Typically used to store whole numbers. Size is usually 4 bytes.int count = 100;
  • short: A smaller integer type, usually 2 bytes.short smallNumber = 10;
  • long: A larger integer type, usually 4 or 8 bytes.long largeNumber = 1000000;
  • unsigned int: An integer type that cannot store negative numbers.unsigned int positiveCount = 100;

Floating-Point Types

  • float: Used to store single-precision floating-point numbers. Size is usually 4 bytes.float temperature = 36.5;
  • double: Used to store double-precision floating-point numbers. Size is usually 8 bytes.double preciseValue = 123.456789;

Character Type

  • char: Used to store single characters. Size is usually 1 byte.char letter = 'A';

Boolean Type

Although C does not have a built-in boolean type, you can use int to represent boolean values:

int isTrue = 1; // 1 for true
int isFalse = 0; // 0 for false

Example: Declaring and Initializing Variables

#include <stdio.h>

int main() {
int age = 25;
float height = 5.9;
char grade = 'A';

printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("Grade: %c\n", grade);

return 0;
}

Explanation

  • int age = 25;: Declares an integer variable age and initializes it to 25.
  • float height = 5.9;: Declares a float variable height and initializes it to 5.9.
  • char grade = ‘A’;: Declares a char variable grade and initializes it to ‘A’.
  • printf: Used to print the values of the variables.

Derived Data Types

C also provides derived data types like arrays, pointers, structures, and unions.

Arrays

Arrays are used to store multiple values of the same type.

int numbers[5] = {1, 2, 3, 4, 5};
char name[10] = "John";

Pointers

Pointers store the address of another variable.

int number = 10;
int *ptr = &number;

Structures

Structures are used to group different types of variables under a single name.

struct Person {
char name[50];
int age;
float salary;
};

struct Person person1;
person1.age = 30;

Unions

Unions allow storing different data types in the same memory location.

union Data {
int i;
float f;
char str[20];
};

union Data data;
data.i = 10;

Type Conversion

C allows conversion of one data type to another. This can be implicit (automatic) or explicit (manual).

Implicit Conversion

Automatic conversion performed by the compiler.

int i = 100;
float f = i; // int to float

Explicit Conversion

Manual conversion using type casting.

float f = 3.14;
int i = (int) f; // float to int

Scope and Lifetime of Variables

Scope

The scope of a variable determines where it can be accessed within the code.

  • Local Scope: Variables declared inside a function or block are local to that function or block.void myFunction() { int localVar = 10; // Local to myFunction }
  • Global Scope: Variables declared outside all functions are global and can be accessed anywhere in the program.int globalVar = 20; void myFunction() { // globalVar can be accessed here }

Lifetime

The lifetime of a variable is the period during which it retains a value.

  • Automatic (Local) Variables: Exist only within the block or function they are declared in.
  • Static Variables: Retain their value even after the function exits.void myFunction() { static int staticVar = 0; // Initialized only once staticVar++; }
  • Global Variables: Exist for the duration of the program.

Example: Scope and Lifetime

#include <stdio.h>

int globalVar = 20;

void myFunction() {
int localVar = 10; // Local variable
static int staticVar = 0; // Static variable

staticVar++;
printf("Local: %d, Static: %d, Global: %d\n", localVar, staticVar, globalVar);
}

int main() {
myFunction();
myFunction();
myFunction();
return 0;
}

Explanation

  • Global Variable: globalVar can be accessed from any function.
  • Local Variable: localVar is recreated each time myFunction is called.
  • Static Variable: staticVar retains its value between function calls.

Conclusion

Understanding variables and data types is fundamental to programming in C. They are the building blocks that allow you to store and manipulate data. By mastering these concepts, you lay the groundwork for more advanced programming topics.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *