COSC 2P91 Introduction Part Deux Week 1b Brock University Brock University (Week 1b) Introduction Part Deux 1 / 14
Source Files Like most other compiled languages, we ll be dealing with a few different file types, which will have different associated uses. First, let s deal with the source files themselves. Traditionally, C source files end with.c Additionally, we ll eventually learn about headers, which end with.h Brock University (Week 1b) Introduction Part Deux 2 / 14
Source Files (cont) As is common for source files, in C they re just plaintext. Sources can be written to be combined with other files for a project (libraries, etc.), or one may simply be for a program itself. In a fashion similar to Java, a C source file that represents the program will contain a procedure or function called main It may be a void procedure, or a function to return an int, and may or may not receive parameters, but we ll get into both of these topics at a later date Additionally, the file may also contain: Preprocessor directives (typically at the beginning) Constant and variable declarations External variables Other procedures and functions We ll be getting to these later, but for now it s worth explicitly noting that we don t need to encapsulate everything within a class block (because ain t got no class) Brock University (Week 1b) Introduction Part Deux 3 / 14
Gnu C Compiler GCC We ll be using GCC for compiling and linking our projects Technically, gcc actually offloads many responsibilities to other programs It automatically invokes the preprocessor before compiling, and the linker afterwards, unless directed otherwise This is a topic for later, but the preprocessor is a program that takes an initial look over all source files to look for directives specifically included for it e.g. If the compiler is going to need to know about code already provided by an IO library, then #include<stdio.h> tells the preprocessor to also look in the IO header for possible functions that might be used when the compilation starts Brock University (Week 1b) Introduction Part Deux 4 / 14
Gnu C Compiler Compilation The basic procedure for compilation is very similar to what we ve already seen for Java: 1 A preprocessor looks over all of the code to see what else needs to be included, substituted, etc. 2 The compiler runs through each source file, generating object files 3 The linker connects the object files, as well as additional libraries, sets the entry point, and produces the binary executable Invoking it as gcc -c skips the linker phase; you can manually invoke the linker later. You could actually explicitly invoke the linker ld, but I d advise against it unless you have a specific reason to. Similarly, you could only invoke the preprocessor, but outside of limited testing purposes, you likely won t ever have to. Brock University (Week 1b) Introduction Part Deux 5 / 14
Gnu C Compiler A last tidbit... Like everything in command-line linux, you really should check out the man page for gcc, but it s worth at least pointing out the -Wall option, which tells it to include extra warnings. Of course, we ve already seen similar things in Java. e.g. getting better feedback on issues with generics Brock University (Week 1b) Introduction Part Deux 6 / 14
The Preprocessor The preprocessor runs simple macros to aid in the compilation process. We ll get into some neat tricks later on (when we have an actual use for them), but for now we should at least know: #include Roughly analogous to import in Java, but not quite the same thing! Think of it as being closer to actually copy&pasting code from other files into the current source file, but without literally having to have it all in that same file Good for function prototypes, and also for sharing constants and typedefs This is the starting point for using multiple source files, but can also introduce its own conflicts #define Though many will disagree, I d suggest using this sparingly for its most common use There s really no need to use macros for things as simple as constants when you could simply define a constant the right way We ll see some really important uses of this in a later lecture Brock University (Week 1b) Introduction Part Deux 7 / 14
The simplest example ever? Pretty dang close #i n c l u d e <s t d i o. h> i n t main ( i n t argc, c h a r a r g v [ ] ) { p r i n t f ( H e l l o c l a s s! ) ; } Let s try compiling and running this, and then coming back to it in juuust a moment... Brock University (Week 1b) Introduction Part Deux 8 / 14
Variables Declaration and assignment Variable declaration and assignment are pretty much identical to Java in basic usage. Of course, some of the available types are different. An unsigned char wouldn t make a lot of sense in Java, now would it? Brock University (Week 1b) Introduction Part Deux 9 / 14
What s a printf? How do I printf numbers and variables and dragons and such? We ll be covering IO later, but for now we should probably look at output and input just a smidgen. Brock University (Week 1b) Introduction Part Deux 10 / 14
Arithmetic and Relational Operations This is reeeally familiar... Java s arithmetic and relational operators are derived primarily from C. As such, expect C to behave pretty much the same as Java. This includes things like order of operations. Just to check though, we remember things like ++,, etc., right? What s going to be fun is using relational operators without explicit boolean types, but of course we don t always even use them explicitly in Java, right? Brock University (Week 1b) Introduction Part Deux 11 / 14
Additional Operators Are we familiar with these? ^ ~ << >>? Brock University (Week 1b) Introduction Part Deux 12 / 14
Other Decimal Bases If necessary, we can express integer literals in hexadecimal or octal notation, in the same way as most other languages that offer them. No explicit binary, though. Brock University (Week 1b) Introduction Part Deux 13 / 14
Final Thoughts Are there any simple examples we d like to try? Brock University (Week 1b) Introduction Part Deux 14 / 14