Sunday, 2 November 2014

find area and perimeter of rectangle

Program to find area and perimeter of rectangle

Let’s see how to write this C program

#include<stdio.h>
#include<conio.h>
int main()
{
int length,width;
int perimeter,area;
printf("enter length and width of rectangle \n");
scanf("%d %d",&length,&width);

perimeter=2*(length+width);
area=length*width;

printf("\n perimeter is = %d \n area is = %d",perimeter,area);

getch();
return 0;
}
Explanation of the program

int length,width;
int perimeter,area;
here we are declaring four variables  as
length to store length of a rectangle.
width to store width of a rectangle.      
perimeter to store perimeter after calculating it. 
area to store area after calculating it. 

scanf("%d %d",&length,&width);
use to store the user desired values in the variables of length and width.

perimeter=2*(length+width);
basic mathematical expression to calculate perimeter of the rectangle it is equivlant to { preimeter=2(length+width) }.

area=length*width;
basic mathematical expression to calculate perimeter of the rectangle it is equivlant to { area=length x width }.

printf("\n perimeter is = %d \n area is = %d",perimeter,area);
this statement will going to print “perimeter is = “ perimeter_value then change line and print “area is = “ area_value

%d
This tells the compiler that we are going to display or read the integer number.

\n  or endl
This signifies the endline or we can say change line.
In this program it is responsible to change the line after printing the perimeter of rectangle.


Getch(); and return 0;
We already discussed this click here to move back.


Pictures of program:

Step 1:  Create the program:-




Step 2: output 



Note:  if any of the programmer is facing the problem of reappering of the previous screen again and again

He can use the function clrscr();
It present in the conio.h header file
After declaring the variables

Sunday, 26 October 2014

find area of circle


Program to find area of circle

Let’s see how to write this C program

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float area,radius;
const float pi=22/7;
printf("enter the radius of circle ");
scanf("%f",&radius);

area=2*pi*pow(radius,2) ;
//area=2*pi*radius*radius;

printf("area of the circle is = %f",area);
getch();
return 0;
}


Explanation of the program


float area,radius;
Float is a kind of data type which is of size ‘4bytes’.
it is use store the decimal values or we can say real number.
we are declaring area and radius as the variable of float datatype.

const float pi=3.1428;
Here we are declaring a variable named as pi with the constant value of 3.1428.
Const is keyword which specify that variable pi is a constant so its value can’t be changed in run time of the program.

scanf("%f",&radius);
It is use to receive the the value for variable radius.
As we know that the radius is of datatype float so we need to use “%f” to store that float value.

area=2*pi*pow(radius,2);
This is a mathematical expression equivalent to ‘area=2*π*radius2 ‘.
We can also write it as     area=2*pi*radius*radius;

pow(radius,2)
pow() is a power function in the header file of math.h.
which accepts two parameters one is the number and another is the power of that number.
Eg:- if we want to write
A2  then we write it as pow(A,2);
A4  then we write it as pow(A,4);
2n  then we write it as pow(2,n);

printf("area of the circle is = %f",area);
This statement will display the “area of the circle is = “ followed by the value stored in the variable area.

%f
This tells the compiler that we are going to display or read the real number.

Getch(); and return 0;
We already discussed this click here to move back.


Pictures of program:

Step 1:  Create the program:-





Step 2: output 




Note:  if any of the programmer is facing the problem of repapering of the previous screen again and again

He can use the function clrscr();
It present in the conio.h header file
After declaring the variables

Sunday, 19 October 2014

print addition of 2 user numbers


Program to print addition of 2 user numbers


Let’s see how to write this C program

#include<conio.h>
#include<stdio.h>
int main()
{
int fnum,snum,result;
printf("enter first number ");
scanf("%d",&fnum);
printf("enter second number ");
scanf("%d",&snum);

result=fnum+snum;

printf("result =%d",result);
getch();
return 0;
}


Explanation of the program


int fnum,snum,result;
Here we initialize our 3 variables named as  fnum,snum and result. These variable currently holds no value.

printf("enter first number ");
use to print as it is on the screen “enter first number“.

scanf("%d",&fnum);
allow user to enter any desired value in fnum variable.

printf("enter second number ");
use to print as it is on the screen “enter second number“.

scanf("%d",&snum);
allow user to enter any desired value in fnum variable.

