Sunday, December 30, 2012

C Program Control Structure : Explain If else with Example, Switch statement with Example

Control Structure in C Program ( if else, if ... else if ... else , switch case, conditional operator)
 
As it name suggest it used to control the flow of program execution. Lets learn by example,
If you want to purchase any Bike then you have to select any one options out of the list of possibilities. 1) Price of the Bike 2) Features / Style of Bike 3) Average km. 4) nearest shop / where to buy. So here you have to find out the answer of the above questions, some many be compulsory while some can be optional.  Depending on the requirement you have to choose one final options and have to Buy Bike.

The control structure of the C Language works on The Same Way !

Lets See  How many control structure provided by the C Language.
1) If... else
2) If... else if ... else..
3) if ... else if....
4) switch () (Switch statement)
5) ? : (Conditional Operator)


Basically there are only three control structure is provided by C Language 1) If...else.... 2) switch case 3) ? :  conditional control

1) If... else
If... else conditional structure is used to select one options out of two options, with reference to above example of Bike, If you want to purchase bike whose price is less than 20,000/-, Then you need to use this conditional structure. Let's check out with C Program to Demonstrate If.. else structure.

C Program If else Example :

#include <stdio.h>
#include <conio.h>

void main()
{
    int bikeprice1=10000;
    int bikeprice2=30000;
    if(bikeprice1 < 20000 )
    {
        printf("\nYou Can Purchase Bike 1 ");
    }
    else
    {
        printf("\nYou Can not Purchase Bike 1 because of Price Limit");
    }

    if(bikeprice2 < 20000 )
    {
        printf("\nYou Can Purchase Bike 2 ");
    }
    else
    {
        printf("\nYou Can not Purchase Bike 2 because of Price Limit");
    }

}

Some Assumption while writing my C Programs :
Here I have assumed Bike 1 cost is : 10000 and Bike 2 Cost is : 30000

Before Moving to Output :
* Can you guess what's should be the output of the above program !
* Think twice and write into the your book and compare with our output.

Lets Check out Output of the Above Program:
You Can Purchase Bike 1
You Can not Purchase Bike 2 because of Price Limit

Now Lets move to the next conditional structure : 2) If... else if ... else..

2) If... else if ... else..
if ... else if ... else is similar to previous one but it has more than one options from it you want to select one way, Lets take example for the same. If you have three ways to go for a specific location then you need to select any one options out of them, so at that time you need to go with the same option.

Lets understand with one example that is also more important for C Language.
Write a program to find that number is is positive, negative or zero. Here in program we have three answers while number is only one so we have to use 2) if... else if ... else conditional structure to solve this problem.

Lets understand with the example :

#include <stdio.h>
#include <conio.h>
void main()
{
    int no=5;
    if(no == 0 )
    {
        printf("Number is Zero");
    }
    else if(no > 0)
    {
        printf("Number is Positive");
    }
    else
    {
        printf("Number is Negative");
    }
}

Output of the program:
Number is positive

Extra Notes for Writing above Programs:
Above Programs can be written in 3 ways, If you want more clarification about this post your comments or sends message to us.

3) if ... else if....
Now Lets move to next Control structure 3) if ... else if....Its similar to 2) if...else if... else... but here last else condition is not present, I think you can do it for following types of questions.

Write a program to find out that Number is positive or Negative (Here i have omit the zero option)

4) Switch statement : Will be available soon

5) ? : (Conditional Operator) : Will be available soon


Note: All programs are developed and tested using Turbo C++ 3.0 under Windows XP. We just want to provide guidelines to the users. If you are using any other Compiler or other operating system they you need to modify this program as per your requirements. 

Friday, December 28, 2012

Basic Structure of C Program

Basic Structure of C Program : Sample Structure to Start with C Language

Following are the suggested structure for C Language.

1. Documentation
2. Pre-processor Directive
3. Global Declaration
4. Main Function
    4.1 Local Declaration
    4.2 Program Statements
5. User Defined Functions

C Language Structure Explained with Hello World Example
//===========================================
// Program Title : C Language Structure Explained with Hello World Example
// Program Description : This is sample C Program to show the basic structure for writing C Program.
// Written Date : 28-Dec-2012
// Author :Latest Technology Guide
//===========================================

#include <stdio.h> 
#include <conio.h>

int count;

void main()
{
    int i;
    clrscr();
    print_function();
    getch();
}
void print_function()
{
   printf("Hello....");
}



Note: All programs are developed and tested using Turbo C++ 3.0 under Windows XP. We just want to provide guidelines to the users. If you are using any other Compiler or other operating system they you need to modify this program as per your requirements. 

Thursday, December 27, 2012

List of Most Frequently Asked Questions of C Language

Most Frequently Asked Questions of C Language for BCA, MCA, PGDCA Students

