1. Why is C called a mid-level programming language?
C has characteristics of both assembly-level i.e. low-level and higher-level languages. So as a result, C is commonly called a middle-level language. Using C, a user can write an operating system as well as create a menu-driven consumer billing system.
2. What are the features of the C language?
Some features of the C language are-
- It is Simple And Efficient.
- C language is portable or Machine Independent.
- C is a mid-level Programming Language.
- It is a structured Programming Language.
- It has a function-rich library.
- Dynamic Memory Management.
- C is super fast.
- We can use pointers in C.
- It is extensible.
3. What is a token?
The individual elements of a program are called Tokens. There are following 6 types of tokens are available in C:
- Keywords
- Constants
- Operators
- Special Characters
- Strings
4. What’s the value of the expression 5[“abxdef”]?
The answer is ‘f’.
Explanation: The string mentioned “abxdef” is an array, and the expression is equal to “abxdef”[5]. Why is the inside-out expression equivalent? Because a[b] is equivalent to *(a + b) which is equivalent to *(b + a) which is equivalent to b[a].
5. What is a Preprocessor?
A preprocessor is a software program that processes a source file before sending it to be compiled. Inclusion of header files, macro expansions, conditional compilation, and line control are all possible with the preprocessor.
6. How can a string be converted to a number?
The function takes the string as an input that needs to be converted to an integer.
int atoi(const char *string)
Return Value:
- On successful conversion, it returns the desired integer value
- If the string starts with alpha-numeric char or only contains alpha-num char, 0 is returned.
- In case string starts with numeric character but is followed by alpha-num char, the string is converted to integer till the first occurrence of alphanumeric char.
7. Why doesn’t C support function overloading?
After you compile the C source, the symbol names need to be intact in the object code. If we introduce function overloading in our source, we should also provide name mangling as a preventive measure to avoid function name clashes. Also, as C is not a strictly typed language many things(ex: data types) are convertible to each other in C. Therefore, the complexity of overload resolution can introduce confusion in a language such as C.
When you compile a C source, symbol names will remain intact. If you introduce function overloading, you should provide a name mangling technique to prevent name clashes. Consequently, like C++, you’ll have machine-generated symbol names in the compiled binary.
Additionally, C does not feature strict typing. Many things are implicitly convertible to each other in C. The complexity of overload resolution rules could introduce confusion in such kind of language.
8. What is the difference between global int and static int declaration?
The difference between this is in scope. A truly global variable has a global scope and is visible everywhere in your program.
#include <stdio.h>
int my_global_var = 0;
int
main(void)
{
printf(“%dn”, my_global_var);
return 0;
}
global_temp is a global variable that is visible to everything in your program, although to make it visible in other modules, you’d need an” extern int global_temp;” in other source files if you have a multi-file project.
A static variable has a local scope, but its variables are not allocated in the stack segment of the memory. It can have less than global scope, although – like global variables – it resides in the .bss segment of your compiled binary.
#include <stdio.h>
int
myfunc(int val)
{
static int my_static_var = 0;
my_static_var += val;
return my_static_var;
}
int
main(void)
{
int myval;
myval = myfunc(1);
printf(“first call %dn”, myval);
myval = myfunc(10);
printf(“second call %dn”, myval);
return 0;
}
9. Difference between const char* p and char const* p?
- const char* p is a pointer to a const char.
- char const* p is a pointer to a char const.
Since const char and char const are the same, it’s the same.
10. What is pointer to pointer in C?
In C, a pointer can also be used to store the address of another pointer. A double pointer or pointer to pointer is such a pointer. The address of a variable is stored in the first pointer, whereas the address of the first pointer is stored in the second pointer.
The syntax of declaring a double pointer is given below:
int **p; // pointer to a pointer which is pointing to an integer
11. Why n++ executes faster than n+1 ?
n++ being a unary operation, it just needs one variable. Whereas, n = n + 1 is a binary operation that adds overhead to take more time (also binary operation: n += 1). However, in modern platforms, it depends on few things such as processor architecture, C compiler, usage in your code, and other factors such as hardware problems.
While in the modern compiler even if you write n = n + 1 it will get converted into n++ when it goes into the optimized binary, and it will be equivalently efficient.
12. What are the advantages of Macro over function?
Macro on a high-level copy-paste, its definitions to places wherever it is called. Due to which it saves a lot of time, as no time is spent while passing the control to a new function and the control is always with the callee function. However, one downside is the size of the compiled binary is large but once compiled the program comparatively runs faster.
13. What is the difference between #include “…” and #include <…>?
In practice, the difference is in the location where the preprocessor searches for the included file.
- For #include <filename> the C preprocessor looks for the filename in the predefined list of system directories first and then to the directories told by the user(we can use -I option to add directories to the mentioned predefined list).
- For #include “filename” the preprocessor searches first in the same directory as the file containing the directive, and then follows the search path used for the #include <filename> form. This method is normally used to include programmer-defined header files.
14. What is a near pointer and a far pointer in C?
- Near Pointer: In general, the near pointer can be considered because it is used to hold the address, which has a maximum size of just 16 bits. We can’t store an address with a size larger than 16 bits using the near pointer. All other smaller addresses that are within the 16-bit limit, on the other hand, can be stored. Because we can only access 64kb of data at a time, you might assume the 16 bits are insufficient. As a result, it is regarded as one of the near-pointer’s biggest drawbacks, which is why it is no longer commonly used.
- Far Pointer: A far pointer is considered a pointer of size 32 bits. It can, however, use the current segment to access information stored outside the computer’s memory. Although, in order to use this type of pointer, we usually need to allocate the sector register to store the data address in the current segment
15. When is the “void” keyword used in a function
The keyword “void” is a data type that literally represents no data at all. The most obvious use of this is a function that returns nothing:
void PrintHello()
{
printf(“Hellon”);
return; // the function does “return”, but no value is returned
}
Here we’ve declared a function, and all functions have a return type. In this case, we’ve said the return type is “void”, and that means, “no data at all” is returned.
The other use for the void keyword is a void pointer. A void pointer points to the memory location where the data type is undefined at the time of variable definition. Even you can define a function of return type void* or void pointer meaning “at compile time we don’t know what it will return” Let’s see an example of that.
void MyMemCopy(void* dst, const void* src, int numBytes)
{
char* dst_c = reinterpret_cast<char*>(dst);
const char* src_c = reinterpret_cast<const char*>(src);
for (int i = 0; i < numBytes; ++i)
dst_c[i] = src_c[i];
}