Back to C Programming
2026-07-125 min read

Variable Declaration in C

Learn Variable Declaration in C step by step with clear examples and exercises.

Why This Matters

In this full guide on Variable Declaration in C, we aim to provide you with an in-depth understanding of the essential concepts surrounding variable declaration, real-world examples, common mistakes, practice questions, and frequently asked questions. By mastering these skills, you'll be well-equipped to write efficient, bug-free programs in C programming.

Why This Matters

Variables play a vital role in storing data and performing operations within C programs. Understanding variable declaration and its intricacies is crucial for writing effective code that meets your project requirements. This lesson will delve into the fundamentals of variable declaration, common pitfalls to avoid, and practical exercises to reinforce your understanding.

Prerequisites

Before diving into variable declaration, it's essential that you have a solid foundation in the following areas:

  1. Basic C syntax, including identifiers, keywords, operators, and expressions.
  2. Data types in C, such as integers, floats, characters, etc.
  3. The structure of a basic C program, encompassing functions, main(), and I/O operations.
  4. Understanding control structures like loops and conditionals.

Core Concept

Declaring Variables

In C programming, you can declare variables using the following syntax:

data_type variable_name;

For example, to create an integer variable named num, you would write:

int num;

You can also initialize a variable during its declaration by assigning it a value:

int num = 10;

Scope of Variables

Variables in C have different scopes, which determine their visibility and lifetime within the program. The two main types of scope are:

  1. Local variables: These are declared inside functions or blocks and are only accessible within that function or block.
  2. Global variables: These are declared outside any function and can be accessed from anywhere in the program.

Local Variables vs Global Variables

Local variables are created when a function is called, and they exist for the lifetime of that function call. Global variables, on the other hand, persist throughout the entire execution of the program.

// Example demonstrating local and global variable scopes
#include <stdio.h>

void func() {
int num = 5; // Local variable 'num' within function 'func'
}

int global_num = 10; // Global variable 'global_num'

int main() {
printf("Global num: %d\n", global_num); // Accessing the global variable from main()
func(); // Local variable 'num' within 'func' is created and destroyed when function returns
printf("Global num: %d\n", global_num); // Still accessing the global variable from main()

return 0;
}

Variable Initialization

When you declare a variable without initializing it, the compiler assigns an indeterminate value to it. It's always a good practice to initialize your variables to avoid unexpected behavior or runtime errors.

Worked Example

Let's create a simple C program that declares and uses variables:

#include <stdio.h>

int main() {
int num1 = 5; // Declare and initialize an integer variable 'num1'
float num2 = 3.14; // Declare and initialize a floating-point number 'num2'
char ch = 'A'; // Declare and initialize a character variable 'ch'

printf("num1 = %d\n", num1);
printf("num2 = %.2f\n", num2);
printf("ch = %c\n", ch);

return 0;
}

In this example, we declare three variables: an integer num1, a floating-point number num2, and a character ch. We then print their values using the printf() function.

Common Mistakes

  1. Forgetting to declare a variable: This results in a compile-time error.
int num; // Correct
// int num; // Compile-time error: 'num' undeclared (first use in this function)
  1. Using an uninitialized variable: This can lead to unexpected behavior or segmentation faults.
int num;
printf("%d\n", num); // Outputs indeterminate value
  1. Declaring a global variable inside a function: This creates a local variable with the same name, hiding the global variable within the function's scope.
int num; // Global variable

void func() {
int num; // Local variable hiding global 'num'
}
  1. Initializing a variable twice: This results in a compile-time error.
int num = 10; // Correct initialization
// int num = 20; // Compile-time error: redefinition of 'num'
  1. Using inconsistent data types for variable declaration and assignment: This can lead to unexpected behavior or runtime errors.
int num = 3.14; // Error: incompatible integer and double initializer
// int num = 3; // Correct initialization of an integer variable 'num'

Practice Questions

  1. Write a program that declares and initializes three variables of different data types (integer, float, and character) and prints their values.
  2. Given the following code snippet:
int num = 5;
printf("%d\n", num++);
printf("%d\n", num);

What will be the output? Explain why.

FAQ

  1. Can I declare multiple variables of the same data type on a single line in C?

Yes, you can. For example: int num1, num2, num3; declares three integer variables on one line.

  1. What happens if I try to assign a value to an undeclared variable in C?

If you try to assign a value to an undeclared variable, the compiler will issue an error: 'variable' undeclared (first use in this function).

  1. Can I change the scope of a variable in C after it has been declared?

No, once a variable is declared with a specific scope, its scope cannot be changed in C. However, you can create a new variable with the same name and a different scope that will hide the original variable within its scope.

  1. What are some best practices for naming variables in C?
  • Use meaningful names that clearly indicate the purpose of the variable.
  • Follow a consistent naming convention, such as lowercase_with_underscores or camelCase.
  • Avoid using reserved keywords and special characters (e.g., @, #, etc.) in your variable names.
  1. What is the difference between a local variable and an automatic variable in C?

The terms "local variable" and "automatic variable" are often used interchangeably to refer to variables declared within a function or block in C. However, some sources may distinguish between them:

  • An automatic variable is created when the function is called and destroyed when the function returns.
  • A local variable is any variable that is not explicitly declared as static (in which case it becomes a "static" variable).

In practice, these terms are used interchangeably to refer to variables declared within functions or blocks with automatic storage class.