JAYASHREE ORACLE CERTIFIED JAIN UNIVERSITY

Friday, September 24, 2010

TRYY!!!!!!!!!!!!!!!! :)

Section 1: Declarations and Initializations [a]
1.1: How do you decide which integer type to use?
Use ``short'' when you need to avoid values over 32,767, ``int'' when you want to store integers, ``long'' for long numbers (more than 6 digits), and ``float'' for numbers over 4 billion.
1.2: What should the 64-bit type on new, 64-bit machines be?
int.
1.3: If I write the code int i, j; can I assume that (&i + 1) == &j?
Only sometimes. It's not portable, because in EBCDIC, i and j are not adjacent. [a]
1.4: What's the best way to declare and define global variables?
In headers; this way, you can get link errors when you include the same header twice. Generally, you will have to define a variable everywhere you want to use it, and then declare it someplace so you know what it is. [a]
1.5: What does extern mean in a function declaration?
It refers to a variable which is not actually in your program. For instance,
main() {
extern int bar;
printf("%d\n", bar);
return 0;
}
will compile without errors because bar is declared as being external. (It won't run, though, because you never assign bar a value.) [a]
1.6: I finally figured out the syntax for declaring pointers to functions, but now how do I initialize one?
With the assignment operator. You were perhaps expecting a screwdriver?
Section 2: Structures, Unions, and Enumerations [a]
2.1: What is the difference between an enum and a series of preprocessor #defines?
The enum doesn't require the preprocessor.
2.2: I heard that structures could be assigned to variables and passed to and from functions, but K&R I says not.
K&R I was wrong; they hadn't actually learned C very well before writing the book. Later, Ritchie got a job at Bell Labs, and worked closely with the authors of C, allowing the 2nd edition of the book to be much more accurate. (Kernighan already worked at Bell Labs, as a video game developer.) [a]
2.3: How does struct passing and returning work?
The structures are put into the low part of the VGA card's VRAM. They are then removed before the next video update. This is why struct passing was not supported for a long time; VGA cards were prohibitively expensive.
If you try to pass very large structures on the stack, you may see odd screen graphics. [a]
2.4: Why can't you compare structs?
Compare them to what? A summer's day?
2.5: How can I read/write structs from/to data files?
Loop with putchar. Be careful; if your machine uses signed chars by default, all of the sign bits in your structure elements will be reversed. [a]
2.6: How can I determine the byte offset of a field within a structure?
It's generally 4 times the number of members of the structure. It may be more or less on some machines. [a]
2.7: How can I access structure fields by name at run time?
foo."name" should work. You may need to overload the . operator, which, in turn, may overload your C compiler.
2.8: Why does sizeof report a larger size than I expect for a structure type, as if there was padding at the end?
Because there's padding at the end. Duh.
2.9: My compiler is leaving holes in structures, which is wasting space and preventing ``binary'' I/O to external data files. Can I turn off the padding, or otherwise control the alignment of structs?
Sure. What you do to eliminate the padding in structures is use unions; for intance,
struct foo {
char c;
long l;
char d;
char e;
char f;
};
may cause struct foo to be padded to 12 bytes, rather than the correct size of 8. Try
union foo {
double _d;
char c, d, e, f;
long l;
};
which will be 8 bytes. (The double is for alignment.) [a]
2.10: Can I initialize unions?
Depends. They may go on strike when provoked. Luckily, if your program involves air traffic control, the ISO standard guarantees that Ronald Reagan will fire any unions that go on strike, and replace them with structs, which should be close enough. [a]
2.11: How can I pass constant values to routines which accept struct arguments?
Try foo((struct foo) 3). [a]
Section 3: Expressions [a]
3.1: Why doesn't the code a[i] = i++; work?
You didn't declare either i or a.
3.2: Under my compiler, the code
int i = 7;
printf("%d\n", i++ * i++);
prints 49. Regardless of the order of evaluation, shouldn't it print 56?
No. The only logical answer would be 81 - two postfix ++'s are automatically converted to prefix.
3.3: I've experimented with the code
int i = 2;
i = i++;
on several compilers. Some gave i the value 2, some gave 3, but one gave 4. I know the behavior is undefined, but how could it give 4?
Because i is 2, the loop is executed twice.
3.4: People keep saying the behavior is undefined, but I just tried it on an ANSI-conforming compiler, and got the results I expected.
They were probably wrong. Flame them mercilessly. Be sure before you do that your compiler is really ANSI conforming, though. If it turns out you were wrong, they get a legal claim on your first-born.
3.5: Can I use explicit parentheses to force the order of evaluation I want? Even if I don't, doesn't precedence dictate it?
No. To force order of evaluation, you must threaten it. Take the comma operator hostage. Using it, you can force the other operators to do what you want. [a]
3.6: But what about the &&, ||, and comma operators? I see code like ``if((c = getchar()) == EOF || c == '\n')'' ...
As noted, once you've captured the comma operator, the others become docile.
3.7: If I'm not using the value of the expression, should I use i++ or ++i to increment a variable?
++i. Only losers and idiots use i++. This is different if your native language would idiomatically use ``i increment'', but in English and related languages, you must use ++i. Note that a modern program must use both, dependent on the current locale.
3.8: Why is i = ++i undefined?
Because it is unclear whether it is shorthand for i = 42; or i = (char *) "forty two";.
Given the ambiguity, the standards committee decided to leave it undefined.
Section 4: Null Statements [a]
4.1: What is this infamous null statement, anyway?
A null statement is an expression statement consisting solely of the terminating semicolon. The optional expression is dropped. It can be distinguished from any other statement by byte count or study of side-effects.
4.2: How do I ``get'' a null statement in my programs?
In ANSI C, there are six types of statements; labeled statements, compound statements, expression-statements, selection statements, iteration statements, and jump statements. All of them, except the jump and expression statments, are defined in terms of optional preceeding text, and other statements. The jump statements are never null statements. An expression statement is considered to be ``a null statement'' if the optional expression part of it has been left out. A null statement can appear on its own, or (most frequently) as the statement body of an iteration statement. These two null statements are equivalent, though neither of them is equivalent to any non-null statement. [*]
You may accidentally get a null statement by deleting the body of a non-null statement.
[*] Actually, they are functionally equivalent to a large set of non-null statements, namely, those with no side-effects. However, the FDA has yet to approve any such, as their lack of side effects is conjectured, and not clinically proven. This applies only to the ANSI standard, and not the ISO standard, as the FDA has no jurisdiction outside the U.S. [a]
4.3: Is there more than one null statement?
Sort of. You can use ``;'', ``0;'', or ``1;'' - they will all act like a null statement. Only the first is a ``true'' null statement (all bits zero). They are basically equivalent. Note that (void *) 0; is a null statement of type pointer to void, for instance. [a]
4.4: But I thought { } was a null statement!
No. { statement-list[opt] } is a compound statement. An empty block is not the same as a null statement, however, although it can be used in many of the same places. It's really a null block. (You can convert it with a cast, but it's not directly compatible. For instance, you can't use a null block as one of the controlling statements of a for loop.)
4.5: I use the statement #define NULLSTMT(F) (F) ; to allow me to cast a null statement to an appropriate type.
This trick, though popular in some circles, does not buy much. The resulting code is invalid, and will not compile. This (in the author's opinion) outweighs any arguable type consistency. It may be more common in industrial code. If it becomes common practice, C++ will probably legalize it. [a]
4.6: I use the statement #define NULLSTMT(F) (F) 0; to allow me to cast a null statement to an appropriate type.
This trick will likely work, but think: what does it really buy you? Mostly, it will indicate to even the most casual observer that you are shakey on the concept of null statements, making it harder for them to check your code.
4.7: But wouldn't it be better to use ; (rather than 0;) in case the value of 0 changes, perhaps on a machine with nonzero no-op instructions?
No. The '0' of '0;' is not evaluated as an instruction, rather, it is just ignored. The only reason to use '0;' instead of ';' is to help keep 1-heavy code properly balanced (in C, which uses binary representations for numbers, it is possible for code to become unbalanced; an unbalanced binary tree is a common source of poor performance.
4.8: Is a null statement a null pointer?
No. A null pointer is a pointer where all of the address bits are zero (no matter what the segment bits are), and can be obtained by typing '(char *) (int) 0'. A null statement is not a pointer to anything. They are not interchangeable, although you can combine them to get an effectively-null statement, such as NULL;. This does not buy you anything. [a]
4.9: I'm still confused. I just can't understand all this null statement stuff.
Follow these two simple rules:
1. When you don't want to do anything in source code, don't write it.
2. If you need a null statement to round out an expression, use an unadorned ; to provide it.
3. Send large donations, checks, and money orders to the author of the FAQ, or the moderator of the group, whichever you prefer. Then, cross the top question off the FAQ, answer the question at the bottom, and mail it to three people. Within two weeks, you will receive 729 answers to various questions! Do not break the chain; Emily Postnews broke the chain, and now no one listens to her. [a]
Section 5: Arrays and Pointers
5.1: I had the definition char a[6] in one source file, and in another I declared extern char a[]. Why did it work?
The declaration extern char a[] simply matches the actual definition. The type ``array-of-type-T'' is the same as ``array-of-type-T.'' Go ahead and use extern char a[]. (For greater portability, use it in both files, not only in one of them.) [a]
5.2: But I heard that char a[] was different from char a[6].
This is true. However, the declaration a[] is compatible with the definition a[6].
5.3: So what is meant by the ``equivalence of pointers and arrays'' in C?
Very little.
5.4: Then why are array and pointer declarations interchangeable as function formal parameters?
Classism. We consider arrays ``second class objects''. They don't vote, and they get treated as pointers. Additionally, they're merely objects, not citizens. Marx wrote about this a lot. [a]
5.5: Why doesn't sizeof properly report the size of an array which is a parameter to a function?
Part of the ANSI conspiracy to restrict people to passing pointers; this was undertaken after the first discovery that passing large arrays recursively could cause crashes. Since then, with the passing of MS-DOS, it has become a non-issue; since all serious machines have virtual memory, you can pass as much data as you want on the stack without detectable problems. [a]
5.6: Someone explained to me that arrays were really just constant pointers.
Cool. Someone I know says he saw Elvis in a local bar. [a]
5.7: Practically speaking, what is the difference between arrays and pointers?
About the difference between alcohol and marijuana; they have different characteristics, and that's not a problem if you don't mix them too carelessly.
5.8: I came across some ``joke'' code containing the ``expression'' 5["abcdef"]. How can this be legal C?
It was added to allow people to avoid the character constant 'f' which may not be available on some systems. (Actually, it's a side-effect of the equivalence of arrays and pointers.) [a]
5.9: How would I initialize an entire array from standard input?
You have to use a loop. For instance, the following code reads the numbers zero through 99 into the array a.
for (i = 0; i < 100; ++i)
a[i] = (scanf, ("%d", i));
Make sure to include , or this may not work. [a]
Section 6: Memory Allocation
6.1: Why doesn't this fragment work?
char *answer
printf("Type something:\n");
gets(answer);
printf("You typed \"%s\"\n", answer);
The semicolon after ``answer'' is missing. [a]
Section 12: Library Subroutines
12.1: How can I convert numbers to strings (the opposite of atoi)? Is there an itoa function?
There's frequently an itoa function. Better yet, write your own; it'll be good practice. On some implementations, (char *) x; will convert x to a string.
12.2: How can I get the current date or time of day in a C program?
fprintf(stderr, "please enter the current time and date..."); fflush(stderr); gets(stdin);
12.3: I need a random number generator.
Count errors in Herbert Schildt's C books. No one has detected any consistent pattern.
12.4: How can I get random integers in a certain range?
random(n) returns random numbers between n and INT_MAX.
12.5: Each time I run my program, I get the same sequence of numbers back from rand().
This is so your results will be reproducible.
12.6: I need a random true/false value, so I'm taking rand() % 2, but it's just alternating 0, 1, 0, 1, 0...
That seems pretty random to me.
12.7: I need some code to do regular expression matching.
So do I. Let me know if you find some.
12.8: I read through the standard library, but there's no function to multiply two floating point numbers! Help!
Many C compilers offer an extension ``mult'' to do just this. If your compiler doesn't, just hang tight; ANSI is likely to add it in the next revision.
For now, you can try
float mult(float m, n)
{
float i = 0, j = 0;
for (i = 0; i < n; ++i)
j += m;
return j;
}
which is fine as long as n is an integer.
12.9: How do I get past the snake?
Release the bird. You will have to drop the rod to get the bird in the cage. [a]
Section 13: Floating Point
13.1: My floating-point calculations are acting strangely and giving me different answers on different machines.
One of the machines is probably a Pentium. Scrap it and get a real machine. [a]
13.2: I'm trying to do some simple trig, and I am #including , but I keep getting ``undefined: _sin'' compilation errors.
You forgot to define the sin() function. Most math texts should cover it in some detail. The easiest way to fix this should be:
double sin(double x) {
return sqrt(1 - cos(x) * cos(x));
}
Warning: You must not declare this function as ``extern'', or you will still have link problems.
13.3: Why doesn't C have an exponentiation operator?
It does. It looks like the multiplication operator, but you use it more. For instance, the C way of expressing ``x squared'' is ``x*x''. ``x cubed'' would be ``x*x*x''. Easy, isn't it?
13.4: How do I round numbers?
Multiply by 10. Numerical Recipies in C has a section on this, but there's reputedly a bug in their algorithm.
13.5: How do I test for IEEE NaN and other special values?
Using an electron microscope; the patterns are obvious once you know them.
13.6: I'm having trouble with a Turbo C program which crashes and says something like ``floating point formats not linked.''
Turbo C is notoriously buggy. Get a compiler with floating point support.
13.7: What is so ``unsafe'' about floating point?
Have you tried EXAMINE STICK? The stick has a sharp point, which punctures the raft, which no longer floats. Don't bring the stick into the raft with you.
13.8: Which is larger, ``2'' or ``2.0''?
Numerical Recipes in C has a function for comparing two values to see which is greater. It may have a slight bug, where it would report incorrect results if the numbers differ by less than FLOAT_MAX / INT_MAX.
13.9: When I try to compile the following code, I get the error ``invalid use of floating point'', what does this mean?

x=663608941*y%pow(2,32);
Remember that * is the indirection operator, as well as the multiplication operator; try putting spaces before and after the ``*'' so the compiler knows what you mean. Do the same with the % operator.
13.10: How can I copy a float into a string?
strcpy(string_var, float_var);
13.11: What are float variables, anyway?
The term ``float variable'' is actually redundant; they are simply variables whose value can ``float'' during execution. For instance:
float f, g = 3;
f = g; /* f ``floats'' to g */
Easy!
What will be output ?
(1) void main()
{
clrscr();
printf("%d",sizeof(3.8));
getch();
}
output: 8
Explanation: 3.8f is float constant, 3.8 is double constant and 3.8L is long double constant .Here are finding size of double constantan which is 8.
For more detail click here (data type)
(2) What will be output ?
void main()
{
char *str1="powla";
char *str2="er";
clrscr();
printf("%s\b\b%s",str1,str2);
getch();
}
output: power
Explanation: \b escape sequence back the cursor one position left .We are using two /b so after writing str1 cursor is at the position of l of powal .So when it write er it will override the la so output will be power.
(3)What will be output ?
void main()
{
int a=270;
char *p;
p=(char *)&a;
clrscr();
printf("%d",*p);
getch();
}
output: 16
(4)What is missing statement of in the following program ?
Void main()
{
int sort(int,int);
int I;
i=sort(5,6);
}
Int sort(int a,int b)
{
int c;
c=a;
a=b;
b=c;
return a;
}
Ans: Function sort returning a value but we are not using return value so there is weastge of two byte memory. So missing statement is ,there should statement which uses the return value.
(5)Write following in term of if and else :
void main()
{
int a=1,b=2,c=3;
clrscr();
if(a==5&&b==6&&c==7)
printf("india");
else
printf("pak");
getch();
}
Ans:

void main()
{
int a=1,b=2,c=3;
clrscr();
if(a==1)
{
if(b==2)
{
if(c==3)
{
printf("india");
}
else
{
printf("pak");
}}
else
{
printf("pak");
} }
else
{
printf("pak");
}
getch();
}
(q)
Give the memory representation of

Struct xxx
{
char a;
int b;
char c;
};
Ans: Memory representation :




More detail click here (union)
(q) Write the following program in term of switch and case ?
void main()
{
int a=3;
if(x>2)
{
printf(“INDIA IS BEST”);
}
else
{
printf(“PAK IS BEST”);
}

}
Ans: if condition always return two value.
1 if condition is true.
0 if condition is false.
So program is
void main()
{
int x=3;
switch(x>2)
{
case 0:printf("India is best");
break;
case 1:printf("Pak is best");
}
getch();
}

No comments:

Post a Comment