C Programming

C Programming Interview Questions and Answer and Answer

Q. What are the different types of pointers used in C language?

A.

There are three types of pointers used in C language. These pointers are based on the old system architecture. The types are as follows:

  1. Near pointer: this is the pointer that points only to the data segment. There is a restriction of using it beyond the data segment. The near pointer can be made by using the keyword as "near".
  2. Far pointer: it will access the total memory of the system and can be use to point to every object used in the memory.
  3. Wild pointer: it is a pointer that is not being initialized.

Q. What is a dangling pointer?

A.

A dangling pointer is one that has a value (not NULL) which refers to some memory which is not valid for the type of object you expect. For example if you set a pointer to an object then overwrote that memory with something else unrelated or freed the memory if it was dynamically allocated.

Q. What do the 'c' and 'v' in argc and argv stand for?

A.

The c in argument count, argc stands for the number of the command line argument that the program is invoked with.

And v in argument vector, argv is a pointer to an array of the character string that contains the argument.

Q. What are storage classes in C?

A.

  1. Automatic storage classes : Variable with block scope but without static specifier.
  2. Static storage classes: Variables with block scope and with static specifier. Global variables with or without static specifier.
  3. Allocated storage classes: Memory obtained from calls to malloc(), alloc() or realloc().

Q. How do you initialize pointer variables?

A.

Pointer variables are initialized in two ways:

  • By static memory allocation
  • By dynamic memory allocation

Q. What is the purpose of main() function?

A.

  • Execution starts from main() in C
  • It can contain any number of statements
  • Statements are executed in the sequence in which they are written
  • It can call other functions. The control is passed to that function

E.g. int main();
OR
int main(int argc, char *argv[]);

Q. Explain argument and its types.

A.

Argument : An expression which is passed to a function by its caller for the function to perform its task.

Arguments are of two types: actual and formal.

Actual argument: These are the arguments passed in a function call. They are defined in the calling function.

Formal argument: Formal arguments are the parameters in a function declaration. Their scope is local to the function definition. They belong to the function being called and are a copy of actual arguments. Change in formal argument doesn't get reflected in actual argument.

Q. What are pointers? Why are they used?

A.

Pointers are variables which store the address of other variables.

Advantages of using pointers:

  • They allow to pass values to functions using call by reference - especially useful while large sized arrays are passed as arguments to functions.
  • They allow dynamic allocation of memory.
  • They help in resizing the data structures.

Q. How can I change the size of the dynamically allocated array?

A.

You can change the size of the array if it is defined dynamically, by using the realloc() function. realloc() function's syntax is written as:

dynarray = realloc(dynarray, 20 * sizeof(int));

But, realloc() function can't just enlarge the memory, and this memory allocation depends on the space available in the system. If realloc() can?t find the space available, then it returns null pointer.

Q. How to write a multi-statement macro ?

A.

Macro are simple statements which are written to, dynamically execute the method or function. The function call syntax is:

MACRO(arg1, arg2);

Macro can be included with the conditional statements using the external else clause. You can include simple expressions in macro with no declarations or loops like:

#define FUNC(arg1, arg2) (expr1, expr2, expr3)

Q. Why can't I perform arithmetic on a void* pointer?

A.

There are no arithmetic operations that can be performed on void* pointer because the compiler doesn’t know the size of the pointed objects. Arithmetic operations can only be done on the pointed objects. Some compilers make it an exception to perform the arithmetic functions on the pointer.

Q. How can I return multiple values from a function?

A.

A function can return multiple values in several ways by using the pointers. These include the examples like hypothetical calculation of polar-to-rectangular coordinate functions. The pointer allows you to have a function in which you can pass multiple values and use them.

Example code:

polar_to_rectangular(double a, double b, double *xp, double *yp)
{	
  a  = 10;
  b  = 20;
 *xp = a;
 *yp = b;
}

Q. Can "this" pointer by used in the constructor?

A.

It is not advised to use "this" pointer inside the constructor, because of the object initialization, as it takes more time for the object to be ready for the execution. You can use "this" pointer in the constructor body part and also in the initialization list. The rules of the programming language have to be known before using "this" operator in the constructor.