result=fnum+snum;;
in this statement we add up the values of fnum and snum and then we pass that value to the variable result. Now result is holding up the addition of fnum and snum variable.

printf("result is = %d",result);
this will going to display the value stored in the variable result.
In this we combine it with the string as firstly it will display the 
“result is = “
Then the value of result is being displayed.

%d
This tells the compiler that we are going to display or read the value of integer type.

Getch(); and return 0;
We already discussed this click here to move back.


Pictures of program:

Step 1:  Create the program:-






Step 2: output 



Note:  if any of the programmer is facing the problem of reappearing of the previous screen again and again

He can use the function clrscr();
It present in the conio.h header file
After declaring the variables

Sunday, 12 October 2014

print addition of 2 default numbers


Program to print addition of 2 default numbers


Let’s see how to write this C program

#include<conio.h>                   
#include<stdio.h>
int main()                                   
{
int first= 10,second=12;       
int result;   
              
result = first+second;

printf("result is = %d",result);
getch();
return 0;
}

Explanation of the program

Int first=10,second=12;
This statement we going to initialize the two variables in the memory named as first and second which holds the default values of 10 and 12 respectively.

int result;
Here we initialize our 3rd variable named as result. This currently holds no value

result=first+second;
in this statement we add up the values of first and second and then we pass that value to the variable result. Now result is holding up the addition of first and second variable.

printf("result is = %d",result);
this will going to display the value stored in the variable result.
In this we combine it with the string as firstly it will display the 
“result is = “
Then the value of result is being displayed.

%d
This tells the compiler that we are going to display the value of integer variable

Getch()and return 0
We already discussed this click here to move back.

Pictures of program:

Step 1:  Create the program:-




Step 2: output 



Note:  if any of the programmer is facing the problem of reappearing of the previous screen again and again

He can use the function clrscr();
It present in the conio.h header file
After declaring the variables

Sunday, 5 October 2014

Printing your name on the screen using gets() in C language

Printing your name on the screen using gets() in C language



Let’s see how to write this C program

#include<conio.h>
#include<stdio.h>
int main()
{
char myname[20];
printf(“enter your name : “);
gets(myname);
printf(“my name is %s“,myname);
getch();
return 0;
}


Details about scanf() and gets()

As you can see this program is almost similar to the previous program we have done.

The difference is that in this program we have used the gets() instead of scan() function to get input from the user.

Gets() is the function under the stdio.h header file.

Gets() can also be capable of receiving blank space inputs as scanf() is not

It is specially made for character type inputs on other hand scanf() is better for receiving the numeric inputs .
                            


Explanation of the program


As it is similar to the previous program so. I start explaining it from little ahead

Whatever user is going to type it will be store in the variable myname as a string or character.

And then we going to display it on the screen similarly as we have done it into our previous program using printf() function.


Pictures of the program


Step1: write a program




Step2: compile and run
Now I am not going to show how to run the program I have tell all that in my previous program or you can move to my first program for that.




Step3: output




Now here you can enter any of the string may be with or without blank space

With the help of gets() function we can also receive the character inputs like blank space.

You may increase the size of myname variable to hold the string of larger size

Example:
char myname[I];
//here I = any positive integer        

Note:  if any of the programmer is facing the problem of reappearing of the                      previous screen again and again
           He can use the function clrscr();
           It present in the conio.h header file
           After declaring the variables


Example in this program

……
………..
char myname[20];
clrscr();
printf(“enter your name : “);
…………
……………….


Sunday, 28 September 2014

Printing your name on the screen in C language

Printing your name on the screen in C language


Let’s see how to write this C program

#include<conio.h>
#include<stdio.h>
int main()
{
char myname[20];
printf(“enter your name : “);
scanf(“%s”,&myname);
printf(“my name is %s“,myname);
getch();
return 0;
}


Explanation of the above program


Preprocessors, header files, main()
We had discussed it in our previous program.

char myname[20] ;
This statement declares a variable myname of type character whose size is of 20. Which means myname variable can store up to 20 characters.

Printf()
This is a kind of a function which is use to display something on the screen
What so ever is written in the double quotes “ ”

Scanf()
This is also a function which is similar to print().
It is used to get the input from the user and stores it into the particular variable

%s
Signifies the character format of values we need to store in the variable
And this is also used to display the value of character variable

