Best Industrial Training in C,C++,PHP,Dot Net,Java in Jalandhar

Thursday, 3 April 2014

Last Year C Questions??

1. // program to count characters,spaces,tabs,new lines in a file //
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
int c;
int b=0,t=0,nl=0;
clrscr();
fp=fopen("good.txt","w");
printf("enter the string:");
while((c=getchar())!=EOF)
{
if(c==' ')
++b;
if(c=='\t')
++t;
if(c=='\n')
++nl;
}
fprintf(fp,"input has %d blanks,%d tabs and %d newlines\n",b,t,nl);
fclose(fp);
getch();

}





2. what do you mean by structure? How they are declared and initialized? How they are different from arrays?

STRUCTURES—these are the collection of heterogeneous elements in simple words we can say that is a collection of different data types. Each element in structure is called member. If you want to access structure members in C language ,structure variable should be declared. Many structure variable can be declared for same structure and memory will be allocated for each separately.

STRUCTURE DECLARATION—
A structure declaration specifies the grouping of variables of different types in a single unit. It simply acts as a temple or blueprint that may be used to create structure variables.
The syntax for declaring a structure in C is:

Struct struct_name
{
data_type member1;
data_type member2;
……………………
data_type membern;
};

STRUCTURE INITIALIZATION—
Lets us consider an example of employee structure which is declared as follows’
Struct employee
{
int emp_no;
char emp_name[20];
int dapt_code;
double salary;
}
we can define and initialize structure variable emp1 of type employee as follows,
employee emp1={101,”raghav”,05,23453.50};

PROGRAM TO ILLUSTRATE—
#
#
struct employee
{
int empid;
char name[50];
char add[50];
float salary;
char email[50];
}s1;
void main()
{
clrscr();
scanf(“%d”,&s1.empid);
scanf(“%s”,&s1.name);
scanf(“%s”,&s1.add);
scanf(“%f”,&s1.salary);
scanf(“%s”,&s1.email);
printf(“%d”,s1.empid);
printf(“%s”,s1.name);
printf(“%s”,s1.add);
printf(“%f”,s1.salary);
printf(“%s”,s1.email);
getch();
}

DIFFERENCE BETWEEN STRUCTURES AND ARRAYS

STRUCTURES                                                      ARRAYS
1.they are a group of heterogeneous               1.they are defined as a finite set
elements.                                                             of homogeneous elements.
 2.syntax                                                                                                         
 struct struct_name                                                2.syntax:
{                                                                             storage_class datatype arrayname[size];
datatype  variable name;
----------------------------
----------------------------
};
3.initialization:                                                      3.initialization:
employee emp1={101,”raghav”,05,23453.50};    float digit[3]={0.25,0,0.50};
  

No comments:

Post a Comment