cosc 2p91
play

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


  1. COSC 2P91 BasiCs Week 2a Brock University Brock University (Week 2a) BasiCs 1 / 24

  2. 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 architectures and platforms, so program behaviour may not be consistent Typically programs will rely on platform-dependent libraries (for the sake of simplicity, we mostly won’t concern ourselves with the different versions of the C specification) Brock University (Week 2a) BasiCs 2 / 24

  3. Variables Variable declaration is done in effectively the same means as in Java ◮ Scope and extent rules are mostly the same Declaration of a variable is sufficient for memory allocation ◮ Outside of dynamic allocation, of course External variables (declared in the outer level of the file) persist for the entire execution ◮ We’ll be returning to this again a bit later ◮ External variables can also be implicitly or explicitly made available within procedures By default, variables declared inside procedures are automatic ◮ Allocated upon entering the block ◮ Deallocated upon exiting the block ◮ Allocated on the stack Brock University (Week 2a) BasiCs 3 / 24

  4. Variable Names Allowable variable names follow fairly typical rules: Alphanumeric characters Must start with a letter Spaces aren’t allowed, but underscores are Reserved words are... reserved Names are case sensitive ◮ But don’t be “clever” with that knowledge If you’d like to declare multiple variables of the same type, it’s done the same as in Java: i n t i , j , k=3, kwyjibo ; Brock University (Week 2a) BasiCs 4 / 24

  5. auto vs register As already mentioned, variables declared within blocks are automatic implicitly.If desired, you can explicitly declare this by using the auto modifier. Another alternativ If desired, you can explicitly state this via the auto modifier ◮ Note: unlike Java, you generally can’t expect an automatic variable to be initialized to some specific value (e.g. 0) ◮ If you want a variable to have a specific value, give it that value! Alternatively, the register modifier will add a hint that the compiler should try to ensure that the variable is assigned to a register in the CPU ◮ Depending on the amount of optimization employed by the compiler, it might do this by default anyway ◮ 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! Let’s look at an example! Brock University (Week 2a) BasiCs 5 / 24

  6. Constants Obviously, peppering your code with literals isn’t ideal if it can be avoided. This is especially true if you’re using the same value more than once (and each instance will always be the same value as each other). In these cases, some form of constant is probably more appropriate. #define can be used to define a replacement for the preprocessor ◮ e.g. #define PI 3.14 will tell the preprocessor to substitute the literal 3.14 for every instance of PI within the code ⋆ By the time it gets to the compiler, it’s just 3.14 ◮ Some swear by this technique, but it completely obliterates the concept of scope , so you might want to go with the alternative const defines a variable as being a constant ◮ The compiler may make use of the knowledge that this value won’t change ◮ Of course, since you can’t change it later, remember to initialize it in your declaration line ◮ Note: don’t ignore warnings. Seriously. Brock University (Week 2a) BasiCs 6 / 24

  7. static There are two basic uses for the static keyword: A local variable (declared in a block) that’s declared static is not deallocated upon leaving the block ◮ This means you can use it to retain data across invocations of the same procedure A function or global variable declared static will become inaccessible outside of that source file ◮ Think of it as being roughly analogous to private in Java Brock University (Week 2a) BasiCs 7 / 24

  8. volatile It doesn’t seem likely that this will be used much in this course, but adding the volatile modifier acts as an indication to the compiler that a variable might be externally modified. There are two situations in particular where this may be relevant: If the program is threaded, and another thread might be making changes to the variable, then the compiler should know to not rely on it still maintaining the last known value ◮ Java has this feature, too, for effectively the same reason If the compiler would normally employ optimizations that relied on the presumption that the provided code was the only source of changes to the variable — but that wasn’t really the case — that would be... bad ◮ Consider cases where external programs or hardware could be manually changing the variable’s value Brock University (Week 2a) BasiCs 8 / 24

  9. Scope For the most part, C follows the same scope rules as Java (and other C derivatives). Global ◮ 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 variable with the same name is not also declared within the procedure ⋆ Again, global variables to be used locally may be explicitly declared as such via the extern modifier Local ◮ We’ve already discussed this ◮ Let’s look at an example, showing why it’s advisable to declare all local variables at the beginning of the procedure... Brock University (Week 2a) BasiCs 9 / 24

  10. Types There are several types available for use: int ◮ short int (or short ) ◮ int ◮ long int (or long ) ◮ long long int (or long long ) ◮ sizeof(short) ≤ sizeof(int) ≤ sizeof(long) char Floating point ◮ float – typically 32 bit ◮ double – typically 64 bit ◮ long double is also a thing, but less common Both char and int types can be signed or unsigned . Literals can be long if ending with L , and unsigned when ending with U Of course, unsigned long values are possible with UL Brock University (Week 2a) BasiCs 10 / 24

  11. Type conversions Of course, binary operators normally only make sense when both operands are of the same type . You can explicitly cast values: i n t i ; double d =3.6; i =( i n t ) d ; p r i n t f ( ” i c o n t a i n s : \ %i \ n” , i ) ; What about this? i n t i , j ; double d ; i=d=j =3.6; p r i n t f ( ” \ %i \ %f \ %i \ n” , i , d , j ) ; Brock University (Week 2a) BasiCs 11 / 24

  12. Simple IO printf Proper IO is a topic for another time, but we’ve already seen enough basic IO that it’s probably a good idea to make it a bit more official: printf – print formatted ◮ Accepts a String as its first parameter ◮ The string may contain special markers for where to insert substitutions ⋆ 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

  13. Simple IO scanf scanf is the complement to printf ; it reads input from the user, using the same style of formatting tokens The biggest difference comes from the fact that we may wish to receive multiple values — but C doesn’t allow for multiple returns from a function — so we need to get a bit “clever” ◮ Essentially, we need to allow the procedure to gain direct access to our local variables ⋆ This requires the use of a pointer, which we’ll explain in greater detail at a later time It’s worth noting that actually using scanf for multiple values at once might be more trouble than it’s worth ◮ It doesn’t cooperate very well when it encounters text formatted at all differently from expected Brock University (Week 2a) BasiCs 13 / 24

  14. Arrays Arrays in C are handled very similarly to other languages, but aren’t quite as flexible. You can still declare them ◮ e.g. int dealies[10]; ◮ Note that the square brackets must come after the variable name; it isn’t integrated into the type in C You can also pre-initialize them automatically ◮ e.g. int dealies[]= { 1,2,3,4,5 } ; However, dynamic allocation is substantially more complicated (and thus saved for a later date) Brock University (Week 2a) BasiCs 14 / 24

  15. Multidimensional arrays So long as you still aren’t allocating them dynamically, they’re still created “in the usual way”. However, don’t even bother attempting ragged arrays yet The compiler will need to at least know the number of columns Brock University (Week 2a) BasiCs 15 / 24

  16. Strings Unlike some other languages — e.g. Java — strings in C aren’t really anything special; they’re just arrays of characters. However, consider some of the common uses for strings: Data read from a file Data read from a user These might not allow for knowing the precise number of characters in advance; that requires figuring it out during execution. But wait, we aren’t dealing with dynamic allocation yet... that means overallocation is required! ◮ i.e. creating a static character array with more than enough space for the expected input ◮ So, how do we know how much of the array to use? Strings may be initialized in the conventional format (with quotation marks), or explicitly as any other array is initialized The string library ( string.h ) includes several helpful tools. Brock University (Week 2a) BasiCs 16 / 24

Recommend


More recommend