Getch()
This function is found in the conio.h library/header file   
This will going to hold the screen until any button is pressed from the keyboard

Return 0
This will going to return the null value to the main() as main() is declared in the int data type so it has to return a integer value to main()



Pictures of program:

Step 1:  Create the program:-


Step 2: compile and run:



Step 3: output 

In the output you need to enter only your first name and press enter



Step 4: run the program again and enter your full name in it



In this case it will only going to show the character entered before space-bar
Next we learn how to overcome this problem

Note:  if any of the programmer is facing the problem of reappearing of the                      previous screen again and again
           He can use the function clrscr();
           It present in the conio.h header file
           After declaring the variables

Example in this program
……
………..
char myname[20];
clrscr();
printf(“enter your name : “);
…………

……………….

Sunday, 21 September 2014

First C program (Hello, World)

First C language Program

Lets see how to write a simple c program 

#include< stdio.h>
#include< conio.h>
int main()
{
 printf("Hello,World");
 getch();
 return 0;
}

Different parts of C program.

        Pre-processor

        Header file

        Function

        Variables

        expression

        Comment

Pre-processor 

#include, the first word of any C program. It is also known as pre-processor. The main work of pre-processor is to initialize the environment of program, i.e to link the program with the header file.

 

 

Header file

Header file is a collection of built-in functions that help us in our program. Header files contain definitions of functions and variables which can be incorporated into any C program by pre-processor #include statement. Standard header files are provided with each compiler, and cover a range of areas like string handling, mathematical functions, data conversion, printing and reading of variables.
To use any of the standard functions, the appropriate header file must be included. This is done at the beginning of the C source file.
For example, to use the printf() function in a program, the line #include< stdio.h> is responsible.

 

 

main() function

main() function is a function that must be used in every C program. A function is a sequence of statement required to perform a specific task. main() function starts the execution of C program. In the above example, int in front of main() function is the return type of main() function. we will discuss about it in detail later. The curly braces { } just after the main() function encloses the body of main() function.

Compile and Run

There are many different ways to compile and run a C program. All that is required is a C compiler. We will recommend you to use turbo c IDE, oldest IDE for c programming. It is freely available over internet and is good for a beginner.


Step 1 : Open turbo C IDE(Integrated Development Environment), click on File and then click on New


first c program
Step 2 : Write the above example as it is 

Write a C program

Step 3 : Click on compile or press Alt+f9 to compile the code

Compiling a C program

Step 4 : Click on Run or press Ctrl+f9 to run the code


Running a C program
Step 5 : Output
Output of C program
that's all you need to do .
run your first C language program
i hope it will run fine on your systems.

Wednesday, 17 September 2014

Basic structure of C program

Basic structure of C program


Let’s see what the basic structure is


#include<header_file(1).h>
#include<header_file(2).h>
….
..                                                  //all header files are to declared first
….
#include<header_file(n).h>

main()     // main()function is the first function which is executed by a C                                      program

{                                                        //start of the program
Statement(1);
Statement(2);
Statement(3);
Statement(4);
…                           //N number of statements can be written
….                         //All C Statements are written within main function
……
…..
Statement(n);


}                                                 //end of the program


Sunday, 14 September 2014

About Preprocessors

