C Changing Variable Values
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- Writing
5 = x;to assign, when the variable must be on the left side. - Expecting a function to change the caller's variable when it received a copy.
- Confusing
=(assignment) with==(comparison).
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- C Introduction
- C History & Features
- C Environment Setup
- C First Program
- C Syntax & Structure
- C Statements
- C Comments
- C Keywords & Identifiers
- C Data Types
- C Character Data Type
- C Numeric Data Types
- C Decimal (Floating-Point) Numbers
- C sizeof Operator
- C Extended Data Types
- C Type Conversion
- C Booleans
- C Variables
- C Changing Variable Values
- C Multiple Variables
- C Constants
- C Fixed-Width Integers