Friday, 15 May 2015

Steps For Developing a Program

Greetings everyone, Today I will show you how to develop a program. Developing a program is very easy if you follow this method. First you need to know, which type of program you want to develop. If you have any doubts and problem with developing a program then first create algorithm.

An algorithm is a plan of developing a program. The first step in the program development is to device and describe a precise plan of what you want the computer to do. This plan expressed as sequence of operation, is called in algorithm. Algorithm is just idea behind a program. An algorithm is a finite set of step by step defining the solution of a particular problem. The main advantage of algorithm is proper understanding of problem.

The second step after algorithm development is the flowchart development.
Flowchart is the graphical representation of algorithm. It makes use of symbols which are connected among them to indicate the flow of information and processing. It will show the general outline of how to solve a problem or perform a task. It is prepared for better understanding of the algorithm.
(if you don't know what is flowchart and algorithm then Follow the link .)

Algorithm and flowchart is only used for understand the actual logic of a program
Here, some simple examples so we can understand

Example

*Write a program to sum of two number

Algorithm

Step 1 - Start algorithm
Step 2 - Read two numbers a and b
Step 3 - Calculate the sum of a and b and store it in sum
Step 4 - Display the value of sum
Step 5 - Stop

Flowchart




After completing the two steps, the program become very easy to develop.
Now, create your program

When you create a program first you have to include a header files.
It is a library files ans system file.

Example: include<stdio.h>

After include header file you have to enter main() function.
the program always execute from main() function which may access another function. any other function definition must be define separately before or after main() function.
When you enter main() function, it instruct the compiler that program begin from here. Compilation and execution of the program starts from main() function.

After enter the main() function open the curly brace ({) and in a new line enter the variables that you have to use in your program.

If you have to enter integer numbers then use 'int' datatype.
If you have to enter character then use 'char' datatype.
If you have to enter floating point then use 'float' datatype.
If you have to enter double floating point then use 'double' datatype.

here in our program we use int.
Example; int a,b,c;

(note -The semicolon is use for end of the statement. and comments may appear anywhere within the program using '//')

Here, a and b store the values of two numbers and c store the sum of the two numbers.

Example:
printf("\n enter two number");
scanf("%d%d",&a,&b);

Here, printf only use to display the statement
and scanf is use for inserting values of the variables.
I'm using here int datatype so in scanf i'm gonna use %d%d for read an integer values.
'&'(ampersand) is used because here i'm inserting the values.

After enter two values insert the formula of sum.
c=a+b;
Then print the value of C
Example:
printf("\n Sum is %d",c); // \n is used to enter a new line
getch();  // getch() is used for display the output result.
When your program is completed then close curly brace(})

Full example of the program

#include<stdio.h>
#include<conio.h>
void main()
{

int a,b,c;
clrscr();  // clrscr is used for clear the previous result of output screen.
printf("\n Enter two numbers");
scanf("%d%d",&a,&b);
c=a+b;
printf("\n Sum=%d",c);
getch();
}

So, that was simple program to understand the development of the program. You can also practice using the formula of subtraction, division, multiplication. Developing an efficient program requires lot of practice and skill.

If you have any doubts and problem then ask me in comments, I will help you any time.

Monday, 11 May 2015

INPUT & OUTPUT STATEMENT

Hello folks, I hope my C language related topics are helping you out. C language is very easy to learn.If you have any problem or queries related to C language you can ask me in comments.

And today topic is Input & output statement

- To make our program interactive Input statements are used, which takes input at the run-time. Output statements are used for print result, which we have to evaluated or accepted.

Formatted I/O statements
*   Printf ()

The usually used output statement is printf (). It is one of the library function.
Syntax: printf(“format string”,argument list);

-          Format string may be a collection of escape sequence or/and conversion specification or/and string constant.
-          The format string directs the printf() to display the entire text enclosed within the double quotes without any change.

  •  Conversion specification

Conversion specification is also a pair of character. It is preceded by % and followed by a quote which may be a character. The conversion specification inscribes the printf() function that it could print some value at that location in the text. The conversion characters supported by C are as follow


Conversion character
                   meaning
%d
Data item is displayed as a signed decimal integer.
%i
Data item is displayed as a single decimal integer.
%f
Data item is displayed as a floating-point value without an exponent.
%c
Data item is displayed as a single character.
%e
Data item is displayed as a floating point value with an exponent.
%g
Data item is displayed as a floating point value using either e-type of f-type conversion depending on value.
%o
Data item is displayed as an octal integer, without a leading zero.
%s
Data item is displayed as string.
%u
Data item is displayed as an unsigned decimal integer.
%x
Data item is displayed as a hexadecimal integer, without a leading 0x.
 


Example

#include<stdio.h>
#include<conio.h>
Void main()
{
Char a=’x’;
Int b=10;
Float c=3.14;
Double d=3.14e+10;
Clrscr();
Printf(“character a=%C\n “,a);
Printf(“Integer b=%d\n”,b);
Printf(“float no c=%f\n”,c);
Printf(“double float d=%e\n”,d);
Getch();

}



*       Scanf()
The usually used input statement is scanf() function.

Syntax: Scanf(“format string”,argument list”);

-          The format string must be a text enclosed in double quotes. It contains the information for interpreting the entire data for connecting it into internal representation in memory.
Example: integer(%d),  float(%f), character(%c) or string(%s).
-          The argument list contains a list and separated by comma. The number of argument is not fixed; however corresponding to each argument there should be format specifier.
-          Inside the format string the number of argument should tally with the number of format specifier.
Example: if I is an integer and j is a floating point number, to input these two numbers we may use scanf(“%d%f”,&i,&j);

Example:
Include<stdio.h>
Include<Conio.h>
Void main()
{
            char a;
int b;
float c;
double d;
clrscr();
printf(“\n Enter a character”);
scanf(“%C”,&d);
printf(“\n Enter an integer”);
scanf(“%d”,&b);
printf(“\n enter float no”);
scanf(“\%d”,&c);
printf(“\n enter double no”);
scanf(“%1f”,&d);
getch()
}

 Unformatted I/O Statements
Getchar():
Getchar() will accept a character from the console or from a file, diplays immediately while typing and need to press Enter key for proceeding.

Syntax: int getchar(void)

It returns the unsigned char that they read. If end-of-file or an error is encountered getchar() functions return EOF.

Example: char a;
                                    a=getchar();

The function “getchar()” reads a single character from the standard input device and assigns it to the variable ”a”.

Example:

#include<stdio.h>
#include<conio.h>
Void main()
{
char a;
clrscr();
printf(“\n enter a character “);
a=getchar();
printf(“\n given character is %c”,a);
getch();
}

* Getch():
Getch() is used to get a characters from console but does not echoto yhe screen.

Syntax:
 int getch(void)

Example declaration:
char ch;
ch=getch();


Getch() reads a single character directly from the keyboard, without echoing to the screen, this function return the character read from the keyboard.

Example:
Void main()
{
Char ch;
Ch=getch();
Printf(“\n input char is :%c”,ch);
}
Here, declare the variable ‘ch’ as char data type, and then get a value through getch() library function and store it in the variable ‘ch’. And then, print the value of variable ‘ch’.

During the program execution, a single character is get or read through getch(). The given value is not displayed on the screen and the compiler does not wait for another character to be typed. And then, the given character is printed through the printf().

*   getche():
getche() is used to get a character from console, and echoes to the screen. Getche reads a single character from the keyboard and echoes it to current text windows, using direct video or BIOS. This function return the character read from the keyboard.

Syntax:
Int getche(void)

Example Declaration:

char ch;
ch=getche();

Example:
Void main()
{
char ch;
ch=getche();
printf(“input char is:%c”,ch);
}

Here, declare the variable ch as char data type, and then get a value through getche() library function and store it in the variable ‘ch’. And then, print the value of variable ‘ch’.

During the program execution, a single character is get or read through getch(). The given value is displayed on the screen and the compiler does not wait for another character to be typed.

*  putchar() :
putchar() function displays a single character on the screen.
Syntax: int putchar(int c);

Example:

#include<stdio.h>
#include<conio.h>
{
char a;
clrscr(0;
printf(“\n enter a character “);
a=getchar();
printf(“\n given character is”)
putchar(a);
getch();
}

* puts():
It is to display a string on a standard output device. The puts() function automatically insert a newline character at the end of each string it display so each subsequent string displayed with puts() is on its own line. puts() is on its own line. puts() return non-negative on success, or EOF on failure.


Example:

#include<stdio.h>
#include<conio.h>
void main()
{
char *str;
clrscr();
printf(“\n enter a string”);
gets(str);
printf(“\n given no is”);
puts(str);
getch();
}

Thursday, 7 May 2015

Structure of a C Program, Execution of a C Program & Compiler & Interpreter

  Structure of C Language

-          Documentation section
-          Link section
-          Definition section
-          Main()   function section
{
  Declaration part
  Execution part
}
-          Subprogram section(UDF)
        Function 1
        Function 2

           C Program can be as a group of block called functions. Function is subroutine that may include one or more statements designed to perform a specific task.
1)      Documentation section :

It consist of set of comment lines. Comment lines starts with /* and end with */. It contains the name of program, author name, program details etc. Anything which written in document section is not executed. Comment may not embedded and nested.

2)      Link section :