Q. What is break statement?

A.

A break statement causes the loop to terminate. The control then passes to the statement following the body of the loop.

Q. What is the difference between a string copy (strcpy) and a memory copy (memcpy)? When should each be used?

A.

The strcpy() function is designed to work exclusively with strings. It copies each byte of the source string to the destination string and stops when the terminating null character () has been moved. On the other hand, the memcpy() function is designed to work with any type of data.

Because not all data ends with a null character, you must provide the memcpy() function with the number of bytes you want to copy from the source to the destination. The following program shows examples of both the strcpy() and the memcpy() functions:

#include 
#include 
typedef struct cust_str {
     int  id;
     char last_name[20];
     char first_name[15];
} CUSTREC;
void main(void);
void main(void)
{
     char*   src_string = "This is the source string";
     char    dest_string[50];
     CUSTREC src_cust;
     CUSTREC dest_cust;
     printf("Hello!  I'm going to copy src_string into dest_string!
");
     /* Copy src_string into dest_string. Notice that the destination
        string is the first argument. Notice also that the strcpy()
        function returns a pointer to the destination string. */
     printf("Done! dest_string is: %s
",
            strcpy(dest_string, src_string));
     printf("Encore! Let's copy one CUSTREC to another.
");
     printf("I'll copy src_cust into dest_cust.
");
     /* First, initialize the src_cust data members. */
     src_cust.id = 1;
     strcpy(src_cust.last_name, "Strahan");
     strcpy(src_cust.first_name, "Troy");
     /* Now, use the memcpy() function to copy the src_cust structure to
        the dest_cust structure. Notice that, just as with strcpy(), the
        destination comes first. */
     memcpy(&dest_cust, &src_cust, sizeof(CUSTREC));
     printf("Done! I just copied customer number #%d (%s %s).",
               dest_cust.id, dest_cust.first_name, dest_cust.last_name);
}

When dealing with strings, you generally should use the strcpy() function, because it is easier to use with strings. When dealing with abstract data other than strings (such as structures), you should use the memcpy() function.

Q. What are static functions? What is their use?

A.

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. Therefore, 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.

Q. What is Dangling pointer?

A.

Dangling Pointer is a pointer that doesn’t point to a valid memory location. Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory.

Q. What is NULL pointer?

A.

NULL is used to indicate that the pointer doesn’t point to a valid location. Ideally, we should initialize pointers as NULL if we don’t know their value at the time of declaration. Also, we should make a pointer NULL when memory pointed by it is deallocated in the middle of a program.

Q. When should we use pointers in a C program?

A.

  1. To get address of a variable
  2. For achieving pass by reference in C: Pointers allow different functions to share and modify their local variables.
  3. To pass large structures so that complete copy of the structure can be avoided.
  4. To implement "linked" data structures like linked lists and binary trees.

Q. Write a C program to print ";" without using a semicolon.

A.

#include 
int main() 
{    
   // ASCII value of ; is 59 
   if (printf("%c", 59)) 
   { 
   } 
} 

Q. How will you print "Hello World" without semicolon?

A.

int main(void) 
{ 
    if (printf("Hello World")) ; 
}

Q. What are different storage class specifiers in C?

A.

auto, register, static, extern



Python if , elif and else

Python Conditions and If statements

  • 0
Python for beginners

Learning Python Part 1

  • 3
Struct Alignment and Padding

Struct Alignment and Padding in C++ And C

  • 0
Friend function

Friend function C++

  • 0
The diamond problem Solution C++

Solving the Diamond Problem with Virtual Inheritance

  • 0
Pointers

C++ Pointers

  • 0
Structures

C++ Structures

  • 0
Types of Inheritance in C++

Inheritance and access specifiers C++

  • 0
Java date pattern

Java Date Pattern Syntax

  • 0
Java Date and Calendar

Java Date formats

  • 0
JAVA Data Type

Data types in Java

  • 0
Java unreachable code

Unreachable Code Error in Java

  • 0
INTERVIEW EXPERIENCES

Articles

09

FEB

×

Forgot Password

Please enter your email address below and we will send you information to change your password.