← Back to C Course | Chapter 1: Introduction & Basics | Lesson 17 of 21

C Variables

A variable is like a labeled box where you keep information. You say what kind of thing goes in it, give it a name, and can change what is inside later.
Syntax
c
data_type variable_name;          // declaration
variable_name = value;            // assignment

data_type variable_name = value;  // declaration with initialization

Variable Declaration

Declaring a variable tells the compiler to reserve a block of memory of the right size for that data type, and gives that memory a name you can refer to -- but the memory's contents are undefined until you actually assign a value.

Example: Variable Declaration

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int age;
	age = 25;
	// Print formatted text to the screen
	printf("%d", age);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Variable Initialization

Initializing a variable at the same time you declare it (int age = 25;) avoids the risk of accidentally reading garbage leftover memory, which is what happens if you try to use a variable's value before ever assigning one.

Example: Variable Initialization

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare age and set it to 25
	int age = 25;
	// Print formatted text to the screen
	printf("%d", age);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Lvalue and Rvalue

An lvalue is something with an addressable memory location (the left side of an assignment, like a variable), while an rvalue is a value that doesn't necessarily have a persistent address (like a literal number or the right side of an expression).

Example: Lvalue and Rvalue

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int x;
	x = 5 + 3;
	// Print formatted text to the screen
	printf("%d", x);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Multiple Variables

Declaring int a, b, c; creates three separate int variables in one line, saving a bit of typing -- but each still needs its own explicit initialization, since grouping the declaration doesn't share a starting value between them.

Example: Multiple Variables

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int a = 1, b = 2, c = 3;
	// Print formatted text to the screen
	printf("%d %d %d", a, b, c);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Variable Scope

A variable declared inside a function only exists and is only accessible while that function is running; a variable declared outside every function (a global) persists for the program's entire lifetime and is visible everywhere.

Example: Variable Scope

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Declare counter and set it to 100
int counter = 100;
void show() {
	// Print formatted text to the screen
	printf("%d", counter);
}
// Program execution starts in main()
int main() {
	show();
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using a variable that was declared but never initialized, which holds an unpredictable value.
  2. Declaring a variable after use in strict C89 mode, where declarations must come at the start of a block.
  3. Assigning a string to an int variable, such as int x = "5";, which is a type mismatch.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.