Saturday, January 29, 2011

The big picture of a c program

Each program must have a starting point. When you run a program, the operat­ing system (OS) sends it off on its way — like launching a ship. As its last dockmaster duty, the OS hurls the microprocessor headlong into the program. The microprocessor then takes the program’s helm at a specific starting point.

In all C programs, the starting point is the main() function. Every C program has one; GOODBYE.C, ERROR.C, and all the other C programs you ever create. The main() function is the engine that makes the program work. The main() function is also the skeleton upon which the rest of the program is built.


main() is the name given to the first (or primary) function in every C pro­gram. C programs can have other functions, but main() is the first one.


In C, functions are followed by parentheses. The parentheses can be empty, or they can contain information — it all depends on the individ­
ual function.


When I write about C language functions in this book, I include the paren­theses, as in main().


A function is a machine — it’s a set of instructions that does something. C programs can have many functions in them, though the main() func­tion is the first function in a C program. It’s required.

 

See also:

Extra help in typing the GOODBYE.C source code

The compiler and the linker

How to compile goodbye.c?

Try the error!

Don’t dispense with the ERROR.C file just yet. Don’t close the window, and don’t zap the project. (If you did, use your editor to load the ERROR.C file and prepare to reedit.)


Change Line 6 in the ERROR.C source code file to read this way:
retrun(0);


In case you don’t see it, the word return has been mangled to read retrun; the second r and the u are transposed. Otherwise, the zero in the parentheses and the semicolon are unchanged.


The way C works is that it just assumes that retrun is something you’re seri­ous about and not a typo. The compiler couldn’t care less. But the linker goes nuts over it. That’s because it’s the linker that glues program files together. It catches the error when it doesn’t find the word retrun in any of its libraries.


And, like any frazzled librarian, the linker spews forth an error message. Save the modified ERROR.C file to disk. Then recompile. Brace yourself for an error message along the lines of
temporary_filename.o: In function ‘main’:
temporary_filename.o: undefined reference to ‘retrun’


Or, the message may look like this:
temporary_filename.o(blah-blah):error.c: undefined reference to ‘retrun’


It’s harder to tell where the error took place here; unlike compiler errors, linker errors tend to be vague. In this case, the linker is explaining that the error is in reference to the word retrun. So, rather than use a line-number reference, you can always just search for the bogus text.


To fix the error, reedit the source code and change retrun back to return. Save. Recompile. The linker should be pleased.


As I mention elsewhere in this book, the GCC compiler both compiles and links.


If the linker is run as a separate program, it obviously produces its own error messages.


A temporary file is created by the compiler, an object code file that ends in .O — which you can see in the error message output. This object code file is deleted by GCC.

The linker’s job is to pull together different pieces of a program. If it spots something it doesn’t recognize, such as  , it assumes, “Hey,
maybe it’s something from another part of the program.” So the error slides by. But, when the linker tries to look for the unrecognized word, it hoists its error flags high in the full breeze.

 

See also:

The Four Steps to build a C program

5 steps to reedite your C source code file

What's the meaning of recompiling the C program?

Repairing the malodorous program

To make the world right again, you have to fix the program. This process requires editing the source code file, making the needed correction, saving the source code file back to disk, and then recompiling.

You can fix the ERROR.C program by adding a semicolon. Edit Line 5 and add a semicolon to the end of the line. Also correct the sentence displayed on the screen so that it reads as follows:
printf(“This program will no longer err.\n”);


Other than changing Line 5, everything else in the program remains untouched.
Save ERROR.C back to disk. Recompile the program and then run it:
This program will no longer err.


Indeed, it does not!


I can’t always tell you where to fix your programs. ERROR.C is the only program listed in this book that contains an on-purpose error. When you get an error message, you should check it to see where the error is in your source code. Then cross-check your source code with what’s listed in this book. That way, you find what’s wrong. But when you venture out on your own and experiment, you have only the error message to go by when you’re hunting down your own errors.


Pull two Rs out of ERRORS and you have Eros, the Greek god of love. The Roman god of love was Cupid. Replace the C in Cupid with St and you have Stupid. Stupid errors — how lovely!

 

 

