/* ** $Id: data.c 720 2008-02-01 15:41:23Z phf $ ** ** Here's a mess of code we used in lecture to look at the ** basic bits and pieces of C programs. I simply added some ** comments, but you should really read the book instead of ** using this as your primary source about C programming. */ #include #include /* needed for extremal values of various data types */ #include /* needed for string functions such as strlen() and strcpy() */ #include /* needed for type bool as well as constants true and false */ #include int main(void) { // C doesn't guarantee an initial value for variables as // you'll see for sure if you compile with -O (well, at // least on my laptop anyway). Also with -O and the usual // warning options, you should at least get a warning about // this issue. int oops; printf( "oops == %d\n", oops ); oops = 0; // So be safe and *always* initialize a variable when // you declare it! Here are some examples, including // simple expressions as well: int x = 10; int y = x - 3; int z = x + y * 3; printf( "x == %d, y == %d, z == %d\n", x, y, z ); // C also doesn't guarantee what size numbers a variable // of type int can hold; see the book for details, but // using limits.h you can find out what the size is for // a given compiler: int max = INT_MAX; int min = max + 1; // overflow, but no error! printf( "min == %d, max == %d\n", min, max ); // C offers a number of different sizes for integers, // but unless you *must* use one of these, just stick // with plain ints. short int klein = SHRT_MAX; long int gross = LONG_MAX; // on my laptop, this is the same as just int printf( "klein == %d, gross == %ld\n", klein, gross ); // notice %ld! // Even weirder, in C you can say that you want ints // without a sign, but this is getting a little esoteric // so I won't persue it further: unsigned long int wow = -1; printf( "wow == %lu\n", wow ); // notice %lu! // Aside from size and sign, you can also add various storage // classes such as register, volatile, static, etc. We'll see // some of these later, but one is really important: const! const int cant_touch_this = 42; printf( "cant_touch_this == %d\n", cant_touch_this ); // If you try to assign to it, the compiler won't let you; // remove the comment below to see that: // cant_touch_this = 10; // Of course there are also other types, for example float // and double for "floating point" arithmetic. Won't go into // details here, but a few examples: const double pi = 3.14; // literal 3.14 has type double, not float! const double radius = 4; // automatic conversion from int to double const double area = radius*radius*pi; // note the different format codes and their effects! printf( "pi == %f, radius == %e, area == %g\n", pi, radius, area ); // here's a troubling thing... printf( "not pi: %d\n", 355/113 ); // this has type int printf( "yep pi: %f\n", 355.0/113 ); // this has type double // and a more complex formatting code: printf( "%10.3e\n", pi ); // Hey, quiz time! What's the deal here? :-) int groovy = 0x12; int funky = 010; printf( "groovy == %d, funky == %d\n", groovy, funky ); // Stuff in single quotes has type char char c = 'c'; char lf = '\n'; char t1 = '\t'; char t2 = '\011'; printf( "%c%c%c%c%c%c", c, t2, c, t1, c, lf ); // Remember that C confuses int and bool a lot; however, // in code you write you should use bool when you mean // a boolean value, don't rely on the old int convention! // In either case, printing a bool is not supported by // printf(), so you'll have to do that otherwise... // (see the "riddle" at the end of this file :-) int one = (c == 'x') || funky < 100; bool two = (c == 'x') || funky < 100; bool three = false; printf( "one == %d\n", one ); printf( "two == %d\n", two ); // no explicit format code for bool available! printf( "three == %d\n", three ); // Ugly truth, don't ever use this! Remember that I said the // C guys have to pay attention to backward compatibility? _Bool condition = x == 10; // Strings in C are messy, and I'll only show some bare // essentials here; if in doubt, try to avoid strings // for a while... :-) const char *s = "Peter"; // stuff in double quotes is a string printf( "%s | %s\n", s, s ); printf( "%zd\n", strlen( s ) ); // returns size_t, special format code! // Can't just use "+" to concatenate strings as in Java... // char* t = s + " Froehlich"; // Instead we have to call various functions and make sure that // we have enough memory for the string we want to build; tricky // in detail, but here's a short version of it: char t[1024]; strcpy( t, s ); // copy string s into buffer t strcat( t, " Froehlich" ); // append " Froehlich" to buffer t printf( "%zd\n", strlen( t ) ); printf( "%s\n", t ); // Finally, some scary stuff for the end. :-) Read up // on the "?:" operator if you want, but use it *very* // sparingly and only if concision is worth the mess // you create! printf( "one == %s\n", (one ? "true" : "false") ); printf( "two == %s\n", (two ? "true" : "false") ); printf( "three == %s\n", (three ? "true" : "false") ); return EXIT_SUCCESS; } /* ** Really, read the book, this file is not enough to get ** these portions of C! */