COSC 2P91
BasiCs Week 2a
Brock University
Brock University (Week 2a) BasiCs 1 / 24
COSC 2P91 BasiCs Week 2a Brock University Brock University (Week - - PowerPoint PPT Presentation
COSC 2P91 BasiCs Week 2a Brock University Brock University (Week 2a) BasiCs 1 / 24 Reminder on C source code Though far less-so than many newer languages, C is still fairly portable . It still needs to be compiled for different target
Brock University (Week 2a) BasiCs 1 / 24
Brock University (Week 2a) BasiCs 2 / 24
◮ Scope and extent rules are mostly the same
◮ Outside of dynamic allocation, of course
◮ We’ll be returning to this again a bit later ◮ External variables can also be implicitly or explicitly made available
◮ Allocated upon entering the block ◮ Deallocated upon exiting the block ◮ Allocated on the stack Brock University (Week 2a) BasiCs 3 / 24
◮ But don’t be “clever” with that knowledge
Brock University (Week 2a) BasiCs 4 / 24
◮ Note: unlike Java, you generally can’t expect an automatic variable to
◮ If you want a variable to have a specific value, give it that value!
◮ Depending on the amount of optimization employed by the compiler, it
◮ It still potentially starts with ‘junk data’, so initialize it before use ◮ Note the word try; it isn’t a guarantee that a register will be available!
Brock University (Week 2a) BasiCs 5 / 24
◮ e.g. #define PI 3.14 will tell the preprocessor to substitute the
⋆ By the time it gets to the compiler, it’s just 3.14 ◮ Some swear by this technique, but it completely obliterates the concept
◮ The compiler may make use of the knowledge that this value won’t
◮ Of course, since you can’t change it later, remember to initialize it in
◮ Note: don’t ignore warnings. Seriously. Brock University (Week 2a) BasiCs 6 / 24
◮ This means you can use it to retain data across invocations of the
◮ Think of it as being roughly analogous to private in Java Brock University (Week 2a) BasiCs 7 / 24
◮ Java has this feature, too, for effectively the same reason
◮ Consider cases where external programs or hardware could be manually
Brock University (Week 2a) BasiCs 8 / 24
◮ Declared in the outer level of the source file (outside of any blocks) ◮ May be accessible within any procedures within the file, if a new local
⋆ Again, global variables to be used locally may be explicitly declared as
◮ We’ve already discussed this ◮ Let’s look at an example, showing why it’s advisable to declare all local
Brock University (Week 2a) BasiCs 9 / 24
◮ short int (or short) ◮ int ◮ long int (or long) ◮ long long int (or long long) ◮ sizeof(short)≤sizeof(int)≤sizeof(long)
◮ float – typically 32 bit ◮ double – typically 64 bit ◮ long double is also a thing, but less common
Brock University (Week 2a) BasiCs 10 / 24
Brock University (Week 2a) BasiCs 11 / 24
◮ Accepts a String as its first parameter ◮ The string may contain special markers for where to insert
⋆ All such substitutions are provided as additional arguments ◮ Common tokens: ⋆ %d or %i – int ⋆ %ld or %li – long ⋆ %f – float ⋆ %lf – double ⋆ %c – char ⋆ %s – string ⋆ %x – formatted as hexadecimal ◮ Don’t forget common special characters (\n, \t, etc.) Brock University (Week 2a) BasiCs 12 / 24
◮ Essentially, we need to allow the procedure to gain direct access to our
⋆ This requires the use of a pointer, which we’ll explain in greater detail
◮ It doesn’t cooperate very well when it encounters text formatted at all
Brock University (Week 2a) BasiCs 13 / 24
◮ e.g. int dealies[10]; ◮ Note that the square brackets must come after the variable name; it
◮ e.g. int dealies[]={1,2,3,4,5};
Brock University (Week 2a) BasiCs 14 / 24
Brock University (Week 2a) BasiCs 15 / 24
◮ i.e. creating a static character array with more than enough space for
◮ So, how do we know how much of the array to use?
Brock University (Week 2a) BasiCs 16 / 24
Brock University (Week 2a) BasiCs 17 / 24
Brock University (Week 2a) BasiCs 18 / 24
◮ (mwahaha) Brock University (Week 2a) BasiCs 19 / 24
Brock University (Week 2a) BasiCs 20 / 24
Brock University (Week 2a) BasiCs 21 / 24
Brock University (Week 2a) BasiCs 22 / 24
Brock University (Week 2a) BasiCs 23 / 24
Brock University (Week 2a) BasiCs 24 / 24