This section is use to include library files and system files. The general syntax to include file is

#include<file name>
EX. #include<stdio.h>

3)      Definition section :

It is used to define symbolic constant. The general syntax is
#define variable constant name
EX. #define pi 3.14


4)      Global declarations section :

Some variable is not only use in main but in other function also. Hence, it has to declare globally. The variable declared outside any function is known as global variable.

5)      Main() function section :

Every C program consist of one or more function. The program always execute from main() function which may access another function. Any other functions definition must be defined separately before or after main(). Structure of main() is

Main()
{
 Variable 1, variable 2           ......Declaration part
Statement 1;
Statement 2;
.
.                                             …… Execution part
.
Statement n:
}


The main() function instruct the compiler that program begin from here. Compilation and execution of the program starts from main(). Main() section consist of 2 parts

1.       Declaration part
2.       Execution part
         Open bracket indicate the start of the main() function and closing bracket indicate end of the  main() function. Between that all the variable are declared. Actual program start from statement 1. Every   statement ends with semicolon (;). Comments may appear anywhere within program.

6)      Sub program section(UDF) :

This section contain all user define functions that are called in main() function.

  Execution of a C program

Execution of program involves some steps
1.       Creating the program

Once we load the OS into the memory the computer is ready to receive program. The program must be entered into the file. The file name should contain of letters, digits and special character.

