Union data type


Union:
  • The union is a derived data type and it is declared like structure.
  • The difference between union and structure is in terms of storage.
  • In structure, each member has its own storage location, whereas all the members of the union use the same location, although the union may contain many members of different types.
  • When we use union the compiler allocates a piece of storage that is large enough to hold.
For example,
Name = 30 byte
Roll_num= 2 byte(int data tpe alway take 2 bytes)
Total bytes=32

The structure was taking 32 bytes but the union take the only a maximum number of 30 bytes and use that place.

Memory allocation: 


Syntax:

union union _name
{
data_type variable_name 1;
data_type variable_name 2;
.................................
.................................
data_type variable_name  n;
};
union union _name short_name;

Example program union:

#include<stdio.h>
#include<conio.h>
//Union declaration
union std
{
int roll_num,t,e,m,s,so,tot;
char name[30];
float avg;
};
union std s;//union short name declare
void main()
{
clrscr();
printf("\nEnter student roll number : ");
scanf("%d",&s.roll_num);//calling structure varaiable via 
                                             union short name
printf("\nEnter student name : ");
scanf("%s",&s.name);
printf("\nEnter student five subject mark: ");
scanf("%d %d %d %d %d",&s.t,&s.e,&s.m,&s.s,&s.so);
s.tot=s.t+s.e+s.m+s.s+s.so;
printf("\nTotal mark: %d ",s.tot);
s.avg=s.tot/5;
printf("\nAverage mark: %f ",s.avg);
getch();
}

Output:
Share:

Structure data type


Structure:
  • It is used to store different data items under a common name.
  • It is opposite to the array because the array stored only similar data items.
Rules:
  • A structure must end with a semicolon.
  • Usually, a structure appears at the top of the main program.
  • Each structure must be terminated.
  • The structure variables must be accessed with structure variable with the dot operator.
Syntax:

struct structure_name
{
data_type variable_name 1;
data_type variable_name 2;
.................................
.................................
data_type variable_name  n;
};
struct structure_name  short_name;

Example program structure:

#include<stdio.h>
#include<conio.h>
//Structure declaration
struct std
{
int roll_num,t,e,m,s,so,tot;
char name[30];
float avg;
};
struct std s;//strucure short name declare
void main()
{
clrscr();
printf("\nEnter student roll number : ");
scanf("%d",&s.roll_num);//calling structure varaiable via strcure 
                                             short name
printf("\nEnter student name : ");
scanf("%s",&s.name);
printf("\nEnter student five subject mark: ");
scanf("%d %d %d %d %d",&s.t,&s.e,&s.m,&s.s,&s.so);
s.tot=s.t+s.e+s.m+s.s+s.so;
printf("\nTotal mark: %d ",s.tot);
s.avg=s.tot/5;
printf("\nAverage mark: %f ",s.avg);
getch();
}

Output:

User defined data types:
‘C’ provides a capability that enables the programmer to assign an alternate name to a data type.
This is done with a statement known as a typedef.
Syntax:

 typedef  type dataname;
dataname short_name;

Example program for typedef:

#include<stdio.h>
#include<conio.h>
#define D 7
void main()
{
typedef int weeks;//declare typedef
weeks wk;//new datatype called weeks
clrscr();
printf("\nEnter weeks:");
scanf("%d",&wk);
printf("\nThe number of days %d in %d weeks",wk*D,wk);
getch();
}

Output:

Nested structure:
  • If a structure contains more than one structure as its members, is known as a nested structure i.e. structure within another structure.
  • It is used to reliability of the program by reading the complexity.

Note: Members are nothing but, if variables are function inside of the main program called members.
Syntax for nested structure members can be accessed as:

Outer_structure_name.inner_strucure_name.inner_structure_member

Example program for if:

