Copyright
Computer Science Courses / Course / Chapter

Assigning Values to Variables in C Programming

Lesson Transcript
Instructor: Martin Gibbs

Martin has 21 years experience in Information Systems and Information Technology, has a PhD in Information Technology Management, and a master's degree in Information Systems Management. He is an adjunct professor of computer science and computer programming.

Variables in C programming are assigned values which can be done right away or declared and assigned later. Learn about variables in C programming, declaring and assigning values, type casting, and converting down. Updated: 01/24/2022

Variables in C Programming

Consider that you are writing a program for a payroll calculation. In order to calculate pay, you're going to need more variables to store pay rate, hours worked, overtime calculations, etc. When you create the variable for pay rate, the following declaration is perfectly okay:

float pay_rate;

Recall that the float data type holds decimal values. Pay rate is usually calculated to two decimal places, so a float works well in this situation.

What do you think the VALUE of pay_rate could be when we execute this statement? Is it 0? Or 0.00? Or is it NULL?

The answer is any of the above! Many compilers will return a value of 0 because you declared a float but gave it no value. However, you may also see NULL (meaning empty). When you declared the variable, all you did was tell C that you needed a table for 4 bytes and to save it for you. You never said who was sitting at the table.

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Math Functions in C Programming: Definition & Example

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 Variables in C Programming
  • 1:01 Declaring & Assigning Values
  • 1:21 Type Casting: Implicit…
  • 2:43 A Note About Converting Down
  • 3:59 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed Speed

Declaring & Assigning Values

Let's try our code again and this time give it a value. There are two options, the first involves extra lines of code, but at the same time it is a good display of what you intend. The double backslashes indicate comments in the code:

~include <stdio.h>
int main(void) {
// Declare variable
float pay_rate;
// Assign Value
pay_rate = 100.75;
}

The next option is to simply give the pay_rate a value right off the bat:

float pay_rate = 100.75;

Type Casting: Implicit & Explicit

These terms may sound like C is up to something nefarious in the movie business. In reality, we're talking about the ability to type cast. This means that you can convert one variable type into another. Sadly, it only works for variables and not casting lead to gold.

One of the common methods is to convert an integer value up to a float or double. This might help us in the payroll program:

int days_worked = 5;
float pay_rate = 155.35;
float total = 0;
// type cast to float
total = (float)days_worked * pay_rate;

Here we have converted the integer to a float. C will change 5 to 5.0 and then accurately calculate the resulting value for total. This code we're looking at is an explicit type cast, because we told the compiler that we expect days_worked to be a float. To do that, we put (float) in front of the integer value we were elevating to float.

Another type cast is the implicit type cast. In this case you don't tell the compiler exactly what you want (you are not being explicit). There is no data type in parentheses. Instead, it would look like this:

int days_worked = 5;
float pay_rate = 155.35;
float total = 0;
total = days_worked * pay_rate;

The problem with this approach is that there's no indication to yourself or other programmers of the intent. The compiler is going to perform the type cast to what fits the data. If it thought double was better, it would type cast to double. Further, this action will happen even if it's a good idea or not.

With either type cast, there is a possibility of data loss.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back

Resources created by teachers for teachers

Over 30,000 video lessons & teaching resources‐all in one place.
Video lessons
Quizzes & Worksheets
Classroom Integration
Lesson Plans

I would definitely recommend Study.com to my colleagues. It’s like a teacher waved a magic wand and did the work for me. I feel like it’s a lifeline.

Jennifer B.
Teacher
Jennifer B.
Create an account to start this course today
Used by over 30 million students worldwide
Create an account