Skip to main content
Contents Index
Search Book
Search Results:
No results.
Readability settings Prev Up Next
\(\newcommand{\N}{\mathbb N}
\newcommand{\Z}{\mathbb Z}
\newcommand{\Q}{\mathbb Q}
\newcommand{\R}{\mathbb R}
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 4.7 Common Variable Errors
We all make mistakes. In coding there are several types of errors we are all prone to. Some are akin to spelling or grammar errors (we call them syntax errors) and some are of logical nature, for example. It can be helpful to think about potential errors before getting started so that you can be on the lookout. Letβs look at some common errors in working with variables.
The following three activities present erroneous code. Can you spot the error and fix it? Donβt be afraid to play around and try out multiple solutions. You cannot break anything! Remember that you can always go back to the original problem by clicking on the βStart freshβ button.
What does
7 / 2 evaluate to in C when both numbers are
int?
3.5
3
4
It is a compile error
In one sentence: why does
7 / 2 give
3 and not
3.5?
Write a program that prints the remainder of
17 divided by
5. Paste it here.
How confident do you feel about variables so far?
Lost
Getting there
Comfortable
Could teach it
printf displays a message on the screen
\n starts a new line; quotes are escaped as \"
Loops repeat instructions multiple times
Comments: /* ... */ across lines, // to end of line
If you could use a time machine, would you travel to the future or to the past? When, and where would you go?
Common variable errors
Variables and loops
User input using scanf()
The char datatype
The float datatype
Division in C
Please go around and introduce yourselves: your name, your class year and (intended) major, and the best gift youβve ever given or received. Then brainstorm a team name together and enter the name you chose below.
Variable names contain only letters, numbers and _
They must start with a letter or _
They are case sensitive
Variables must be initialized BEFORE they are used
What was wrong with the given code? Answer in a sentence or two.
What was wrong with the given code this time?
And this one: what was wrong with the given code?
Subsection 4.7.1 Fix Me!
Activity 4.2 .
Youβd like to print your age to the screen. What is wrong with the following code? Can you fix it?
If youβd like our autograder to check your program for correctness, please do not change the
printf() statement.
What was wrong with the given code?
Record your answer below.
Subsection 4.7.2 Fix me too...!
Activity 4.3 .
Again, youβd like to print your age to the screen. And again, something is wrong with the following code. Can you fix it?
If youβd like our autograder to check your program for correctness, please do not change the
printf() statement.
What was wrong with the given code?
Record your answer below.
Subsection 4.7.3 Fix me three...!
Activity 4.4 .
Another error! Youβd like to print your age to the screen. And again, something is wrong with the following code. Can you fix it?
If youβd like our autograder to check your program for correctness, please do not change the printf statement.
What was wrong with the given code?
Record your answer below.
Subsection 4.7.4 Summary
Here is what we have learned:
Variable names can only contain letters, numbers and the underscore character β_β.
Variable names must start with a letter or an underscore β_β.
Variable names are case sensitive.
Variables need to be initialized (assigned a value) BEFORE they are used.