Department of Electrical Engineering and Computer Science
6.087: Practical Programming in C
IAP 2010
Problem Set 1 – Solutions
Writing, compiling, and debugging programs. Preprocessor macros. C file structure. Variables.
Functions and program statements. Returning from functions.
Out: Monday, January 11, 2010. Due: Tuesday, January 12, 2010.
Problem 1.1
(a) What do curly braces denote in C? Why does it make sense to use curly braces to surround
the body of a function?
Answer: The curly braces denote a block of code, in which variables can be declared. Variables
declared within the block are valid only until the end of the block, marked by the matching
right curly brace ’}’. The body of a function is one such type of block, and thus, curly braces
are used to describe the extent of that block.
(b) Describe the difference between the literal values 7, "7", and ’7’.
Answer: The first literal describes an integer of value 7. The second describes a null-
terminated string consisting of the character ’7’. The third describes the character ’7’,
whose value is its ASCII character code (55).
(c) Consider the statement
double ans = 10.0+2.0/3.0−2.0∗2.0;
Rewrite this statement, inserting parentheses to ensure that ans = 11.0 upon evaluation of
this statement.
Answer: double ans = 10.0+2.0/((3.0−2.0)∗2.0);
Problem 1.2
Consider the statement
double ans = 18.0/squared(2+1);
For each of the four versions of the function macro squared() below, write the corresponding value
of ans.
1. #define squared(x) x*x
Answer: 18.0/2 + 1 ∗ 2 + 1 = 9 + 2 + 1 = 12.
2. #define squared(x) (x*x)
Answer: 18.0/(2 + 1 ∗ 2 + 1) = 18/5 = 3.6.
1