/* Write a C program detail about emloyee*/
#include<stdio.h>
#include<conio.h>
void main()
{
int expe=2020;//To calculate working experience
struct date//Inner structure
{
int day,year;
char month[20];
};
struct employee//outer structure
{
int emp_id,salary;
char name[30];
struct date doj;//date strcture defined in outer structure
};
struct employee emp;
clrscr();
printf("\nEnter employee id: ");
scanf("%d",&emp.emp_id);
printf("\nEnter employee name: ");
scanf("%s",&emp.name);
printf("\nEnter employee salary: ");
scanf("%d",&emp.salary);
printf("\nEnter date of joining: ");
//Nested structure of members calling
scanf("%d %s %d",&emp.doj.day,&emp.doj.month,&emp.doj.year);
printf("\nEmployee Name : %s",emp.name);
printf("\nEmployee Employee id: %d",emp.emp_id);
printf("\nEmployee Salary : %d",emp.salary);
printf("\nEmployee DOJ : %d %s %d ",emp.doj.day,emp.doj.month,
                                                                                           emp.doj.year);
expe=expe-emp.doj.year;
printf("\nWorking experience %d years",expe);
getch();
}

Output:
Share:

Pre-defined function


Library function:
  • C language provides built-in functions called library function.
  • The C standard library consists of wide verity of header files which can be included into a programmer’s project with a single directive.
  • Each header file contains one or more function declarations, data type definition, and macros.
  • The following library functions are,
1. sqrt(x):
This function evaluates the square root of ‘x’. Note that the argument must be non-negative.
Example: sqrt(25)
                Here the square root of 25 evaluated as 5.
2. log(x):
Where the argument ‘x’ must be real. Here loge x is evaluated.
Example:   log(7.5-x*y) this evaluate loge(7.5-xy)
3. exp(x):
This function evaluates ex. The argument must be of real quantity.
Example:   exp(7.5) this evaluate e7.5
4. pow(a,b):
Where the arguments ‘a’ and ‘b’ are maybe integer or float values.
This evaluates the value of ab.
Example:   pow(4,2) this evaluate (4)2.
5. ceil(x):
It is used to rounding x to the next integer value.
Example.    Ceil(5.6) evaluates 6
6. fmod(x,y):
It reterurn the remainder of x/y.
Example:   fmod(7,3) evaluates 1, because, when 7 is divided by 3 the remainder is 1.
7. sin(x):
This evaluates the trigonometric sine value.
Example:   sin(30) evaluate 0.5
8. cos(x):
This evaluates the trigonometric cosine value.
Example:    cos(30) evaluate 0.75
9. tan(x):
This evaluate the trigonometric tangent(  )value.
Example:    tan(30) evaluate 0.577
10. toascii(x):
Returns the integer value for the articular character.
Example:    toascii(a)     evaluates 97
11. tolower(x):
It is used to convert the character capital letter into a small letter.
Example:    tolower(‘Z’) evaluates z
12. toupper(x):
It is used to convert the character small letter into capital letter.
Example:    toupper(‘z’) evaluates Z.
13. ctime(t):
It is used to show the current time in your system.
Using #include<time.h> header file and time_t datatype.       
Example program for library function:

#include<stdio.h>
#include<conio.h>
#include<math.h>//all mathematic calculations
#include<ctype.h>//to find ascii value
#include<time.h>//To display time
void main()
{
          time_t t; //time data type
          time(&t);//calling current time
clrscr();
printf(" The current date and time is : %s\n",ctime(&t));
printf(" The sequre root of 25 value is : %lf\n",sqrt(25));
printf(" The log(5.6) value is : %lf\n",log(5.6));
printf(" The exp(12.0) value is : %lf\n",exp(12.0));
printf(" The power 4 of 2 is : %lf \n",pow(4,2));
printf(" The round of 5.6 value is : %lf \n",ceil(5.6));
printf(" The modulo of 7/3 value is :%lf \n",fmod(7,3));
printf(" sin(90) value is : %lf\n", sin(90));
printf(" cos(90) value is : %lf\n", cos(90));
printf(" tan(45) value is : %lf\n", tan(45));
printf(" The ASCII chacter w value is : %d \n", toascii('w'));
printf(" The lower value of chacter H is : %c\n",tolower('H'));
printf(" The upper value of chacter e is : %c\n",toupper('e'));
getch();
}


Output:
Share:

Parameter passing methods and Recursion function


Parameter passing methods:
In ‘C’ language there are two ways that the parameters can be passed to a function, they are
i).  Call by value
ii). Call by reference
1.  Call by value:
  • This method copies the values of actual parameters into the formal parameters of the function.
  • Here, the changes in the formal parameters cannot affect the actual parameters, because formal arguments are a photocopy of actual arguments.
Note: If no idea about actual and formal arguments means the below link to use for learning this topic.
Example:

