L06_c_lang Minimal Program

The main() function is where program execution begins.

Note that it returns an int. This is the exit status of the program. The possible exit statuses are the numbers between 0 - 255 inclusive, but the most common are 0 and 1, success and fail, respectively. Compile -> Link -> Execute Compile -> Link -> Execute

Actually we won’t be worrying about the “link” step, since we won’t be using external libraries (at least for a while).

● Compile - this step produces an executable file. ○ gcc file.c // stands for “gnu compiler collection.” Google gnu if curious. ○ gcc options - use any combination as desired ■ -g // produce debugging information ■ -Wall // enable all warnings about questionable (but not technically wrong) // coding constructions, and that are easy to avoid ■ -o file // specify output executable name (default name is simply “a.out”) ■ gcc myprogram.c -g -Wall -o runme // e.g. ● Execute - if compilation was successful, the executable file will be generated. To execute it, simply type its file path into the terminal. Usually, that’s simply “./executablename ” Compile -> Link -> Execute printf()

The manuals are your friends, if you want to know how to use some function in your program, type “man funcname” into a terminal window. C’s standard library funcs are on manual page 3, accessed by “man 3 funcname ”.

Highlights printf()’s man page: must write this at the top of your file in order to use printf

it’s ok if this is confusing right returns an int now printf() e.g.

Format specifiers, which start with the % sign, in printf()’s first argument are placeholders that indicate what type will go there. Subsequent arguments to printf (after the first one) are the actual values that replace the format specifiers.

printf(“Hi %s, my name is %s”, “Allison”, “Greg”);

format specifiers, specifying s for type “string”

the actual strings that replace the instances of “%s”

prints: “Hi Allison, my name is Greg ” Escape Sequences

Escape sequence - An escape sequence is a sequence of characters that does not represent itself when used inside a or , but is translated into another character or a sequence of characters that may be difficult or impossible to represent directly.

In C, all escape sequences consist of two or more characters, the first of which is the , \; the remaining characters determine the interpretation of the escape sequence. For example, \n is an escape sequence that denotes a character. (Wikipedia)

e.g.: printf(“%s\n\n%s”, “hi”, “there”); prints: hi

there Escape Sequences