BlueSmoke? You just try to get it back in!
   BlueSmoke? You just try to get it back in! Friday - 21st November, 2014 - 11:08:54 

Site Menu  
 
Home
Web News
Reviews
Previews
Guides
Case Gallery
Contact Us
About Us
Sponsors
Links
 
     

  Visitors  
   
     

   Guide : Fundamental C Programming (Rev 1.0) »  
 

 

Fundamental C Programming (Rev 1.0) - Output
   
 Date  : Feb 17th, 2001
 Category  : Programming
 Manufacturer   : N/A
 Author  : Jin-Wei Tioh
You can think of these first few articles as a language class. As with any new language, we must first be aware of the syntax of the language. C uses notations that may appear strange to people who have not programmed before. Let us begin by writing a simple program which simply prints a line of text. For reasons of sentimentality, it will be based on the program written by Kernighan and Ritchie.

Code :

/* My first C program */

#include

main()
{

printf("Hello World!\n");
return 0;

}


Output :

Hey, this is simpler than you thought right? Although it doesn't teach very much programming, it gives you a chance to learn the mechanics of typing and compiling code. The goal of this program is not to learn how to print words to the terminal. It's to learn how to type, save and compile a program. This is often a non-trivial procedure, and there are a lot of things that can go wrong even if your source code is correct. This simple program highlights several important features of the C language. The line :

/* My first C program */

begins with /* and ends with */, indicating that the line is a comment. Comments are remarks in a program's source code intended for human readers, acting as internal documentation. They are ignored by the C compiler and do not cause any additional code to be generated, ie. if you put a whole essay (not a good idea, btw) as a comment, the resulting compiled program would be no different. As programs become more and more complicated, well written comments can greatly ease the maintenance/modification of the code. However, be extremely wary of unterminated comments. It is very easy, even for veteran programmers, to forget to end a comment with a */, or to accidentally type it as * /, or /*. In these cases, the compiler will treat parts of the program as comments until if finds a correct comment terminator. This can cause weird program behavior, or result in weird compilation errors, potentially wasting lots of time and effort. Next, the line :

#include

is a preprocessor directive. Lines beginning with # are processed by the preprocessor before the program is compiled. This specific line in our first program tells the preprocessor to include the contents of the standard input/output header file (stdio.h) in the program. While you can get by without including header files, it is best done. Header file store the details and declarations used when compiling functions, in this case, the standard input/output function printf. This helps the compiler to determine if calls to library functions have been written correctly. For now, don't worry about header files. I'll get back to them later.

Next, the line :

main()

is part of every C program. The parentheses after the word main indicates that main is a function. C programs can either can a single function (like our program above), or multiple functions, one of which must be main. When a C program executes, the function main is executed first. If there are multiple functions, functions besides main are never implicitly/automatically executed unless they are somehow called either directly or indirectly from main.

The body of every function must start with a left brace, {. The end of every function must have a corresponding right brace, }. The portion of the program between the braces is also termed a block, an important unit in the C language. The next line :

printf("Hello World!\n");

basically instructs the computer to perform an action. In our case, it is to print the message Hello World! to the screen. This message is a string, which is simply just a sequence of characters. The entire line is what we will call a statement, which must end with a semicolon ( ; ). This particular statement consists of printf and its arguments within the parentheses.

Alright, you now know that the string to be printed is Hello World!\n. But hey, where is the \n? The backslash ( \ ) is an escape character, and it indicates that printf is supposed to do something unordinary. When printf encounters a backslash, it looks ahead at the next character, combining it with the backslash to form an escape sequence. The \n sequence is the newline, which makes the cursor advance to a fresh line. Here's a listing of the escape sequences in C :

Escape Sequences Details
\n Newline escape sequence. Advances the cursor to the beginning of a new line.
\r Carriage return escape sequence. Positions the cursor at the start of the current line.
\t Tab escape sequence. Advance the cursor to the next tab stop.
\a Alert escape sequence. Makes the PC speaker beep.
\" Double quote escape sequence. Prints a double quote when used in a printf statement.
\\ Backslash escape sequence. Prints a backslash when used in a printf statement.

The line :

return 0;

passes the value 0 (zero) back to the operating system. This indicates that the program has executed successfully and passes control back to the operating system. While it is not absolutely necessary, it is generally good programming practice to write this line in all C programs.

Finally, the right brace, }, indicates the end of the main function.

I'm sure you noticed that the two lines between the { and } were indented. Indenting the entire body of each function by one level emphasizes the structure of the program, while also making it much easier to read. Hence, it is another good programming practice.

There are several different ways that you can use to print the Hello World! message. For example,

Code :

/* My first C program - variation 1 */

#include <stdio.h>

main() {

printf("He");
printf("llo ");
printf("World!\n");
return 0;

}

Output :

Another way would be to spread the message across multiple lines :

Code :

/* My first C program - variation 2 */

#include <stdio.h>

main() {

printf("Hello\nWorld!\n");
return 0;

}

Output :

Feel free to experiment with the printf function whilst familiarizing yourself with the basic syntax of C. Next, the scanf function will be introduced, which will enable you obtain input typed by a user on the keyboard.

 Print this article Input/Output

 E-mail this article

 Discuss this article

 
 
     


Copyright 2000-2005 BlueSmoke. All rights reserved. Terms, Conditions and Privacy Information.
Site Design by Jin-Wei Tioh

Sitemap