Sunday 14 October 2012

Basic Structure of C program-IZZHAM AND SYAMIN


 Basic Structure of C program

Text Box: preprocessor directive

main()
{
statements;
}

All C  programs consist of the following elements:

Explanations
preprocessor         Refer to next page
main()                    Every program must have a main() function and it is a first function that will be called.
                               The main() function is the one to which control is passes when the program is executed.
{ }                           These are known as braces or curly brackets.  The statements  of the functions are enclosed in these curly brackets. The opening curly brackets indicates that a block of statements is about to begin. the closing curly brackets terminates the block of the statements.

Example
// A first C  program
#include <stdio.h>

main()
{
  printf( “Welcome  to C\n”);
  printf( “Thank you for using me!\n”);
  return 0;
}

 
 






 


Explanation

//A first .......                It is a comments.
#include <stdio.h>  It is one of the preprocessor directives. Line begins with # are processed by the preprocessor before the program is compiled.  This specific line tells the preprocessor to include in the program the contents of the standard input/output stream header file
printf                          Also called console output, instruct the computer to print on the screen the string of characters contained between the quotation marks.
\                                  This is called escape character.Some common escape sequences ie \n.
\n                               The escape sequence means newline.
return 0                      This statements terminates the function containing it and return the specified value to that function caller. In this example, it will terminate main and return 0 to its caller.
Console Output (printf)
printf is an object of the stdio class and is said to be “tied to” or connected to the standard output device, normally the display screen. 

// another example display data on screen
#include <stdio.h>
main()
{             
  printf( “45 plus 50 is  %d“ ,45 + 50);
  return 0;
}
 
Example


            




 

Header File

Standard libraries (and most C libraries) typically come with what are called “Header files”. These files provide information to the C compiler about the inputs and results produce by each function in the libraries.  Header files usually accessed by the #include directive.

Preprocessor Directives

Preprocessing occurs before a program is compiled. Some possible actions are : inclusion of other files in the file being compiled, definition of symbolic constants and macros, conditional compilation of program code and conditional execution of preprocessor directives.                 Preprocessing refer to the first step in translating.  All preprocesssor directives begin with #.  Preprocessor directives are not C statements,  so they do not end in (;).  It is processed fully before compilation begins.  You can utilize three major capabilities of the preprocessor to make your programs modular,  more readable and easier to customize:
1.             #include
                This directive is to insert the contents of a file into your program. With this, you can place common declarations in one location and use them in all source file through file inclusion.  The result is a reduced risk of mismatches between declarations of variables and functions in separate program modules. If the file is enclosed in angle brackets ( < and > ) - used for standard header files.
               
Examples:
                #include <stdio.h>

2.             #define
                This directive define macros that enable you to replace one string with another.  You can use it to give meaningful names to numeric constants, thus improving constants,  thus improving the readability of your source files.
                Examples:
                #define PI 3.149
3.                   With directives such as #if,  #ifdef,  #else and #endif,  you can compile only selected portion of your program.  You can use this feature to write source files with code for two nor more systems,  but compile only those parts that apply to the computer system on which you compile the program.  With this strategy ,  you can maintain multiple versions of a program using a single set of source files.

Text Box: /* C program that illustrates the usage of #define  */

#include <stdio.h>
#define X1  b+c
#define X2  X1+X1
#define X3  X2* c+X1-d
#define X4  2*X1+3*X2+4* X3

main()
{
  int b=2;
  int c=3;
  int d=4;
  int e=X4;
  printf(" %d  %d  %d  %d  %d ", e, X1, X2, X3, X4);
  return 0;
}

Example

Escape Sequence

An escape sequences always begins with the backslash \ and is followed by one or more special characters. 

Escape Sequence
Description
\n
Newline
\t
Horizontal tab
\a
Alert. Sound the system bell
\\
Backslash. Used to print backslash character.
\”
Double quote. Used to print double quote character.

Example

// A first C  program
#include <stdio.h>

void main(void)
{
  printf( “Welcome  to C \a \n”);
  printf( “Thank you for using me! \a \n”);
}
 
 








Commenting of C  programs

C programs can include comments that provide information to a person reading the program. Comments in C are preceded by the two characters  / and * and are followed by the same characters * and / in reversed order.  A most common feature of most programs written by professional programmers is that there is a program heading as shown below:

/**************************************************************
                PROGRAMMER NAME : yosof kadase
                DATE :  January 17, 2011
                PURPOSE OF PROGRAM : To Print Hello
****************************************************************/
#include <stdio.h>
main()
{
                  printf( “Welcome  to C \n”);
                  printf( “Thank you for using me! \n”);
                  return 0;
}


 
 





























< Programming Questions 


PQ1     Write a C  program to print your name, address and telephone number in the following format:

            Name...
                     Address...
                                 Telephone

PQ2     Write a C  program to print your name, address and telephone number in the following format:

                                 Name...., Address...., Telephone.....

 PQ3    Write a C program to print your initials in a large size type using the letter of your initials as components of the output. Adjust the initial by fully used of escape sequences and save your program as Initial.c.
            Example: if your initial is ADL

                           AAA            DDDDDD         LL
                        AA   AA         DDDDDDD      LL
                      AA      AA        DD      DDD      LL
                      AAAAAA        DD       DDD   LL
                      AAAAAA        DD       DDD   LL
                      AA      AA       DD      DDD      LL
                      AA      AA       DDDDDDD       LLLLLLLL
                      AA       AA       DDDDDD         LLLLLLLL
                     




 
 



1 comment:

  1. The 11 Best Casinos in San Diego - Mapyro
    Find Casinos & 김해 출장샵 Casinos near San Diego in San Diego, CA. - 순천 출장샵 Check out 양주 출장마사지 real-time ratings of casinos and 영주 출장마사지 other popular locales at 익산 출장마사지 Mapyro.

    ReplyDelete