Lecture 1 - Introduction. Writing, compiling, and debugging C programs. Hello world.

这篇博客涵盖了C语言的基础知识,包括低级语言概念、GDB在MAC M1上的使用、字符串表示、算术运算符优先级以及宏定义。通过示例解析了代码块、字面量值的区别、表达式求值以及程序编译和执行的过程。同时,讨论了如何修复错误的代码声明,并给出了正确的代码实现。
摘要由CSDN通过智能技术生成

课程重点速记

  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 ‘\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.

  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 ‘=’

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值