How to Write First Program in C Language ?understanding syntax & structure of C and writing method , Rules ?

The Write First Program  in C Language ?

Armed with the knowledge about the types of variables, constants & keywords the next logical step is to combine them to form instructions. However, instead of this, we would write our first C program now. Once we have done that we would see in detail the instructions that it made use of. 

what are the Common Rules that are applicable to most of the programs languages ?


C program rules that are applicable to most of the programs language : 

Before we begin with our first C program do remember the following rules that are applicable to all C programs: 

(a) Each instruction in a C program is written as a separate statement. Therefore, a complete  C program would comprise a series of statements. 

(b) The statements m a program must appear m the same order in which we wish them to be executed, unless of course the logic of the problem demands a deliberate ‘jump" or transfer of control to & statement, which is out of sequence, 

(c) Blank spaces may be inserted between two words to improve the readability of the statement However, no blank spaces are allowed within a variable, constant or keyword 

(d) All statements are entered in small case letters. 

(e) C has no specific rules far the position at which a statement ts to be written That's why it is often called a free-form language. 

(f) Every C statement must end with a; Thus ¢ acts as a Statement terminator. 

Let us now write down out first C program ft would simply calculate simple interest for a set of values  representing principle, number of years and rate of interest 


/* Calculation of simple interest */

/*Author gakay Date 25/05/2006 */ 

# include <stdio.h> 

void main( } 

{

int p, n ;

float r , si ; 

p= 1000; 

n=3; 

r=85, 

/*formula for smote interest */

si=p*n*r*/100 ;

printf ("%f", si); 

}



Top useful tips in C programming 

Now  few useful tips about the program... 

- Comment about the program should be enclosed within /* */, For example, the first two statements in our program are comments. 

- Though comments are not necessary, it is a good practice to begin a program with a comment indicating the purpose of the program, its author and the date on which the program was written. 

- Any number of comments can be written at any place in the program For example, a comment can be written before the statement, after the statement or within the statement as shown below: 

/* formula */

 si=p*n*r/ 100; si=p*n*r/100; /*formula */

 si=p*n*r//* formula */ 100; 


- Sometimes it is not very obvious as to what a particular statement in a program accomplishes. At such times it is worthwhile mentioning the purpose of the statement (or a set of statements) using a comment. For example: 

/* formula for simple interest */ 

si=p*n*r/ 100; 


- Often programmers seem to ignore writing of comments. But when a team is building big software well commented code is almost essential for other team members to understand it.


- Although a lot of comments are probably not necessary in this program, it is usually the case that programmers tend to use too few comments rather than too many. An adequate number of comments can save hours of misery and suffering when you later try to figure out what the program does. 

- The normal language rules do not apply to text written within /* ., */ Thus we can type this text in small case, capital or a combination. This is because the comments are solely given for the understanding of the programmer or the fellow programmers and are completely ignored by the compiler. 


- Comments cannot be nested. For example, 

/* Cal of SI /* sam date 01/05/2005 */ */ is invalid. 


- A comment can be split over more than one line, as in, 

/* This is 

a lazzy

 comment */ 


Such a comment is often called a multi-line comment. 


- main() is a collective name given to a set of statements. This name has to be main( ), it cannot be anything else. All statements that belong to main( ) are enclosed within a pair of  braces { } as shown below. 


void main( ) 

Statement 1;

Statement 2; 

Statement 3 ; 


- Technically speaking main( ) is a function. Every function has a pair of parentheses () associated with it, 


- The way functions in a calculator return a value, similarly, functions in C also return a value. If we want that the main( ) function should not return any value then we should precede it with the keyword void. We would discuss functions and their working in great detail in Chapter 5. 


- Any variable used in the program must be declared before using it. For example, 


int p,n;      /* declaration */ 

float r,si;   /* declaration */ 

si=p*n*r/100;  /* usage */ 


- Any C statement always ends with a

  For example-

float r, si; 

r=85: 


- In the statement, 

si=p*n*r/ 100; 

* and / are the arithmetic operators. The arithmetic operators available in C are +, -, * and /. C is very rich in operators. There are about 45 operators available in C. Surprisingly there is no operator for exponentiation... a slip, which can be forgiven considering the fact that C has been developed by an individual, not by a committee.

Once the value of si is calculated & weeds to be displayed on the screen, Ulike other language, C does not contain any instruction to display output om the screen. All output to screen is achieved using readymade library functions. One such function is Printf() We have wed it display on the screen the value contained ya Si 


~ Far us to be able to use the printf( ) function, if is necessary to we #include <stdio.h>  at the beginning of the program 


#include is a predecessor directive. I

- The general form of printf () function is,

 printf( “<format string>", <list of variables> ) ;


<format string> can contain, 


% for printing real values 

% for printing integer values 

% for printing character values 


ii addition to format specifiers like %f, %d and %c, the format string may also contain any other characters. These characters are printed as they are when the printf( ) is executed. 


Fallowing are same examples of usage of printf() function: 


printf ("%f "si), 

printf ("%d %d %f %f ",p, n, r, si): 

printf ("Simple interest = Rs. %f, si);

printf ( "Prin= %d \nRate = %f",p,t): 


The output of the last statement would look like this... 

Prin = 1000 . 

Rate = 8.500000 


What is “\n’ doing in this statement? It is called newline and # takes the cursor to the next line. Therefore, you get the output split over two kines. ‘\n" is one of the several Escape Sequences available in C. These are discussed in detail in Chapter 11. Right now, all that we can say is ‘\n’ comes in handy when we want to format the output properly on separate lines. 

printf() can not only print values of variables, it can also print the result of an expression. An expression is nothing but a valid combination of constants, variables and operators Thus, 3, 3 + 2, c and a+b * ¢-d all are valid expressions. The results of these expressions can be printed as shown below: 


printf (“%d Md %d Md", 3, 3+26,a+b*c-d}: 


Note that # and c also represents valid expressions. 


Comments

Trending now

What is thyroid eye disease ? thyroid associated ophthalmopathy (TAO), thyroid orbitopathy, Graves’ orbitopathy or Graves’ ophthalmopathy (GO).

Who discovered electron ? How to discovered electron ? What is cathode ray discharge tube ? What is cathode ray and cathode ray particles ? | Inorganic chemistry | Chemistry.

Who discovered proton and neutron ? How to discovered proton and neutron ? What is canal ray? What is anode ray ? | Inorganic chemistry | chemistry.

Nature as depicted in the Kalidasa's literature

What is password attacks ? How many types of password attacks ? How to keep safe your self from hacking by password attacks ?|Computer science|ethical hacking

Monetary Policy of Malaysia

Architecture and sculptures of Assam