Wednesday, October 13, 2010

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 →