Title | Intro to C Notes: - Summary The C Programming Language |
---|---|
Author | Javi Arevalo |
Course | Computer Organiz&Program |
Institution | Georgia Institute of Technology |
Pages | 14 |
File Size | 51 KB |
File Type | |
Total Downloads | 18 |
Total Views | 156 |
Textbook notes on Intro to C...
Intro to C Notes:
Part 1 Notes: Progress so far: -Have taken apart a computer from the circuit level to the assemblylanguage level -Know how to work those pieces -Thus, now we are ready to talk seriously about problem-oriented languages Problem-Oriented language: -A language whose statements resemble terminology of the user application-oriented langauge rather than machine language. -On other words, a langauge in which algorithms are expressed in human terminology (math, science, business) as opposed to machine implemenatation terms. -There have been and are lots of them.
One more abstraction: -These days we have abstraction layers within the problem-oriented language layer Level 5: Problem oriented language level: -Object oriented languages: Java fits here -Procedural Languages (C fits here) -Transition (compiler) to Level 4 Level 4: Assembly Language level -Transition (assembler) to Level 3 Level 3: Operating System Machine level -Partial interpretation (operating system) Level 2: Instruction set architecture level -Interpretation (microprogram) or direct execution Level 1: Microarchitecture Level -Hardware Level 0: Digital logic level High-Level Languages: -Give symbolic names to values -Provide expresiveness -Abstract the underlying hardware
-Enhance code readability -Provide Safeguards against bugs Generations: -Ancestors: Fortran, Algol 60, PL/I -Parents: (CPL, BCPL, B) -> C -Descendents: Perl, Java, Python, C++, C# The C Language (history): -BCPL (Typeless) - 1967 - Operating Systems -B - Ken Thompson (UC Berkeley) - First Version of Unix -C - 1972 - Dennis Ritchie (Harvard University) - Implemented on PDP-11 -Unix rewritten in C in 1974 -Publication in 1978 of "The C Programming Language" by Brian Kernighan (Princeton University) and Dennis Ritchie (Harvard University). -ANSI C (1989) -Major updates to C in standard in 1994 and 1999 C Programming Language: -C is the language that will allow me to do anything. -It is used to write operating systems, other languages, low level hardware drivers, cryptography, networking, among others. -It was designed to be easily compiled and to produce compact, efficient code -Wont check for many runtime errors. -Key: it was invented to implement system software for a small computer. -C is not my friend -C trusts me and will do exactly as I ask -Assumes I am a certain, careful, and knowledgeable programmer. -All the symbols mean something -"I'll just type something in and the cmopiler will fix it for me" will not work in C. Languages: -C gives me full control over most of my system, like assembly. Unlike Java, C does not do the following: -Objects -Bounds checking for arrays -Exception handling -Native support of strings
Translation of High-Level languages: -Interpretation (Python)
-Compilation (Java, C) Pros/Cons of each? -Execution -Programming/Debugging -Portability The Compiler: -From C Source Files and Header Files to C Preprocessor Block: -From C Preprocessor to preprocessed source code to compiler. Compiler: -Contains Symbol Table, Source Code Analysis, and Target Code analysis -Source Code Analysis connected to Symbol Table -Target Code synthesis connected to Symbol Table End Compiler -Transition (Object Module(s)) to Linker Linker: -Contains Object Files and Library Object Files. End block -Block produces Executable Image Complete path: -C Source Code (Preprocessor)-> Preprocessed Source Code -Preprocessed Source Code (Compiler)-> Object Files -Object Files + Libary Files (Linker) -> Machine Executable Program -Machine Language Executable Program (Linus) (Loader) -> PC
The C preprocessor: the C preprocessor does four things: -File Inclusion (processes "#include") -Macro expansion (#define) -Conditional compilation -Code line identification File Inclusion: #include -Conventionally, we only include files that end in ".h" -These consist of declarations (including function prototypes) and macro definitions but no executable code If I surround a file name with: -Double quotes "": the preprocessor looks in the current directory and
then the system directories for the file -Angle brackets (), it looks only in the system directories ("/usr/ include") #include Example: -#include -#include -#include -#include "mydefs.h" Header files typically contain Macro definitions, declarations, and function prototypes. However, they do not contain code. Macro Processing: -Macro processing is just text substitution usng some very specific rules. -We are going to need to want to use symbolic names for constants in several places. Example: -#define NULL ((void *)0) -#define MAXWORDLEN 256 -These symbolic names are textually replaced in the source code before it is compiled. Macro Example: char but[MAXWORLDEN], *cp, i; ... if (cp !- NULL && i < MAXWORDLEN) buf[i] = *cp; After the C Preprocessor, the previous code becomes: char buf[256], *cp, i; ... if (cp != ((void*)0) && i < 256) buf[i] = *cp;
Macros with Arguments: Can also use macros like functions (sort of): -#define PRODUCT (a,b) a * b -Now PRODUCT(4,5) would expand to 4*5 However, what about PRODUCT(x+3, y+4)? -Would expand to x+3*y+4 which is not what I intended Solution: use parentheses (order of operation)
-#define PRODUCT (a, b) ((a) * (b)) -PRODUCT(x+3, y+4) now expands to ((x+3) * (y+4)) Key: #define is basically a text search-and-replace operation on my source code file, it is not that smart. -Must be careful with parenthesis. Sometimes because of the simple search and replace operation might not work as I intended it to. I/O printf(): -Printf() is a function in the Standard IO Library -Thus, to use it, I need to put "#include " -First argument is a format string -Characters in the format string are copied to standard output (typically connected to terminal) -Format codes beggining with % reference arguments that come after the format string, in order. I/O scanf(): -scanf() is a function in the Standard IO Library (must include "#include ") -First argument is a format string -Second argument is a reference to the target variable that receives the input value from keyboard input (actually it is a pointer) -Format codes beggining with %reference arguments (the variables) that come after the format string. I/O Format Codes: -%d:decimal number (int) -%x: Hex number (int) -%f: Floating number (float) -%s String (char *) -%c: Character (char) -%p: Pointer (for debugging) Example: printf("Person: %s GPA %f\n", name, gpa) might print: Person: Dan GPA: 2.5 Linux Commands: some important commands: -cd, ls -.== current directory; ..== directory above -cat, more -gcc -rm, mv, mkdir rmdir -man: type man followed by a linux command or C statement/library to learn more -man ls -man printf
Part 2 Notes: Four basic data types: -int -char -float/double -bool (requires #include ) Traditionally in C, we used integers for Boolean values -O means false -Any non zero value means true
Data types: Integer Types -[unsigned] char -[unsigned] short [int] -[unsigned] int -[unsigned] long [int] -[unsigned] long long [int]
Typical size* 8 bits 16 bits 16/32 bits 32/64 bits 128 bits
Floating Point types: -float -double -long double
Typical size 32 bits 64 bits 128 bits
Key: typical size of each data type is system dependent. How big? It depends on my platform -char: at least 8 bits -short int: at least 16 bits -int: at least 16 bits -long int: at least 32 bits -I can tell with sizeof -sizeof is a compile-time constant reflecting the number of bytes held by a data type or instance -sizeof(char)...