09~2-Cmiscellany

09~2-Cmiscellany - Computer Science 136: Elementary...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Computer Science 136: David R. Cheriton School of Computer Science Elementary Algorithm Design and Data Abstraction Winter 2012 Lecture 9¾ – A Miscellany of C-isms So far we have taken an idiomatic approach to learning C. Here we are a little more systematic in examining some language features C has many language constructs and intricacies that can be useful. C also has many features that are simply reflections of the 1970s-era PDP-11 architecture for which it was first designed. Many of these are quite effective at obfuscating the meaning of your code. Be aware, but use them with care. We also sketch the function call mechanism and the run-time stack CS 136 – Winter 2012 09.75 – A Miscellany of C-isms 1/23
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Booleans There is no actual boolean type in C – only int s Comparison operators (like > , < , ...) and logical operators (like and || ) return “ 0 ” for false and non-zero for true C99 has a type _Bool , which is really just another name for int , but with values restricted to 1 and 0 You can include <stdbool.h> which defines the nicer name bool for _Bool , and defines constants like true and false #include <stdio.h> #include <stdbool.h> int main( void ) { bool getout=false; int i=0; while (!getout) { printf("i=%d\n",i); if (i>50) { getout=true; } } } CS 136 – Winter 2012 09.75 – A Miscellany of C-isms 2/23
Background image of page 2
More int types in C C has quite a number of “primitive” data types, many of them variants of the int type. short , int , long , and long long are int s, with a sign, of different lengths C offers no exact specification of size other than sizeof ( short ) 6 sizeof ( int ) 6 sizeof ( long ) 6 sizeof ( long long ) range of values available on a particular machine in <limits.h> , which defines SHRT_MIN , INT_MIN , LONG_MIN , LLONG_MIN (and similarly for SHRT_MAX , . . . , LLONG_MAX ) a char is actually a kind of int , with sizeof ( char )==1 int types can also be designated as unsigned , which means they only represent positive number, with range 0 . . . 2 sizeof ( int ) * 8 - 1 CS 136 – Winter 2012 09.75 – A Miscellany of C-isms 3/23
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Named Constants Programs often have programmer-defined constants in them. Literals (like 10, ’a’, etc.) can appear anywhere a value of the appropriate type is legal But literals might need to change, and can be scattered (and repeated) throughout code So far in C, all declared variables can be changed. Might declare int maxval=100; but could then say maxval=20; Use a const modifier when declaring a variable: variable must be initialized, but can’t be modified thereafter: it’s constant For example: const int max_nodes = 17; const char first_letter = ' A ' ; Constants improve code maintainability, clarity and possibly improve performance CS 136 – Winter 2012 09.75 – A Miscellany of C-isms 4/23
Background image of page 4
Use of Named Constants Constants may be used within any scope (i.e., global, static global, or local) const int important_global = 42; // A static const int internal_const = 43; // B int
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 23

09~2-Cmiscellany - Computer Science 136: Elementary...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online