1. 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].
2. What is a built-in function in C?
The most commonly used built-in functions in C are sacnf(), printf(), strcpy,
strlwr, strcmp, strlen, strcat, and many more.
Built-function is also known as library functions that are provided by the
system to make the life of a developer easy by assisting them to do certain
commonly used predefined tasks. For example, if you need to print output or
your program into the terminal, we use printf() in C.
3. In C, What is the #line used for?
In C, #line is used as a preprocessor to re-set the line number in the code,
which takes a parameter as line number. Here is an example for the same.
#include <stdio.h> /
*line 1*/
int main(){ /
*line 3*/
printf("Nello world\n"); /*line 5*
/
//print current line /*line 6*
/
printf("Line: %d\n",_LINE_); /*line 7*/
//reset the line number by 36 /*line 8*/
#line 36 /*reseting*/
, //print current line /*line 36
*/
printf("Line: %d\n",_LINE_); /*line 37*/
printf("Bye bye!!!\n"); /*line 39
*/
return 0; /
*line 41*/
}
4. 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.
, Converting String to
Number
5. How can a number be converted to a string?
The function takes a pointer to an array of char elements that need to be
converted, and a format string needs to be written in a buffer as a string
int sprintf(char *str, const char *format, ...)
#include<stdio.h>
#include <math.h>
int main()
{
char str[80];
sprintf(str, "The value of PI = %f", M_PI);
puts(str);
return 0;
}
Below is the output after running the above code:
Output: Value of Pi = 3.141593
6. 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
7. 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("%d\n", 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.