How to write c program in Right way

    25 Powerful Rules to write C program in Correct way

Basic rules to begin:
  1. In separate statement each instruction should be written. Therefore , complete C program consist of set of instructions.
  2. The statements in program must be written in sequencial order to get desired output.Unless any logic problem can be arise.
  3. All statement should written in small case letters.
  4. C has no particular rules for position at which statement is to be typed.
  5. In C program every statement must end with ; (semicolon).It act as a terminator.
Rules for Comment:
  • Comment in the program should be enclosed within /* ..  */ .Look example below the first line is comment.
           For ex. /*This my first program*/
                       #include<stdio.h>
                       main()
                       {
                        Statement;
                        Statement;
                        }
  • Though comments are not necessary,but it good practice to begin program with comment indicating purpose of program so that other person can get idea of program.
  • You can write any number comment at any place in program mentioning the purpose of the statement.

  • Use few Comment instead of too many. 
  • A comment cannot be nested. For ex.,  /* The/* first/*program*/*/*/. 
  • A comment can split over more than one line. For ex. 
                    /*THE
                       First
                       Program*/

Main method:
  • main() is a collection of the set of statements. Statements are always enclosed belongs to main() within pairs of {} (opening and closing braces).For ex.
                                void main()
                                 {
                                   1st statement;
                                   2nd statement;
                                  } 
  • Technically main() is a function because every function has the pair of parentheses ( ) associated with it.
  • The function in c have their return type, Similarly, If we want that main() function should not return any value of particular type precede it with void ,int etc.

Declaration:
Any variable used in the program must be declared before using it. For ex,
                 int a,b,c;            /*declaration*/
                 float a,b,c;         /*declaration*/
                 a=a+b;               /*usage*/
For declaration see the datatype table as follow:
                Datatype                          number                         Declaration                       format string
                 Integer                            1,+5,-5                              int a;                                 "%d", a
                 Float                     11.363,+1.14,-12.11                   float a;                              "%f", a
                 Double                  +1.123123,-1.234345                double a;                           "%l", a
                 Character                        'A','B'                               char a;                               "%c",a
                 String                            "Dev.com"                         char a[20];                           "%c",a

Ways of declaration in C lets see:
            a) While declaration you can also initialize it as 
                                    int a=1,b=24; ,                 float a=1.5, b=1.98+3.8*2.7;
            b) The order sometimes matter in initialization.For ex.
     
                int i = 1, j=24; is same as int j=24,j=1;
                float a=1.8 , b= 3.1+a; is alright but float b =3.1+a ,a=1.8; is not this why we have to initialize a 
                                    before. 
    
           c) Lets another statements:
                                    int x,y,z;
                                    x=y=z=10;                work perfectly
        
   int x=y=z=10;           not work because once again we are trying to use y (assign to y) before                                                      defining it.
                                   
Statement:

For ex, 
                     float p,n, r,si;
                     scanf("%f", &si);
                     si= p*n*r/100;
                     printf("%f", si);
                     printf("problem done \n");

you can notice *, / are arithmetic operator in C. C is very rich in operator hence there about 45 operator available in C.

For input use scanf("%d,%f",&a,&c); function i.e syntax is scanf("<format string1>,<format string2>,...",&variable1 ,&variable2..);

For output use printf("%d,%f",&a,&c); function i.e syntax is printf("<format string1>,<format string2>",variable1,variable2); , & is used in scanf function is must because it is "Address of operator".It gives the location number used by variable.

printf() function can print values of variable as well as print the result of an expression:for ex printf("%d,%d,%d,%d",3,3+2,c,a+b*c-d);.

Format specifier or format string are like %f,  %d, %c, %l used for input and output.\n in the statement is for print output in next line.




For complete Rules click on the following Links 

How to write c program in Right way | part 1

How to write c program in right way | Follow this important rules of c | Part 2




Mahesh Kondawar
(Trainer)

                                                                             Thanks




Comments

Popular posts from this blog

The History of c programming language | Everything you need to know

Complete Syllabus of C programming from Beginners to Advance or System Level

Technical Aspects of Fundamental Data types in C