Reader small image

You're reading from  Learn C Programming. - Second Edition

Product typeBook
Published inAug 2022
PublisherPackt
ISBN-139781801078450
Edition2nd Edition
Right arrow
Author (1)
Jeff Szuhay
Jeff Szuhay
author image
Jeff Szuhay

Jeff Szuhay is the principal developer at QuarterTil2 which specializes in graphics-rich software chronographs for desktop environments. In his software career of over 35 years, he has engaged in a full range of development activities from systems analysis and systems performance tuning to application design, from initial development through full testing and final delivery. Throughout that time, he has taught computer applications and programming languages at various educational levels from elementary school students to university students, as well as developed and presented professional, on-site training.
Read more about Jeff Szuhay

Right arrow

Chapter 4: Using Variables and Assignments

Programs manipulate data values. Whether a program performs a single calculation, such as, say, converting a temperature value from Fahrenheit into Celsius, reads data only to display it, or performs much more complex calculations and interactions, the values a program manipulates must be both accessible and assignable. Accessible means that a value must reside somewhere in computer memory and should be retrievable. Assignable means that a given value, or the result of a calculation, must be stored somewhere in computer memory to be retrieved and/or changed later. Each value that can be accessed and assigned has a data type and a named location where it is stored. These can either be variables or constants.

Variables, or non-constant variables, hold values that are accessible and assignable. Their values may change during the execution of the program. Constant variables are variables that don't change ...

Technical requirements

For this chapter, you will need the following:

  • A plaintext editor of your choice
  • A console, terminal, or command-line window (depending on your OS)
  • A compiler – either GCC or clang – for your particular OS

The source code for this chapter can be found at https://github.com/PacktPublishing/Learn-C-Programming-Second-Edition/tree/main/Chapter04

Understanding data types and values

Every value in a computer program has an associated type. The type of a value can be inferred by how it is expressed in the program code and how it is coded. Alternatively, the type of a value can be explicitly determined by you. A value in C always has a type. So, a value can have either an inferred or implicit type or an explicit type.

There are also inferred types from literal values. A literal value is a sequence of digits in the program code whose value is implicitly determined by the compiler at compile time, which is when the program is compiled. The value of a literal can never change; it is baked into the program code. 

When a value is given an explicit type, the compiler assigns a type to that value. A value of one type can also be converted into another type, either implicitly by how it is used or explicitly with typecasting.

So, we should always think...

Introducing variables

variable is a location in memory that holds a value of a specified type that can vary over the life of the variable, identified by its name. When the variable is defined with both a type and an identifier, its life begins. It can hold the same value throughout its life, or it can be modified or overwritten with a new value of that type. The variable's life ends – that is, the memory it identifies is deallocated – when the block that it was declared in ends. We'll talk more about variable lifetimes in Chapter 25Understanding Scope.

So, a variable is a memory location with an identifier (name) associated with a type that contains a value. The following three components are essential: 

  • A unique identifier or name
  • A type
  • A value

A variable is created by declaring it; this is also called defining a variable. When we declare a variable, we specify the data type to be stored...

Exploring constants

We use variables to hold values that are computed or can change over their lifetime, such as counters. However, we often have values that we don't ever want to change during their lifetimes. These are constants and can be defined in several ways for different uses.

Literal constants

Consider the following literal character sequences:

      65
     'A'
     8.0f
131072.0

Each of these has an internal byte stream of 0100 0001. However, because of the punctuation surrounding these values, the compiler can infer what types they have from their context:

      65 --> int
      'A' --> unsigned char
      8.0f --> float
 131072.0 --> double

These values are typed into our source code and their...

Using types and assignments

So, we have variables to hold values of a specified type that we can retrieve and manipulate by their identifiers. What can we do with them? Essentially, believe it or not, we can just copy them from one place to another. Values in variables or constants can only be changed through assignment. When we use them, their value is copied as part of the evaluation, but the value remains unchanged. A variable's value can be used in many ways over its lifetime, but that value will not change except for when a new value is copied over it. We will now explore the various ways that variables are copied:

  • Explicit assignment using the = operator
  • Function parameter assignment
  • Function return assignment
  • Implicit assignment (this will be covered when we look at expressions in the next chapter)

Let's look at the first three ways of copying variables in the subsequent sections.

Using explicit assignment...

Summary

Variables are how we store values and their associated types. Variables are identified by a given name. Variable declarations allocate memory for the lifetime of the variable. This depends on where the variable is declared. Variables that are declared within a block, between { and }, only exist while that block is executing. There are variables whose values can change while the program executes, constant variables whose values do not change once they are given a value, and literal values that never change.

Variables are declared with explicit types. However, the type of a value can be implicitly inferred by how it is used. Literal values are constants whose type is inferred both by how they appear and how they are used.

The only way to change the value of a variable is by assigning a value to it. Initialization is the means of giving a variable or constant a value when it is declared. Otherwise, the values of the variables can only change through direct...

Questions

Answer the following questions to test your knowledge of this chapter:

  1. What are the three components of a variable?
  2. What are the various types of constant values?
  3. Why do we initialize variables?
  4. How do we give a variable a value?
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learn C Programming. - Second Edition
Published in: Aug 2022Publisher: PacktISBN-13: 9781801078450
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Jeff Szuhay

Jeff Szuhay is the principal developer at QuarterTil2 which specializes in graphics-rich software chronographs for desktop environments. In his software career of over 35 years, he has engaged in a full range of development activities from systems analysis and systems performance tuning to application design, from initial development through full testing and final delivery. Throughout that time, he has taught computer applications and programming languages at various educational levels from elementary school students to university students, as well as developed and presented professional, on-site training.
Read more about Jeff Szuhay