[Week 1-3] Programming Basics - 복사본
[Week 1-3] Programming Basics - 복사본
Hello, World!
How a program works
A Simple C Program : Printing a Line of Text
This simple program will print out "Hello World!" on the screen.
Contents inside of the figure of bottom-right corner is the output of the program.
A Simple C Program : The Linker and Executables
The parentheses after main indicate that main is a program building block called a function.
C programs contain one or more functions, one of which must be main.
Every program in C begins executing at the function main.
The keyword int to the left of main indicates that main "returns" an integer (whole number) value.
Functions also can receive information when they’re called upon to execute.
The void in parentheses here means that main does not receive any information.
A Simple C Program : The main function
An output statement instructs the computer to perform an action, namely, to print on the screen the
string of characters marked by the quotation marks.
The entire line, including the printf function, its argument within the parentheses and the semicolon
(;), is called a statement.
Every statement must end with a semicolon (also known as the statement terminator).
A Simple C Program : Escape Sequences
Because the backslash has special meaning in a string, i.e., the compiler recognizes it as an escape
character, we use a double backslash (\\) to place a single backslash in a string.
Printing a double quote also presents a problem because double quotes mark the boundaries of a
string—such quotes are not printed.
A Simple C Program : Escape Sequences
Standard library functions like printf and scanf are not part of the C programming language.
When the compiler compiles a printf statement, it merely provides space in the object
program for a “call” to the library function.
When the linker runs, it locates the library functions and inserts the proper calls to these
library functions in the object program.
Now the object program is complete and ready to be executed.
- For this reason, the linked program is called an executable.
If the function name is misspelled, it’s the linker that will spot the error, because it will not be
able to match the name in the C program with the name of any known function in the libraries.
Variations on our first program
- Variations and some error cases will be shown during the demo.
Follow-up question
- Make a program that prints out the following.
Hello, "Young"!⤶