CS50 2019 哈佛公开课 Lecture 1 C Note

Lecture 1 C

1. New words

backslash反斜杠 \n newline

>greater than < less than

2. C language

2.1 first C code

#include <stdio.h >

int main(void)  
{
  printf("hello,world\n")
}
  1. c is an old school language, when you define a variable, you should explicitly ensure what type of the variable.
string anwser = get_string("What‘s your Name?\n");
 int counter = 0
printf("hello, %s\n", answer);
  1. In programming, = is not simply equal, but right to left assign or assignment operator move from the right something to the left

  2. in command line often concern first error

  3. In C, like JavaScript, for CONDITION CLAUSE

    if (x > y)
    {
      printf("x is greater than y\n");
    }
    else
    {
      printf();
    }
    
  4. loop

    //while loops
    int i = 0
    while (i<50)
    {
      printf("Hello,world\n");
      i++;
    }
    //for loops
    for (int i = 0; i < 50; i++)
    {
      printf("Hello,worldd\n")
    }
    
    #include <cs50.h>
    #include <stdio.h>
    
    int get_positive_int(string prompt);
    
    int main(void)
    {
        int i = get_positive_int("Positive integer: ");
        printf("%i\n", i);
    }
    
    // Prompt user for positive integer
    int get_positive_int(string prompt)
    {
        int n;
        do
        {
            n = get_int("%s", prompt);
        }
        while (n < 1);
        return n;
    }
    
  5. int.c

    #include <cs50.h>
    #include <stdio.h>
    
    int main(void)
    {
    		int age = get_int("what's your age?\n");
      	printf("you are at least %i days old", age*365);
    	// %i int 占位符  
    }
    
  6. float.c

    #include <cs50.h>
    #include <stdio.h>
    
    int main(void)
    {
      float price = get_float("what's the price\n");
      printf("you total price is %.2f",price*1.065);
      // .2f 2位小数点 占位符
    }
    
  7. terminal special tag ,like * /

    * represent this document can be executed.

    / represent this is a folder.

  8. function

    #include <stdio.h>
    
    void cough(void);
    
    int main(void)
    {
        for (int i = 0; i < 3; i++)
        {
            cough();
        }
    }
    
    void cough(void)
    {
        printf("cough\n");
    }
    

    void cough(void) is for declaring a new function. We should first declare cough before main function calls. void means no variables in this function.

    #include <stdio.h>
    
    void cough(int n);
    
    int main(void)
    {
        cough(3);
    }
    
    void cough(int n)
    {
        for (int i = 0; i < n; i++)
        {
            printf("cough\n");
        }
    }
    

    we abstract our code, give cough function a variable call n, int type.

2.2 Memory, imprecision, and overflow

Our computer has memory, in hardware we called RAM (Random-access memory). Our computer uses RAM to store data, and it's finite. So our computer has a certain number of bits for each float and int, and has to round to the nearest decimal value at a certain point.

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    // Prompt user for x
    float x = get_float("x: ");

    // Prompt user for y
    float y = get_float("y: ");

    // Perform division
    printf("x / y = %.50f\n", x / y);
}

// x: 1
// y: 10
// x / y = 0.10000000149011611938476562500000000000000000000000

It turns out that this is called floating-point imprecision.

#include <stdio.h>
#include <unistd.h>

int main(void)
{
  for (int i= 1; ;i*=2)
  {
    printf("%i\n", i);
    sleep(1);
  }
}
1073741824
overflow.c:6:25: runtime error: signed integer overflow: 1073741824 * 2 cannot be represented in type 'int'
-2147483648
0
0
...

This problem is called integer overflow. for example, when the max memory is 99, when i > 99,

for computer it will omit 1 in 100, turns out 00. There is no place to put 1.

2.3 shorts

1. Data Types

  • int

  • int integers

  • 整数占有内存的4个字节,一个字节有8位,则一共可以存储32位, 二进制 32bit, 数字2也就是 0000 0000 0000 0000 0000 0000 0000 0010

  • unsigned int (无符号的整数)

  • short, long and const these are all qualifier(限定词)

  • char

    char 1个字符占用1个字节(8 bits) 127位,所以ASCII 中有相对应的number

  • float

    浮点数(实数)和整数一样只有32位,所以产生了精度问题precision problem

  • double

    更精确了64位,8字节

  • void

    void is a type, not data types.

  • bool and string is provided by CS50, if you use it, add #include <cs50.h>

2. Operators

  • + - * / %(modulus operator : give us reminder) (Arithmetic Operators)

  • boolean expression

  • logic operators

3. Conditional Statements

  • if-else-else-if

  • switch case

    #include <cs50.h>
    #include <stdio.h>
    void swt(int x);
    int main(void)
    {
    	int x = get_int("x: ");
    	swt(x);
    }
    void swt(int x)
    {
    	switch(x)
    	{
    		case 1:
    		printf:("One!\n");
    		break;
    		case 2:
    		...
    		default:
    		printf:("Sorry! %i is invalid\n", x);
    	}
    }
    
    
  • () ? :

    (x > 10)? x =11 : x = 5
    

4. Loops

5. Command Line

  • ls

    • short for 'list'. can show all your folders and files in your current directory
  • cd

    • short for 'change directory'

    • . name for current directory

    • .. name for parent directory

    • pwd short for 'present working directory'

  • mkdir

    -short for 'make directory'

  • cp

    • short for 'copy'

    • if you want to copy all in the folders, you can add -r cp -r <source> <destination>

    • -r short for recursive

  • rm

    • short for 'remove' the command line will ask you y/n?

      --f short for 'force' skip asking y/n, there is no undo. BE CAREFUL

    • similarity, like cp rm -r <source> is for totally deleting all in the directory.

    • -rf combine with -f BE CAREFUL.

  • mv

    • short for 'move'. can rename files in current directory.
  • chmod

  • ln

  • touch

  • rmdir

  • man

  • diff

  • sudo

  • clear

  • telnet

  • for more in the future.

posted @ 2020-05-26 12:11  PerrySu  阅读(237)  评论(0)    收藏  举报