Top

C Programming

71.

Write a C program to print hello world without using a semicolon (;).

Ans:

#include      
void main()
{      
      if(printf("hello world")){} 
}

Output:

hello world

72.

How can you print a string with the symbol % in it?

Ans:

There is no escape sequence provided for the symbol % in C. So, to print % we should use ‘%%’ as shown below.

printf(“there are 90%% chances of rain tonight”);

 

73.

Explain the # pragma directive.

Ans:

The following points explain the Pragma Directive.

  • This is a preprocessor directive that can be used to turn on or off certain features.
  • It is of two types #pragma startup, #pragma exit, and pragma warn.
  • #pragma startup allows us to specify functions called upon program startup.
  • #pragma exit allows us to specify functions called upon program exit.
  • #pragma warn tells the computer to suppress any warning or not. 

74.

Which structure is used to link the program and the operating system?

Ans:

The answer can be explained through the following points,

  • The structure used to link the operating system to a program is a file.
  • The file is defined in the header file “stdio.h”(standard input/output header file).
  • It contains information about the file being used, its current size, and its location in memory.
  • It contains a character pointer that points to the character that is being opened.
  • Opening a file establishes a link between the program and the operating system about which file is to be accessed.

75.

Differentiate between the macros and the functions.

Ans:

The differences between macros and functions can be explained as follows:

  • Macro call replaces the templates with the expansion in a literal way.
  • The Macro call makes the program run faster but also increases the program size.
  • Macro is simple and avoids errors related to the function calls.
  • In a function, call control is transferred to the function along with arguments.
  • It makes the functions small and compact.
  • Passing arguments and getting back the returned value takes time and makes the program run at a slower rate.

76.

What are the limitations of scanf() and how can it be avoided?

Ans:

The Limitations of scanf() are as follows:

  • scanf() cannot work with the string of characters.
  • It is not possible to enter a multiword string into a single variable using scanf().
  • To avoid this the gets( ) function is used.
  • It gets a string from the keyboard and is terminated when enter key is pressed.
  • Here the spaces and tabs are acceptable as part of the input string.

77.

Explain toupper() with an example.

Ans:

toupper() is a function designed to convert lowercase words/characters into upper case.

Example:

#include
#include
int main()
{
    char c;
    c=a;
    printf("?fter conversions  %c", c, toupper(c));
    c=B;
    printf("?fter conversions  %c", c, toupper(c));
}

Output:

a after conversions A
B after conversions B

78.

What is Dynamic Memory allocation? Mention the syntax.

Ans:

Dynamic Memory Allocation is the process of allocating memory to the program and its variables in runtime. Dynamic Memory Allocation process involves three functions for allocating memory and one function to free the used memory.

malloc() – Allocates memory

Syntax:

ptr = (cast-type*) malloc(byte-size);

calloc() – Allocates memory

Syntax:

ptr = (cast-type*)calloc(n, element-size);

realloc() – Allocates memory

Syntax:

ptr = realloc(ptr, newsize);

free() – Deallocates the used memory

Syntax:

free(ptr);

 

79.

Which built-in library function can be used to re-size the allocated dynamic memory?

Ans:

realloc().

80.

What do you mean by Memory Leak?

Ans:

Memory Leak can be defined as a situation where programmer allocates dynamic memory to the program but fails to free or delete the used memory after the completion of the code. This is harmful if daemons and servers are included in the program.

#include
#include
int main()
{
     int* ptr;
     int n, i, sum = 0;
     n = 5;
     printf("Enter the number of elements: %dn", n);
     ptr = (int*)malloc(n * sizeof(int));
     if (ptr == NULL)
     {
            printf("Memory not allocated.n");
            exit(0);
     }
     else
     {
            printf("Memory successfully allocated using malloc.n");
            for (i = 0; i<= n; ++i)
            {
                  ptr[i] = i + 1;
             }
             printf("The elements of the array are: ");
             for (i = 0; i<=n; ++i)
            {
                  printf("%d, ", ptr[i]);
            }
     }
     return 0;
}

Output:

Enter the number of elements: 5
Memory successfully allocated using malloc.
The elements of the array are: 1, 2, 3, 4, 5,

Loading…
Tags: C Programming Interview Questions and Answers || C Programming Sort Questions and Answers || C Programming Detailed Questions and Answers || C Programming Tutorial