Sunday 15 January 2012

Some Question Answers related to C


Q. What is identifier? What are the rules to be followed for naming an identifier?
A. Identifier refers to the name of variables, function and arrays. These are user defined names. The names may be in upper case and lower case.
Rules for naming an identifier are given below:
1.     First character must be an alphabet (or underscore).
2.     Name must consist of only letters, digits or underscore.
3.     Name must be up to 31charecters long.
4.     We can not use any keyword.
5.     Name must not contain white space.
int float; is wrong identifier name where as int sum; is right.


Q. How are arrays usually processed in C? Can entire array be processed with single instructions without repetition?
A. C supports a derived data type known as array that can be used for a powerful data type that would facilitate efficient storing, accessing and manipulation of data items.
An array is a fixed size sequenced collection of elements of the same data type.
Yes, we can process entire array with single instruction without repetition. Program written bellow is an example of that.
main()
{
int a[5];
printf("Enter the elements of an array ");
scanf("%d%d%d%d%d",&a[0],&a[1],&a[2],&a[3],&a[4]);
printf("%d%d%d%d%d",&a[0],&a[1],&a[2],&a[3],&a[4]);
getch();
             }


Q. What is Subscript?  How are they written? What restrictions apply to the values that can be assigned to subscripts?
A. We can use an array name to represent a set of elements. We can refer to the individual element by writing a number called subscript. It is also known as index. Subscripts always begin at 0 (not 1) and end at size-1. the subscript in a two dimension array represent rows and columns
Subscripts are written in brackets after the array name.
Following are the restrictions apply to the values that can be assigned to subscripts:
1.     It accept only positive value that is greater than 0.
2.     An incorrect or invalid subscript may cause unexpected results.
3.     There is no bound checking for the subscript in C.
//program to store the elements in an array & copy to another array & print it
void main()
{
int a[5],b[5],i;
clrscr();
printf("Enter the element of an array");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
for(i=0;i<5;i++)
b[i]=a[i];
for(i=0;i<5;i++)
printf("%d ",b[i]);
getch();
}
Here, a & b are the array names and 5 in bracket [ ] after the array names is known as subscript



No comments:

Post a Comment