Original Course link: Writing, Running, and Fixing Code in C
Assignment
GitHub - mvirgo/c-programming: Code worked on for Duke’s Intro to Programming in C course
UNIX basic
cat // output file to terminal
emacs // open file in editor Emacs (be attention for the command, use lowercase, not uppercase)
close a terminal tab, ctrl + shift + w
close the entire terminal including all tabs, ctrl + shift + q
githttp://git-scm.com/book/en/v2/
Emacs command line
copy in Macos, Esc-w
kill(cut), C-w
yank(paste), C-y
undo, C-x u
help, EmacsWiki: Emacs Newbie Key Reference
https://www.gnu.org/software/emacs/manual/html_node/emacs/index.html
void
• void means that the function does not have a return value.
A.6.7 Void (on book: c-programming-language-v2)
main
C compiler
gcc - GNU Compiler Collection
macros ?
pound sign #
angle brackets <>
Debug
#include <stdio.h>;
#include <stdlib.h>;
int main(void)
{printf("Hello World\n");
return EXIT_SUCCESS;
}
hello.c:1:19: warning: extra tokens at end of #include directive
1 | #include <stdio.h>;
| ^
extra tokens ?
when run the code, why professor input so many command :
$ gcc -o hello -Wall -Werror -pedantic -std=gnu99 hello.c
gcc, compiler;
-o, define the name of files;
hello, execute file;
hello.c, source code file;
the details of the compiling process
hello.c - hello.i - hello.s - hello.o - hello
- Preprocessing:
cpp hello.c -o hello.iC Preprocessor scan source code file and then save as middle(temp) filehello.i; - Compiling:
gcc -S hello.i -o hello.sgcc compile the middle filehello.ias assembly filehello.s; - Assembling:
as hello.s -o hello.oAssembler transfer assembly filehello.sas object filehello.o; - Linking:
gcc hello.o -o hellolinker links togetherhello.o, other object files and library files, and create the final execute filehello;
a short command for the above whole process.
$ gcc hello.c -o hello
mod
15 ≡ 3 (mod 12)
Max Function
more concise and readable way to write the code.
int w = max(size1, x_offset + size2);
define the max function, takes two integers as arguments and returns the larger of the two. It uses the ternary operator (?:) as below.
int w = size1 > x_offset + size2 ? size1 : x_offset + size2;
int max(int a, int b) {
return (a > b) ? a : b;
}