See also:

What to do when you come across an error?

The autopsy Part1

The autopsy Part2

Tuesday, January 25, 2011

The autopsy Part2

The solution? You have to reedit the source code file and fix what’s wrong. In this case, you would reedit ERROR.C and add the semicolon to the end of Line 5. Even if you looked at Line 6 (per the output’s insistence), you would see nothing wrong there. If so, your eyes would wander back and — because you’re aware of the Missing Semicolon Syndrome — you would see the prob­lem and mend it.


Errors are not the end of the world! Everyone gets them.
Syntax refers to the way a language is put together. Some compilers use that term rather than parse, which means “the order in which things are put together.” Eh.
Some versions of GCC put double quotes around “return” rather than the tick marks shown in the preceding example. Beyond that, GCC is
remarkably consistent with its error messages.
Missing semicolons are one of the most popular types of errors in the C language. You find out in the next chapter more about semicolons and the role they play.
The error message’s line number refers to a line in the source-code text file. That’s why nearly all text editors use line numbers, which you can see at the top or bottom of the screen or editing window.
The line number may or may not be accurate. In the case of a missing semicolon, the next line may be the “error line.” This statement holds
true with other types of errors in C. Oh, well — at least it’s close and not a generic honk of the speaker and “ERRORS GALORE, YOU FOOL” plas­tered onscreen.
A good habit is to fix the first error listed and then recompile. Often, the first error is the only real one, but the compiler lists others that follow
because it becomes confused.
Of course, you may be thinking “Okay, smarty-pants computer, you know what’s wrong — fix it!”

But computers don’t just jump to conclusions like that. That is the evil of the statement “Do what I mean”: Computers can’t read minds, so you must be precise. They are champs, however, at point­ing out what’s wrong (and almost snobbishly so).

Welcome to reproduce this passage. Please indicate the source when you reproduce this passage.

The autopsy Part1

