Post

CS50x 02. C

This is the summary of CS50x course.

CS50x 02. C

This is the summary of CS50x course.

Intro


  • Recall that machines only understand binary. Where humans write source code, a list of instructions for the computer that is human readable, machines only understand what we can now call machine code. This machine code is a pattern of ones and zeros that produces a desired effect.
  • It turns out that we can convert source code into machine code using a very special piece of software called a compiler.

Visual Studio Code for CS50



Hello World


  • We will be using three commands to write, compile, and run our first program:
    • code hello.c creates a file and aloows us to type instuctions for this program.
    • make hello compiles the file from our instructions in C and creates an executable file called hello.
    • ./hello runs the program called hello
  • We can build your first program in C by typing code hello.c into the terminal window.
1
2
3
4
5
6
7
8
// A program that says hello to the world

#include <stdio.h>

int main(void)
{
	printf("hello, world\n");
}
  • printf is a function that can output a line of text.
  • Notice that the \n creates a new line after the words hello, world.
  • make is a compiler that will look for our hello.c fine and turn it into a program called hello.
  • Now, type ./hello and your program will execute saying hello, world.
  • You will notice that there is now both a file called hello.c and hello.
    • hello.c is able to be read by the compiler: It’s where your code is stored.
    • hello is an executable file that you can run but cannot be read by the compiler.
  • \ character is called an escape character that tells the compiler that \n is a special instruction to create a line break. There are other escape characters you can use:
1
2
3
4
5
\n    create a new line
\r    return to the start of a line
\"    print a double quote
\'    print a single quote
\\    print a backslash

This post is licensed under CC BY 4.0 by the author.