Assigning Values to Variables
Once you've declared a variable, you can initialize it, which means giving that variable a value. Assigning values to simple variables like numbers is very easy and requires only the assignment operator (=):
int model_year; model_year = 2004; float cost; cost = 1.95;
Although you must declare a variable before assigning a value to it, C does allow you to perform both acts in one step:
int model_year = 2004; float cost = 1.95
Let's build on the previous example by assigning values to the declared variables.
To assign values to variables
1. | Open var1.c (Script 2.1) in your text editor or IDE. |
2. | After declaring the variables but before the return statement, assign each variable a value (Script 2.2):
age = 40; ... |
Get C Programming: Visual Quickstart Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.