The ERROR.C program erred! What a shock.
Don’t be alarmed — it was expected. (In fact, you may have seen this type of error before.) Here is a sample of what the cruel message may look like:
error.c: In function `main’:
error.c:6: parse error before “return”

How rude! It’s not that reassuring hand on your shoulder and the kind, avun­cular voice explaining that you boo-booed. Still, what the error message lacks in personality, it makes up for in information.


On the up side, though the error message is cryptic, it’s informative. What­ever your compiler, you should be able to single out the following bits of information:


The source code file that contained the error, error.c
The line that contains the error, Line 6 (though it may not be — youcan’t really trust computers too much)

The type of error, a parse error or syntax error or something similar
The location of the error (before the word return)

It still may not be clear exactly what’s wrong, but you’re given many clues. Most important, you’re given a line number: The error is in Line 6.
Okay, it’s really in Line 5, but the C programming language is flexible, and the compiler doesn’t discover that “something’s missing” until Line 6. (You can cut the compiler some slack here.)

The error type is also important. A parse, or syntax, error means that an item of C language punctuation is missing, and, therefore, two things that aren’t supposed to run together have run together. In this case, a missing semicolon character at the end of Line 5 is to blame.

Welcome to reproduce this passage. Please indicate the source when you reproduce this passage.

What to do when you come across an error?

Here is a new program, ERROR.C. Note the optimism in its name. It’s a flawed C program, one that contains an error (albeit an on-purpose error):
#include <stdio.h>
int main()
{
printf(“This program will err.\n”)
return(0);
}

Type the source code exactly as it appears here. Do not use the GOODBYE.C source code as a base; start over here with a clean editor window.

When you’re done entering the source code, save it to disk as ERROR.C. Com­pile it, and then. . . .

Unfortunately, when you compile this program, it produces an error. The next section provides the autopsy.


Pay careful attention as you type! Each little oddball character and nutty parenthesis is important to the C language!
Here’s a hint on the common GCC command to compile this source code: gcc error.c -o error

That’s the last compiling hint you get in this book!

Welcome to reproduce this passage. Please indicate the source when you reproduce this passage.
Source: C Language 
Partner:Best DVD Ripper to convert DVD to mp3

Thursday, January 20, 2011

What's the meaning of recompiling the C program?

Recompiling means to make the program one more time — to rework the steps you went through to create the program originally. This process usually hap­pens after you modify or change the source code, such as you do in the pre­ceding section. Because the source code is different, you have to feed it to the compiler again to generate the new, better (and, hopefully, bug-free) program.

To recompile the new GOODBYE.C source code, use your compiler as outlined in Appendix A. For most everyone, that’s
gcc goodbye.c -o goodbye

Rip DVD to avi, watch it on computer and store it on your phone, share it to your friend.   dvd-to-avi-convertor.com
Convert dvd to mp3. Watch your movie by your hand. Enjoy it everywhere.  convertDVDtoMp3.com
Convert dvd to mp4. Show it to everybody. Attract eyeballs.  dvds-to-mp4.com
                                                                                                                            PaidMini

Press the Enter key and pray that no error messages appear, and then you’re done. The new program has been created.

Run the program! Type the proper command — either goodbye or ./goodbye — at the prompt to see the new, stunning output. Who knew that it would be so darn easy to display such crap on the computer’s screen?

After you reedit your source code file, you have to recompile to re-create the program file. That is how you fix an error or modify the program.

If you’re programming in an IDE (Integrated Development Environment) such as Dev-C++ or Microsoft Visual C++, you may need to use a Rebuild or Rebuild All command to create a new program after modifying your source code.

If you see any errors after recompiling, you must re-reedit your source code and then re-recompile again. (You only “reedit” and recompile”; no sense in getting re-happy.)

Welcome to reproduce this passage. Please indicate the source when you reproduce this passage.
Source: C Language 

5 steps to reedite your C source code file

Source code is not carved in stone — or silicon, for that matter. It can be changed. Sometimes, the changes are necessary, in the case of errors and boo-boos. At other times, you may just want to modify your program, adding a feature or changing a message or prompt — what the hard-core C geeks call tweaking or twiddling. To do that, you have to reedit your source code file.

For example, the GOODBYE program from Chapter 1 displays a message on the screen:
Goodbye, cruel world!

This program can easily be modified to show any message you like. To do so, use your editor and change the source code file, replacing the original mes­sage with your newer, pithier message. Follow these steps:

1.  Use your text editor to reedit the GOODBYE.C source code.

2.  Edit Line 5, which looks like this:
printf(“Goodbye, cruel world!\n”);

3.  Replace the text Goodbye, cruel world! with Farewell, you ugly toad!
printf(“Farewell, you ugly toad!\n”);

Change only the text between the double quotes. That’s the information that is displayed on the screen. Everything else — don’t touch!

4.  Double-check your work.

5.  Save the file to disk.
It’s okay to overwrite the original; your modified file becomes the new GOODBYE.C source code file.
Now you’re ready to recompile your source code, as covered in the next section.


“Reedit your source code file” means to use your text editor to modify the source code, the text file that contains the C language instructions.

You reedit the source code file to repair an error caught by the compiler or linker or to modify the program. This situation happens a lot.

If you’re using the command prompt to run your editor, don’t forget that you can use the up-arrow key to recall previous commands (in certain command-prompt environments). In this case, press the up-arrow key a few times until the original command to edit the GOODBYE.C source code file reappears at the prompt.

Welcome to reproduce this passage. Please indicate the source when you reproduce this passage.
Source: C Language 
Paid Recommendations: Convert DVD To AVI . Rip the DVD movie to your computer, protect your DVD. Convert DVD to mp3. Convert DVD to mp4
Find out more at 
http://www.daniusoft.com/dvd-ripper.html

The Four Steps to build a C program

Four steps are required in order to build any program in C. They are save, com­pile, link, and run. Most C programming language packages automatically per­form the linking step, though whether or not it’s done manually, it’s still in there.

Save! Saving means to save your source code. You create that source code in a text editor and save it as a text file with the C (single letter C) extension.

Compile and link! Compiling is the process of transforming the instructions in the text file into instructions the computer’s microprocessor can under­stand. The linking step is where the instructions are finally transformed into a program file. (Again, your compiler may do this step automatically.)

Run! Finally, you run the program you have created. Yes, it’s a legitimate pro­gram, like any other on your hard drive. You have completed all these steps in this chapter, culminating in the cre­ation of the GOODBYE program. That’s how C programs are built. At this stage, the hardest part is knowing what to put in the source file, which gets easier as you progress through this book. (But by then, getting your program
to run correctly and without errors is the hardest part!)

You find the instructions to save, compile, and run often in this book. That’s because these steps are more or less mechanical. What’s more important is understanding how the language works. That’s what you start to find out about in the next chapter.

Welcome to reproduce this passage. Please indicate the source when you reproduce this passage.

Sunday, January 16, 2011

Extra help in typing the GOODBYE.C source code

The first line looks like this:
#include <stdio.h>

Type  a  pound  sign  (press  Shift+#)  and  then include and a space. Type a left angle bracket (it’s  above  the  comma  key)  and  then stdio, a
period, h, and a right angle bracket. Everything must  be  in  lowercase  —  no  capitals!  Press Enter to end this line and start the second line.

Press the Enter key alone on the second line to make it blank. Blank lines are common in pro­gramming code; they add space that separates
pieces of the code and makes it more readable. And, trust me, anything that makes program­ming code more readable is okay by me!

Rip DVD to avi, watch it on computer and store it on your phone, share it to your friend.   dvd-to-avi-convertor.com
Convert dvd to mp3. Watch your movie by your hand. Enjoy it everywhere.  convertDVDtoMp3.com
Convert dvd to mp4. Show it to everybody. Attract eyeballs.  dvds-to-mp4.com
                                                                                                                            PaidMini

Type the word int, a space, a main, and then two parentheses hugging nothing:

int main()

There  is  no  space  between  main and  the parentheses and no space inside the parenthe­ses. Press Enter to start the fourth line.

Type a left curly brace:

{

This character is on a line by itself, right at the start of the line. Press Enter to start the fifth line.

printf(“Goodbye, cruel
world!\n”);

If your editor was smart enough to automati­cally indent this line, great. If not, press the Tab key to indent. Then type printf, the word print with a little fat then end, (It’s pronounced “print­eff.”)  Type  a  left  parenthesis.  Type  a  double quote. Type Goodbye, cruel world, followed by
an exclamation point. Then type a backslash, a little n, double quotes, a right parenthesis, and, finally, a semicolon. Press Enter to start the sixth ine.
return(0);

If  the  editor  doesn’t  automatically  indent  the sixth line, press the Tab key to start the line with an indent. Then type return, a paren, 0 (zero), a paren, and a semicolon. Press Enter.

On the seventh line, type the right curly brace:
}
Some editors automatically unindent this brace for you. If not, use your editor to back up the brace so that it’s in the first column. Press the Enter key to end this line.

Leave the eighth line blank.

Welcome to reproduce this passage. Please indicate the source when you reproduce this passage.
Source: C Language

The compiler and the linker

Paid Recommendations: Convert DVD To AVI . Rip the DVD movie to your computer, protect your DVD. Convert DVD to mp3. Convert DVD to mp4
Find out more at 
http://www.daniusoft.com/dvd-ripper.html

After the source code is created and saved to disk, it must be translated into a language the computer can understand. This job is tackled by the compiler.

The compiler is a special program that reads the instructions stored in the source code file, examines each instruction, and then translates the information into the machine code understood only by the computer’s microprocessor. If all goes well and the compiler is duly pleased with your source code, the compiler creates an object code file. It’s a middle step, one that isn’t necessary for smaller programs but that becomes vital for larger programs.


Finally, the compiler links the object code file, which creates a real, live com­puter program.


If either the compiler or the linker doesn’t understand something, an error message is displayed. At that point, you can gnash your teeth and sit and stew.

Then go back and edit the source code file again, fixing whatever error the compiler found. (It isn’t as tough as it sounds.) Then you attempt to compile the program again — you recompile and relink.

1 The compiler translates the information in the source code file into instruc­tions the computer can understand. The linker then converts that infor­mation into a runnable program. 
2 The GCC compiler recommended and used in this book combines the compiling and linking steps. An object file is created by GCC, but it is
automatically deleted when the final program file is created.
3 Object code files end in OBJ or sometimes just O. The first part of the object file name is the same as the source code filename.
4 Feel free to cheerfully forget all this object code nonsense for now. 
5 Text editor➪Compiler.
6 Source code➪Program.

Welcome to reproduce this passage. Please indicate the source when you reproduce this passage.
Source: C Language 

How to compile goodbye.c?

The gritty details for compiling a program are in Appendix A. Assuming that you have thumbed through it already, use your powerful human memory to recall the proper command to compile and link the GOODBYE.C source code. Here’s a hint:
gcc goodbye.c -o goodbye

Type that command at your command prompt and see what happens. Well?

Nothing happens! If you have done everything properly, the GCC compiler merely creates the final program file for you. The only time you see a mes­sage is if you goof up and an error occurs in the program.

If you do get an error, you most likely either made a typo or forgot some tiny tidbit of a character: a missing “ or ; or \ or ) or ( or — you get the idea. Very carefully review the source code earlier in this chapter and compare it with what you have written. Use the editor to fix your mistake, save the code to disk, and then try again.


Note that GCC reports errors by line number, or it may even specifically list the foul word it found. In any event, note that Chapter 2 covers error-hunting in your C programs.

Welcome to reproduce this passage. Please indicate the source when you reproduce this passage.
Source: C Language 

Sunday, January 9, 2011

Stuff you don’t need to know about language levels

Programming languages have different levels, depending on how much they resemble human languages. Programming languages that use common  words  and  are  relatively  easy  for most folks to read and study are called highlevel  languages.  The  opposite  of  those  are low-level  languages,  which  are  not  easy  to read or study.

High-level languages include the popular BASIC programming  language  as  well  as  other  lan­guages that just aren’t that popular any more.BASIC reads almost like English, and all its com­mands and instructions are English words — or at least English words missing a few vowels or severely disobeying the laws of spelling.

The lowest of the low-level programming lan­guages is machine language. That language is the  actual  primitive  grunts  and  groans  of  the
microprocessor itself. Machine language con­sists  of  numbers  and  codes  that  the  micro­processor understands and executes. Therefore,
no one really writes programs in machine lan­guage;  rather,  they  use  assembly  language, which is one step above the low-level machine language  because  the  grunts  and  groans  are spelled out rather than entered as raw numbers.

Paid Recommendations: Daniusoft DVD Ripper. Rip the DVD movie to your computer, protect your DVD. Convert DVD to iPad. Convert DVD to iPhone
Find out more at 
http://www.daniusoft.com/dvd-ripper.html

Why would anyone use a low-level language when high-level languages exist? Speed! Pro­grams written in low-level languages run as fast
as the computer can run them, often many times faster than their high-level counterparts. Plus, the size of the program is smaller. A program
written in Visual Basic may be 34K in size, but the same program written in assembly language may be 896 bytes long. On the other hand, the
time it takes to develop an assembly language program is much longer than it would take to write the same program in a higher-level lan­guage. It’s a trade-off.

The C programming language is considered a mid-level language. It has parts that are lowlevel grunting and squawking, and also many high-level parts that read like any sentence in a Michael Crichton novel, but with more charac­ter development. In C, you get the best of the high-level  programming  languages  and  the speed of development they offer, and you also get the compact program size and speed of a low-level language. That’s why C is so bitchen.

Welcome to reproduce this passage. Please indicate the source when you reproduce this passage.

The C Development Cycle and the C Programmer

Use DVD Ripper To Rip Your DVD. Transfer your DVD to your computer. Keep it save.
Convert dvd to ipad. Watch your movie by your hand. Enjoy it everywhere.
Convert dvd to iphone. Show it to everybody. Attract the eyeballs.
                                                                                                                            Sponsor

Here is how you create a C program in seven steps — in what’s known as the development cycle:
1.  Come up with an idea for a program.
2.  Use an editor to write the source code.
3.  Compile the source code and link the program by using the C compiler.
4.  Weep bitterly over errors (optional).
5.  Run the program and test it.
6.  Pull out hair over bugs (optional).
7.  Start over (required).
No need to memorize this list. It’s like the instructions on a shampoo bottle, though you don’t have to be naked and wet to program a computer. Eventually, just like shampooing, you start following these steps without thinking about it. No need to memorize anything.
The C development cycle is not an exercise device. In fact, program­ming does more to make your butt fit more snugly into your chair than
anything.
Step 1 is the hardest. The rest fall naturally into place.
Step 3 consists of two steps: compiling and linking.

When you create a program, you become a programmer. Your friends or rela­tives may refer to you as a “computer wizard” or “guru,” but trust me when I say that programmer is a far better title.

As a programmer, you job is not “programming.” No, the act of writing a pro­gram is coding. So what you do when you sit down to write that program is code the program. Get used to that term! It’s very trendy.


The job of the programmer is to write some code! Code to do what? And what type of code do you use? Secret code? Morse Code? Zip code?
The purpose of a computer program is to make the computer do something.
The object of programming is to “make it happen.” The C language is only a tool for communicating with the PC. As the programmer, it’s your job to trans­late the intentions of the computer user into something the computer under­stands and then give users what they want. And if you can’t give them what they want, at least make it close enough so that they don’t constantly com­plain or — worse — want their money back.


The tool you have chosen to make it happen is the C programming language. That’s the code you use to communicate with the PC. The following sections describe how the process works. After all, you can just pick up the mouse and say “Hello, computer!”
Programming is what TV network executives do. Computer programmers code.
You use a programming language to communicate with the computer, telling it exactly what to do.

Welcome to reproduce this passage. Please indicate the source when you reproduce this passage.

The Source file and the GOODBYE.C practice

Because the computer can’t understand speech and, well, whacking the computer — no matter how emotionally validating that is for you — does little to the PC, your best line of communications is to write the computer a note — a file on disk.


To create a PC epistle, you use a program called a text editor. This program is a primitive version of a word processor minus all the fancy formatting and print­ing controls. The text editor lets you type text — that’s about all.


Using your text editor, you create what’s called a source code file. The only spe­cial thing about this file is that it contains instructions that tell the computer what to do. And although it would be nice to write instructions like “Make a funny noise,” the truth is that you must write instructions in a tongue the com­puter understands. In this case, the instructions are written in the C language.

The source code file is a text file on disk. The file contains instructions for the computer that are written in the C programming language.
You use a text editor to create the source code file. See Appendix A for more information on text editors.

Use DVD Ripper To Rip Your DVD. Transfer your DVD to your computer. Keep it save.
Convert dvd to ipad. Watch your movie by your hand. Enjoy it everywhere.
Convert dvd to iphone. Show it to everybody. Attract the eyeballs.
                                                                                                                            Sponsor

Use your text editor to create the following source code. Carefully type each line exactly as written; everything you see below is important and necessary. Don’t leave anything out:

#include <stdio.h>
int main()
{
printf(“Goodbye, cruel world!\n”);
return(0);
}


As you review what you have typed, note how much of it is familiar to you. You recognize some words (include, main, “Goodbye, cruel world!”,
and return), and some words look strange to you (stdio.h, printf, and that \n thing).


When you have finished writing the instructions, save them in a file on disk. Name the file GOODBYE.C. Use the commands in your text editor to save this file, and then return to the command prompt to compile your instructions into a program.

 You can GOOGLE for more information on using a text editor to write C language programs as well as for instructions on where you should save the source code file on disk.
In Windows Notepad, you must ensure that the file ends in .C and not in .TXT. Find a book about Windows for instructions on showing the file­
name extensions, which makes saving a text file to disk with a .C exten­sion easier.
Note that the text is mostly in lowercase. It must be; programming lan­guages are more than case sensitive — they’re case-fussy. Don’t worry when English grammar or punctuation rules go wacky; C is a computer language, not English.
Also note how the program makes use of various parentheses: the angle brackets, < and >; the curly braces, { and }; and the regular parentheses, ( and ).

Welcome to reproduce this passage. Please indicate the source when you reproduce this passage.
Sponsor:DVD Rippers