2.       Compiling the program

The source code instructions are now translated into a form that is suitable for execution by the computer. The translation is done after checking each instruction for its correctness. If everything is all right, the compilation processed and translation program is stored on another file known as object file. If any mistakes in syntax and semantics of language are discovered, they are listed and compilation processes stop here. The error should be corrected in the source code compilation is done again.

3.       Linking the program

It is the process of putting together other program files and functions that are require by the program.
Ex. If the program use pow() function the code of this function should be brought from math library of system.

4.       Execution the program :

It is very simple task. During execution, the program may request for some data to be entered through the keyboard. Sometimes the program doesn't produce desired result so there might be something wrong with a program.


* Compiler & Interpreter

1) Compiler

- A translator program that translates a high-level language program into its equivalent machine language program.
- It translates whole programs and at end it displays all errors or warning at a time.

- It translates whole programs at a time.

Compilation



- When a user writes a code in a C language and wants it to execute, a specific compiler which designed for C is used before it will be executed. The compiler scans the entire program first and then translates it into machine code which will be executed by the computer processor and the corresponding task will be performed.



2) Interpreter

- It is also a translator program that translates a high-level language program into its equivalent machine language program.
- But it interpret (translates) one line and any error or warning is there then it displays it and stops the program conversion until you solve that error.
- It translates programs line by line.

Interpretation
- Each time when an interpreter gets a C language code to be executed, it converts the code into an intermediate code before converting it into the machine code. Each part of the code is interpreted and then execute separately in a sequence and an error is found in a part of the code it will stop the interpretation of the code without translating the next set of the codes.

3) Editor

- Interface development environment in which you can execute your C source program is called editor.
- The editor which helps user to write and edit his programs.
- It is tool for writing a programs.

Wednesday, 6 May 2015

Two Type Of Programming Languages, Features, Uses & 3 Levels Of Different Languages


Computer programming languages are developed with the primary objective of facilitating a large number of people to use computers without the need for them to know in detail the internal structure of the computer. Languages are designed to be machine-independent. Most of programming languages ideally designed, to execute a program on any computer regardless of who manufactured it or what model it is.
Programming languages can be divided into two categories:
i) Machine oriented languages
The languages whose design in governed by circuitry and the structure of the machine is known as machine language. This language is difficult to learn and use. It is specific to a given computer and is different for different computers i.e. these languages are machine-dependent. These languages have been designed to give better machine efficiency, i.e. faster program execution. Such languages are also known as low level languages.
ii) Problem oriented languages
These languages are particularly oriented towards describing the procedures for solving the problem in a concise, precise and unambiguous manner. Every high level language follows a precise set of rules. They are developed to allow application programs to be run on a variety of the computers. These languages are machine independent. Languages falling in this category FORTRAN, BASIC, PASCAL etc. They are easy to learn programs may be written in these languages with much less effort. However, the computer cannot understand them and they need to be translated into machine language with the help of other programs known as compiler or translators.

