Saturday, January 26, 2013

C Preprocessor

What is C Preprocessor?

It is a program that processes our source program before it is passed to the compiler.

Preprocessor command also known as directives.

Each preprocessor directory is begin with a # symbol.

It is can be placed anywhere in the program but are most often placed at the beginning of a program.

The C program is often known as 'Source Code'. The preprocessor works on the source code and creates 'Expanded Source Code'.
If the source code is stored in a file say MYPROG.C, then the expanded source code gets stored in a file MYPROG.I. It is this expanded source code that is sent to the compiler for compilation.

Example of some preprocessor directives as:

  • Macro Expansion
  • Conditional compilation
  • File inclusion
  • Miscellaneous directives

We discuss all above preprocessor in detailed one by one.

1 and 0 Pyramid


Q. Write a C program to print the following 1, 0, and 1 number pyramid as:

1
01
101
0101
10101

Ans.

/*c 1 and 0 triangle program*/
#include<stdio.h>
int main()
{
 int r,c,num,v;
 printf("Enter number of rows: ");
 scanf("%d"&num);
 for(r=1; r<=num; r++)
 {
  for(c=1; c<=r; c++)
  {
    if( (r%2) == 0 )
    {
      v = (c%2==0) ? 1 : 0 ;
      printf("%d",v);
    }
    else
    {
      v = (c%2==0) ? 0 : 1 ;
      printf("%d",v);
    }
  }
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

Output of 1 and 0 pyramid C program
Figure: Screen shot for 1 and 0 Number pyramid C program

Common php MySql Query Function


Now we learn about common function that are used in various database queries like as connecting database, create, select, update, delete.

Let's start to make all these function in php file say, mysql.php


mysql.php


<?php

class db_sql
{
 var $totrec;
 var $recid;
 var $fetch_rec;
 var $m_query;
 var $query;
 var $row;

 //connection function

function db_connect($server = DB_SERVER, $username = DB_SERVER_USERNAME, $password = DB_SERVER_PASSWORD, $database = DB_DATABASE, $link = 'db_link')
{
 global $$link;
 $$link =mysql_connect($server, $username, $password);
 if($$link)
 {
   $$link=mysql_select_db($database);
     if(!$$link)
     {
        $msg="Can't connect from Database";
        $this->db_error($msg,mysql_errno(),mysql_error());
    }
  }
  else
  {
      $msg="Can't connect to MYSQL Server";
      $this->db_error($msg,mysql_errno(),mysql_error());
  }
  return $$link;
}


//database close function

function db_close($link = 'db_link')
{
  global $$link;
  return mysql_close($$link);
}

// Function to print database error message, number and query

function db_error($query, $errno, $error) 
{
  die($errno .  $error . $query);
}


// Insert query function

function db_insert($table,$data,$condition='', $link = 'db_link'
{
   reset($data);
   $query = 'insert into ' . $table . ' (';
          
         whilelist($columns, ) = each($data) )
             {
             $query .= $columns . ', ';
           }
          $query = substr($query, 0, -2) . ') values (';
          reset($data);
         while (list(, $value) = each($data)) 
           {
            switch ((string)$value) 
               {
            case 'now()':
                   $query .= 'now(), ';
                   break;
            case 'null':
                  $query .= 'null, ';
                  break;
            default:
                  $query .= '\'' .$value. '\', ';
                    break;
          }

        }

       $query = substr($query, 0, -2) . ')';
     
       $this->db_query($query);

       $this->recid=mysql_insert_id();

       return $this->recid;

  }


// Update query function

function db_update($table,$data,$condition = '', $link = 'db_link')
{
  $query = 'update ' . $table . ' set ';
  while (list($columns, $value) = each($data)) 
  {
    if($columns=='inccredits')
    {
      $query .= 'credits'' ='.$value.', ';
      continue;
    }
     switch ((string)$value)
   {
      case 'now()':
          $query .= $columns . ' = now(), ';
          break;
      case 'null':
          $query .= $columns .= ' = null, ';
          break;
      default:
           $query .= $columns . ' = \'' .$value . '\', ';
           break;
   }
 }
 $query = substr($query, 0, -2) . ' where ' . $condition;
 $this->db_query($query);
 return $this->m_qry;
}


// Insert Update query function   

function insert_update($tblname,$arrfield,$action=" insert into ",$wherecondition="")
{
  if (is_array($arrfield))
  {
    while(list($column,$value) = each($arrfield))
     {
       @$query.= ",".$column."='".$value."'";
    }
   }
  $query1 = substr($query,1);
   $sql = "$action $tblname set $query1 $wherecondition";
   $res = $this->db_query($sql);
   $insertid = mysql_insert_id();
   return $insertid;
}

   
//Delete record function

function db_delete($table,$condition = '', $link = 'db_link')
{
  $query='Delete from '. $table .' where '. $condition;
   $this->db_query($query);
   return $this->m_qry;
}


//database common query function

function db_query($query)
{
   $this->query=$query;
   $this->m_qry=mysql_query($this->query) or 
   $this->db_error($query,mysql_errno(),mysql_error());
}


//database fetch record

function db_num_rows()
{
  return $this->totrec=mysql_num_rows($this->m_qry); 
}


//database record fetch function

function db_fetch_array()
{
   return mysql_fetch_array($this->m_qry, MYSQL_ASSOC);
}

}

//making object

 $obj1= new db_sql();
 $obj2= new db_sql();
 $obj4= new db_sql();
 $obj6= new db_sql();

 $obj1->db_connect();


?>

Now let's create index.php file that contains above mysql.php file.

Friday, January 25, 2013

Global variable V/S Local variable

Local Variable v/s Global Variable

The difference between local variable and global variable as following:

Local VariableGlobal Variable
 A variable which is defined inside a function is known as local variable.  A variable which is defined outside a function is known as global variable.
 The scope of local variable is limited to the function in which they are declared.  The scope of global variable is throughout in the porgram
 The memory is allocated to local variable each time, the control enters
the function and released when the control leaves the function.
 Gloval variable remains in memory throughout the execution of the program.
 They can be used with automatic, static and register storage classes.  They can be used with only external storage class.
 Example of local variable:
  #include<stdio.h>
  int i = 10;
  int main()
  {
     int x = 5;
     printf("%d",x);
  }
  Here, x is a local variable
  Example of local variable:
  #include<stdio.h>
  int i = 10;
  int main()
  {
     int x = 5;
     printf("%d",i);
  }
  Here, i is a global variable so we can use i's value anywhere in the program.
Table: Local variable v/s Global variable

Related Article:

  1. Scope of the variable V/S Life of the variable

Scope of the variable V/S Life of the variable

Q. What is meaning of scope of the variable and life of the variable?
Explain with example.

Ans.

scope of the variable

The range of code in program over which a variable has a meaning is called a scope of the variable.

In other words, the scope of variable decides as to how it can be accessed by the different parts of the program.

Following are the types of scopes:

  1. Local scope
  2. Global scope
  3. Function scope
Life of the variable

Life of the variable means how long would the variable exist.


Related Article:

0 and 1 Triangle

Q. Write a C program to print the following 0 and 1 number triangle:

1
01
010
1010
10101

Ans.

/*c 0 and 1 number triangle program*/
#include<stdio.h>
int main()
{
 int r,c,num,v1=1;
 printf("Enter number : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(c=1; c<=r; c++)
  {
    printf("%d",v1);
    if(v1==1)
       v1=0;
    else
       v1=1;
  }
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

Output of 0 and 1 Number Triangle C program
Figure: Screen shot for 0 and 1 number triangle C porgram


Thursday, January 3, 2013

Number Character Triangle

Q. Write a C program to print the following number character triangle:

1      a
21     ba
321    cba
4321   dcba
54321  edcba

Ans.

/*c program for number character pyramid*/
#include<stdio.h>
int main()
{
 int num,n,r,c,sp;
 char p,rch='a';
 printf("Enter no of rows: ");
 scanf("%d", &num);
 n=num;
 for(r=1; num>=r; r++,n--,rch++)
 {
  for(c=r; c>=1; c--)
    printf("%d",c);
  for(sp=0; sp<=n ;sp++)
    printf(" ");
  for(p=rch; p>='a'; p--)
    printf("%c",p);
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

Output of number-character pyramid C program
Figure: Screen shot for number-character pyramid C program