#include<stdio.h>
#include<conio.h>
void main()
{
int n=2;
clrscr();
printf("The %d of cubic value is %d",n,cube_func(n));//function calling
getch();
}
//function definition
int cube_func(int x)
{
x=x*x*x;
return(x);
}

Output:
2. Call by reference:
  • It is another way of passing parameters to the function.
  • Here, the address of arguments is copied into the parameters inside the function, the address is used to access the actual arguments used in the call.
  • Hence changes made in the arguments are permanent.
  • Here pointers are passed to function, just like any other arguments and we need not declare the parameters as a pointer type.
Example: Interchanging two values.

#include<stdio.h>
#include<conio.h>
void interchange_func(int *x,int *y);//function declaration with pointer
void main()
{
int a=5,b=7;
clrscr();
printf("\n a and b values before interchanging : %d %d\n",a,b);
interchange_func(&a,&b);//pass address to the function
printf(" a and b values after interchanging : %d %d\n",a,b);
getch();
}
//function definition
void interchange_func(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}

Output:
Recursion:
  • It is the process being performed where one of the instructions is to “repeat the process”.
  • This makes it sound very similar to a loop because it repeats the same code, and in some ways, it is similar to looping.
  • It is the process of calling the same function itself again and again until some condition is satisfied. This process is used for repetitive computation in which each action is satisfied in terms of a previous result.
Syntax:

Function1()
{
          Function1();
}

Example:

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("\n Enter the number: ");
scanf("%d",&a);
printf("\n The factorial of %d=%d",a,recursive_func(a));// function call
getch();
}
recursive_func(int x)// function declaration
{
int f;
if(x==1)
          return(1);
else
          f=x*recursive_func(x-1);// recursive function
          return(f);
}

Output:
Share:

Function prototypes in c


Introduction:
  • The function prototype is nothing but, it is number of way to calling function and return the value in c.
  • The functions are classified into the following types depending on whether the arguments (parameters) are present or not and whether the value is returned or not
Number of ways to give user-defined functions:
                  i).   Function with no arguments and no return value.
                  ii).  Function with arguments and no return value.
                  iii). Function with arguments and with the return value.
                  iv). Function with no arguments and with the return value.
1. Function with no arguments and no return value:
  • The called program does not receive any data from the calling program and does not send back value to the calling program.
  • This function is used to perform the only operation they act independently (They read data values and print results in the same block.

Syntax:
Example: Addition of two numbers

#include<stdio.h>
#include<conio.h>
void main()
{
void add_func(void);//function definition
add_func();// function call  with no arguments
getch();
}
//function definition with no return value
void add_func()
{
int a,b,c;
clrscr();
printf("\n Enter two numbers :");
scanf("%d %d",&a,&b);
c=a+b;
printf("\n Sum is %d",c);
}

Output:
2. Function with arguments and no return value:
  • The called program receives some data from the calling program and does not send back any values to the calling program. (One way communication).

Syntax:
Example: Addition of two numbers

#include<stdio.h>
#include<conio.h>
void main()
{
void add_func(int,int);//function definition
int a,b;
clrscr();
printf("Enter two values: \n");
scanf("%d %d",&a,&b);
add_func(a,b);// function call  with arguments
getch();
}
//function definition with no return value
void add_func(int x,int y)
{
int z;
z=x+y;
printf("Sum is....%d",z);
}

Output:
3. Function with arguments and with return value:
  • The called program receives some data from the calling program and sends back any values to the calling program. (Two-way communication)

Syntax:
Example: Addition of two numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int add_func(int,int);//function definition
int a,b,c;
clrscr();
printf("Enter two values: \n");
scanf("%d %d",&a,&b);
c=add_func(a,b);// function call  with arguments
printf("Result is .....%d",c);
getch();
}
//function definition with return value
int add_func(int x,int y)
{
int z;
z=x+y;
return(z);
}

Output:
4. Function with no arguments and with return value:
  • The calling program cannot pass any arguments to the called program but, the called program may send some return value to the calling program.

Syntax:
Example: Addition of two numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int add_func();//function definition
int a,b,c;
clrscr();
c=add_func();// function call  with no arguments
printf("Result is .....%d",c);
getch();
}
//function definition with return value
int add_func()
{
int a,b,c;
printf("Enter two values: \n");
scanf("%d %d",&a,&b);
c=a+b;
return(c);
}

Output:


Share:

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me