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  

}  

Top Blogs
C Functions ! What is a Function Published at:- Types of Function in C ! Library Function in C ! User Defined Function In C ! Function Definition Published at:- Functions that return multiple values -C Example Published at:- Functions with arguments and return values -C Examples Published at:- Functions with arguments and no return values. Published at:- Example of Function with no return type and no argument Published at:- Loops in C Published at:- Structure in C: Introduction Published at:- C Memory Management ! Dynamic memory allocation Published at:- Learn C Programming language with example Published at:- C Interview Questions And Answers Published at:- What values are printed when we run following? Published at:- C Program example: Input a number and print sum of its digits Published at:- Pointer declaration in C ,Address operator Published at:- C Language Interview Question and Answers Published at:- Benefits of C language over other programming languages Published at:- History of C Language : Introduction to C Programming Language Published at:- How does C Programming Language Work Published at:- Importance of C Programming Language Published at:- Input and Output Functions in C Published at:- Introduction to Implementation of Queue using Linked List Published at:- First C Program Published at:- Inception Of C Language Tutorial for Beginners Published at:- The C Compiler work in C language and its important Published at:- Program Structure with “Hello World” Example Published at:- C Programming Interview Questions Set 1 Published at:- C Programming Interview Questions Set 2 Published at:- C Programming Interview Questions Set 3 Published at:- C Programming Interview Questions Set 4 Published at:- C Programming Interview Questions Set 5 Published at:- C Programming Interview Questions Set 6 Published at:- C Programming Interview Questions Set 7 Published at:- C Programming Interview Questions Set 8 Published at:- C Programming Interview Questions Set 9 Published at:-
R4R.co.in Team
The content on R4R is created by expert teams.