C Programming Interview Questions Set 6
Categories: C Programming language
Ques. What is a union?
Ans. The union is a user-defined data type that allows storing multiple types of data in a single unit. However, it doesn't occupy the sum of the memory of all members. It holds the memory of the largest member only.
In union, we can access only one variable at a time as it allocates one common space for all the members of a union.
Syntax of union
union union_name
{
Member_variable1;
Member_variable2;
.
.
Member_variable n;
}[union variables];
Let's see a simple example
#include<stdio.h>
union data
{
int a; //union members declaration.
float b;
char ch;
};
int main()
{
union data d; //union variable.
d.a=3;
d.b=5.6;
d.ch='a';
printf("value of a is %d",d.a);
printf("\n");
printf("value of b is %f",d.b);
printf("\n");
printf("value of ch is %c",d.ch);
return 0;
}
Output:
value of a is 1085485921
value of b is 5.600022
value of ch is a
Ques. What is an auto keyword in C?
Ans. In C, every local variable of a function is known as an automatic (auto) variable. Variables which are declared inside the function block are known as a local variable. The local variables are also known as an auto variable. It is optional to use an auto keyword before the data type of a variable. If no value is stored in the local variable, then it consists of a garbage value.
Ques What is the purpose of sprintf() function?
Ans. The sprintf() stands for "string print." The sprintf() function does not print the output on the console screen. It transfers the data to the buffer. It returns the total number of characters present in the string.
Syntax
int sprintf ( char * str, const char * format, ... );
Let's see a simple example
#include<stdio.h>
int main()
{
char a[20];
int n=sprintf(a,"javaToint");
printf("value of n is %d",n);
return 0;}
Output:
value of n is 9
Ques Can we compile a program without main() function?
Ans. Yes, we can compile, but it can't be executed.
But, if we use #define, we can compile and run a C program without using the main() function. For example:
#include<stdio.h>
#define start main
void start() {
printf("Hello");
}
Ques. What is a token?
Ans. The Token is an identifier. It can be constant, keyword, string literal, etc. A token is the smallest individual unit in a program. C has the following tokens:
Identifiers: Identifiers refer to the name of the variables.
Keywords: Keywords are the predefined words that are explained by the compiler.
Constants: Constants are the fixed values that cannot be changed during the execution of a program.
Operators: An operator is a symbol that performs the particular operation.
Special characters: All the characters except alphabets and digits are treated as special characters.
Ques. What is command line argument?
The argument passed to the main() function while executing the program is known as command line argument. For example:
main(int count, char *args[]){
//code to be executed
}