Anda di halaman 1dari 15

C Programming Introduction : Tips

1. Every C Program Should have exactly one main function


2. C Program Execution Always Starts from main.
3. Execution of C Program begins at Opening brace of function and ends at closing brace of the
function
4. Generally all statements in c are written in Lowercase Letters.
5. Uppercase Letters are used for Symbolic names,output strings and messages
6. Every C statement must ends with semicolon
7. All variables must be declared with respective data types before using .
8. C is free form-Language
9. Comments can be inserted anywhere in C Program , but nested comments are not supported
by C .
10. Braces are Generally Used for the Grouping of statements

Sample C Program :
//C hello world example
#include <stdio.h>

int main()
{
printf("Hello world\n");
return 0;
}

Step By Step Execution Of C Program


Step 1 : Edit
1. This is First Step i.e Creating and Editing Program.

2. First Write C Program using Text Editor , such as [ Borland C/C++ 3.0 , Notpad++,Notpad ]
3. Save Program by using [.C] Extension.
4. File Saved with [.C] extension is called Source Program.

Step 2 : Compiling
1. Compiling C Program : C Source code with [.C] Extension is given as input to compiler and
compiler convert it into Equivalent Machine Instruction.
2. In Borland C/C++ 3.0 program can be compiled using key [Alt + F9 ].
3. Compiler Checks for errors . If source code is error-free then Code is converted into Object File
[.Obj ].

Step 3 : Checking Errors


1. During Compilation Compiler will check for error, If compiler finds any error then it will report
it.
2. User have to re-edit the program.
3. After re-editing program , Compiler again check for any error.
4. If program is error-free then program is linked with appropriate libraries.

Step 4 : Linking Libraries


1. Program is linked with included header files.

2. Program is linked with other libraries.


3. This process is executed by Linker.

Step 5 : Error Checking


1. If run time error occurs then Run-time errors are reported to user.
2. Again programmer have to review code and check for the solution.

Compiler :
Compiler takes high level human readable program as input and convert it into the lower level
code.This conversion takes place using different phases.First phase of compiler is lexical analysis.
Must Read : [What is Compiler ?]

Different phases of compilers :


1. Analysis Phase
2. Synthesis Phase

1.Analysis Phase :

Lexical analysis

Syntax analysis

Semantic analysis

Lexical Analysis Phase :

Task of Lexical Analysis is to read the input characters and produce as output a sequence of tokens that
the

parser

uses

for

1. Lexical Analyzer is First Phase Of Compiler.


2. Input to Lexical Analyzer is Source Code
3. Lexical Analysis Identifies Different Lexical Units in a Source Code.
4. Different Lexical Classes or Tokens or Lexemes
o Identifiers
o Constants
o Keywords
o Operators
5. Example : sum = num1 + num2 ;

syntax

analysis.

So Lexical Analyzer Will Produce following Symbol Table

Token

Type

sum

Identifier

Operator

num1

Identifier

Operator

num2

Identifier

Seperator
6. Lexical Analyzer is also called Linear Phase or Linear Analysis or Scanning
7. Individual Token is also Called Lexeme
8. Lexical Analyzers Output is given to Syntax Analysis.

Analysis Phase : 2nd Phase of Compiler (Syntax Analysis)


During the first Scanning phase i.e Lexical Analysis Phase of the compiler , symbol table is created by
the compiler which contain the list of leximes or tokens.

Syntax Analysis :

1. It is Second Phase Of Compiler after Lexical Analyzer


2. It is also Called as Hierarchical Analysis or Parsing.
3. It Groups Tokens of source Program into Grammatical Production
4. In Short Syntax Analysis Generates Parse Tree

Parse Tree Generation :


sum = num1 + num2

Now Consider above C Programming statement. In this statement we Syntax Analyzer will create a
parse

tree

from

the

tokens.

[box]Syntax Analyzer will check only Syntax not the meaning of Statement[/box]

Explanation : Syntax Analysis

We know , Addition operator plus (+) operates on two Operands

Syntax analyzer will just check whether plus operator has two operands or not . It does not
checks the type of operands.

Suppose One of the Operand is String and other is Integer then it does not throw error as it only
checks whether there are two operands associated with + or not .

So this Phase is also called Hierarchical Analysis as it generates Parse Tree Representation of
the Tokens generated by Lexical Analyzer

Syntax analyzer will just create parse tree. Semantic Analyzer will check actual meaning of the
statement parsed in parse tree. Semantic analysis can compare information in one part of a parse tree
to that in another part (e.g., compare reference to variable agrees with its declaration, or that
parameters to a function call match the function definition).

Semantic Analysis is used for the following


1. Maintaining the Symbol Table for each block.
2. Check Source Program for Semantic Errors.
3. Collect Type Information for Code Generation.
4. Reporting compile-time errors in the code (except syntactic errors, which are caught by
syntactic analysis)
5. Generating the object code (e.g., assembler or intermediate code)

Now In the Semantic Analysis Compiler Will Check


1. Data Type of First Operand
2. Data Type of Second Operand
3. Check Whether + is Binary or Unary.
4. Check for Number of Operands Supplied to Operator Depending on Type of Operator (Unary |
Binary | Ternary)

