Wednesday, October 13, 2010

Graphics in C Language Tutorial

,


C and C++ Very Useful Language for Creating Application and programs. It provide a platform for developing a application with many functionality. But we know that mostly users attract Interface design. Design of application gives easy to use for users, he can user any application easily by UI. All programmings language can easily implement with graphic eg: C,C++,PHP,.net etc. Here we are learning about Graphic in C language with example, graphics.h library is used for create GUI (Graphic User Interface).
Learn with below example :


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


void main()
{
  int gd=DETECT,gm;
  clrscr();
  initgraph(&gd,&gm,"..\bgi");
  putpixel(100,100,WHITE);
  line(50,50,150,150);
  rectangle(200,200,400,400);
  closegraph();
  getch();
}
Read more →

What is Call By Value in C

,


In C Programmings we have different ways to parameter passing schemes such as Call by Value and Call by Reference.

Call By Value:- In the Call By Value the function is to be called by passing the value in the parameter of a function called. By default, C programming language uses call by value method to pass arguments.

There are the two type of a arguments which are passed in the function.

1) Formal Arguments:- Formal Arguments is the parameters which are passed in th function where the function body define.

2) Actual Arguments - Actual Arguments are those arguments which are passed in the function ,where the function called.

Why we use call By Value:-  In the Call By value when a function is called then the actual arguments are used and the value is passed in this argument.The control transfers to the function body then these values of actual arguments are being copied to the formal arguments and further processing start.

So there values are local to this block and does not take effect in the actual arguments. So that once the control get back to the main block then these values are completely vanished.These whole procedure are occurred in call by value. In below example we are swapping values with Call By Value


Example:-


/* function definition to swap the values */
void swap(int a, int b)
{
   int xx;
   xx = a;
   a = b;
   b = xx;
   return;
}

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

void swap(int a, int b);
int main ()
{
   int m = 20;
   int n = 10;
   printf("Before swap, value of m : %d\n", m );
   printf("Before swap, value of n : %d\n", n );

   swap(m, n);

   printf("After swap, value of m : %d\n", m );
   printf("After swap, value of n : %d\n", n );

   return 0;
}
Read more →

What is Call By Reference in C

,


In C Programmings we have different ways to parameter passing schemes such as Call by Value and Call by Reference.

Call by Reference:- Reference means the address not the actual value, the address of a variable or anything. So in the call by reference we use the address of a variable instead of passing the value of a variable in the function. When we want to use address we have to use the concept of pointer because pointers have the capability to store the addresses of a variable.

In the function calling the actual arguments can send the addresses of a variables. In the function body the format arguments are to be treated as the pointers in which the address of a actual arguments are to be stored. So that the value is passed on the actual arguments are to be get by address in the formal arguments and the operations are to performed on addresses and when the control get back on the main block. These value would be remain same. Hence, result is that the changes made in the arguments are permanent.

In below example we are swapping values with Call By Reference.

Example:-

/* function definition to swap the values */
void swap(int *a, int *b)
{
   int xx;
   xx = *a;
   *a = *b;
   *b = xx;
   return;
}


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

void swap(int *a, int *b);
int main ()
{
   int m = 60;
   int n = 80;
   printf("Before swap, value of m : %d\n", m );
   printf("Before swap, value of n : %d\n", n );

   swap(&m, &n);

   printf("After swap, value of m : %d\n", m );
   printf("After swap, value of n : %d\n", n );

   return 0;
}
Read more →

How to Use Union in C Programming

,



Union is a derived datatypes which group together a number of variable . Union is like a structure in c language which used to store one of a set of different types. A union holds the value of one-variable at a time. C Programming provide this facility which help to share same memory with different different variables. Accessing members of a union is via “.” member operator or, for pointers to unions, the -> operator.

union syntax in c :

union union_name
 {
  <datatype> first_element 1;
  <datatype> first_element 2;
  <datatype> first_element 3;
 };


Examples of  Union in C language

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

union tech
{
 int i;
 char name[50];
};

void main()
{
  union tech a;
  printf("\n\t Enter developer id : ");
  scanf("%d", &a.i);
  printf("\n\n\t Enter developer name : ");
  scanf("%s", &a.name);
  printf("\n\n Developer ID : %d", a.i);//Garbage
  printf("\n\n Developed By : %s", a.name);
  getch();
}
Read more →

C Storage Classes With Examples

,

Storage Class refers to the persistence of a variable (life time) and its scope within the program, that is, the portion of the program over which the variable is recognized.

The following types of storage-class specifications in C Language.

  • global
  • automatic or local
  • static
  • extern

