Loading, please wait...

Programming in C

A simple C Program :

Printing a Line of Text

/* first program in C */
#include <stdio.h>
/* function main begins program execution */
int main( void )
{
printf( “Welcome to tutorial link \n” );
return 0; /* indicate that program ended successfully */
} /* end function main */

Output:

Welcome to tutorial link

 

Even though this program is simple, it illustrates several important features of the C language. Lines 1 begin with /* and end with */ indicating that this is a comment. You insert comments to document programs and improve program readability. Comments do not cause the computer to perform any action when the program is run. Comments are ignored by the C compiler and do not cause any machine-language object code to be generated. Comments also help other people read and understand your program, but too many comments can make a program difficult to read.

Line

#include <stdio.h>

is a directive to the C preprocessor. Lines beginning with # are processed by the preprocessor before the program is compiled. It tells the preprocessor to include the contents of the standard input/output header (<stdio.h>) in the program.

 

This header contains information used by the compiler when compiling calls to standard input/output library functions such as printf.

Line

int main( void )

is a part of every C program. The parentheses after main indicate that main is a program building block called a function. C programs contain one or more functions, one of which must be main. Every program in C begins executing at the function main. Functions can return information.

 

The keyword int to the left of main indicates that main “returns” an integer (whole number) value. Functions also can receive information when they’re called upon to execute. The void in parentheses here means that main does not receive any information.

 

A left brace, {, begins the body of every function. A corresponding right brace ends each function. This pair of braces and the portion of the program between the braces is called a block. The block is an important program unit in C.

Line

printf

instructs the computer to perform an action, namely to print on the screen the string of characters marked by the quotation marks. A string is sometimes called a character string, a message or a literal. The entire line, including printf, its argument within the parentheses and the semicolon (;), is called a statement. Every statement must end with a semicolon (also known as the statement terminator). When the preceding printf statement is executed, it prints the message “Welcome to tutorial link" on the screen. The characters normally print exactly as they appear between the double quotes in the printf statement.

 

Notice that the characters \n were not printed on the screen. The backslash (\) is called an escape character. It indicates that printf is supposed to do something out of the ordinary. When encountering a backslash in a string, the compiler looks ahead at the next character and combines it with the backslash to form an escape sequence. The escape sequence \n means newline. When a newline appears in the string output by a printf, the newline causes the cursor to position to the beginning of the next line on the screen. Some common escape sequences are listed .

 

 

Escape sequence

Description

\n

Newline. Position the cursor at the beginning of the next line.

 

\t

Horizontal tab. Move the cursor to the next tab stop.

 

\a

Alert. Sound the system bell.

 

\\

Backslash. Insert a backslash character in a string.

 

\”

Double quote. Insert a double-quote character in a string.

Line

return 0;

is included at the end of every main function. The keyword return is one of several means we’ll use to exit a function. When the return statement is used at the end of main as shown here, the value 0 indicates that the program has terminated successfully.