Cart(0 items)![]()
![]()
Enter a zip code
(Paperback - REV)
Average Customer Rating:
(1 ratings)
Dan Gookin loves a challenge. He must, otherwise he wouldn't have taken on so daunting a topic as the C programming language. And as you can see from C For Dummies, Volume One, Gookin was definitely up to the task: Right off the bat, this plain-English guide to programming in one of the most widely accepted computer languages can have you coding in C just like a pro. Using your C compiler of choice -- whether it's Borland C++, Turbo C, Microsoft Visual C++, or some other -- you'll get the hang of the basic building blocks of C programs, from printf to scanf. You'll master the C way of doing math and making decisions, as well as declaring and using all types of variables. Best of all, you can count on C For Dummies, Volume One (along with C For Dummies, Volume Two) to provide the best possible foundation as you advance to the latest stage of C evolution, C++.
More Reviews and RecommendationsDan Gookin (Coeur d'Alene, Idaho) wrote the first-ever For Dummies book, DOS For Dummies, as well as the bestselling PCs For Dummies and Word For Dummies. He wrote C For Dummies Volumes One and Two. Dan's books have been translated into 32 languages and have more than 11 million copies in print.
Number of Reviews: 1
Average Rating:
![]()
Write a Review
not for linux
A reviewer, A reviewer, 07/27/2005
It's a good starting out book on C,but the lessons don't compile in linux.
while (dead_horse)
beat ():
If you’re like most people, the above seems like nonsense. Actually, it’s computer sense—C programming. After digesting C For Dummies, 2nd Edition, you’ll understand it. C programs are fast, concise and versatile. They let you boss your computer around for a change. So turn on your computer, get a free compiler and editor (the book tells you where), pull up a chair, and get going. You won’t have to go far (page 13) to find your first program example. You’ll do short, totally manageable, hands-on exercises to help you make sense of:
• All 32 keywords in the C language (that’s right—just 32 words)
• The functions—several dozen of them
• Terms like printf(), scanf(), gets (), and puts ()
• String variables, numeric variables, and constants
• Looping and implementation
• Floating-point values
In case those terms are almost as intimidating as the idea of programming, be reassured that C For Dummies was written by Dan Gookin, bestselling author of DOS For Dummies, the book that started the whole library. So instead of using expletives and getting headaches, you’ll be using newly acquired skills and getting occasional chuckles as you discover how to:
• Design and develop programs
• Add comments (like post-it-notes to yourself) as you go
• Link code to create executable programs
• Debug and deploy your programs
• Use lint, a common tool to examine and optimize your code
A helpful, tear-out cheat sheet is a quick reference for comparison symbols, conversion characters, mathematical doodads, C numeric data types, and more. C For Dummies takes the mystery out of programming and gets you into it quickly and painlessly.
Number of Reviews: 1
Average Rating:
![]()
Write a Review
not for linux
A reviewer, A reviewer, 07/27/2005
It's a good starting out book on C,but the lessons don't compile in linux.
| Introduction | 1 | |
| Pt. I | Introduction to C programming | 7 |
| Ch. 1 | Up from the primordial C | 9 |
| Ch. 2 | C of sorrow, C of woe | 19 |
| Ch. 3 | C straight | 29 |
| Ch. 4 | C what I/O | 39 |
| Ch. 5 | To C or not to C | 55 |
| Ch. 6 | C more I/O with gets() and puts() | 65 |
| Pt. II | Run and scream for variables and math | 73 |
| Ch. 7 | A + B = C | 75 |
| Ch. 8 | Charting unknown Cs with variables | 93 |
| Ch. 9 | How to C numbers | 107 |
| Ch. 10 | Cook that C variable charred, please | 121 |
| Pt. III | Giving your programs the ability to run amok | 131 |
| Ch. 11 | C more math and the sacred order of precedence | 133 |
| Ch. 12 | C the mighty if command | 147 |
| Ch. 13 | What if C = C? | 165 |
| Ch. 14 | Iffy C logic | 175 |
| Ch. 15 | C you again | 185 |
| Ch. 16 | C the loop, C the loop++ | 201 |
| Ch. 17 | C you in a while loop | 215 |
| Ch. 18 | Do C while you sleep | 225 |
| Ch. 19 | Switch case, or, from 'C' to shinning 'c' | 239 |
| Pt. IV | C level | 251 |
| Ch. 20 | Writing that first function | 253 |
| Ch. 21 | Contending with variables in functions | 265 |
| Ch. 22 | Functions that actually funct | 275 |
| Ch. 23 | The stuff that comes first | 293 |
| Ch. 24 | The printf() chapter | 305 |
| Ch. 25 | Math madness! | 313 |
| Ch. 26 | The old random-number function | 325 |
| Pt. V | Part of tens | 337 |
| Ch. 27 | Ten more things you need to know about the C language | 339 |
| Ch. 28 | Ten tips for the budding programmer | 347 |
| Ch. 29 | Ten ways to solve your own programming problems | 353 |
| App. A: The stuff you need to know before you read all the other stuff in this book | 359 | |
| App. B: ASCII table | 371 | |
| Index | 377 |
Array is one of those words that loses its meaning the more you say it. Array. A ray. Air ray. Hairy. Hooray!
An array is nothing more than an organized collection of stuff. Suppose that you're decorating cookies, and, because you're obsessively neat, you have arranged them into two lines: one ready for decorating and another already decorated. Nerdy folk would say that you have two arrays of cookies. C programmers would say that you have an initialized array (decorated cookies) and an uninitialized array (undecorated cookies). Me? I would just eat the cookies and suck the raw frosting from the decorating tube.
What does all this mean? Nothing more than identifying a collection of stuff with the word array.
In the C language, an array is a collection of variables, all of the same type. You can have an array of integers, single-character variables, floats, doubles, and so on.
Alas, you can't have an array of strings -- not yet, anyway. You have enough to do with integer arrays to keep you busy for this lesson.
Suppose that a bunch of football jocks are standing in line at the school café. Since you're standing behind them, they'll probably hoard all the good food before you get there. But that's not the point. What you should notice instead are the numbers printed across the back of their jerseys.
Unbeknownst to the football players, integers are printed on their jerseys. (They know that they have numbers on their jerseys, but not all of them know that the numbers are integers.) Standing there, they make up, in a way, an integer array, which you could write like this:
16, 88, 45, 99, 6, 33
Hike!
You could also make an array from their names -- a string array:
Sherman, Fernan, Hastings, Harrison, Best, Hayden
Then you could make an array from the first letter of their girlfriend's names:
S, E, K, K, K, K
(Kelly is very popular.)
Each of these three examples is an array. The examples are a collection of items all of the same type: integers, strings, and characters.
Now for the nerdy terms:
Element: Each item in an array is called an element. The array can have as many elements as it needs (although in your computer the true size is limited by how much memory your PC has). In the preceding example, each of the arrays contains six elements: six jersey numbers, six names, six girlfriend's initials.
(Yes, even though Kelly appears four times in the last array, the K still counts as four elements. Not all the elements in an array have to be different.)
Here's something weird:
Zero: The first element in an array is numbered zero. Normally, if C were the least bit normal, the first element would be one. After all, all humans begin counting things as "One, two, three. . . ." Because C has to be different, though, the first element is zero.
0x00000). The first argument typed at the DOS prompt is referred to as argv[0], which is also an array, as described in Lesson 6-3 in C For Dummies®, Volume I. Also gander at the PRINTME1.C program in Lesson 8-4. See how the first printer port in your PC (called LPT1) is really zero, or 0x00? LPT2, the second printer port, is value 0x01. See? I'm not making this stuff up either. | The element zero deal (which you don't have to read) This element zero thing can drive you nuts, but don't let it. For example, the first jock in line has 16 on his jersey. That's element zero of the array. Here's how you could type that up in a nerdy C language-like way, assuming that the array is named jock_array[0] = 16; jock_array[1] = 88; jock_array[2] = 45; jock_array[3] = 99; jock_array[4] = 6; jock_array[5] = 33; Although this stuff may look complex, it's just a way to write down how each of the jock's jersey numbers would fit into an array. The zeroth (yes, that's a real word) element is 16; then 88, 45, 99, and 6; finally, the fifth element is 33. Though 33 is the sixth item in the array, in weirdo C it's referenced as item 5. |
An array can hold any series of numbers, such as the numbers in a safe's combination. The following program, SAFE.C, is the same program the manager at my local bank uses to remind herself how to open the Big Safe. (I'm reprinting this program with her permission; as long as you don't know that it's the Bank of America on First Street, she said that it would be okay.)
Carefully type this source code into your editor. The integer array, which is discussed in the following sections, is the only new item in there. For now, just type carefully, and you'll be okay:
Name: SAFE.C
#include <stdio.h>
void main() {
int numbers[] = { 36, 24, 12 }; printf("The combination for the safe: "); printf("1. Turn to %i left. ",numbers[0]); printf("2. Turn to %i right. ",numbers[1]); printf("3. Turn to %i left, open. ",numbers[2]);
} Compile and run. Here's what you should see:
The combination for the safe: 1. Turn to 36 left. 2. Turn to 24 right. 3. Turn to 12 left, open.
The array stored the three numbers, and then various printf statements reached into the array to grab the numbers back out.
int for integer arrays, float for floating-point number arrays, and so on. Like everything else in C, you can't pluck an array from thin air. No, as with other types of variables, you must declare it and type it in a proper and cryptic manner along with any other variables you plan to use in your program.
An array in C can be declared in two ways, depending on whether the array is empty or full.
A full array is one that already has information in it, like the numbers array in the SAFE.C program. It's defined like this:
int jock_array[] = {16, 88, 45, 99, 6, 33};
Table 9-1 tells you how each item fits into the big picture. Basically, there are three things: the type of array, int (integer); the array name, jock_array; and the values assigned to that array. The whole thing ends with a semicolon, just as any variable declaration does.
Cryptogram | What It Does |
| Defines it as an array of integers |
| The name of the array (square brackets are required and should be empty); the compiler figures out the array's size for you |
| Says "Set the contents of the array equal to the following values" |
| Curly brackets contain the array's info |
| Numbers, separated by commas, to put into the array |
| Final curly bracket |
; | Semicolon at the end of the variable declaration |
The other type of array in C is empty, one you create to be filled in later. It's defined this way:
float coworker_IQ[20];
Rather than have numbers or other data assigned to this array, it merely has a size specified; 20 elements, in this example. It means that the coworker_IQ array has room for as many as 20 floating-point numbers.
The following check marks should answer most of the questions buzzing around in your head right now:
jock_array. The second array is a float array named coworker_IQ. coworker_IQ array is set equal to 20; that array can have as many as 20 elements in it. int chairs[12];
you're telling the compiler to set aside space for an array that contains 12 elements of integer number size. It's the same as though you were throwing a dinner party and invited 12 people -- you would need a long table with 12 chairs. (And then pray that no more than 12 people show up and each of them needs only one chair on which to sit.)
char cards[] = {'A', 'J', 'Q', 'K'};
the compiler creates a single-character array named cards that can hold four elements. Then the characters A, J, Q, and K are stuffed into the array.
Each element in an array is its own variable. It has to be. Otherwise, you would never be able to pluck out any individual item in the array, and then, well, what would be the point?
To refer to any item in an array, you have to know the array's name and the element's number. The array's name is like any other variable name in C. The element number, however, is weird because the first element is zero, not one. When you know those two things, you just write down the array name, stick the element number between the square brackets, and -- presto! -- you have a variable just like any other.
In the SAFE.C program, for example, the first item in the array is referred to as numbers[0]. Even though a funky square bracket is in there, the compiler sees the thing as a single integer variable.
orbit[1] = 53.449;
or
election[2000] = 78123;
or
input[7] = getch();
loop[x] = counter();
Here, variable x is used to indicate an element in the loop array, an element that has the value returned by the counter function.
Here's a truly meaningless program for you. It creates an empty (uninitialized) array that can hold 20 elements (numbered 0 through 19, natch). Then the program fills those elements with the numbers 1 through 20, then 20 through 1, and then random numbers.
Carefully type this program into your editor. It uses the random-number-generation routines from the program RANDOM4.C in Lesson 5-7 in Volume I. You can cut and paste that chunk of text (the rnd and seedrnd functions). Although the source code seems rather long, it's really the same for loop repeated three times. Cut and paste!
Name: HURRAY.C
#include <stdio.h> #include <stdlib.h> #include <time.h> //for seeding randomizer
#define ELEMENTS 20
int rnd(int range); void seedrnd(void); void main()
{int blorf[ELEMENTS];int x;
/* First pass, fill array with values 1 through 20 */
printf("Initialize array from 1 to 20: ");for(x=0;x<ELEMENTS;x++)
{blorf[x] = x+1;printf("%i\x20",blorf[x]);}
/* Second pass, fill array with values 20 through 1 */
printf(" And now from 20 to 1: ");for(x=0;x<ELEMENTS;x++)
{blorf[x] = 20-x;printf("%i\x20",blorf[x]);}
/* Final pass, fill array with random values */
printf(" And now random numbers: ");for(x=0;x<ELEMENTS;x++)
{blorf[x] = rnd(20)+1;printf("%i\x20",blorf[x]);}
}
int rnd(int range)
{int r;r=rand()%range; //spit up random numberreturn(r);}
void seedrnd(void)
{srand((unsigned)time(NULL));}
Double-check your work. An error is bound to be in there somewhere -- a missing semicolon or comment character.
Compile and run.
Here's the result:
Initialize array from 1 to 20: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 And now from 20 to 1: 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 And now random numbers: 7 11 3 11 17 18 16 16 9 7 5 19 12 20 13 1 13 2 4 8
| Soothing words for the harried HURRAY.C program Like most long programs, the HURRAY.C program contains a great deal of repetition. The idea is to fill the array and then display the results. Normally, your program might fill an array only once, either by itself or with user input. The array's contents may change, but probably not as shown in HURRAY.C. The The
This shortcut is used throughout the program to represent the array's size. Oh, you could use the value directly, but by using the The following line creates an array of integers named
The
(See? Element zero does come in handy.) Element
If The
The value is displayed followed by the space character, The second In the final
Again, refer to Lesson 5-7 for details about the |
The array named blorf is filled with three different sets of numbers, each of which can be changed like any other variable. See the previous sidebar, "Soothing words for the harried HURRAY.C program," for more detailed information about what's going on.
scanf or gets functions for that; refer to Lesson 3-1 in Volume I for information about reading numerical values from the keyboard. gets to read information from the keyboard. More and more, I'm growing to hate the scanf function. ints for integer arrays, floats for floating-point arrays, and so on. #define directive in this program sets the size of the blorf array to 20 elements. That's done so that the array size can be adjusted easily by editing that one line. Refer to Lesson 3-4 in Volume I for information about how handy the #define directive can be. \x20 escape sequence in the various printf statements inserts a blank (a space) between the array numbers. Refer to Table 2-1 in Volume I for more information about escape sequences. The following program is a lottery-number picker, which is listed at the end of Volume I. Now that you're familiarizing yourself with arrays, the program should make a little more sense to you. It's listed next, minus the extensive comments from Volume I so that you can peruse the array aspects of the program:
Name: LOTTO.C
#include <stdio.h> #include <stdlib.h> #include <time.h> //for the seedrnd() function
#define RANGE 50 //number of numbers #define BALLS 6 //number of balls to draw #define DELAY 1000000 //delay interval between picks
int rnd(int range); void seedrnd(void);
void main()
{int numbers[RANGE]; //array that holds the ballsint i,b;unsigned long d; //delay variableprintf("L O T T O P I C K E R ");seedrnd(); //seed the randomizer
/* initialize the array */
for(i=0;i<RANGE;i++) //initialize the arraynumbers[i]=0;printf("Press Enter to pick this week's numbers:");getchar();
/* draw the numbers */
printf(" Here they come: ");for(i=0;i<BALLS;i++){for(d=0;d<=DELAY;d++); //pause here
/* picks a random number and check to see whether it's already been picked */ do {b=rnd(RANGE); //draw number}
while(numbers[b]); //already drawn? numbers[b]=1; //mark it as drawn printf("%i ",b+1); //add one for zero } printf(" Good luck in the drawing! "); } /* Generate a random value */
int rnd(int range)
{int r;r=rand()%range; //spit up random numberreturn(r);}
/* Seed the randomizer */
void seedrnd(void)
{srand((unsigned)time(NULL));}
Enter this source code into your editor (if you haven't already done so from Volume I). Compile and run.
L O T T O P I C K E R
Press Enter to pick this week's numbers:
Press the Enter key.
Here they come: 35 11 49 34 26 37
Good luck in the drawing!
The program picks six numbers from 1 to 50 as your lucky lotto numbers this week. (You can change the range of numbers by entering a new value for the RANGE define, and you can choose more or fewer numbers by changing the BALLS define, both at the beginning of the source code.)
This lotto program works the same way any computer card game works. Like cards in a deck, balls in a lottery machine can be drawn only once. The object is not to generate the same random number twice. That is, you can't draw the same lotto ball, bingo ball, or card from a deck in the real world, so you shouldn't be able to do that in a computer program either.
The solution is to keep track of the numbers (cards or whatever) drawn by using an array. The array is initialized with all zeros to start, which is done in Lines 23 and 24 in LOTTO.C:
for(i=0;i<RANGE;i++) //initialize the array
numbers[i]=0;
Then, as the random numbers are drawn, a one is inserted into the array to indicate which numbers (cards, whatever) have already been used. The following snippet makes that happen:
do { b=rnd(RANGE); //draw number } while(numbers[b]); //already drawn?
numbers[b]=1; //mark it as drawn printf("%i ",b+1); //add one for zero
The variable b holds the number drawn, using the rnd() function introduced in Lesson 5-7 in Volume I. The rnd() function returns a value representing an element in the array. The do-while loop checks to see whether that value is equal to one -- which is TRUE -- if so, it keeps looping, generating new random numbers and checking the array for a zero or a one.
If the random number returned hasn't yet been drawn, the program continues; first, the value one (TRUE) is placed into the array with the numbers[b]=1; statement. That marks that number as "used." Then the number drawn is displayed on-screen. Note that the number displayed is b+1. That's because the rnd() function returns a value from zero through "RANGE." That's okay for filling the array, which also begins at element zero, but normally there's no zero ball in a lottery game.
The loop continues until the number of balls (represented by the shortcut word BALLS) has been drawn.
1 into the array, you ensure that that number isn't drawn again in your program. BALLS to 50 in the define statement. Notice that the program takes longer and longer to display the last few numbers as it waits for the random-number generator to produce a number that hasn't yet been drawn. do-while loop similar to the one in LOTTO.C to determine which cards have already been drawn.
int high_scores[] = { 99, 98, 99, 97}; high_scores.
float ice_cream[3]; float buoy[] = {1.0794, 33.0, 1678.5}; float point[3] = { 1, 2, 3}; float root_beer[2];
loading...
Terms of Use, Copyright, and Privacy Policy
© 1997-2008 Barnesandnoble.com llc