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

C Changing Variable Values

A variable's value can be reassigned at any time with =, modified based on its own current value with compound operators, or changed indirectly through a pointer passed to a function.
Syntax
c
variable_name = new_value;
variable_name += amount;  // also -= *= /=

Reassigning a Variable

A variable's value can be changed at any point after it's declared simply by assigning it a new value with the = operator, and the new value completely replaces whatever was stored there before.

Example: Reassigning a Variable

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

Changing Values with Arithmetic

A variable's value is often changed based on its own current value, such as adding to it, and C provides compound assignment operators like += and -= as a shorthand for reading, modifying, and storing back in one step.

Example: Changing Values with Arithmetic

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

Copying One Variable's Value to Another

Assigning one variable's value to another copies that value at that specific moment; the two variables are then independent, and later changing one has no effect on the other.

Example: Copying One Variable's Value to Another

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

Changing a Variable's Value in a Function

A function can only change a variable that belongs to its caller if it receives a pointer to that variable; without a pointer, the function only receives a copy and any changes are local to the function.

Example: Changing a Variable's Value in a Function

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

Constants Cannot Be Changed

A variable declared with the const qualifier can be initialized once but never reassigned afterward, and attempting to do so is a compile-time error, making const a way to guarantee a value stays fixed.

Example: Constants Cannot Be Changed

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare MAX and set it to 100
	const int MAX = 100;
	// Print formatted text to the screen
	printf("%d", MAX);
	// 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. Writing 5 = x; to assign, when the variable must be on the left side.
  2. Expecting a function to change the caller's variable when it received a copy.
  3. Confusing = (assignment) with == (comparison).

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.