#LyX 1.3 created this file. For more info see http://www.lyx.org/ \lyxformat 221 \textclass article \begin_preamble \usepackage{ragged2e} \RaggedRight \setlength{\parindent}{1 em} \end_preamble \language english \inputencoding auto \fontscheme times \graphics default \paperfontsize 12 \spacing single \papersize Default \paperpackage a4 \use_geometry 1 \use_amsmath 0 \use_natbib 0 \use_numerical_citations 0 \paperorientation portrait \leftmargin 1in \topmargin 1in \rightmargin 1in \bottommargin 1in \secnumdepth 3 \tocdepth 3 \paragraph_separation skip \defskip medskip \quotes_language english \quotes_times 2 \papercolumns 1 \papersides 1 \paperpagestyle default \layout Author Paul Johnson \layout Date Feb 28, 2005 \layout Title C Intro Notes #1 \layout Section Read some book, do some exercises \layout Standard No matter how much you watch me \begin_inset Quotes eld \end_inset blather on and on, \begin_inset Quotes erd \end_inset you don't learn anything unless you do exercises, make mistakes, try again. \layout Section Variables types \layout Standard C is a \begin_inset Quotes eld \end_inset strongly typed \begin_inset Quotes erd \end_inset language. That means variables are declared by specific types. \layout Subsection Integers: \layout Standard The number of bits assigned for each type depends on the kind of CPU and the C library you are using, these are usually about right. \layout Standard short 16 bits, \layout Standard int 32 bits, range of values: -32,768 to 32,767 \layout Standard long \layout Standard unsigned int, unsigned long \layout Subsection Real-valued variables \layout Standard float 32 bits \layout Standard double 64 bits \layout Subsection Characters \layout Standard char \layout Standard unsigned char \layout Standard Comment: handling of \begin_inset Quotes eld \end_inset strings \begin_inset Quotes erd \end_inset in C is a pain and you don't really understand it until you practice. \layout Subsection Declarations allowed only in the \begin_inset Quotes eld \end_inset Top of Blocks \begin_inset Quotes erd \end_inset \layout Standard A variable exists only within its \begin_inset Quotes eld \end_inset scope. \begin_inset Quotes erd \end_inset A scope is a somewhat abstract term we should talk over. \layout Standard All variables have to be defined at the top of blocks, and the top of a block is the beginning of a scope. You an do \layout LyX-Code { \layout LyX-Code int x; \layout LyX-Code int y; \layout LyX-Code double z; \layout LyX-Code x = 2 * x; \layout LyX-Code } \layout Standard But you must NOT do some calculations, and then declare another variable \layout LyX-Code { \layout LyX-Code int x; \layout LyX-Code int y; \layout LyX-Code x = 2 * x; \layout LyX-Code double z; \layout LyX-Code z = x * y; \layout LyX-Code } \layout Subsection Take the easy route. \layout Standard Don't bother over variable types. Declare your integers as \begin_inset Quotes eld \end_inset int \begin_inset Quotes erd \end_inset and your real-valued variables as \begin_inset Quotes eld \end_inset double \begin_inset Quotes erd \end_inset . If you run out of memory, buy more. \layout Subsection Cast one variable as another. \layout Standard In C, you might need to do some math. And it gets ugly, especially if you divide 2 integers. It does not give you back a floating point number, as you expect. It rounds off. So you can force division to act as if 2 integers are floats: \layout LyX-Code int x=3; \layout LyX-Code int y=3; \layout LyX-Code double z; \layout LyX-Code z = (double)x/(double)y; \layout Standard When you use the \begin_inset Quotes eld \end_inset cast \begin_inset Quotes erd \end_inset to \begin_inset Quotes eld \end_inset promote \begin_inset Quotes erd \end_inset or \begin_inset Quotes eld \end_inset demote \begin_inset Quotes erd \end_inset a variable, you tell the compiler to do its best to transfer the thing from one type to another. A cast like this \layout LyX-Code x = (int)z; \layout Standard can, on most systems, do a \begin_inset Quotes eld \end_inset rounding down \begin_inset Quotes erd \end_inset of z to the integer value. Its not always predictable, and there are other functions for rounding. \layout Section Printing to the screen \layout Subsection fprintf is it! \layout Standard I've tried to shift from using \layout LyX-Code printf( \begin_inset Quotes eld \end_inset hello, this is a message to the screen \begin_inset Quotes erd \end_inset ); \layout Standard to \layout LyX-Code fprintf(stderr, \begin_inset Quotes erd \end_inset hello, this is a message to the screen \begin_inset Quotes erd \end_inset ); \layout Standard I do that because \begin_inset Quotes eld \end_inset stderr \begin_inset Quotes erd \end_inset is the \begin_inset Quotes eld \end_inset standard error \begin_inset Quotes erd \end_inset stream of messages that go to the screen and this style encourages the program to send the message right away. \layout Subsection Access variables from fprintf \layout LyX-Code int x = 7; \layout LyX-Code double y = 7.7; \layout LyX-Code fprintf(stderr, \begin_inset Quotes erd \end_inset the value of an int is %d \backslash n \begin_inset Quotes erd \end_inset , x); \layout LyX-Code /* note %d means \begin_inset Quotes eld \end_inset an integer \begin_inset Quotes erd \end_inset follows */ \layout LyX-Code fprintf(stderr, \begin_inset Quotes erd \end_inset the value of a double is %f \backslash n \begin_inset Quotes erd \end_inset ,y); \layout LyX-Code fprintf(stderr, \begin_inset Quotes erd \end_inset x=%d,y=%f \backslash n \begin_inset Quotes erd \end_inset ,x,y); \layout Section Control structures \layout Subsection Special use of = and & and | \layout Standard Please beware that the & is a dangerous thing because it is used in \begin_inset Quotes eld \end_inset bitwise \begin_inset Quotes erd \end_inset computing. You might use it thinking it measn \begin_inset Quotes eld \end_inset and \begin_inset Quotes erd \end_inset . It is a mistake. In C, \layout Standard \series bold \size larger == means \begin_inset Quotes eld \end_inset equal to \begin_inset Quotes erd \end_inset in a conditional statement \layout Standard \series bold \size larger && means \begin_inset Quotes eld \end_inset AND \begin_inset Quotes erd \end_inset \layout Standard \series bold \size larger || means \begin_inset Quotes eld \end_inset OR \begin_inset Quotes erd \end_inset \layout Subsection conditional statements \layout LyX-Code int x, y, z; \layout LyX-Code if ( x == 1 ) \layout LyX-Code { \layout LyX-Code y = 2; \layout LyX-Code } \layout LyX-Code else \layout LyX-Code { \layout LyX-Code y = 5; \layout LyX-Code } \layout LyX-Code if ((x == 1) && (y == 2)) \layout LyX-Code { \layout LyX-Code z = 10; \layout LyX-Code } \layout Subsection for loops \layout Standard This is the predominant method of iterating through some problem. \layout LyX-Code int i; \layout LyX-Code for (i = 0; i M_PI) \layout LyX-Code { \layout LyX-Code fprintf(stderr, \begin_inset Quotes erd \end_inset x is greater than pi \backslash n \begin_inset Quotes erd \end_inset ); \layout LyX-Code } \layout Standard The pre-processor would replace the letters M_PI with the desired value. \layout Standard Macros are often used to mark of big sections of code that are ignored or included. \layout LyX-Code #define WHATEVER 1 \layout LyX-Code /* that set the value to 1, which the system sees as true*/ \layout LyX-Code #ifdef WHATEVER \layout LyX-Code /*if WHATEVER is true, do this */ \layout LyX-Code #endif \layout Standard You can also make a conditional that includes unless a macro is defined. \layout LyX-Code #ifndef WHATEVER \layout LyX-Code #endif \layout Standard Why would anybody want to do this? Well, if you want a fast program, and don't need some elements at all, then you make the pre-processor leave them out altogether. \layout Section functions in C \layout Subsection single-valued return \layout Standard In C, a function can take input arguments and give back a single value. \layout LyX-Code double myFunction ( int x, double y) \layout LyX-Code { \layout LyX-Code /* x y are \begin_inset Quotes eld \end_inset input variables \begin_inset Quotes erd \end_inset */ \layout LyX-Code double z = (double)x * y; \layout LyX-Code return z; \layout LyX-Code } \layout Standard Please note, inside a function you can declare more \begin_inset Quotes eld \end_inset local variables \begin_inset Quotes erd \end_inset but you must not use the same names as the inputs \layout Standard If you don't want a return value from a function, you can give it the type \begin_inset Quotes eld \end_inset void \begin_inset Quotes erd \end_inset and leave out the return; \layout Subsection Yes, I mean you can't return an array \layout Subsection But you can return a \begin_inset Quotes eld \end_inset pointer \begin_inset Quotes erd \end_inset to an array. \layout Section What's a pointer. \layout Standard This is where all hell breaks loose. \layout Subsection Static versus Dynamic memory \layout Standard Programs can automatically allocate memory, but only up to a point. If you need an array that holds \begin_inset Quotes eld \end_inset too much stuff, \begin_inset Quotes erd \end_inset then you can't count on the program to handle it. SO a declaration like this \layout LyX-Code int x[1000000000000000000]; \layout Standard will be a non-starter. \layout Standard If an array that big is even possible, you have to use dynamically allocated memory. To do that, first declare a \begin_inset Quotes eld \end_inset pointer \begin_inset Quotes erd \end_inset : \layout LyX-Code int *x; \layout Standard and then you use a command like \begin_inset Quotes eld \end_inset malloc \begin_inset Quotes erd \end_inset or \begin_inset Quotes eld \end_inset alloc \begin_inset Quotes erd \end_inset to claim a block of memory from the computer. \layout Standard I'm not writing down the malloc or alloc command here because you won't usually need to do that. In Swarm, there's an alloc option I use instead. \layout Subsection A pointer is the \begin_inset Quotes eld \end_inset first position \begin_inset Quotes erd \end_inset of a piece of memory. \layout LyX-Code int *x; \layout LyX-Code int i; \layout LyX-Code x = allocate this for N items, however you want \layout LyX-Code x[0] = 3; puts the value of 3 into the first position. \layout LyX-Code for (i = 0; i< N; i++) \layout LyX-Code { \layout LyX-Code x[i] = i*2; \layout LyX-Code } \layout LyX-Code Note an array works like a pointer. \layout Subsection There is other stuff worth knowing about pointers \layout Standard but not a hell of a lot, once you move into using Swarm. \layout Section Include, import, header files. \layout Standard You can write a big, monstrous program all in one file if you want to. But it gets tough to manage and confusing. So you declare a file in 2 parts, the \layout Enumerate header file, such as \begin_inset Quotes eld \end_inset MyFile.h \begin_inset Quotes erd \end_inset \layout Enumerate implementation file, such as \begin_inset Quotes eld \end_inset MyFile.c \begin_inset Quotes erd \end_inset in c, \begin_inset Quotes eld \end_inset MyFile.cc \begin_inset Quotes erd \end_inset in C++, or \begin_inset Quotes eld \end_inset MyFile.m \begin_inset Quotes erd \end_inset in Objective C. \layout Standard The \begin_inset Quotes eld \end_inset include \begin_inset Quotes erd \end_inset statement at the top of a file gives that file access to commands that are defined in the included file. \layout Standard Most C programs have \layout LyX-Code #include \layout Standard That's where printf and other basic features are defined. \layout Standard Look in /usr/include in your file system to see many *.h files. \the_end