The visibility of a variable determines how much of the rest of the program can access that variable. You can arrange that a variable is visible only within one part of one function, or in one function, or in one source file, or anywhere in the program.


Global Variables : A variable declared outside of any function is a global variable, and it is potentially visible anywhere within the program.

int sum=0; /* Declare global variables outside */

main()
{
 
}

Automatic or local variables : A variable declared within the braces {} of a function is visible only within that function; variables declared within functions are called local variables. You can use the keyword auto to declare automatic variables, but, however it is optional.

#include<stdio.h>

main()
{
 auto int n; /* Here the keyword auto is optional */
}

Static Variables : Static variables are defined within individual functions and therefore have the same scope as automatic variables, i.e. they are local to the functions in which they are declared. Unlike automatic variables, however, static variables retain their values throughout the life of the program.

static int count;

main()
{

}

External Variables : When you have multiple files and you define a global variable or function which will be used in other files also, then extern will be used in another file to give reference of defined variable or function.
Read more →

Static Function in C Language

,

By default any function that is defined in a C file is extern, which means they're visible across translation units. But when we use “static” keyword it restricts visibility of the function to the translation unit in which it's defined.

static int sfun(void)
{
  printf("I am a static function ");
}

when we want to restrict access to functions, we make them static. Static functions are functions that are only visible to functions in the same file and they are not callable from outside.

/* a.c */
static void sfun1(void)
{
  puts("sfun1 called");
}

And store following program in another file b.c

/* b.c  */
int main(void)
{
  sfun1();
  getchar();
  return 0;
}

