课程重点速记
- C is a low-level language. low-level is also known as machine
level language. - gdb on MAC M1 gdb is not support, but you can use lldb.
- strings stored as character array
- null-terminated (last character in array is ‘\0’ null) not writen explicitly in string
literals
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?
- curly brances define the region of code block.
b. Describe the difference between the literal values 7, “7”, and ’7’.
- 7 is a int type.
- “7” is a string. it is ‘7’ plus ‘\0’
- ‘7’ is a char.
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.
- 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.
- #define squared(x) x*x
- #define squared(x) (x*x)
- #define squared(x) (x)*(x)
- #define squared(x) ((x)*(x))
-
ans1 = 18.0/2+1*2+1 = 9.0+3 = 12.0
-
ans2 = 18.0/(2+1*2+1) = 18.0/5 = 3.6
-
ans3 = 18.0/(2+1)*(2+1) = 18.0
-
ans4 = 18.0/((2+1)*(2+1)) = 18.0/9 = 2.0
Problem 1.3
Write the “Hello, 6.087 students” program described in lecture in your favorite text editor and compile and execute it. Turn in a printout or screen shot showing
• the command used to compile your program
• the command used to execute your program (using gdb) • the output of your program
~/D/C/M/lecture1 [2]> lldb p1-3.o
(lldb) target create "p1-3.o"
Current executable set to '/Users/dudu/Documents/C_Programming/MIT_6087/lecture1/p1-3.o' (arm64).
(lldb) r
Process 869 launched: '/Users/dudu/Documents/C_Programming/MIT_6087/lecture1/p1-3.o' (arm64)
Hello, 6.087 studentsProcess 869 exited with status = 0 (0x00000000)
Problem 1.4
The following lines of code, when arranged in the proper sequence, output the simple message “All your base are belong to us.”
- return 0;
- const char msg[] = MSG1;
- }
- #define MSG1 “All your base are belong to us!”
- int main(void) {
- #include <stdio.h>
- puts(msg);
Write out the proper arrangement (line numbers are sufficient) of this code.
6-4-5-2-7-1-3
Problem 1.5
For each of the following statements, explain why it is not correct, and fix it.
(a) #include <stdio.h>;
(b) int function(void arg1) {
return arg1-1;
}
© #define MESSAGE = “Happy new year!” puts(MESSAGE);
-a no ‘;’
-b the return type of function shall be int but not viod
-c no ‘=’