当前位置:首页 >> 工学 >>

C


LECTURE ONE – VARIABLES AND FUNCTIONS
What these lecture notes cover These lecture notes should cover the following topics: ? Some introductory material in which I justify this course’s existence. ? More detail about the "Hello World" program. ? Elements of C – pre-processor, language and libraries. ? Keywords in C – a first look. ? Variables and types in C. ? What are functions and what is prototyping? ? What is scope and why is it useful? ? Syntax of the C language These notes assume you have already worked through and understood the exercises in Worksheet One. Lecture One – Variables and Functions.................................................................................................... 1 What these lecture notes cover ............................................................................................................. 1 Introduction – What we hope to teach you (and why) .......................................................................... 1 Why teach C? ....................................................................................................................................... 2 Writing compiling and running a C program........................................................................................ 2 Hello World revisited ........................................................................................................................... 3 Elements of C – Pre-processor, Language and Library ........................................................................ 4 Keywords in C ...................................................................................................................................... 4 Types of variable .................................................................................................................................. 5 Choosing names for variables ............................................................................................................... 5 char variables ..................................................................................................................................... 6 Other types signed/unsigned short/long and const ............................................................................... 6 Some more simple mathematical operations ++, --, +=, -=, *=, /= and % ......................... 7 Functions .............................................................................................................................................. 8 void functions ..................................................................................................................................... 10 Returning from functions (the return statement) ................................................................................ 10 Prototypes of functions ....................................................................................................................... 12 Local variables, Global Variables and Scope ..................................................................................... 12 Summary of Language elements learned in worksheet and lecture one ............................................. 14 Example code using language elements from worksheet and lecture one .......................................... 16 Introduction – What we hope to teach you (and why) This course aims to teach you how to program in the C programming language (obviously). There are very good reasons why a mathematician should learn how to program: 1) A good chunk of modern mathematics (much of chaos theory and proving the four colour theorem to give but two examples) relies on computer programming and would be inaccessible without it. 2) Computer simulation techniques allow us to apply mathematical ideas to solve real-world problems (such as setting traffic signals or curing congestion on the internet). 3) If you decide that you don't like being a mathematician after all, computer programming will make you employable. finally, and most importantly, 4) Computer programming can be fun (really, it can be fun I don't necessarily guarantee that it will be fun). In this course I hope to teach you: ? How to program in the C language and use the most common libraries. ? How to write stylish and elegant programs. ? How to write mathematical programs that are small and fast running.

1

? ? ?

How to document code so that other people who might have to use it don't want to kill you. How to tell a good algorithm from a bad algorithm (well, how to make an educated guess anyway). Some simple and handy techniques which will serve for everyday programming needs.

