Variables and Data Types · Astro Tech Blog

Variables and Data Types

In C, a variable is a named storage location in memory that can hold a value. Each variable has a specific data type that determines the kind of data it can store and how much memory it occupies. C provides several built-in data types, which can be categorized into the following groups:

  1. Basic Data Types:
  • Integer number:
    • char for characters.
    • int for integers,
    • short for short integers,
    • long for long integers,
    • long long for long long integers.
  • Floating-point number:
    • float for single-precision floating-point numbers,
    • double for double-precision floating-point numbers,
    • long double for extended-precision floating-point numbers.
  • void for functions that do not return a value,
  1. Derived Data Types:
  • Arrays, which are collections of elements of the same type,
  • Pointers, which store memory addresses,
  • Structures, which are collections of variables of different types.
  1. Enumeration Types:
  • enum for defining a set of named integer constants.

Example of declaring variables in C:

#include <stdio.h>
int main() {
    int age = 25; // Integer variable
    float height = 5.9; // Floating-point variable
    char grade = 'A'; // Character variable

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

    return 0;
}

In this example, we declare three variables: age of type int, height of type float, and grade of type char. We then print their values using the printf function, where %d is used for integers, %.1f for floating-point numbers with one decimal place, and %c for characters.

Variable Naming Rules in C:

  • Variable names must begin with a letter (A-Z or a-z) or an underscore (_).
  • Variable names can contain letters, digits (0-9), and underscores.
  • Variable names are case-sensitive (e.g., age and Age are different variables).
  • Variable names cannot be the same as C keywords (e.g., int, float, return, etc.).

Data Type Sizes in C:

The size of each data type can vary depending on the system and compiler, but typically:

  • char is usually 1 byte.
  • short is usually 2 bytes,
  • int is usually 4 bytes,
  • float is usually 4 bytes,
  • double is usually 8 bytes,
  • long and long long can be 4 or 8 bytes depending on the system.

Formate Specifiers in C:

When using printf to display variable values, you need to use the correct format specifier for each data type:

  • %c for char
  • %d for int
  • %f for float
  • %lf for double
  • %s for strings (character arrays)
  • %p for pointers
  • %u for unsigned integers
  • %x for hexadecimal integers
  • %o for octal integers
  • %e for scientific notation (floating-point numbers)

Range of Values for Integer Types in C:

  • char: -128 to 127 (signed), 0 to 255 (unsigned)
  • short: -32,768 to 32,767 (signed), 0 to 65,535 (unsigned)
  • int: -2,147,483,648 to 2,147,483,647 (signed), 0 to 4,294,967,295 (unsigned)
  • long: -2,147,483,648 to 2,147,483,647 (signed), 0 to 4,294,967,295 (unsigned)
  • long long: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (signed), 0 to 18,446,744,073,709,551,615 (unsigned)

Range of Values for Floating-Point Types in C:

  • float: approximately 1.5 x 10^-45 to 3.4 x 10^38 (7 decimal digits of precision)
  • double: approximately 5.0 x 10^-324 to 1.7 x 10^308 (15 decimal digits of precision)
  • long double: approximately 3.4 x 10^-4932 to 1.1 x 10^4932 (18 decimal digits of precision)

How to determine the size of the type on your computer?

Write this program to check size:

#include <stdio.h> 

int main(void) { 
    printf("char size: %lu bytes\n", sizeof(char)); 
    printf("int size: %lu bytes\n", sizeof(int)); 
    printf("short size: %lu bytes\n", sizeof(short)); 
    printf("long size: %lu bytes\n", sizeof(long)); 
    printf("float size: %lu bytes\n", sizeof(float)); 
    printf("double size: %lu bytes\n", sizeof(double)); 
    printf("long double size: %lu bytes\n", sizeof(long double)); 
} 

The problem with overflow and underflow in C:

Overflow occurs when a calculation exceeds the maximum limit of a data type, while underflow occurs when a calculation goes below the minimum limit. For example, if you try to store a value larger than 127 in a char variable, it will wrap around and give you an incorrect value due to overflow. Similarly, if you try to store a value smaller than -128 in a char variable, it will also wrap around due to underflow. This can lead to unexpected behavior in your program, so it’s important to be aware of the limits of the data types you are using and to handle potential overflow and underflow situations appropriately, such as by using larger data types or implementing checks before performing calculations.

Understanding variables and data types is fundamental to programming in C, as they allow you to store and manipulate data effectively. Always choose the appropriate data type for your variables to optimize memory usage and ensure that your program behaves as expected.

Variable scope:

Variable scope refers to the region of a program where a variable is defined and can be accessed. In C, there are three main types of variable scope:

  1. Local Scope: Variables declared inside a function or block are local to that function or block and cannot be accessed outside of it. Example:
#include <stdio.h>
void myFunction() {
    int localVar = 10; // local variable
    printf("Local variable: %d\n", localVar);
}
int main() {
    myFunction();
    // printf("%d", localVar); // This will cause an error because localVar is not accessible here
    return 0;
}
  1. Global Scope: Variables declared outside of all functions are global and can be accessed from any function in the program. Example:
#include <stdio.h>
int globalVar = 20; // global variable
void myFunction() {
    printf("Global variable: %d\n", globalVar);
}
int main() {
    myFunction();
    printf("Global variable in main: %d\n", globalVar);
    return 0;
}
  1. Static Scope: Variables declared with the static keyword inside a function retain their value between function calls and are only accessible within that function. Example:
#include <stdio.h>
void myFunction() {
    static int staticVar = 0; // static variable
    staticVar++;
    printf("Static variable: %d\n", staticVar);
}
int main() {
    myFunction(); // Output: Static variable: 1
    myFunction(); // Output: Static variable: 2
    return 0;
}