1. Explain Major Components of C Program.
2. Write a note on Control Structure and Looping Structure with example.
3. Differentiate between getch() and getchar().
4. What is an Array ?, Write a note on 1D, 2D and 3D Array with Example.
5. What is Function ? Explain types of Function.
6. Define Variable. Whey there is a need of variable ?
7. List out the keywords of C Language.
8. Explain use of escape sequence with an example.
9. Compare use of if-else statement with the use of ? :  (Conditional operator) operator. When will you prefer to use ? : (conditional operator) ? Why ?
10. Explain nested IF Else Statement.
11. What are the different storage classes available in C Language?
12. What is the difference between break and continue?
13. Distinguish between while and do-while loop.
14. What do you understand by the scope and lifetime of a variable ? Describe various storage classes that a C Language Variable can have.
15. What is the significance of Array ?
16. Discuss dynamic memory allocation vs. Static memory allocation.
17. What do you mean by dynamic memory allocation? Explain how memory can be allocated dynamically.

Wednesday, December 26, 2012

Hello World Using C Language : C Language Program Example

Hello World Program Using C Language : C Language Program Example
First step to learn any programming language is to understand the syntax and semantics of the language. Lets Start with the basic very first Hello World Program Using C Language.

Hello World Example Using C Language


#include <stdio.h>

#include <conio.h>
void main()
{
    clrscr();
    printf("Hello...");
    getch();
}



Now you may be thinking !!!!
* how to start program ? 
* where to write ? 
* how to execute ? 
* what's output of the program ?

Lets Discuss how to do so.

Which Program or Software is required for writing and executing C Program ?

Turbo C++ Editor
Check out the Link for More Details about Turbo C Compiler ( Link )

What's next step after writing C Program ?

For Compilation : Select Compile Menu -> Compile.
Or 
Press Alt + F9
When you compile your program you will receive message as follow:

Error : 0, Warning : 0.

How to Run C Program (Shortcut to Run C Program):

Select Run Menu -> Run Option
or
(Ctrl + 9)

Output of above Program(Hello World Using C Language) Will be:

Hello...

If you still have questions, you can post comment, we will try to solve your query.




Note: All programs are developed and tested using Turbo C++ 3.0 under Windows XP. We just want to provide guidelines to the users. If you are using any other Compiler or other operating system they you need to modify this program as per your requirements. 

Tuesday, December 25, 2012

C Language Advantages and Disadvantages : C Language Features


: C Language Advantages :

C Language has list of advantages due to this it is very much popular language around the world and best suitable for the programmer to learn at the fist stage of the programming.

1. Procedure Oriented Language
C Language is procedure oriented language, Here user creates procedures or functions to execute their task. Procedure oriented language is very much easy to learn because it follows algorithm to execute your statements. To develop program using procedure oriented language, you need to draw/prepare algorithm and then start converting it into procedure or functions.

2. Lots of Libraries
C Language provides lots of functions which consist of system generated functions and user defined functions. C Compiler comes with list of header files which consist of many general functions which can be used to develop program. while programmer can also create function as per their requirements that is called as user generated/defined function.

3. Speed of Compilation
C compiler produces machine code very fast compared to other language compiler. C compiler can compile around 1000 lines of code in a seconds or two. One more benefit of the C Compiler is that it also optimize the code for faster execution.

4. Easy to Learn (Syntax is near to English Language)
C Language syntax is very easy to understand. It uses keyword like if, else, goto, switch, goto, main, etc. This kind of keyword we all are using in our day to day life to convey meaning or to get some decisions. 

5. Portable 
C Language setup is around 3-5 MB. So you can carry this language in your Floppy Drive or Pen Drive. Its very easy to install and operate, Again its output is exe file which can be executed in any computer without any other framework / software.


: C Language Disadvantages :

Every coin has two sides, as C Language has also some disadvantages. C Language has not any major disadvantages but some features is missing in the C Language, obviously that's why C Language is very much powerful now. 

1. Object Oriented Programming Features (OOPS)
Object Oriented Programming Features is missing in C Language, You have to develop your program using procedure oriented language only.

2. Run Time Type Checking is Not Available
In C Language there is no provision for run time type checking, for example i am passing float value while receiving parameter is of integer type then value will be changed, it will not give any kind of error message.

3. Namespace Feature
C does not provides namespace features, so you can't able to use the same variable name again in one scope. If namespace features is available then you can able to reuse the same variable name.

4. Constructor and Destructor is not available:
C does not provides object oriented features, so it don't have Constructor and Destructor features. Constructor and Destructor is used to construct object and destroy object. So in C Language you have to implement manually construction and destruction of the variable using function or by other means.


Monday, December 24, 2012

What is C Language | Define C Language | Introduction to C Language


What is C Language or Define C Language
C Language is a high level language developed by Dennis Ritchie during 1969. C Language is widely popular language because of their features. Most important features of the C Language is that: It is very much easy to understand and its similar to our English Language. Another features of C Language is that its very near to Machine or assembly language. (Machine can understand only machine language that is 0 and 1 while assembly language can have some instructions like add, sub, mul, etc.) before discussing the syntax and example of C Language lets discuss the background of the C Language and how C Language came into existence.

What is Language
Language is the way to explain something. For better understanding both person (speaker and listener) should require the knowledge of the same language or the method.

What is Machine Language
The language which can be understand by the Machine is called as machine language, Generally machine can understand only binary language that is know as 0 (off) and 1 (on). Its basically done by  preparing circuits.

What is Assembly Language
Assembly language is the language near to machine language and somewhat difficult to understand than C Language.