we get the error “undefined reference to `sfun1′”. 
Read more →

Type Casting in C Language

,

Type casting is a way to convert a variable from one data type to another data type. Type Casting is force to convert one data type to another data type. The process of such local conversion is known as casting a value. The general form of cast is: (type-name) expression where type-name is one of the standard C data types.

Consider, for example, the calculation of salary of employee to engineers in a town.

salary = amount / days

Since amount and days are declared as integers in the program, the decimal part of the result of the division would be lost and salary would represent a wrong figure. This problem can be solved by converting locally one of the variables to the floating point as shown below:

salary = (float) amount / days

The operator (float) converts the amount to floating point for the purpose of evaluation of the expression.


Example 
Action 
X=(int) 8.5  8.5 is converted to integer by truncation. 
A=(int) 21.3 / (int) 4.5  Evaluated as 21/4 and the result would be 5. 
B=(double) sum/n  Division is done in floating point mode. 
Y= (int) (a+b)  The result of a+b is converted to integer. 
Z= (int) a+b  a is converted to integer and then added to b. 
P=cos(( double)x)  Converts x to double before using it. 
Read more →

What is Loops in C and C Plus Plus Programmings

,
Introduction

Loops in simple languages it's process of round (circle) of a work or activity. C and C Plus Plus also provide this function in c  and c plus plus. It provide a function which repeat your code in a block. It itterate you code and execute until work when condition will not be false.

Types of Loops

1. While Loop
2. Do While Loop
3. For Loop

While Loop:- While Loop is another type of loop which very useful for C Programmings.  In this loop in case we are using increament or decrement with condition. then atleast one time increament or decrement will be run. Either it's true or false condition.

Examples of Do While loop

# Include
main()

{

int a,i;

a=1;
while (x=10);
{

printf(x++);

}

}


Do While:-Do While Loop is another type of loop which very useful for C Programmings.  In this loop in case we are using increament or decrement with condition. then 1st Condition check after then increment and decrements is possible.


# Include
main()

{

do
{
while (x=10);
}
{

printf(x++);

}

};


For Looop:-For Loop is another type of loop which very useful for C Programmings.  In this loop in case we are using increament or decrement with condition. then we can intialize the value, test the condition and increament or decrement at a time

# Include
main()

{

int a;
for (i=0;i<=5;i++)
{

printf("%d",i);
}

};
Read more →

Data Types in C Language With Size

,

C has a concept of 'data types' which are used to define a variable before its use. C compilers support four fundamental data types, namely integer (int), character (char), floating point (float), and double-precision floating point (double). Like integer data type, other data types also offer extended data types such as long double and signed char.

Data Type

Data Type of a variable tell to the compiler
  • Name of variable
  • Which type of value that variable can hold
  • How much memory space it reserve
1. Primary data type or predefined data type or Fundamental data type - are those data type that are defined in c already.
Data Type
Used for
Size(in Byte)
Range
Format
Int Integer 2 -32768 to 32767
%d
Char Character 1 -128 to 127
%c
Float Single precision floating point number 4 -3.4e38 to +3.4e38
%f
Double double precision floating point number 8 -1.7e308 to +1.7e308
%lf
Void - - -
0


Data Type Modifiers –There are four modifiers available.
  • Signed
  • Unsigned
  • Short
  • Long
Data Type
Size(in Byte)
Range
Format
signed char  1 -128 to + 127
%c
unsigned char  1 0 to 255
%c
signed int  2 -32768 to +32767
%d
unsigned int  2 0 to 65535
%u
short signed int  2 -32768 to +32767
%d
short unsigned int  2 0 to 65535
%u
long double  10 -1.7e4932 to +1.7e4932
%Lf

2. Derived data type - are those data type that are derived by predefined data type.

Example-Array, pointer


3. User defined data type - are those data type that are defined by the user itself.

Example-struct, union

Read more →

File Handling in C Language

,

File handling means one can perform operations like create, modify, delete etc on system files. You want to read from or write to one of the files you're working with, you identify that file by using its file pointer.

File Handling Funtions :

fopen() : The fopen() function is used to open a file and associates an I/O stream with it.

The mode string "r" indicates reading. Mode "w" indicates writing, so we
could open. The third major mode is "a" for append. mode is "+" for read and write. Mode "b" character to indicate that you want to do “binary''

fread() and fwrite() : The functions fread/fwrite are used for reading/writing data from/to the file opened by fopen function.

fseek() : The fseek() function is used to set the file position indicator for the stream to a new position.

fclose() : The fclose() function first flushes the stream opened by fopen() and then closes the underlying descriptor.

You declare a variable to store a file pointer like this:

FILE *fp;

If you were reading from one file and writing to another you might declare an input file pointer and an output file pointer:

FILE *ifp, *ofp;

One thing to beware of when opening files is that it's an operation which may fail, file might not exist, or it might be protected against reading or writing. Fopen returns a null pointer if it can't open the requested file, and it's important to check for this case before going off and using fopen's return value as a file pointer. eg:

ifp = fopen("input.dat", "r");
if(ifp == NULL)
{
 printf("can't open file\n");
 exit or return
}


Example : Writing a data to the file

#include <stdio.h>
main( )
{
  FILE *fp;
  char stuff[25];
  int index;
  fp = fopen("TENLINES.TXT","w");
  strcpy(stuff,"This is an example line.");
  for (index = 1; index <= 10; index++)
  fprintf(fp,"%s Line number %d\n", stuff, index);
  fclose(fp);
}

Read more →

String In C Language

,


The string in C programming language are represented by arrays of characters. The end of the string is marked with a special character, the null character, which is simply the character with the value 0. Whenever we write a string, enclosed in double quotes, C automatically creates an array of characters for us, containing that string, terminated by the \0 character.

For example, we can declare and define an array of characters, and initialize it with a string constant:

char string[ ] = "Hellopg";



Print With Example :

#include <stdio.h>
int main ()
{
   char Value[8] = {'H', 'e', 'l', 'l', 'o','p','g', '\0'};
   printf("Greeting message: %s\n", Value );
   return 0;
}

 
 Function  Purpose
 strcpy(s1, s2); Copies string s2 into string s1.
 strcat(s1, s2); Concatenates string s2 onto the end of string s1.
 strlen(s1); Returns the length of string s1.
 strcmp(s1, s2); Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
 strchr(s1, ch); Returns a pointer to the first occurrence of character ch in string s1.
 strstr(s1, s2); Returns a pointer to the first occurrence of string s2 in string s1.

Copy Function :

#include <string.h>

char string1[ ] = "Hello, world!";
char string2[20];
strcpy(string2, string1);

Try all function and observe result.
Read more →

How structures are to be used in c

,
Read more →

Thursday, October 7, 2010

What is Inheritance?

,
What is Inheritance?

Inheritance is one of the important feature of the OOP.Inheritance is the method by which objects of one class gets the properties of another class.

Advantages of Inheritance?

Sometimes we need to repeat the code or we need repeat the whole class properties. So It helps in various ways.

1.) It saves memory space.

2.) It saves time.

3.) It will remove frustration.

4.) It increases reliability of the code

5.) It saves the developing and testing efforts.

Why we use Inheritance?

To increase the reusability of the code and to make further usable for another classes. We use the concept of inheritance.

How Inheritance can be achieved?

In the inheritance there will be a parent child relationship .There is a class called in which all the properties are defined and generally,it is refered as a base class.There is also an another class in which is derived from the existing one are known as a derived class.

Types of Inheritance

There are Five types of Inhertance found in Object Oriented Programming.

1.)Single Inheritance

2.)Multiple Inheritance

3.)Hierarchical Inheritance

4.)Multiple Inheritance

5.)Hybrid inheritance
Read more →