C programming questions
Happy Reading...😊
1. What are the main characteristics of c programming language?
Simplicity - c has rich collections of in-built functions, keywords and data types that make the program simple.
clarity - the keywords, function, and data types are in simple English language
modularity- Dividing the program into subprograms (modules/function) to achieve the given task is the modular approach.
portability - the program can run in different machines ie., computer independent
2. What are the different ways of passing parameters to the functions?
Call by value − We send only values to the function as parameters. We choose this if we do not want the actual parameters to be modified with formal parameters but just used.
Call by reference − We send the address of the actual parameters instead of values. We choose this if we do want the actual parameters to be modified with formal parameters.
3. What is the difference in including the header file with <> and " "?
If the header file is included within in <> then the compiler searches for the particular header file only within built-in include path.
If a header file is included with " ", then the compiler searches for the particular header file first in the current working directory, if not found then in the built-in include path.
4.What are the valid places for the keyword break that can appear?
The break can appear only with looping and control statements. The use of the break statement is to bring the control out of the loop.
5. Distinguish between malloc and calloc?
6.What is pointer to pointer?
A pointer to a pointer is the first pointer which contains the address of the second pointer, which points to the location that contains the actual value
example:-Eg: int x = 5, *p=&x, **q=&p;
7.What does printf() and scanf() returns?
printf() return the number of characters that have to be printed and scanf() return the number of the inputted character.
8. How does recursion works?
Each time when the function is called the value will be stored in the stack when the termination occurs the values are popped and returned.
9. What is the static variable?
A static local variable retains its value between the function call and the default value is 0. The following function will print 1 2 3 if called thrice.
void f() {
static int i;
++i;
printf(“%d “,i);
}
10. What is a dangling pointer?
If a variable is used most frequently then it should be declared using register storage specifier, then possibly the compiler gives CPU register for its storage to speed up the lookup of the variable.
12. What is the remainder of 5.0%2?
Error, It is invalid that either of the operands for the modulus operator (%) is a real number.
13. What are the formal and actual parameters?
The parameters sent to the function at calling end are called actual parameters while at the receiving of the function definition called formal parameters.
14. What are self-referential structures?
A structure containing the same structure pointer variable as its element is called a self-referential structure.
15. What is the output of the code
char *s1 = "hello",*s2 = "welcome";
strcat(s1,s2);
Error, s1 points to a string constant and cannot be altered.
18.How will you print “Hello World” without semicolon?
#include <stdio.h>
int main(void)
{
if (printf("Hello World")) {
}
}
When we want to restrict access to functions, we make them static. Another reason for making functions static can be reuse of the same function name in other files.
30.What does getchar() return?
It return int that contains end of line special character ascii value.Which says the failure and terminates the console.
1. What are the main characteristics of c programming language?
Simplicity - c has rich collections of in-built functions, keywords and data types that make the program simple.
clarity - the keywords, function, and data types are in simple English language
modularity- Dividing the program into subprograms (modules/function) to achieve the given task is the modular approach.
portability - the program can run in different machines ie., computer independent
2. What are the different ways of passing parameters to the functions?
Call by value − We send only values to the function as parameters. We choose this if we do not want the actual parameters to be modified with formal parameters but just used.
Call by reference − We send the address of the actual parameters instead of values. We choose this if we do want the actual parameters to be modified with formal parameters.
3. What is the difference in including the header file with <> and " "?
If the header file is included within in <> then the compiler searches for the particular header file only within built-in include path.
If a header file is included with " ", then the compiler searches for the particular header file first in the current working directory, if not found then in the built-in include path.
4.What are the valid places for the keyword break that can appear?
The break can appear only with looping and control statements. The use of the break statement is to bring the control out of the loop.
5. Distinguish between malloc and calloc?
6.What is pointer to pointer?
A pointer to a pointer is the first pointer which contains the address of the second pointer, which points to the location that contains the actual value
example:-Eg: int x = 5, *p=&x, **q=&p;
7.What does printf() and scanf() returns?
printf() return the number of characters that have to be printed and scanf() return the number of the inputted character.
8. How does recursion works?
Each time when the function is called the value will be stored in the stack when the termination occurs the values are popped and returned.
9. What is the static variable?
A static local variable retains its value between the function call and the default value is 0. The following function will print 1 2 3 if called thrice.
void f() {
static int i;
++i;
printf(“%d “,i);
}
10. What is a dangling pointer?
A pointer pointing to a deleted memory location
1.Return local variable in the function call
#include<stdio.h>
#include<string.h>
char *getHello()
{
char str[10];
strcpy(str,"Hello!");
return(str);
}
int main()
{
//str falls out of scope
//function call char *getHello() is now a dangling pointer
printf("%s", getHello());
}
2.Variables go out of scope
#include<stdio.h>
int main()
{
char **strPtr;
{
char *str = "Hello!";
strPtr = &str;
}
// str falls out of scope
// strPtr is now a dangling pointer
printf("%s", *strPtr);
}
3. de-allocating variable memory
#include<stdio.h>
#include<stdlib.h>
int main()
{
char **strPtr;
char *str = "Hello!";
strPtr = &str;
free(str);
//strPtr now becomes a dangling pointer
printf("%s", *strPtr);
}
11. When should we use a register storage specifier?1.Return local variable in the function call
#include<stdio.h>
#include<string.h>
char *getHello()
{
char str[10];
strcpy(str,"Hello!");
return(str);
}
int main()
{
//str falls out of scope
//function call char *getHello() is now a dangling pointer
printf("%s", getHello());
}
2.Variables go out of scope
#include<stdio.h>
int main()
{
char **strPtr;
{
char *str = "Hello!";
strPtr = &str;
}
// str falls out of scope
// strPtr is now a dangling pointer
printf("%s", *strPtr);
}
3. de-allocating variable memory
#include<stdio.h>
#include<stdlib.h>
int main()
{
char **strPtr;
char *str = "Hello!";
strPtr = &str;
free(str);
//strPtr now becomes a dangling pointer
printf("%s", *strPtr);
}
If a variable is used most frequently then it should be declared using register storage specifier, then possibly the compiler gives CPU register for its storage to speed up the lookup of the variable.
12. What is the remainder of 5.0%2?
Error, It is invalid that either of the operands for the modulus operator (%) is a real number.
13. What are the formal and actual parameters?
The parameters sent to the function at calling end are called actual parameters while at the receiving of the function definition called formal parameters.
14. What are self-referential structures?
A structure containing the same structure pointer variable as its element is called a self-referential structure.
15. What is the output of the code
char *s1 = "hello",*s2 = "welcome";
strcat(s1,s2);
Error, s1 points to a string constant and cannot be altered.
16. What are preprocessor directives?
Preprocessors directive are programs that preprocess our source code before compilation.
Types Syntax
Macro: #define
Header file: #include <file_name>
Conditional : #ifdef, #endif, #if, #else, #ifndef
Other: #undef, #pragma
17. What are the different storage class specifiers in C?
17. What are the different storage class specifiers in C?
18.How will you print “Hello World” without semicolon?
#include <stdio.h>
int main(void)
{
if (printf("Hello World")) {
}
}
19. What are the local static variables? What is their use?
Ans: A local static variable is a variable whose lifetime doesn’t end with a function call where it is declared. It extends for the lifetime of the complete program.
Static variables can be used to count the number of times a function is called. Also, static variables get the default value is 0.
20.What are static functions? What is their use?
In C, functions are global by default. The “static” keyword before a function name makes it static. Unlike global functions in C, access to static functions is restricted to the file where they are declared. When we want to restrict access to functions, we make them static. Another reason for making functions static can be reuse of the same function name in other files.
30.What does getchar() return?
It return int that contains end of line special character ascii value.Which says the failure and terminates the console.
Comments
Post a Comment