week 1 introduction Ritsumeikan University College of Information Science and Engineering Ian Piumarta 1 / 20
class format 30 minutes (give or take a few) presentation 60 minutes (give or take a few) practice : finish the practical exercises the only way to learn a language is to use it recommended books Kernighan & Ritchie, The C Programming Language, second edition concise, no-nonsense explanation from the inventor of C Kernighan & Pike, The Practice of Programming deep wisdom and great advice about writing programs in C, C++ and Java resources on the EPL network, look for epl1server relevant material is in: Shared/Books/Programming/C Ian Piumarta 2 / 20
adjective denoting the mood of a verb that expresses a command or exhortation, as in come here! programs contain explicit commands about how to manipulate data are closely related to the way the machine language manipulates data embrace assignment and mutable state have simple and explicit control flow emphasise the interdependence of data structures and algorithms we will learn C, one of the most successful languages Ian Piumarta 3 / 20
of C once upon a time, operating systems were written entirely in machine code hard to maintain, harder to port to new hardware, slow to evolve UNIX OS (invented by Ken Thompson) first written in machine language new computers (with new machine code) were being developed rapidly porting was painful: invent a portable language for OS development? Ken Thompson Dennis Ritchie C (invented by Dennis Ritchie) was used to re-implement UNIX portably only a very small amount of non-portable machine code remained Linux, Darwin (Mac), Windows, etc., OS kernels are all written in C used often for embedded and systems compilers, interpreters, game platforms, libraries, databases,... Ian Piumarta 4 / 20
of C general-purpose, low-level language basic data types are the same as those implemented by hardware: characters, numbers, addresses operations on data closely resemble typical machine instructions simple control structures: conditionals, loops, functions no composite objects no strings, lists, arrays, dictionaries, etc. (if you want them, you must implement them yourself) no memory management the programmer must allocate and de-allocate memory explicitly no input/output facilities terminal, file, network I/O provided by libraries library functions implement OS services (system calls) directly Ian Piumarta 5 / 20
C is small language description can be brief basic language can be learned quickly competent programmers understand and use the entire language C is close to the OS good at file manipulation, network communication, etc. popular for writing servers, databases C is close to the machine hardware I/O devices directly desktop: operating system kernels, UI frameworks embedded: physical interfaces, timing, control, communication, configuration C is easy to mix with machine code for optimisation high-performance applications: Photoshop, AI/ML, etc. Ian Piumarta 6 / 20
: edit, compile, run, (debug,) repeat C is a compiled language between editing and running, you have to compile your program source file #include <stdio.h> int main() { prinf("yo dude\n"); return 0; } executable file 0110010100100100 1001010100110010 0100100101010010 1001010110010010 0101001010110101 1101100100010101 edit compile run debug cycle advantages: some kinds of error are caught at compile time undefined variables, missing return values, wrong number of arguments,... disadvantages: you cannot modify and resume a program while debugging Ian Piumarta 7 / 20
everyone s first program: hello world the first C program that everyone writes is (traditionally) always the same: print the string hello, world on the terminal #include <stdio.h> int main() { printf("hello, world\n"); return 0; } C programs contain just two things: variables and functions variables store values used during computation functions contain statements specifying what that computation is our program has no variables, and just one function (called main) functions can have any name you like, but the name main has special significance when you run the program, it starts executing at main 1 1 Ian Piumarta compare to Java, which begins execution at the method called main 8 / 20
everyone s first program: hello world #include <stdio.h> int main() { printf("hello, world\n"); return 0; } our main function uses a library function called printf printf prints a string on the terminal it is part of the standard I/O library the first line of our program, #include <stdio.h> tells the C compiler to read the contents of the file stdio.h the stdio.h file contains information about the standard I/O library including how to call the printf function (without that information, printf cannot be used safely) Ian Piumarta 9 / 20
everyone s first program: hello world #include <stdio.h> int main() { printf("hello, world\n"); return 0; } the second line declares our main function first comes the kind of value the function returns in the case of main, an integer result is returned next comes the name of the function followed by a list of parameters that the function expects the parameter list appears inside parentheses in this case, main expects no parameters and so the list is empty Ian Piumarta 10 / 20
everyone s first program: hello world #include <stdio.h> int main() { printf("hello, world\n"); return 0; } the statements of a function appear inside curly braces: {... } there is only one statement in our function printf("hello, world\n"); this is a function call the function name appears first a parenthesised list of arguments follows (if there are no arguments, an empty list must still be written) the semicolon terminates the function call statement in this example, a single argument is passed to printf the string that we want to print within that string is an escape sequence \n these two characters represent a single newline character in the string Ian Piumarta 11 / 20
everyone s first program: hello world #include <stdio.h> int main() { printf("hello, world\n"); return 0; } main returns an integer result the result of main() is always an integer the result returned from main indicates if there were any errors returning 0 from main() indicates there were no errors during execution returning non-0 indicates there was an error the value can indicate what kind of error Ian Piumarta 12 / 20
compiling the C compiler is called cc just tell it the name of the file to be compiled $ cc hello.c if there are no errors, the C compiler remains completely silent no errors, so no output from cc the executable file produced by the C compiler is called a.out once upon a time, a.out stood for assembler output $ cc hello.c $ ls a.out hello.c Ian Piumarta 13 / 20
running the compiler accepts many command-line options e.g., to change the output filename use: cc -o filename $ cc hello.c -o hw $ ls hw hello.c to run a program, type its name you may have to tell the OS to look for the program in the current directory the current directory is called., so your program is./hw $ cc hello.c -o hw $ ls hw hello.c $./hw hello, world Ian Piumarta 14 / 20
the form (and quality) of vary between compilers on Darwin (MacOS), the compiler gives message that look like this line column number number error message program.c:5:27: error: expected ; after expression printf("hello, world\n") problematic line of source code ˆ marker showing where the error occurred ; possible hint(s) about how to fix it each error message contains: the line number in the source file where the compiler became upset the column number within that line an error message explaining why the compiler is unhappy the actual line of source code that the compiler disapproves of a small marker showing the position where the problem begins corresponding to the column number shown above and, occasionally, a suggestion how to fix your error Ian Piumarta 15 / 20
English translations of some common compiler messages: program.c:3:3: warning: implicitly declaring library function printf you forgot to #include the file declaring printf in this case, the file you forgot to include was stdio.h use the man (manual) command to find out about other functions program.c:3:1: warning: type specifier missing, defaults to int you forgot to indicate the type of a function result or a variable program.c:5:27: error: expected ; after expression you forgot to terminate a statement with a semicolon program.c:5:3: warning: implicit declaration of function print is invalid you are trying to call a function that you did not declare maybe because you misspelled a library function name program.c:6:1: warning: control reaches end of non-void function you forgot to return a value at the end of a function Ian Piumarta 16 / 20
one error in a program can cause a torrent of the first error message points to the real problem the rest are, all caused by the first error I forgot to give this function a return type... f() { } which causes a relevant error message program.c:3:1: warning: type specifier missing, defaults to int f() ˆ and a secondary error message, for a problem that really does not exist program.c:5:1: warning: control reaches end of non-void function } ˆ Ian Piumarta 17 / 20
another example: misspelling printf as print program.c:5:3: warning: implicit declaration of function print is invalid Undefined symbols for architecture x86_64: "_print", referenced from: secondary, caused by my typing mistake _main pro-tip: pay most attention to the first error message only many of the errors following it may be secondary I suggest you ignore (literally) all C compiler after the first one: 1 edit 2 compile 3 fix the first error only 4 repeat immediately from step 2, until there are no more errors Ian Piumarta 18 / 20
specifying an output file name cc sourcefile.c -o outputfile turn on all warning messages (make a habit of doing this!) cc -Wall sourcefile.c -o outputfile include information for the run-time debugger (can be useful, and does no harm) cc -Wall -g sourcefile.c -o outputfile when the program works perfectly, optimise it (for speed or for space) cc -Wall -g -O sourcefile.c -o outputfile -O1 optimises a bit (hardly ever useful) -O2 optimises the normal amount (makes the executable faster) -O3 optimises as much as possible (e.g., for lots of heavy mathematics) -Os optimises for space (makes the executable as small as possible) to see precisely what the compiler is doing, tell it to be verbose cc -v -Wall -g -O sourcefile.c -o outputfile Ian Piumarta 19 / 20
reading The C Programming Language introduction (page 5) section 1.1 (pages 5 8) the rest of chapter 1, if you like it will help you prepare for next week exercises questions 1 8 plus the further exploration questions, if possible they will help you prepare for next week Ian Piumarta 20 / 20