Structure-c-program-notes

  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Structure-c-program-notes as PDF for free.

More details

  • Words: 973
  • Pages: 4
Programming Fundamentals with C

Padmashree Desai, Asst.Prof, CSE Dept

Structure of C program Steps involved in problem solving 1. Problem definition: To obtain correct solution a problem must defined precisely. It will help to understand what must be done. 2. Problem analysis : Ti determines what is input,ouput and the relation ship between each of the data items. 3. Design • Algorithm : Step by step procedure to solve a problem • Flowchart : Pictorial representation of algorithm • Psedoalgorithm : I t is neither an algorithm nor a program. It is an abstract form of program. Psedoalgorithms are written for complex problems. The program can be developed easily from a psedocode rather than a flowchart 4. coding: Translation of an algorithm/flowchart into a set of instructions which computer can understand. Algorithms are converted to a program. Programs are written using programming language. 5. Compiling : It is process of translating a High level language program to M/c language. 6. Debugging : Removing errors from program. Syntactical errors are removed 7. Executing(Running ) a program : The program is executed by CPU 8. Testing: The program is tested by executing with different sets of input data. (logical errors are removed) 9. Documentation : Writing an explanatory note for program or program instructions.

Structure of C program Documentation section Link section Definition section Global declarations; main ( ) { declaration part; executable statements; } user defined functions 1

Programming Fundamentals with C

Padmashree Desai, Asst.Prof, CSE Dept

sample program 1 /* Title / purpose of the program */ /* Author Identification : Name of the student & Roll Number */ /* Date : ----------------- */ #include<stdio.h> main ( ) { printf ( “ First C program “); /* displaying message */ } sample program 2 /* To perform addition, subtraction, multiplication, division*/ /* Author Identification ---------------- */ /* Date ------------- */ # include<stdio.h> main ( ) { int a =10, b = 20; /* declaration and initialization of input data */ int sum, sub; add = a + b ; diff = a – b ; /*computation of arithmetic operation */ printf ( “ sum = % d “, sum ) ; /* Display the results */ printf ( “difference = % d “, sub ) ; } Documentation Section : It consists of a set of comment lines giving the name of the program, the author and other details, which the programmer would like to see later. Comments can be added to any of instructions in the program Link Section: This provides instructions to the compiler to link functions from the system library. It consists of preprocessor statements(directives) which begin with # symbol. These direct the C preprocessor to include header files(linking functions) Examples : #include <stdio.h> #include”test.h” #include<math.h>

2

Programming Fundamentals with C

Padmashree Desai, Asst.Prof, CSE Dept

Definition section : It consists of symbolic constants Example : #define NULL 0 #define PI 3.1415 Global declarations : Variables or functions that are used in the main( ) function and also in other user defined function are called global variables (or functions)The declaration is made before main( ) main() :Every C program must have one main ( ). Execution of C program starts from main(). No C program is exeuted without main(). It should be written in lowercase letters and should not be terminated by a semicolon. It calls other library functions and user defined functions.There must be one and only one main( ) function in every C program . Braces : Every c program uses a pair of curly braces { }. { indicates beginning of main ( ) function } indicates end of main ( ) function These braces are also used to indicate the beginning and end of user defined functions and compound statements. Declaration part : All the variables, arrays, user defined functions etc used in the C program are declared and may be initialized with their basic data types. Executable part : These statements instruct the computer to do specific operations. These may be input/output, arithmetic statements, control statements and other statements. Executable and declaration part are terminated by a semicolon. User defined Functions : These are subprograms generally called as functions. These contain set of statements to perform a specific task. These are written by the user, hence the name user defined functions. Programming Style  Develop a habit of writing a program in lower case  Upper case letters are used only for symbolic constants  Use braces to group programming statements and mark the beginning and the end of functions  Use proper indentation  Use documentation Documentation It is an explanatory note on the purpose/use of instruction/program for which it has been written. Explanatory note is called a comment. It explains how the program

3

Programming Fundamentals with C

Padmashree Desai, Asst.Prof, CSE Dept

works and how to interact with it. Thus it helps the other programmer to understand the program There are two types of documentation • Internal documentation • External documentation Internal documentation • It is a comment statement within a program • It helps the programmer to understand / update the code at later stage. It is done by • Defining meaningful variable names • Including remarks or comments in a program code • Adding comments on every module indicating its purpose • Presenting the program code clearly by inserting spaces, blank lines and using indentation External documentation • It is an executable statement in a program. It may be a message to the user to respond to the program requirement. This is accomplished using output statements. It makes the program more attractive and interactive. Some sample examples are : • printf(“Input numbers one by one”); • printf(“Do you want to continue?”); Indentation : means arranging programming constructs properly to enhance readability and understandability of a program. -------------------end------------------------Uploaded by Nix

4