Sunday, November 3, 2013

Multidimensional Array in C Programming

,

Array is a variable that can hold more than one value of same type. You specify which of the several values you're referring to at any given time by using a numeric subscript. (Arrays in programming are similar to vectors or matrices in mathematics.)

Arrays are of two types:


Multidimensional arrays : When we create arrays of arrays known as multidimensional arrays. C Programming provide to create multidimensional array.A two dimensional array looks like matrix form.

Syntax :

<array_type> <array_name> [<number of rows>][<number of columns];

Example :

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

int main ()
{
   int y[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
   int i, j;

   for ( i = 0; i < 5; i++ )
   {
      for ( j = 0; j < 2; j++ )
      {
         printf("y[%d][%d] = %d\n", i,j, y[i][j] );
      }
   }
   return 0;
}

Compile above and observe output.
Read more →

One Dimensional Array in C Programming

,

Array is a variable that can hold more than one value of same type. You specify which of the several values you're referring to at any given time by using a numeric subscript. (Arrays in programming are similar to vectors or matrices in mathematics.)

Arrays are of two types:


One Dimensional Array : Size of array defines the number of elements in an array. Each element of array can be accessed and used by user according to the need of program.

Syntax :

<array_type> <array_name> [<number of elements in array>];

Example: If the user want to store salary of 100 employee. This can be done by creating 100 variable individually but, by array it's now become simple.

int salary[100];


The following program initializes an integer array with five values and prints the array.

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

int main()
{
     int a[]={9,8,7,6,5};
     int i;
     clrscr();
     printf("Array elements are\n");
     for(i=0;i<=4;i++)
     printf("%d\n",a[i]);
     getch();
     return 0;
}
Read more →

Saturday, November 2, 2013

Null Pointer in C Language

,

A Pointer is variable which contains the address in memory of another variable.value of a pointer variable is a pointer to some other variable. There is one other value a pointer may have: it may be set to a null pointer. A null pointer is a special pointer value that is known not to point anywhere. What this means that no other valid pointer, to any other variable or array cell or anything else, will ever compare equal to a null pointer. A void pointer is declared like a normal pointer, using the void keyword as the pointer’s type.

To initialize a pointer to a null pointer (It is also possible to refer to the null pointer by using a constant 0), you might use code like

#include <stdio.h>
int *ip = NULL;

and to test it for a null pointer before inspecting the value pointed to, you might use code like

if(ip != NULL)
printf("%d\n", *ip);


Example :

#include <stdio.h>

int main()
{
 if(!NULL)
 {
  printf("I know preprocessor");
 }
 else
 {
  printf("I don't know preprocessor");
 }
 return 0;
}

Read more →

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 →