Chapter 1.2 Exemplify the elementary C concepts through sample programs | Part - 1


SAMPLE PROGRAM 1: PRINTING A MESSAGE




C language tutorials for beginners

The above example is the Syntax of C Programming. The highlights the general syntax/format of such simple programs. All C programs need a least one function they are the main function.
Sample Program Of Printing A Message
Fig - 2 Syntax Of C Programming

The main Function 

The main is a part of every C program. C permits different forms of the main statement. Following forms are allowed. 
  • main()
  • int main()
  • void main()
  • main(void)
  • void main(void)
  • int main(void)


The empty pair of parentheses indicates that the function has no arguments. This may be explicitly indicated by using the keyword void insides the parentheses. We may also specify the keyword int or void before the word main. The keyword void means that the function does not return any information to the operating systems and int means that the function returns an "integer value" to the operating system. When an int is specified, the last statement in the program must be "return 0". For the sake of simplicity, we use the first form in our programs.  



Consider a very simple program given in above fig

print line using C
Fig - 3 A Program to print one line of text

This program when executed will produce the following output.
I want, I need
Let us have a close look at the program. The first line informs the system that the name of the program is main and the execution begins at this line. The main() is a special function used by the C system to tell the computer where the program starts. Every program must have exactly one main function. If we use more than one main function, the compiler cannot understand which one marks the beginning of the program. 


The empty pair of parentheses immediately following main indicates that the function main has no arguments (or parameters). The concept of arguments will be discussed in detail later when we discuss function (in a chapter of Function).
The opening brace "{" in the second line marks the begging of the function main and the closing brace "}" in the last line indicates the end of the function. In this case, the closing brace also marks the end of the program. All the statements between these two braces from the function body. The function body contains a set of instructions to perform the given task.



In this case, the function body contains three statements out of which only the printf line is an executable statement. The lines beginning with /* are known as comment lines. These are used in a program to enhance its readability and understanding. Comments lines are not executable statements and therefore anything between /* and */  is ignored by the compiler. In general, a comment can be inserted wherever blank spaces can occur at the beginning, middle or end of a line-"but never in the middle of the word".
Although comments can appear anywhere, they cannot be nested in C. That means, we cannot have comments inside comments. Once the compiler finds an opening token, it ignores everything until it finds a closing token. The comment line.
/*======/*=====*/=====*/
is not valid and therefore result in an error. 
Since comments do not affect the execution speed and the size of the compiled program, we should use them liberally in our programs. They help the programmers and others users in understanding the various functions and operations of a program and serve as an aid to debugging and testing. We shall see the use of comment lines more in the examples that follow.
Let us now look at the printf() function, the only executable statement of the program.
 printf("I want, I need");
printf is a predefined standard C function for priting output. Predefined means that it is a function that has already been written and compiled, and linked together with our program at the time of linking. The concept of compilation and linking are explained later in this post. The printf function causes everything between the starting and ending quotation marks to be printed out. In this case, the output will be: 
I want, I need
Note that the print line ends with a semicolon. Every statement in C should end with a semicolon (;) mark.
Suppose we want to print the above quotation in two lines as.
I want,I need!
This can be achieved by adding another printf function as shown below: 
printf("I want, \n");
   printf("I need!");  
  
The information contained between the parentheses is called the argument the function. This argument of the first printf function is "I want, \n" and the second is "I need!". These arguments are simply strings of characters to be printed out.

It is also possible to produce two or more lines of output by one printf statement with the use of newline character at appropriate places. For example the statement.

printf("I want, \n I need!");

Will Output -: I want,
                                I need!

While the statement -:  printf("I.. want,\n ... ... ... I\n... ... ... need!");

Will print out     I               
                        .. want
                           ... ... ... I 
                                  ... ... ... need!

Note -: Some Authors recommend the inclusion of the statement.
#include<stdio.h> 

At the beginning of all programs that use any input/output library functions. However, this is not necessary for the functions printf and scanf  which have been defined as a part of the C language.



Before we proceed to discuss further examples, we must note one important point. C does make a distinction between uppercase & lowercase. For example, scanf and SCANF are not the same In C, everything is written in lowercase letters.  

Post a Comment

0 Comments