C guide
  • Hello reader!
  • C programming
    • C
    • Roadmap
    • Basics
      • Variables in C
      • I/O in C
      • Data types
      • Operators
      • Control Flow
      • Arrays
        • Strings (char arrays)
        • Multidimensional Arrays
    • Functions!
      • Recursion
  • Pointers
    • Pointer-Array Relationship
  • Structures and Unions
  • Dynamic memory allocation
  • File I/O (Input/Output)
  • Advanced topics
  • Debugging & Optimization
  • Practices
  • Cheatsheet
Powered by GitBook
On this page

Was this helpful?

  1. C programming
  2. Basics

Variables in C

Variables in C

Variables are like labels for storage locations in memory where we can store data. Each variable has:

  • Type: What kind of data it holds (e.g., integers, floats, characters).

  • Name: The identifier you give to access that value (like a tag).

  • Value: The actual data stored in the variable.

Declaring Variables:

Before using a variable in C, you must declare it (tell the compiler what type it will hold). The syntax looks like this:

type variable_name;
  • Type: Specifies the kind of data the variable can store.

  • Variable Name: A unique name to identify the variable.

Examples:

int age;      // declares an integer variable
float height; // declares a floating-point variable
char grade;   // declares a character variable

2. Initialization

You can also declare and assign a value to a variable in one step. This is called initialization:

int age = 19;     // age is initialized to 19
float height = 5.9;
char grade = 'A';

If you don’t initialize a variable, it contains garbage values, meaning random values from memory.

3. Naming Variables

In C, there are some rules and conventions for naming variables:

  • Rules:

    • Must begin with a letter (a-z, A-Z) or an underscore (_), not a number.

    • After the first character, you can use numbers (e.g., score1 is valid).

    • Can’t use C keywords like int, float, return as variable names.

  • Conventions:

    • Variable names should be meaningful and self-explanatory. Instead of x, use age or height.

    • Use lowercase for variables, e.g., student_age, and uppercase for constants.

4. Scope of Variables

Variables in C have different scopes, meaning where they can be accessed. The two main types are:

  • Local Variables: Declared inside a function, accessible only within that function.

  • Global Variables: Declared outside all functions, accessible from anywhere in the program.

Example of local variable:

int main() {
    int age = 20;  // age is local to the main function
    printf("Age: %d\n", age);
    return 0;
}

Example of global variable:

int age = 20;  // global variable

int main() {
    printf("Age: %d\n", age);
    return 0;
}

5. Constant Variables

Sometimes, you don’t want a variable’s value to change. For that, we use the keyword const to make a variable constant.

Example:

const int DAYS_IN_WEEK = 7;

Now, DAYS_IN_WEEK is fixed to 7, and trying to change it later in the code will cause a compilation error.

6. Memory and Variable Sizes

Each data type takes up a certain amount of memory. The size of the variable depends on its data type and the system architecture (32-bit or 64-bit).

Here’s a typical size chart for common data types (on a 32-bit system):

  • int: 4 bytes

  • float: 4 bytes

  • double: 8 bytes

  • char: 1 byte

You can check the size of any variable using the sizeof() function.

Example:

int main() {
    int age;
    printf("Size of int: %lu bytes\n", sizeof(age)); // %lu for long unsigned int
    return 0;
}

7. More on Data Types

  • int: Holds integer values, both positive and negative (e.g., int x = -10;)

  • float: Used for single-precision floating-point numbers (e.g., float x = 3.14;)

  • double: Used for double-precision floating-point numbers (e.g., double x = 3.1415926535;)

  • char: Used for single characters (e.g., char letter = 'A';), but it also stores ASCII values internally.

Unsigned Types:

For integers, you can declare unsigned versions of the type, which can only store positive numbers but offer a larger positive range.

unsigned int u_age = 25;

PreviousBasicsNextI/O in C

Last updated 9 months ago

Was this helpful?