26) What is a stream? How many types of streams are in C?
|stdaux||Standard Auxiliary||COM1: port|
|stdprn||Standard Printer||LPT1: port|
Streams can be classified into two types: text streams and binary streams. The text streams are interpreted as per the ASCII values starting from 0 to 255. Binary streams are raw bytes which C can't interpret, but application has to interpret it itself. Text modes are used to handle, generally text file where as binary modes can be used for all files. But they won't give you the content of a file, rather they will give you the file properties and content in raw binary format.
28) Which one to use, a stream function or a system calls?
Stream files are generally better to use, since they provide sufficient amount of buffer for read and write. That is why it is more efficient.
But in a multiuser environment, files can be shared among users. These shared files are secured with lock, where only one user will be able to write at a time. In this scenario, buffering will not be efficient, since the file content will change continuously and it will be slower.
So, normally it is good to use stream functions, but for shared files system calls are better.
29) What is the difference between a string copy (strcpy) and a memory copy (memcpy)?
Generally speaking, they both copy a number of bytes from a source pointer to a destination pointer. But the basic difference is that the strcpy() is specifically designed to copy strings, hence it stops copying after getting the first '\0'(NULL) character. But memcpy() is designed to work with all data types. So you need to specify the length of the data to be copied, starting from the source pointer.
30) How can I pad a string to a known length?
The "%-20.20s" argument tells the printf() function that you are printing a string and you want to force it to be 20 characters long. By default, the string is right justified, but by including the minus sign (-) before the first 20, you tell the printf() function to left-justify your string. This action forces the printf() function to pad the string with spaces to make it 20 characters long.
31) How can I convert a number to a string?
The following functions can be used to convert integers to strings:
|itoa()||Integer value to a string.|
|ltoa()||Long integer value to a string.|
|Ultoa()||Unsigned long integer value to a string.|
You can write your own functions too, because these functions are not that safe, as they don't check if the value given is NULL or not.
32) What does const keyword do?
The access modifier keyword “const” tells compiler that the value of this variable is not going to be changed after it is initialized. The compiler will enforce it throughout the lifetime of the variable.
33) char *p="SAMPLETEXT" , *q ="SAMPLETEXT"; Are these two pointers equal ? If yes , then explain ?
In C, strings(not array of characters) are immutable. This means that a string once created cannot be modified. Only flushing the buffer can remove it. Next point is, when a string is created it is stored in buffer. Next time, when a new string is created, it will check whether that string is present in buffer or not. If present, that address is assigned. Otherwise, new address stores the new string and this new address is assigned.
34) When should a type cast be used?
There are two main uses of type cast.
- The first one is to convert some value of datatype A to a datatype B. Such as, if you type cast a float variable of value 1.25 to int, then it will be 1.
- The second use is to cast any pointer type to and from void *, in order to use it in generic functions such as memory copy functions, where the execution is independent of the type of the pointer.
35) What is the difference between declaring a variable and defining a variable?
Declaration is done to tell compiler the data type of the variable, and it inherently meant that somewhere in this scope, this variable is defined or will be defined. And defining a variable means to allocate space for that variable and register it in the stack memory. For example:
extern int decl1; /* this is a declaration */ int def2; /* this is a definition */