The C Preprocessor is not part of the compiler, but is a separate step in the compilation process. In simplistic terms, a C Preprocessor is just a text substitution tool and they instruct compiler to do required pre-processing before actual compilation. We'll refer to the C Preprocessor as the CPP.
All preprocessor commands begin with a pound symbol (#). It must be the first nonblank character, and for readability, a preprocessor directive should begin in first column. Following section lists down all important preprocessor directives:
DirectiveDescription
#defineSubstitutes a preprocessor macro
#includeInserts a particular header from another file
#undefUndefines a preprocessor macro
#ifdefReturns true if this macro is defined
#ifndefReturns true if this macro is not defined
#ifTests if a compile time condition is true
#elseThe alternative for #if
#elif#else an #if in one statement
#endifEnds preprocessor conditional
#errorPrints error message on stderr
#pragmaIssues special commands to the compiler, using a standardized method

Preprocessors Examples

Analyze the following examples to understand various directives.
#define MAX_ARRAY_LENGTH 20
This directive tells the CPP to replace instances of MAX_ARRAY_LENGTH with 20. Use#define for constants to increase readability.
#include <stdio.h>
#include "myheader.h"
These directives tell the CPP to get stdio.h from System Libraries and add the text to the current source file. The next line tells CPP to get myheader.h from the local directory and add the content to the current source file.
#undef  FILE_SIZE
#define FILE_SIZE 42
This tells the CPP to undefine existing FILE_SIZE and define it as 42.
#ifndef MESSAGE
   #define MESSAGE "You wish!"
#endif
This tells the CPP to define MESSAGE only if MESSAGE isn't already defined.
#ifdef DEBUG
   /* Your debugging statements here */
#endif
This tells the CPP to do the process the statements enclosed if DEBUG is defined. This is useful if you pass the -DDEBUG flag to gcc compiler at the time of compilation. This will define DEBUG, so you can turn debugging on and off on the fly during compilation.

Predefined Macros

ANSI C defines a number of macros. Although each one is available for your use in programming, the predefined macros should not be directly modified.
MacroDescription
__DATE__The current date as a character literal in "MMM DD YYYY" format
__TIME__The current time as a character literal in "HH:MM:SS" format
__FILE__This contains the current filename as a string literal.
__LINE__This contains the current line number as a decimal constant.
__STDC__Defined as 1 when the compiler complies with the ANSI standard.
Let's try the following example:
#include <stdio.h>

main()
{
   printf("File :%s\n", __FILE__ );
   printf("Date :%s\n", __DATE__ );
   printf("Time :%s\n", __TIME__ );
   printf("Line :%d\n", __LINE__ );
   printf("ANSI :%d\n", __STDC__ );

}
When the above code in a file test.c is compiled and executed, it produces the following result:
File :test.c
Date :Jun 2 2012
Time :03:36:24
Line :8
ANSI :1

Preprocessor Operators

The C preprocessor offers following operators to help you in creating macros:

Macro Continuation (\)

A macro usually must be contained on a single line. The macro continuation operator is used to continue a macro that is too long for a single line. For example:
#define  message_for(a, b)  \
    printf(#a " and " #b ": We love you!\n")

Stringize (#)

The stringize or number-sign operator ('#'), when used within a macro definition, converts a macro parameter into a string constant. This operator may be used only in a macro that has a specified argument or parameter list. For example:
#include <stdio.h>

#define  message_for(a, b)  \
    printf(#a " and " #b ": We love you!\n")

int main(void)
{
   message_for(Carole, Debra);
   return 0;
}
When the above code is compiled and executed, it produces the following result:
Carole and Debra: We love you!

Token Pasting (##)

The token-pasting operator (##) within a macro definition combines two arguments. It permits two separate tokens in the macro definition to be joined into a single token. For example:
#include <stdio.h>

#define tokenpaster(n) printf ("token" #n " = %d", token##n)

int main(void)
{
   int token34 = 40;
   
   tokenpaster(34);
   return 0;
}
When the above code is compiled and executed, it produces the following result:
token34 = 40
How it happened, because this example results in the following actual output from the preprocessor:
printf ("token34 = %d", token34);
This example shows the concatenation of token##n into token34 and here we have used both stringize and token-pasting.

The defined() Operator

The preprocessor defined operator is used in constant expressions to determine if an identifier is defined using #define. If the specified identifier is defined, the value is true (non-zero). If the symbol is not defined, the value is false (zero). The defined operator is specified as follows:
#include <stdio.h>

#if !defined (MESSAGE)
   #define MESSAGE "You wish!"
#endif

int main(void)
{
   printf("Here is the message: %s\n", MESSAGE);  
   return 0;
}
When the above code is compiled and executed, it produces the following result:
Here is the message: You wish!

Parameterized Macros

One of the powerful functions of the CPP is the ability to simulate functions using parameterized macros. For example, we might have some code to square a number as follows:
int square(int x) {
   return x * x;
}
We can rewrite above code using a macro as follows:
#define square(x) ((x) * (x))
Macros with arguments must be defined using the #define directive before they can be used. The argument list is enclosed in parentheses and must immediately follow the macro name. Spaces are not allowed between and macro name and open parenthesis. For example:
#include <stdio.h>

#define MAX(x,y) ((x) > (y) ? (x) : (y))

int main(void)
{
   printf("Max between 20 and 10 is %d\n", MAX(10, 20));  
   return 0;
}
When the above code is compiled and executed, it produces the following result:
Max between 20 and 10 is 20