O'Reilly logo

C Programming: Visual Quickstart Guide by Marc Liyanage, Larry Ullman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required