What is Interpreter ?
1. Interpreter Takes Single instruction as input .
2. No Intermediate Object Code is Generated
3. Conditional Control Statements are Executes slower
4. Memory Requirement is Less
5. Every time higher level program is converted into lower level program
6. Errors are displayed for every instruction interpreted (if any)
7. The interpreter can immediately execute high-level programs, thus interpreters are
sometimes used during the development of a program, when a programmer wants to add small
sections at a time and test them quickly.
8. In addition, interpreters are often used in education because they allow students to program
interactively.

Examples of Programming Languages Using Interpreter :


Lisp
(defun convert ()
(format t "Enter Fahrenheit ")
(LET (fahr)
(SETQ fahr (read fahr))
(APPEND '(celsisus is) (*(- fahr 32)(/ 5 9)) )
)

BASIC
CLS
INPUT "Enter your name: ", Name$
IF Name$="Mike" THEN
PRINT "Go Away!"
ELSE
PRINT "Hello, "; Name$; ". How are you today?"
END IF

External Reference Links : Advantages and Disadvantages of Interpreter |Interpreter

Difference between Compiler and Interpreter


N
o

Compiler

Interpreter

Compiler Takes Entire program as


input

Interpreter
Takes Single instruction as input .

Intermediate
isGenerated

No Intermediate
isGenerated

Conditional Control
are Executes faster

Object

Code

Statements

Memory
Requirement : More(Since Object

Object

Code

Conditional Control Statements


are Executes slower
Memory Requirement is Less

Code is Generated)
not

Every time higher level program is


converted
into
lower
level
program

Program
need
be compiledevery time

Errors are displayed after entire


program is checked

Errors are displayed for every


instruction interpreted (if any)

Example : C Compiler

Example : BASIC

Explanation : Compiler Vs Interpreter


Just understand the concept of the compiler and interpreter
1. We give complete program as input to the compiler. Our program is in the human readable
format.
2. Human readable format undergoes many passes and phases of compilerand finally it is
converted into the machine readable format.
3. However interpreter takes single line of code as input at a time and execute that line. It will
terminate the execution of the code as soon as it finds the error.
4. Memory requirement is less in Case of interpreter because no object code is created in case of
interpreter.
Following video will say much more about compiler
Difference

Between

Source

Source Code :
1. Source Code is In the form of Text.
2. Source Code is Human Readable.
3. Source Code is Generated by Human.
4. Source Code is Input Given to Compiler.
Object Code :
1. Object Code is in the form of Binary Numbers.

Code

and

Object

Code

2. Object Code is in Machine Readable.


3. Object Code is Generated by Compiler.
4. Object code is Output of Compiler.
Also See : Compile Vs Interpreter
Tips for New Programmers 1 : About Simple Program

1. Include following header file in order to print simply Hello


#include<stdio.h>

2. Dont forgot to write main function.


3. Separate line for each header file .
4. Each Statement is terminated by Semicolon (;) .
5. Main function starts with opening parenthesis ( { ) and ends with closing parenthesis ( } ) .
6. Simple Hello Word Program :
#include<stdio.h>
main()
{
printf("Hello World");
}

Tips For Writing Readable And Eye Catching C Program

Tip 1 : Use Stylish Comment to Specify Title Of Program !!


/*********************************************
* Title
* Date

: Your Title
: DD/MM/YYYY

* Program By : Your Name *********************************************/

Or

/**
* Title

: Your Title

* Date

: DD/MM/YYYY

* Program By : Your Name


**/

Tip 2 : One Should Use Horizontal Rulers To Differentiate Different Modules


#include< stdio.h>
#include< conio.h>
//---------------------------------------int add(int,int);
int sub(int,int);
int div(int,int);
//---------------------------------------void main()
{
............................
}
//---------------------------------------int add(int a,int b)
{
.....................
}

1. Horizontal Rules Differentiate Source Code into Three Parts


o Header File Section
o Prototype Declaration Section
o Main Function
2. Horizontal Rule makes Program more Readable

Tip 3 : Must Specify Function Description Before Writing Function Definition.


/*-----------------------------------------------

Search Function : Used to Search Element


-----------------------------------------------*/
int search()
{
.....
.....
.....
}

Tip 4 : Use Proper Indentation [i.e Space]


if(condition)
{
for(i=0;i< n;i++)
{
Statement 1;
Statement 2;
while(i<5)
{
Statement 3;
Statement 4;
Statement 5;
}
}
}

1. One can Understand Program if Proper Indentations are Provided.


2. In Above Program one can Easily Understand Statements Under if,for,while.

Tip 5 : Provide Comment After Nested Closing Braces.


if(condition)
{
for(i=0;i< n;i++)
{
Statement 1;
Statement 2;

while(i<5)
{
Statement 3;
Statement 4;
Statement 5;
} //end of while
}//end of for
}//end of if

1. If You Look at last Three Lines , At First Glance we are unable to Distinguish Corresponding
Closing Brace.
2. So Short but Sweet Comment Can be used to Avoid Your Confusion

Anda mungkin juga menyukai