I do not plan to teach you: ? The latest fancy Windows graphical widget (it may be fun but you'll have to learn a new one in a few years). ? How to create all-singing, all-dancing multimedia products (sorry, we're mathematicians not internet consultants). Why teach C? There are a large number of programming languages in the world today. Why am I teaching you C and not (to name but a few) C++, Java, Ada, BASIC, COBOL, Perl, Pascal, Smalltalk, Assembler or FORTRAN. There are several reasons to learn C: ? ? ? C is a common language. There are an awful lot of C programs out there. In fact we would go so far as to say that there are more lines of C code than any other language in the world today. C is a small language. C has only thirty two keywords (and only about twenty of them are in common use). This makes it relatively easy to learn (we hope) compared with bulkier languages. C is a stable language. The ANSI standard for C was created in 1983. The language has not been revised since then. (Sadly this doesn't mean that all C code is standard). In newer languages (mentioning no names) the standard changes regularly and often and what is good code today may not compile properly tomorrow. C is a quick language. A well written C program is likely to be as quick or quicker than a well written program in any other high-level language. C is a core language. That is to say, a number of common and popular computer languages are based upon C. Having learned C, it will be much easier for you to learn languages which are largely, or in part based upon C. Such languages include: C++, Java, awk and Perl.

? ?

Indeed, it is often said that C is the second best language for any given programming task. The best language depends on the nature of the particular task but the second best language is C, whatever the task. Writing compiling and running a C program Some jargon listed for you: –Source code: The stuff you type into the computer. The program you are writing. –Compile (build): Taking source code and making a program that the computer can understand. –Executable: The compiled program that the computer can run. –Language: (Special sense) The core part of C central to writing C code. –Library: Added functions for C programming which are bolted on to do certain tasks. –Header file: Files ending in .h which are included at the start of source code. Just to remind you about your experience in worksheet one, there are three stages to writing a C program as shown by the diagram:

2

hello.cpp

Edit the program

#include <stdio.h> int main() { printf (“Hello World”); return 0; }

This is source code

Compile (build) the program Hello World Press any key to continue.

Execute (or run) the program

Firstly you must write the program’s source code – that is to say the C language instructions shown in the first box above. (Teaching you how to do this is what this course is about). The second stage is to compile the program. This causes the computer to take the source code and attempt to make it into an executable – that is to say a program which the computer can run. As a beginner you will find that your programs will often fail at this compilation stage and the compiler will complain about problems with your code. The final stage is to run the executable code. Some errors will still turn up at this stage. Hello World revisited You probably saw easily enough of the Hello world program in Worksheet one. Here it is again to remind you: #include <stdio.h> /* My first C program which prints Hello World */ int main (int argc, char *argv[]) { printf ("Hello World!\n"); return 0; } There's a lot to be learned from this particular example. For one thing, C doesn't care about whitespace much (spaces, returns and tabs). So the program would work just as well as: #include <stdio.h>/* My first C program which prints Hello World */ int main(int argc, char *argv[]){printf("Hello World!\n");return 0;} (But note that nothing but the comment could go on the # line and it was necessary to keep some of the spaces.) On the other hand it could be written as: #include <stdio.h> /* My first C program which prints Hello World */

3

int main ( int argc , char *argv[] ) { printf ( "Hello World!\n" ) ; return 0 ; } (The #include statement cannot be broken up and anything within " marks should stay on the same line but other than that anything goes). The first version of the program looks MUCH nicer and it is expected your code will look more like the first version than the strange example given. Elements of C – Pre-processor, Language and Library There are three main elements to the C language all of which are represented in "Hello World". The pre-processor is so called because it makes alterations to the C source code before the source code is compiled. Pre-processor statements begin with a # character. In this case, the pre-processor statement we have is a #include statement. #include tells the pre-processor to "glue in" another bit of C source code here. Included files are known as header files and by convention have the extension .h. The angle brackets surrounding the file name tell you that the file being included is part of the C libraries on this machine. In this case, it is the standard library header stdio.h which contains information about standard input and output routines. We will learn more about the pre-processor later and also how (and why) C programmers write their own header files. The C language (which is, after all, what is being taught) is an extremely small base of a very few keywords and syntactical elements. (C has only thirty two keywords – they are listed later in these notes – and most of them you don't need to use anyway). Finally, there are the C libraries. The libraries are full of helpful routines which do general "useful things" that you might commonly want to do. If the task you're thinking of is quite simple or common (sorting a list of numbers into order or calculating cosines for example) then the chances are there's something in the C libraries to do it. In C, even things as basic as printing to screen or writing information to disk are library routines. In Hello World, we used the library routine printf which writes to screen. To access the stdio library (standard input and output) we had to include the header file. It sometimes surprises people that C considers disk access and printing to screen as not important enough to be in the language but instead consigns them to a library. Keywords in C Here are the C keywords (the categories depend on the prejudices of your lecturer):

4

Flow control (6) – if, else, return, switch, case, default Loops (5) – for, do, while, break, continue Common types (5) – int, float, double, char, void For dealing with structures (3) – struct, typedef, union Counting and sizing things (2) – enum, sizeof Rare but still useful types (7) – extern, signed, unsigned, long, short, static, const Keywords which are pure and unadulterated evil and which we NEVER use (1) – goto Wierdies that we don't use unless we're doing something strange (3) – auto, register, volatile (Total keywords: 32) We will revisit it later in the course, when, hopefully, it will mean a little more. As you can see, we've already covered a good chunk of the language: int, float, double, while, return, if, else and for means that we've already covered 8/32 of the C language. (Even more if we take into account that the last four keywords won't be used). Don't bother learning the table (most C programmers couldn't list all 32 keywords) but just take heart that you've already got so far through it. Types of variable C is a "weakly typed" language. That means, you can't just plonk down a variable name and start using it – you have to tell the compiler what type of variable it is first. This may seem a nuisance for you but it has a number of benefits to the programmer – not least of which is that it prevents code like this from compiling: my_variable = 1; while (my_varible < 4) { /*Oops – fumble fingered spelling*/ do something my_variable++; } which would cause you a lot of headaches. [A strongly typed language is one where you cannot convert between one type and another without explicitly telling the program are about to do so – the bondage and discipline school of the programming. An untyped or run-time typed language is one where you do not have to declare a variable before using it. In an untyped language the above program would be legal but would not do what you might expect if you didn’t notice they spelling error.]] We've already met int, double and (briefly) float so let's look at some other common variable types. double is like a float but has twice the "precision" (loosely speaking, it has more decimal places). So why not use double all the time? Because it has more digits, it's likely to be slower and it's definitely going to take more memory up. Use a float when you want to save memory or time and double is too large or slow. IMPORTANT RULE: Whenever you want to use a variable in your program it must first be declared. This means you must tell the C compiler its type and its name. This is done at the start of main (or at the start of a function). The type of the variable will be int, float, double (or some other things we will see later). Its name is up to you but there are good names and bad names. Choosing names for variables Variable names can be just about any combination of letters and underlines but must not start with a number. By convention, C programmers use lower case or Mixed Case variable names but not UPPER CASE [the reason should become clear later, but, in any case, if you program in all UPPER CASE,

5

people will think you're a FORTRAN programmer, which is in itself enough reason to not do it]. The exact rules for naming variables are found in K&R on page 35. Good variable names reflect the purpose of the variable and how commonly it is used. They should not be too long nor too short for their role in the program. If you only use a variable three or four times you will not mind calling it My_important_loop_variable_used_to_count_sheep but if you have to use it a lot you might regret all that typing. For historic reasons, it is traditional to use i,j and k as loop counter variables in for loops. You don't have to do this but, when you do, it reassures everyone else that you know what you're doing and also helps clarify what's going on. Try to express what your variable does in the program using its name (if it counts the number of students call it no_students not n or num). You might like to use underscores for this. Variable names are usually lower case or MixedCase. char variables char is a character variable. That is to say that it can hold one (and only one) alpha-numeric character. In C, single characters must be specified using single quotes. For example: char char char char a= 'A'; space= ' '; one= '1'; new_line= '\n';

(The last example might seem a bit confusing since it looks like two characters, as in the Hello world program – '\n' translates to only one character – the character "newline" which returns you to the start of the next line of printing.) Caution: It is a common beginner mistake to put a char in double quotes or a string (see later in the course) in single quotes. In a printf statement you can use %c to print a char. Note: In fact, a char is really just a one byte number – so you can equally well say: char a= 45; You can find out what numbers correspond to what characters by doing something like this: printf ("Character %c is the same as number %d\n",'e','e'); /* 'e' is interpreted first as a character then as a number */ Other types signed/unsigned short/long and const It is important to remember that variables take up a certain amount of storage space and have a limit on what type of numbers they can store. For example, a char variable can never store a number larger than 255. How high a value an int can store depends upon your compiler. There are a number of alterations we can make to variables. For example: signed or unsigned can be added before the type to say whether or not a variable can be negative or not. For example: unsigned char c; declares that C can hold a number from 0 to 255. We can also declare ints to be signed or unsigned.

6

We can also declare ints, floats and doubles to be long or short. long uses the maximum length for that type of variable permitted on that computer and short uses the minimum length. long and short can be combined with signed and unsigned. Note that no guarantees are made that on any given machine longs are bigger than ints or that shorts are smaller. You will not often use long, short, signed or unsigned except when space is extremely low and you want a large number of variables of that type. We can also declare a variable as const meaning that it is constant and cannot be changed. This can be useful for example when declaring fundamental constants. const float e= 2.718281828; Some more simple mathematical operations ++, --, +=, -=, *=, /= and % Recall from the worksheets that ++ is shorthand for "add one to variable" and -- is shorthand for "subtract one" IMPORTANT RULE: ++i means increment and then use and i++ means use then increment. For example: i= 5; printf ("%d\n",i++); would print 5 whereas: i= 5; printf ("%d\n",++i); would print 6. However, it is far clearer and better not to rely on this behaviour since it is confusing and can backfire. It is far safer and clearer to write: i= 5; printf ("%d\n",i); i++; for the first example and: i= 6; printf ("%d\n",i); for the second example. In C it is possible to construct horrendous and complicated examples – but it is usually better for everyone's sake if you don't. In addition we have the following: -- (subtract one from variable) e.g. countdown--; (Count down counts down by one). += (add to a variable) e.g. a+= 5; (add 5 to a). -= (subtract from variable) e.g. num_living-= num_dead; (Subtract the dead from the living ). *= (multiply a variable) e.g. no_bunnies*=2; (They breed like rabbits). /= (divide a variable) e.g. fraction/= divisor; (Divisor was clearly a good name for it). Another mathematical operation that we missed out is modulo %. (x % y) returns the remainder when x is divided by y – obviously using integers.

7

Casting between types Mixing types can cause problems. For example: int a= 3; int b= 2; float c; c= b*(a/b); printf ("2 * (3/2) = %f\n", c); doesn't behave as you might expect. Because the first (a/b)is performed with integer arithmetic it gets the answer 1 not 1.5. Therefore the program prints: 2 * (3/2) = 2.000 The best way round this is what is known as a cast. We can cast a variable of one type to another type like so: int a= 3; int b= 2; float c; c= b* ( (float)a / b); the (float)a construct tells the compiler to switch the type of variable of a to be a float. [b was automatically cast to float to match a). This isn't often useful but can sometimes get you out of trouble. The main use of casting is when you have written a routine which takes a variable of one type and you want to call it with a variable of another type. For example say we have written a power function with a prototype like so: int pow (int n, int m); /*Prototype for square root function */ /*which calculates n^m */

We might well have a float that we want to find an approximate power of. Your compiler should complain bitterly about you writing: float n= 4.0; int squared; squared= pow (n,2); /* The compiler will not like this because it expects n to be of type int not float*/ However, in this case, we want to tell the compiler that we do know what we're doing and have good reason for passing it a float when it expects an int (whatever that reason might be). Again, a cast can rescue us: float n= 4.0; int squared; squared= pow ((int)n,2);

/* We cast the float down to an int*/

IMPORTANT RULE: To move a variable from one type to another then we use a cast which has the form (variable_type)variable_name. CAUTION: It can be a problem when we downcast – that is cast to a type which has less precision than the type we are casting from. For example, if we cast a double to a float we will lose some bits of precision. If we cast an int to a char it is likely to overflow [recall that a char is basically an int which fits into 8 binary bits].

8

Functions An important concept in C is the idea of the function. A function is an effective sub-unit of a program. We have already encountered one function without explicitly calling it that: printf. C allows you to write your own functions (indeed, if you are to write anything but the simplest programs it requires you to do so). main itself is a function. Let's take an example. We might want to have a function which returns the largest of two integers. Here's how you would write it: int maximum (int a, int b) /* Return the largest integer */ { if (a > b) return a; return b; } This function takes two arguments both of which are integers. It returns the value of the largest integer. (If the two values are the same, it will return the value of the second – which doesn't matter because the two values are the same). Note that return is being used here in the same way as return was used from main but returning from main exits the program whereas returning from a function returns control to wherever the function was called from. IMPORTANT_RULE: Function names follow the same rule as variable names. Letters, numbers, underscores and don't start with a number. It's also a wise idea not to call your function the same as a library function – using lower case letters is conventional. We can use this from main or from another function as follows: #include <stdio.h> int maximum (int, int); /* Prototype – see later in lecture */

int main(int argc, char *argv[]) { int i= 4; int j= 5; int k; k= maximum (i,j); /* Call maximum function */ printf ("%d is the largest from %d and %d\n",k,i,j); printf ("%d is the largest from %d and %d\n",maximum(3,5), 3, 5); return 0; } int maximum (int a, int b) /* Return the largest integer */ { if (a > b) return a; return b; } Note that in the printf function, we used the result from a function maximum(3,5) as a function argument. We can use a function inside another function to build up a more complex function as follows:

9

int max_of_three (int i1, int i2, int i3) /* returns the maximum of three integers */ { return (maximum (maximum(i1, i2), i3)); } This example takes a bit more thinking about. We're using our previously defined function maximum in quite a sophisticated way. Perhaps it would help if we think of the example using some actual values. If we called the function max_of_three with the values (4,7,8) using the line max_of_three(4, 7, 8); then we would set the local variables i1, i2 and i3 to the values 4,7 and 8. The complicated looking statement first calls maximum(i1,i2) where i1 is 4 and i2 is 7 which, as we would expect returns 7. The line then calls maximum a second time with 7 and i3 which is 8 which is what the function returns. Therefore it has correctly found the maximum of 4,7 and 8. OK, so that's not exactly rocket science, but it gives you an idea of how functions can build upon each other. We could have made the above bit of code look a bit clearer by writing: int max_of_three (int i1, int i2, int i3) /* returns the maximum of three integers */ { int d; d= maximum (i1, i2); d= maximum (d, i3); return d; } Not only is this second version a bit clearer, it is likely to be just as fast using a modern optimising compiler. (Many people make the mistake of assuming that more semi-colons means a slower program. It isn't necessarily so.) void functions Note that a function doesn't have to take any arguments or return any. To indicate this we declare the function as void. We might for example want to write a function which simply prints hello: void print_hello (void) /* this function prints hello */ { printf ("Hello\n"); } or we might want a function which prints odd or even depending on the argument we send it void odd_or_even (int num) /* this function prints odd or even appropriately */ { if ((num % 2) == 0) { printf ("Even\n"); return; } printf ("Odd\n"); } Returning from functions (the return statement) Note here that we use return to jump back out of the function early without getting to the end. Note that it is an error to "fall off the end" of a function which is supposed to return something.

10

int function_returning_int (void) /* This is an error */ { printf ("I am not going to return an int\n"); } And it is also an error to try and return a value from a function which is supposed to return void. void print_i_value (int i) /* This is also an error*/ { printf ("i is equal to %d\n", i); return i; } IMPORTANT RULE: A function can only return one value. (A void function doesn't return any values). Beginners often see this rule as a fundamental problem with C. "What if I want to return TWO values?" We shall see later that this rule is not as limiting as it seems and when it is a problem, there are ways round it. IMPORTANT RULE: A function does not change the values of its arguments. For example if we define a function to return the square of a number: int squared (int i) /* Squares i */ { i= i*i; return i; } then we can test it with this code: #include <stdio.h> int main() { int i= printf printf return }

2; ("i squared is %d\n",squared(i)); ("i is %d\n",i); 0;

which will print: i squared is 4 i is 2 it will NOT print: i squared is 4 i is 4 The reason for this is that functions only have a local copy of the values sent to them. In C this is known as pass by value. Variables passed by value to a function We shall be returning to the concept of functions a lot since they are one of the most important concepts in the C language. For now, simply remember that, using a function you can create an addition to the C language which takes a number of arguments and may return a single value.

11

Prototypes of functions One important thing about functions is that they should be prototyped. Prototyping involves telling the compiler in advance about the function that you write later. Here are function prototypes from some of the functions above: int maximum (int,int); /* Prototype for maximum function above*/

int max_of_three (int,int,int); /* Return largest of 3 ints */ int function_returning_int (void); /* Returns an int */ void print_i_value (int); /* Prints the value of the integer */ Obviously the comments are optional but function prototypes are an excellent place to add comments to your code. Since the function prototypes are all together in the same place, it is a convenient place for someone browsing your code to look for what the functions actually do. You might like to repeat the comments later when you actually come to write the functions themselves. IMPORTANT RULE: Every function that you write (apart from main) should have a prototype and the prototype should occur before the first time the function is used and before the function is defined. The correct place for a prototype is after any #include statements but before you start any other functions. [Actually, the strictly correct place to put function prototypes is in a header file of their own we will tell you how to do this later – for now put them after the #include statements but before main and the other functions.] Shrewd students might well be asking "Why doesn't printf need a prototype?". It does. The prototype for printf (along with lots of other things) was what you were including in your program when you #include d that mysterious stdio.h file. It doesn't matter which order you actually put the functions in the code. Some people like to have main as the first function in a file – which makes some sense since it is the first one which runs. It doesn't matter to the compiler which order you have them in though. Local variables, Global Variables and Scope In C, variables are local to the function in which they are declared. That is to say if we have a function: float circumference (double r) /* Return the circumference of a circle given its radius*/ { double pi= 3.14; return (2*pi*r); } the value of pi cannot be used in a subsequent function for example: float double(float r) /* Return area of circle given radius */ { double (pi*r*r); /* pi is not defined here although it was defined earlier*/ } would be an error since pi was defined in circumference locally – it is undefined in area. Like many rules of C that seem quirky to the beginner, there is a very good reason for this. Imagine it were not the case. We might, if we were inclined, write a routine to print n asterisks.

12

void print_stars (int n) { int i; for (i= 0; i < n; i++) printf ("*"); printf ("\n"); } If we want to write a routine to print 5 lines of 5 stars we should expect that the following will work: int i; for (i= 0; i < 5; i++) print_stars (5); And, indeed, this does work as we would expect and prints 5 rows of 5 stars. However, if the variable i were NOT local in scope then by the time the first row of stars had been printed, i would be set to 6 and the loop would exit after printing only one row of stars. This would be startling for the programmer and this is the reason why C variables are normally local in scope. If you do want to use global variables (and sometimes you do) then it can be done. Simply put the variable declaration outside function bodies in your program and the variable will be global and will be usable by any function below the point where it is defined. Normally you will want global variables to be visible in the entire program and therefore they are normally positioned after the #includes but before the first function and the main function. IMPORTANT RULE: A variable which is declared outside a function is global and it is usable in any function below the point where you declared it. It is good practice if you are going to use globals to put them at the top of the code (but see the next note). CAUTION: Using global variables is extremely bad style. They have their purpose, of course, but they can be extremely confusing and cause more problems than they are worth. Use EXTREMELY sparingly. Debugging A good technique for "debugging" code is to think of yourself in place of the computer. Go through all the loops in the program and ask "what is in each variable?" Each time you go through a loop ask "is the condition met" and "should I continue" The factorial program shows how to do this. Can you see the bug? CODE #include <stdio.h> int main(int argc, char*argv[]) { int number= 4; int answer; int count; answer= 1; count= number; while (count >= 0) { answer= answer* count; count--; } printf ("%d! = %d\n", number,answer); return 0; } THOUGHTS ABOUT CODE number= 4 answer= 1 count= 4 enter while loop answer= 1*4=4 count=3 enter while loop answer=4*3= 12 count=2 enter while loop answer=12*2= 24 count= 1 enter while loop answer= 24*1= 24 count= 0 enter while loop answer= 24*0= 0 AHA – I see!!!

13

Here are some other debugging techniques: 1. Check missing brackets and commas. 2. Check that you have a semicolon at the end of every line which needs one. 3. Put in some printfs– if you know what your program is DOING you will know what it is DOING WRONG. 4. Try to explain to someone else what the program is meant to do. 5. Take a break, get a cup of coffee and come back to it fresh. (Debugging is FRUSTRATING). Summary of Language elements learned in worksheet and lecture one One of the most confusing things to a new C programmer is the syntax of the language. When do you need to put a semi-colon on the end of the line? When do you need a { or a } to indicate a block of code? When do you use = and when do you use ==? What goes in which order? When do I begin a statement with a # and when don't I? While we will try our very best to answer these questions, there is no substitute for experience. The best way to learn programming is to get out there and practice it. If you can't work out how to "say" something in C then try to find some code that says something similar. [Of course, just because someone out there does something a certain way does not necessarily mean that is the best way to do it.] The complete syntax of C is listed in a mildly confusing way in K&R pages 191-233 and in a really, really confusing way it is summarised on pages 234- 239. No C programmer ever learned how to write C by reading and memorising these rules. Let's instead summarise some of the rules we have learned so far: Comments begin with /* and end with */ and can contain anything (well, anything except for a */). Lines beginning with # are pre-processor directives. #include adds in a header file to your code. Pre-processor directives do not have semi-colons at the end. Every function should have a prototype which comes before the function (preferably at the top of the code just after the #includes – later we'll show you how to use header files which is the proper place for prototypes). Example function prototypes are: int add_three_ints (int, int, int); void print_a_char (char); A function starts with the type of the function followed by its name then the arguments with their types and names – and no semicolon. int add_three_ints (int i1, int i2, int i3) void print_a_char (char c) Every C program has a main function which returns an int. We can declare it like so: int main(int argc, char *argv[]) A function body starts with a { and ends with a }. The first thing in the function should be declarations of variables (which can also be initialised at this point) for example: int milk, brilliant; float pi= 3.14; double big_num= 3e9; char c= 'c'; /* /* /* /* Declares milk and brilliant as integers */ Declares a float pi and sets it to 3.14 */ Big_num = 3,000,000,000*/ Declares a char and sets it to 'c' */

14

int same_as_c= c;

/* Declares an int and sets it to the value of the char c */

Variables declared outside functions are global and are available for use in any function below where they are declared. Global variables are bad style and should be avoided. There are various mathematical assignment operations: +,-,*,/,% and shorthands ++, --, +=, *=, -=, /= while (condition){ commands; } repeats the commands inside the braces until condition is false. (If condition starts false then while never does anything – this can be useful sometimes). If we miss out the braces, then the while loop only repeats the one next command – for example: i= 1; while (i < 6) printf ("%d\n",i++); is the same as: while (i < 6) { printf ("%d\n",i++); } both would print numbers from 1 to 5. [If we'd used ++i it would have printed the numbers from 2 to 6] A for loop has three parts separated by semicolons an initialiser, a condition and an counter increment. (Note that any one of these three parts can be omitted). A for loop sets the initialiser and then repeats the commands within the loop until the condition is false – the count initialiser is performed once at the end of each loop. For example: for (i= 1; (i < 6); i++){ printf ("%d\n",i); } Will also count from 1 to 5. if (condition) { commands; } else { other commands; } will perform commands once if condition is true and other commands once if condition is false. As with for and while, if the braces are omitted then only the next single statement part of the if statement. Conditions for while, for and if are > >= < <= == and !=. != means not equal to - ! means not and can negate any expression for example !(a == 3) is the same as (a != 3) 0 means false and non zero means true. (So if(5) is always true and if(0) is never true). A true expression evaluates to 1 so: a= (3 < 4);

15

will set a to be equal to 1. ?? within a condition means OR, && means AND and ! means NOT. Finally return; returns from a void function and return variable name; returns from a function returning a variable of type variable name. Example code using language elements from worksheet and lecture one /* Program does very simple arithmetic for numbers < 50. Answers are printed as stars. Might be useful for showing children maths */ #include "stdafx.h" #include <stdio.h> void print_number(int); /* Prints int stars in a block */ void print_stars (int); /* Prints a row of stars */ int main(int argc, char* argv[]) { int no1= 48; /* Two input numbers and an answer */ int no2= 7; int answer; int sum_type= 4; /* 1 means add 2 means subtract 3 means multiply and 4 means divide */ char op_sign; /* Sign of this operation */ int remainder; /* Remainder -- if we are doing division */ /* Check our inputs are in range */ if (no1 <= 0 || no1 >= 50) { printf ("The first number is not in the range 1 - 49"); return -1; } if (no2 <= 0 || no2 >= 50) { printf ("The second number is not in the range 1 - 49"); return -1; } /* Check for divide by zero */ if (sum_type == 4 && no2 == 0) { printf ("Cannot divide by zero\n"); return -1; } /* Calculate the answer */ if (sum_type == 1) { answer= no1 + no2; op_sign='+'; } else if (sum_type == 2) { answer= no1 - no2; op_sign='-'; } else if (sum_type == 3) { answer= no1 * no2; op_sign='*'; } else if (sum_type == 4) { answer= no1 / no2; op_sign='/'; Function prototypes go here at the start

16

remainder= no1 % no2; } else { printf ("Unknown sum type %d\n", sum_type); } if (answer <= 0 || answer >= 50) { printf ("The answer is not in the range 1 - 49"); return -1; } print_number(no1); printf (" %c\n", op_sign); print_number(no2); printf (" =\n"); print_number(answer); if (sum_type == 4 && remainder > 0) { printf (" remainder\n"); print_number(remainder); } return 0; } void print_number(int inp_number) /* Print inp_number stars as blocks of ten */ { /* While there are more than ten stars to print then print a full row */ while (inp_number > 10) { print_stars(10); inp_number-= 10; } print_stars(inp_number); Here are the functions themselves } void print_stars(int stars) /* Print 'stars' many stars */ { int i; for (i= 0; i < stars; i++) { printf ("*"); } printf ("\n"); }

17


相关文章:
曲线拟合C语言程序.doc
曲线拟合C语言程序 - 数值分析与算法。最小二细拟合算法程序... { int i,j,k=0,c=1,w=1; float f,a[50][50] , o[50];; do { f=0; for(i=1;...
c语言习题--输入输出函数.doc
c语言习题--输入输出函数 - c 语言习题--输入输出函数 1. 设 i 是
小C、中C、大C.doc
C、中C、大C - C 型臂简介 C 型臂:顾名思义该设备有 C 型的机架,产
C语言复习提纲.doc
C语言复习提纲 - C 语言复习提纲 第一章 C 语言程序设计概述 一、Visu
C语言选择题 (附答案).doc
C语言选择题 (附答案) - 第一单元 C 语言概述 一、选择题 1、C 语言中主函数的个数为( A)1 C)无穷个 A)个。 B)2 D)任意个 D) 。 2、以下关于 ...
c语言填空题题库.doc
c语言填空题题库 - 二,填空题(10 道小题,共 20 分) 1、一个 C
成都理工大学C语言重修上机题库.doc
成都理工大学C语言重修上机题库 - 12468 A 12479 B 12481
HT_C合泰单片机C语言.txt
HT_C合泰单片机C语言 - 第三章 用C语言编程... 39 开始一个C 程序
C语言程序简单例子.doc
C语言程序简单例子 - 实验二参考答案 实验二参考答案 1.输入两个整型数字,输
C型檩条理论重量表.xls
C型檩条理论重量表 - c型钢理论重量表 型号 每米重(kg) C 80 × 50 × 20 × 2.25 3.886 C 80 × 50 × 20 × 2.50 4.318 C...
简单C语言程序的例子.doc
简单C语言程序的例子 - 例子: #include <stdio.h>
SRVCC与ERSVCC.doc
SRVCC与ERSVCC - LTE 的语音解决方案 目前有 CSFB、单卡双待
SCB-C.doc
SCB-C - SCB-C 夹绳器 UCMP 方案 一、MCTC-SCB-C
Turbo_c_使用教程_图文.doc
Turbo_c_使用教程 - 集成开发环境的简介 第一章 1.1 概述 Turbo C 2.0 集成开发环境 Turbo C 集成开发环境是由 Borland 公司开发的一套 C 语言开发...
C语言基本语法.pdf
C语言基本语法 - 第 0 章 C 语言基本语法 一共 80 学时,分两部分:C、数据结构。其中 C 大概 32 学时,数据结构 48 学时。 0.1 C 编译环境 C 语言...
C语言课后作业答案.doc
C语言课后作业答案 - 3-5-1 正确 大写字母转化成小写或者小写变大写 #i
C语言指针习题___附答案.doc
C语言指针习题___附答案 - 一、选择题 1. 变量的指针,其含义是指该变量的___. a)值 c)名 b)地址 d)一个标志 2.若有语句 int *point,a=...
CPU C-states_图文.doc
CPU C-states - 曹操曾经说过:烈士多悲心,小人偷自闲。真的自闲就真
C17032S.doc
C17032S - 单选题 (共 3 题,每题 10 分) 1 . 证券公司开展
C语言作业.doc
C语言作业 - 第一章 使用 printf()函数按下面的形式显示某应用程序的功
更多相关标签: