Wednesday, October 21, 2009

RAGGED ARRARY

The two-dimensional array character type arrays occupy a fixed amount of memory space for each row that is rarely needed .Therefore , instead of making each row fixed number of characters . We can make it a pointer to a string of varying length.
The character Arrays with the rows of varying length are called "RAGGED ARRAY " and are better handled by pointer . For example :
char *cname[3] = {"india", "nepal" ,"bhutan"}; // is the declaration and intialisation of Ragged Array.

Saturday, October 17, 2009

FILE

COMMAND LINE ARGUMENT :- Command line argument is parameter supplied to a program when the program is invoked . This parameter may represent a File name the program should process . In fact main() can take two argument (from the operating system) called ARGC and ARGV .

The variable argc is an argument counter that counts the number of Argument on the commands line .Whereas argv is an argument vector and represent an array of character pointers that points to the command line argument .The size of this array will be equal to the value of argc .

POINTER

BASE ADDRESS AND CONSTANT POINTER :- When an array (any data type ) is declared , the compiler allocate a base address and sufficient amount of storage to contain all the element of the array in contiguous location .The BASE ADDRESS is the location of the first element( index 0) of the array .The compiler also defines the array name as a CONSTANT POINTER to the first element .Example int num = {12 , 5 , 107 , 55 , 291 } -- here an int type array num is declared and "num" is the constant pointer that denotes the base address of &num[0].

Friday, October 16, 2009

SCALE FACTOR

When we increment a pointer , its value is increased by the length of the data type that it points to .this length is called SCALE FACTOR .[i,e.ptr = ptr + 1 or ptr++].The no of bytes used to stored various data types can be found by making use of "SIZE OF" operator . For example , if X is a variable , the size of (X) returns the number of bytes needed for the variable.

SOME C DEFINITION

1.POINTER VALUE : It is the variable address the pointer contains and the VALUE THE POINTER to is the content of that variable whose address stored in the pointer .The "&" operator can be remembered as ADDRESS OF and the indirection operator "*" can be remembered as VALUE AT ADDRESS .We use "%u" format for printing Address values as Memory address are Unsigned Integers.


Sunday, September 20, 2009

FILE IN C

01.WHAT IS FILE IN C?
Ans: A file is a place on the disk where a group of related data stored in. the keyword "FILE" is a data type that is defined in the I/O libary . The FILE type pointer contains all the infomation about the FILE is subsequently used as a communication link between the system and the program.

OPENING THE FILE: when working witha stream-orinted data file , the first step establish a "buffer area" ,where information is temporarily stored while being transferred between the computer's memory and data file .This buffer area allows information to be read from or written to the data file more rapidly than would otherwise be possible .
The buffer area is establish by writing
FILE *fp1 ;

Tuesday, September 8, 2009

CONSTRUCTOR (JAVA)

01.WHAT IS COSTRUCTOR IN JAVA?
ans. in java constructor initialize an object.In consturctor class name and constrructor name must be same .It has no return type even void .

CONSTRUCTOR are two type in java.i)default , ii)parameterized.

EXAMPLE:
class A{

A(){ // DEFAULT CONSTRUCTOR......
}

A(1,2,3){ //PARAMETERIZED CONSTRUCTOR...
}
}

N.B:IN JAVA COPY CONSTRUCTOR NOT SUPPORTED i,e.IN JAVA COPY CONSTRUCTOR NOTHING BUT A GENERAL CONSTRUCTOR.......

Wednesday, September 2, 2009

CALL BY REFENCE.

swapping the two no.

void main()
{
void myudf(int * , int *); //function declaration ....ansi or modern style.....
int nx , ny ;
int *px , *py ;
px = &nx ;
py = &ny ;

printf("ENTER TWO NO.S ") ;
scanf("%d %d" , px ,py);

myudf(px , py); // funtion calling.....................

printf("BACK TO MAIN: THE FISRT NO IS[%d] AT ADDR [u] SECOND IS :[%d] AT ADDR [%u] " , *px , px , *py , py );

getch();
}
//............END OF MAIN............................

void myudf( int *pnx , int *pny)
{
int z ;

z = *pnx ;
*pnx = *pny ;
*pny = z ;

printf("AT UDF : NOW THE FIRST NO. IS:[%d]AT ADDR [%u] THE SECOND NO.[%d] AT ADDR [%u] " ,*pnx , pnx , *pny , pny );

getch();
}

Monday, August 24, 2009

program langauge

program langauge are three types.

i.procedural langauge(C )
ii.object oriented programing(java , SMALL TALK )
iii. object based programing( VB)

in three types VB is not case sensitive langauge..

Sunday, August 2, 2009

block diagram of cpu


c programing

1. main()

{

char string[]="Hello World";

display(string);

}

void display(char *string)

{

printf("%s",string);

}

Answer:

Compiler Error : Type mismatch in redeclaration of function display

Explanation :

In third line, when the function display is encountered, the compiler doesn't know anything about the function display. It assumes the arguments and return types to be integers, (which is the default type). When it sees the actual function display, the arguments and type contradicts with what it has assumed previously. Hence a compile time error occurs.

2. main()

{

int c=- -2;

printf("c=%d",c);

}

Answer:

c=2;

Explanation:

Here unary minus (or negation) operator is used twice. Same maths rules applies, ie. minus * minus= plus.

Note:

However you cannot give like --2. Because -- operator can only be applied to variables as a decrement operator (eg., i--). 2 is a constant and not a variable.

3. #define int char

main()

{

int i=65;

printf("sizeof(i)=%d",sizeof(i));

}

Answer:

sizeof(i)=1

Explanation:

Since the #define replaces the string int by the macro char

4. main()

{

int i=10;

i=!i>14;

Printf ("i=%d",i);

}

Answer:

i=0

Explanation:

In the expression !i>14 , NOT (!) operator has more precedence than ‘ >’ symbol. ! is a unary logical operator. !i (!10) is 0 (not of true is false). 0>14 is false (zero).

5. #include

main()

{

char s[]={'a','b','c','\n','c','\0'};

char *p,*str,*str1;

p=&s[3];

str=p;

str1=s;

printf("%d",++*p + ++*str1-32);

}

Answer:

77

Explanation:

p is pointing to character '\n'. str1 is pointing to character 'a' ++*p. "p is pointing to '\n' and that is incremented by one." the ASCII value of '\n' is 10, which is then incremented to 11. The value of ++*p is 11. ++*str1, str1 is pointing to 'a' that is incremented by 1 and it becomes 'b'. ASCII value of 'b' is 98.

Now performing (11 + 98 – 32), we get 77("M");

So we get the output 77 :: "M" (Ascii is 77).

6. #include

main()

{

int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} };

int *p,*q;

p=&a[2][2][2];

*q=***a;

printf("%d----%d",*p,*q);

}

Answer:

SomeGarbageValue---1

Explanation:

p=&a[2][2][2] you declare only two 2D arrays, but you are trying to access the third 2D(which you are not declared) it will print garbage values. *q=***a starting address of a is assigned integer pointer. Now q is pointing to starting address of a. If you print *q, it will print first element of 3D array.

7. #include

main()

{

struct xx

{

int x=3;

char name[]="hello";

};

struct xx *s;

printf("%d",s->x);

printf("%s",s->name);

}

Answer:

Compiler Error

Explanation:

You should not initialize variables in declaration

8. #include

main()

{

struct xx

{

int x;

struct yy

{

char s;

struct xx *p;

};

struct yy *q;

};

}

Answer:

Compiler Error

Explanation:

The structure yy is nested within structure xx. Hence, the elements are of yy are to be accessed through the instance of structure xx, which needs an instance of yy to be known. If the instance is created after defining the structure the compiler will not know about the instance relative to xx. Hence for nested structure yy you have to declare member.

9. main()

{

printf("\nab");

printf("\bsi");

printf("\rha");

}

Answer:

hai

Explanation:

\n - newline

\b - backspace

\r - linefeed

10. main()

{

int i=5;

printf("%d%d%d%d%d%d",i++,i--,++i,--i,i);

}

Answer:

45545

Explanation:

The arguments in a function call are pushed into the stack from left to right. The evaluation is by popping out from the stack. and the evaluation is from right to left, hence the result.

Friday, June 19, 2009

MCQ

1. The C language terminator is
(a) semicolon (b) colon (c) period (d) exclamation mark

2. What is false about the following -- A compound statement is
(a) A set of simple statements (b) Demarcated on either side by curly brackets
(c) Can be used in place of simple statement (d) A C function is not a compound statement.

3. What is true about the following C Functions
(a) Need not return any value (b) Should always return an integer
(c) Should always return a float (d) Should always return more than one value

4. Main must be written as
(a) The first function in the program (b) Second function in the program
(c) Last function in the program (d)
Any where in the program

5. Which of the following about automatic variables within a function is correct ?
(a) Its type must be declared before using the variable (b) They are local
(c) They are not initialized to zero (d) They are global

6. Write one statement equivalent to the following two statements: x=sqr(a); return(x);
Choose from one of the alternatives
(a) return(sqr(a)); (b) printf("sqr(a)");
(c) return(a*a*a); (d) printf("%d",sqr(a));

7. Which of the following about the C comments is incorrect ?
(a) Comments can go over multiple lines
(b) Comments can start any where in the line
(c) A line can contain comments with out any language statements
(d) Comments can occur within comments

8. What is the value of y in the following code?
x=7;
y=0;
if(x=6) y=7;
else y=1;
(a) 7 (b) 0 (c) 1 (d) 6

9. Read the function conv() given below
conv(int t)
{
int u;
u=5/9 * (t-32);
return(u);
}
What is returned
(a) 15 (b) 0 (c) 16.1 (d) 29

10. Which of the following represents true statement either x is in the range of 10 and 50 or y is zero
(a) x >=10 &&
x <= 50 || y = = 0 (b) x<50 style="mso-spacerun:yes"> x >= 50 (d) None of these

11. Which of the following is not an infinite loop ?
(a) while(1)\{ ....} (b) for(;;){...}
(c) x=0; (d) # define TRUE 0
do{ /*x unaltered within the loop*/ ...
.....}while(x = = 0); while(TRUE){ ....}

12. What does the following function print?
func(int i)
{
if(i%2)return 0;
else return 1;
}
main()
{
int =3;
i=func(i);
i=func(i);
printf("%d",i);
}
(a) 3 (b) 1 (c) 0 (d) 2

13. How does the C compiler interpret the following two statements
p=p+x;
q=q+y;
(a) p= p+x; (b)p=p+xq=q+y; (c)p= p+xq; (d)p=p+x/q=q+y;
q=q+y; q=q+y;

RECURSIVE

1. GCD
int gcd(int,int);
int main()
{
int x,y;
printf("\nENTER TWO NO.S");
scanf("%d %d ",&x,&y);

printf("THE GCD OF TWO NO.IS %d", gcd(x,y));
getch();
}
int gcd(intx,inty)
{
if(y==0)
return(x);
else
return gcd(y,x%y);
}

Thursday, June 18, 2009

some C problem

Note : All the programs are tested under Turbo C/C++ compilers.

It is assumed that,

Ø Programs run under DOS environment,

Ø The underlying machine is an x86 system,

Ø Program is compiled using Turbo C/C++ compiler.

The program output may depend on the information based on this assumptions (for example sizeof(int) == 2 may be assumed).

Predict the output or error(s) for the following:

1. void main()

{

int const * p=5;

printf("%d",++(*p));

}

Answer:

Compiler error: Cannot modify a constant value.

Explanation:

p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".

2. main()

{

char s[ ]="man";

int i;

for(i=0;s[ i ];i++)

printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);

}

Answer:

mmmm

aaaa

nnnn

Explanation:

s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea. Generally array name is the base address for that array. Here s is the base address. i is the index number/displacement from the base address. So, indirecting it with * is same as s[i]. i[s] may be surprising. But in the case of C it is same as s[i].

3. main()

{

float me = 1.1;

double you = 1.1;

if(me==you)

printf("I love U");

else

printf("I hate U");

}

Answer:

I hate U

Explanation:

For floating point numbers (float, double, long double) the values cannot be predicted exactly. Depending on the number of bytes, the precession with of the value represented varies. Float takes 4 bytes and long double takes 10 bytes. So float stores 0.9 with less precision than long double.

Rule of Thumb:

Never compare or at-least be cautious when using floating point numbers with relational operators (== , >, <, <=, >=,!= ) .

4. main()

{

static int var = 5;

printf("%d ",var--);

if(var)

main();

}

Answer:

5 4 3 2 1

Explanation:

When static storage class is given, it is initialized once. The change in the value of a static variable is retained even between the function calls. Main is also treated like any other ordinary function, which can be called recursively.

5. main()

{

int c[ ]={2.8,3.4,4,6.7,5};

int j,*p=c,*q=c;

for(j=0;j<5;j++)>

printf(" %d ",*c);

++q; }

for(j=0;j<5;j++){

printf(" %d ",*p);

++p; }

}

Answer:

2 2 2 2 2 2 3 4 6 5

Explanation:

Initially pointer c is assigned to both p and q. In the first loop, since only q is incremented and not c , the value 2 will be printed 5 times. In second loop p itself is incremented. So the values 2 3 4 6 5 will be printed.

6. main()

{

extern int i;

i=20;

printf("%d",i);

}

Answer:

Linker Error : Undefined symbol '_i'

Explanation:

extern storage class in the following declaration,

extern int i;

specifies to the compiler that the memory for i is allocated in some other program and that address will be given to the current program at the time of linking. But linker finds that no other variable of name i is available in any other program with memory space allocated for it. Hence a linker error has occurred .

7. main()

{

int i=-1,j=-1,k=0,l=2,m;

m=i++&&j++&&k++||l++;

printf("%d %d %d %d %d",i,j,k,l,m);

}

Answer:

0 0 1 3 1

Explanation :

Logical operations always give a result of 1 or 0 . And also the logical AND (&&) operator has higher priority over the logical OR (||) operator. So the expression i++ && j++ && k++’ is executed first. The result of this expression is 0 (-1 && -1 && 0 = 0). Now the expression is 0 || 2 which evaluates to 1 (because OR operator always gives 1 except for ‘0 || 0’ combination- for which it gives 0). So the value of m is 1. The values of other variables are also incremented by 1.

8. main()

{

char *p;

printf("%d %d ",sizeof(*p),sizeof(p));

}

Answer:

1 2

Explanation:

The sizeof() operator gives the number of bytes taken by its operand. P is a character pointer, which needs one byte for storing its value (a character). Hence sizeof(*p) gives a value of 1. Since it needs two bytes to store the address of the character pointer sizeof(p) gives 2.

9. main()

{

int i=3;

switch(i)

{

default:printf("zero");

case 1: printf("one");

break;

case 2:printf("two");

break;

case 3: printf("three");

break;

}

}

Answer :

three

Explanation :

The default case can be placed anywhere inside the loop. It is executed only when all other cases doesn't match.

10. main()

{

printf("%x",-1<<4);

}

Answer:

fff0

Explanation :

-1 is internally represented as all 1's. When left shifted four times the least significant 4 bits are filled with 0's.The %x format specifier specifies that the integer value be printed as a hexadecimal value