* Features of C language

1) structured

A structured language offers a variety of programming possibilities such as WHILE, DO WHILE, FOR LOOPS AND function. Example: Pascal, C C++, java etc
2) portability:
portability means C has an ability of program to run in different environment. Different environment could refer to different computer, OS or compilers where as machines or assembly language varies from computer to computer hence program written in this language are not portable.
Example of portable language are pascal, Fortran, C etc.
3) flexibility:
C language is used for developing system level software ( such as OS, compiler) as well as application software.
4) Modularity
C allows facility that programs can be broken down into a series of identifiable sub task. It is a good programming practice to implement each of this sub as a separate program module. In C such modules are return as function. The use of a modular programming structure enhances the accuracy and clarity of program and it facilities future program alteration.
5) Robust Language:
C is robust language whose rich set of built-in-functions and operators can be used to write any complex program. It combines the capability of an assembly language with the features of a high level language and therefore it is well suited for writting both system software and business packages.

*Uses of C Language

C language is used for developing system applications that forms major portion of operating system such as Windows, UNIX and Linux. Below are some example of C being used.
1. Database system
2. Operating system development
3. Graphics packages
4. compilers and Assemblers
5. Network drivers
6. Word processors
7. spread sheets
8. interpreters


* 3 Levels Of Different Languages


1. High Level

High level languages provides almost everythings that the programmer might need to do as already built into the language.

Ex. JAVA, Python
2. middle Level

C language is belong to middle level of language

Middle level languages don't provide all the built-in functions found in high level languages, but provides all building blocks that we need to produce the result we want.
Ex. C, C++
3) Low Level
Low level languages provides nothing other than access to the machines basic instruction set
Ex. Assembler
 

Tuesday, 5 May 2015

C Programming Language

Hello everyone,
                          Today I'm cheerfully introduce my new blog about C programming language. I'm Happy to start this blog about C programming language.
today we discuss about the history of a C language, problem solving tool and steps that involved in developing a program.

               The UNIX operating system and virtually all Unix applications are written in the C language. C has now become a widely used professional language for various reasons.
- Easy to learn
- Structured language
- It produces efficient programs.
- It can handle low-level activities.
- It can be compiled on a variety of computers.


·         History of a C language

C is an ANSI/ISO standard and powerful programming language for developing real time application. C programming language is invented by Dennis Ritchie at the Bell Laboratories in 1972. It was invented for implementing UNIX operating system. C language is a structure oriented programming language. C language features were derived from earlier language called “B” (Basic Combined Programming Language – BCPL).

C language was invented for implementing UNIX operating system. In 1978, Dennis Ritchie and Brian Kernighan published the first edition “The C Programming Language” and commonly known as K&R C.

In 1983, the American National Standard Institute (ANSI) established a committee to provide a modern, comprehensive definition of C. The result definition, The ANSI standard or “ANSI C”, was completed late 1988. Embedded C includes features not available in normal C like fixed-point arithmetic, names address spaces, and basic I/O hardware addressing. Operating system, C compiler and all UNIX application programs are written in C language. It is also called as procedure oriented programming language. C language is reliable, simple and easy to use. C has been coded in assembly language.

·         Problem Solving Tool

The computer is a resources – a versatile tool – that can help you solve some of the problem that you encounter. A computer is a very powerful general-purpose tool. Computer can solve or help you to solve many types of problems. There are also many ways in which a computer can enhance the effectiveness of the time and effort that you are willing to devote to solving a problem. Thus, it will prove to be well worth the time effort you spend to learn how to make effective use of this tool.

Here, we discuss the steps that involved in developing a program
Program development is a multi -step process that requires you to understand the problem, develop a solution, write the program and test it. This critical process determines the overall quality and success of your program. If you carefully design each program using good structured development techniques, your program will be efficient, error free, and easy to maintain.

Here some steps for developing program
1.       Develop an Algorithm and flow chart.
2.       Write the program in a computer language (Ex. C programming language).
3.       Enter the program using editor.
4.       Test and debug the program.
5.       Run the program, input data and get the results.

The Algorithm and flowcharts topics are not involved here
The algorithm and flow chart are in following link  algorithm & flowchart

If you have any queries in C language you can ask me in comments.