>1) Why do prototypes of some predefined functions in
>ctype.h(ex- tolower(ch)) has 'int' as the data type for formal arguments?
Because they're allowed to accept EOF as a valid argument. Since char can be either signed or unsigned, and EOF is a negative quantity, if char is unsigned and EOF is passed, then Bad Things(TM) can happen. Also note that just because these functions take an int, it doesn't mean that all of the values representable by int are allowed. In fact, the only values allowed by the ctype functions are the range of an unsigned char, and EOF. That's why you'll see the argument cast to unsigned char occasionally:
c = tolower ( (unsigned char)c );
>2) Is there any command in DOS for viewing the non printable characters stored in a file.
This isn't a C question.
>char *p="Hello" //Where will be this "Hello" stored??????
Somewhere safe, implementation defined, and potentially read-only. But it doesn't matter at all to you as long as you don't try to modify it.
>But is it realy valid???? Does it give error in ur compiler?????
No, it's not valid. You're accessing indeterminate memory because a pointer (assuming it's a local variable) isn't initialized to anything, so it could point anywhere. Whether it breaks or not depends on where it points and if accessing that memory will break something. This is undefined behavior, so it could really do anything, including work perfectly.
>5) Why is using 'gets' dangerous(as 'C' compiler in UNIX warns)?
gets doesn't (and can't) check the boundaries of the buffer you pass to it, so if you give it an array of size 20, but gets reads 5000 characters, you'll be writing well beyond the end of the array. This is called a buffer overflow, and alot of exploits have been written to take advantage of them.
>In 'C' do we have to type cast exp2 to the datatype of exp1 explicitly?
It depends on the types. Some conversions are implicit if they're deemed compatible (but not necessarily safe, like double to int). Others are not compatible, therefore you have to use an explicit cast if you really want the assignment to be made.
>Is it same with C++ also?
It's even more so with C++ since C++ has much stricter type checking.
>7) Is a buffer always used while writing or reading from a file in 'C'?
No, that's up to the implementation. However, a buffer is a valuable optimization tool, so you're unlikely to find an implementation that doesn't use one.
>If yes, then when do we need to make a call to setvbuf
>with 'buf=NULL' since the 'C' compiler always assigns a buffer automatically(according to u)?
Well, if you want to specify buffering but don't want to manage your own buffer, you would pass a null pointer and allow setvbuf to handle it.
>If i m not wrong, error flag will set. But is eof flag affected(or set or reset)?
The two flags are typically independent. Hitting the end-of-file won't set the error flag and a read/write error won't set the eof flag. However, that's not required behavior, but it doesn't matter anyway because any methods for clearing one flag will clear the other one too. :)
>Will every file handling function in C affect both the flags?
No (for example, remove or rename), but every function that accepts a FILE pointer as an argument will probably touch one or the other at some point.
>Will every function in C affect both the flags?
Most certainly not.
>Here what is the meaning of 'it may evaluate stream more than once' ?
Macros have a nasty problem with side effects. Let's say you have a macro for squares:
#define square(x) ( (x) * (x) )
If you then say square ( x++ ), fully expecting x to be incremented only once, you'll be disappointed to find that the result is actually ( x++ * x++ ). This is a case where x++ is evaluated more than once, and that's more or less what K&R is talking about.
>And which one shd we prefer to use- fgetc or getc?
It's up to you. You're unlikely to run into a case where there would be a difference between the two, and the performance difference isn't that great anymore even if getc is a macro.
>"It must be called before reading, writing, or any other operation."
>What does this mean?
It means that immediately after opening a stream, you call setvbuf or forever hold your peace. If you do anything with the stream after opening it, it's too late to call setvbuf, and if you do, you'll invoke undefined behavior.
>11) There is one statement:- "fflush must be called between a read and a write or vice versa."
You're going to have to give us more context for this question. I don't have my copy of K&R handy right now. Most likely the context is that of two-way streams where you can read and write, but between a read and a write, or a write and a read, you need to call a positioning function. fflush is considered a positioning function, and it serves the purpose of ensuring that you don't have a read immediately after a write or vice versa.