我是靠谱客的博主 传统板凳,最近开发中收集的这篇文章主要介绍Lecture 1 - Introduction. Writing, compiling, and debugging C programs. Hello world.,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

课程重点速记

  1. C is a low-level language. low-level is also known as machine
    level language.
  2. gdb on MAC M1 gdb is not support, but you can use lldb.
  3. strings stored as character array
  4. null-terminated (last character in array is ‘’ 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 ‘’
  • ‘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.

  1. #define squared(x) x*x
  2. #define squared(x) (x*x)
  3. #define squared(x) (x)*(x)
  4. #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.”

  1. return 0;
  2. const char msg[] = MSG1;
  3. }
  4. #define MSG1 “All your base are belong to us!”
  5. int main(void) {
  6. #include <stdio.h>
  7. 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 ‘=’

最后

以上就是传统板凳为你收集整理的Lecture 1 - Introduction. Writing, compiling, and debugging C programs. Hello world.的全部内容,希望文章能够帮你解决Lecture 1 - Introduction. Writing, compiling, and debugging C programs. Hello world.所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(63)

评论列表共有 0 条评论

立即
投稿
返回
顶部