Structured Programming and Preprocessors
Structured Programming
Structured Programming एक Paradigm है जिसका उद्देश्य Programmer के Point of View से Program को समझने योग्य बनाना है। यह एक Program के माध्यम से Control Flow को Linear करता है।Structured Programming में Execution Flow , Writing Order को Follow करता है।
What Are the Elementary Structures of Structured Programs?
Block :
यह एक Command या Command का एक Set है जिसे Program Linear रूप से Execute करता है। Sequence में एक Single, Entry Point (पहली पंक्ति) और Exit Point (अंतिम पंक्ति) होता है।
Selection :
यह एक Condition के Result के Based पर Control Flow की Outcome की Branch है। Two Sequences are Specified : ‘if’ Block जब Condition True होती है और ‘else’ Block जब Condition False होता है। ‘else’ Block Optional है और no-op हो सकता है।
Iteration:
यह एक Block की Repetition है जब तक कि यह एक Specific Condition को Complete करता है। Condition का Evaluation Block के Starting or Ending में होता है। जब Condition False होती है, तो Loop End हो जाता है और अगले Block पर Move कर जाता है।
Nesting :
उपरोक्त Building Blocks को Nested किया जा सकता है क्योंकि Encapsulated होने पर Conditions और Iterations में Single Entry Point और Single Exit Point होते हैं और किसी भी Other Blocks की तरह ही Behave करते हैं।
Subroutine :
Since in Hole Program में Single Entry Point और Single Exit Point होते हैं, Subroutine में Encapsuling करने से हम one identifier द्वारा Blocks Invoke कर सकते हैं।
Preprocessor
Program के सभी header area में #include <stdio.h> इस प्रकार के files को include करते है | इनकी Starting ‘#’ से होती है | इन्हें ‘Preprocessor’ कहते है | Preprocessor को statement की तरह semicolon (;) नहीं दिया जाता है|
Preprocessor एक Program है, जो Source Code compile होने से पहले ही execute होता है |
यहाँ पर Preprocessors को अलग-अलग Categories में Devide किया है |
Directive Types | Preprocessors |
---|---|
Macro | #define |
File Inclusion | #include |
Conditional Compilation | #if, #else, #ifdef, #endif, #ifndef, #elif |
Other Directives | #pragma, #undef |
Macro
#define
macros identifiers होते है, Program में जहा पर identifier होता है, तो वो replace होकर वहा पर दी हुए value आ जाती है |
For Example #define
#include <stdio.h> #define PI 3.145 int main(){ printf("Value of PI : %f\n", PI); return 0; }
Output
Value of PI : 3.145000 Overwrite value of PI : 3.140000
Example for #define function macro
#include <stdio.h> #define PI 3.145 #define Area(rad) PI*rad*rad int main() { int radius = 5; float area; area = Area(radius); printf("Area of circle is %f", area); return 0; }
Output
Area of circle is 78.625000
Predefined Macros
Macro Description
_DATE_ : ये current date के string को return करता है |
_FILE_ : ये current file name को उसके path के साथ return करेगा |
_LINE_ : ये macro जिस line पर है उस line का number return करेगा |
_STDC_ : अगर compiler ANSI Standard C का पालन करता है तो वो ‘1’ return करेगा |
_TIME_ : ये current time के string को return करता है |
Example for predefined macro
Source Code :
#include <stdio.h> int main(){ printf("Date : %s\n", __DATE__ ); printf("File : %s\n", __FILE__ ); printf("Line : %d\n", __LINE__ ); printf("STDC : %d\n", __STDC__ ); printf("Time : %s\n", __TIME__ ); return 0; }
Output
Date : Dec 19 2016 File : C:\UD\Documents\predefined_macro.c Line : 7 STDC : 1 Time :16:38:22
File Inclusion
#include
Header files को program में include करने के लिए #include का Use करते है |
हर एक program में #include का Use होता है |
Preprocessor Two Types में include जाते है |
#include <file_name>
#include “file_name”
कुछ header files predefined होते है और कुछ header files user-defined भी होती है |
Predefined header files : stdio.h, conio.h, math.h
User-defined header file : myheader.h
Conditional Compilation
Conditional Compilation जैसा कि नाम का तात्पर्य है कि कोड Compiled किया गया है यदि कुछ Conditions True हैं। Generally हम किसी Condition को Check करने के लिए if Keyword का उपयोग करते हैं, इसलिए हमें कुछ अलग Use करना होगा ताकि Compiler यह Decide कर सके कि Code को Compile करना है या नहीं।
अब Scenario को जल्दी से समझने के लिए निम्नलिखित Code Consider करें:
#include <stdio.h> #define x 10 int main() { #ifdef x printf("hello\n"); // this is compiled as x is defined #else printf("bye\n"); // this isn't compiled #endif return 0; }
Conditional compilation example in C language
#include <stdio.h> int main() { #define COMPUTER "An amazing device" #ifdef COMPUTER printf(COMPUTER); #endif return 0; }
इसी तरह से अन्य #pragma, #undef, etc. Preprocessors का Use किया जाता है।