ATOS Frequently Asked C Language Interview Questions Answers

When is a “switch” statement preferable over an “if” statement?

The switch statement is best used when dealing with selections based on a single variable or expression. However, switch statements can only evaluate integer and character data types.

What are global variables and how do you declare them?

Global variables are variables that can be accessed and manipulated anywhere in the program. To make a variable global, place the variable declaration on the upper portion of the program, just after the preprocessor directives section.

What are enumerated types?

Enumerated types allow the programmer to use more meaningful words as values to a variable. Each item in the enumerated type variable is actually associated with a numeric code. For example, one can create an enumerated type variable named DAYS whose values are Monday, Tuesday… Sunday.

What does the function toupper() do?

It is used to convert any letter to its upper case mode. Toupper() function prototype is declared in <ctype.h>. Note that this function will only convert a single character, and not an entire string.

Is it possible to have a function as a parameter in another function?

Yes, that is allowed in C programming. You just need to include the entire function prototype into the parameter field of the other function where it is to be used.

What are multidimensional arrays?

Multidimensional arrays are capable of storing data in a two or more dimensional structure. For example, you can use a 2 dimensional array to store the current position of pieces in a chess game, or position of players in a tic-tac-toe program.

Which function in C can be used to append a string to another string?

The strcat function. It takes two parameters, the source string and the string value to be appended to the source string.

What is the difference between functions getch() and getche()?

Both functions will accept a character input value from the user. When using getch(), the key that was pressed will not appear on the screen, and is automatically captured and assigned to a variable. When using getche(), the key that was pressed by the user will appear on the screen, while at the same time being assigned to a variable.

Dothese two program statements perform the same output? 1) scanf(“%c”, &letter);  2) letter=getchar()

Yes, they both do the exact same thing, which is to accept the next key pressed by the user and assign it to variable named letter.

What is the advantage of a random access file?

If the amount of data stored in a file is fairly large, the use of random access will allow you to search through it quicker. If it had been a sequential access file, you would have to go through one record at a time until you reach the target data. A random access file lets you jump directly to the target address where data is located.
ATOS Frequently Asked C Language Interview Questions Answers
ATOS Frequently Asked C Language Interview Questions Answers

In a switch statement, what will happen if a break statement is omitted?

If a break statement was not placed at the end of a particular case portion? It will move on to the next case portion, possibly causing incorrect output.

Describe how arrays can be passed to a user defined function

One thing to note is that you cannot pass the entire array to a function. Instead, you pass to it a pointer that will point to the array first element in memory. To do this, you indicate the name of the array without the brackets.

What are pointers?

Pointers point to specific areas in the memory. Pointers contain the address of a variable, which in turn may contain a value or even an address to another memory.

Can you pass an entire structure to functions?

Yes, it is possible to pass an entire structure to a function in a call by method style. However, some programmers prefer declaring the structure globally, then pass a variable of that structure type to a function. This method helps maintain consistency and uniformity in terms of argument type.

What is gets() function?

The gets() function allows a full line data entry from the user. When the user presses the enter key to end the input, the entire line of characters is stored to a string variable. Note that the enter key is not included in the variable, but instead a null terminator \0 is placed after the last character.

The % symbol has a special use in a printf statement. How would you place this character as part of the output on the screen?

You can do this by using %% in the printf statement. For example, you can write printf(“10%%”) to have the output appear as 10% on the screen.

How do you search data in a data file using random access method?

Use the fseek() function to perform random access input/ouput on a file. After the file was opened by the fopen() function, the fseek would require three parameters to work: a file pointer to the file, the number of bytes to search, and the point of origin in the file.

Are comments included during the compilation stage and placed in the EXE file as well?

No, comments that were encountered by the compiler are disregarded. Comments are mostly for the guidance of the programmer only and do not have any other significant use in the program functionality.

Is there a built-in function in C that can be used for sorting data?

Yes, use the qsort() function. It is also possible to create user defined functions for sorting, such as those based on the balloon sort and bubble sort algorithm.

What are the advantages and disadvantages of a heap?

Storing data on the heap is slower than it would take when using the stack. However, the main advantage of using the heap is its flexibility. That’s because memory in this structure can be allocated and remove in any particular order. Slowness in the heap can be compensated if an algorithm was well designed and implemented.

How do you convert strings to numbers in C?

You can write you own functions to do string to number conversions, or instead use C’s built in functions. You can use atof to convert to a floating point value, atoi to convert to an integer value, and atol to convert to a long integer value.

Create a simple code fragment that will swap the values of two variables num1 and num2.

int temp;

temp = num1;

num1 = num2;

num2 = temp;

Write a program to print Fibonacci series using recursion?

#include<stdio.h> 
#include<conio.h> 
void printFibonacci(int n) // function to calculate the fibonacci series of a given number.

static int n1=0,n2=1,n3;    // declaration of static variables.
    if(n>0){ 
         n3 = n1 + n2; 
         n1 = n2; 
        n2 = n3; 
         printf("%d ",n3); 
         printFibonacci(n-1);    //calling the function recursively.
    } 

void main(){ 
    int n; 
    clrscr(); 
    printf("Enter the number of elements: "); 
    scanf("%d",&n); 
    printf("Fibonacci Series: "); 
    printf("%d %d ",0,1); 
    printFibonacci(n-2);//n-2 because 2 numbers are already printed 
    getch(); 


Write a program to check prime number in C Programming?

#include<stdio.h> 
#include<conio.h> 
void main() 

int n,i,m=0,flag=0;    //declaration of variables.
clrscr();    //It clears the screen.
printf("Enter the number to check prime:"); 
scanf("%d",&n); 
m=n/2; 
for(i=2;i<=m;i++) 

if(n%i==0) 

printf("Number is not prime"); 
flag=1; 
break;    //break keyword used to terminate from the loop.


if(flag==0) 
printf("Number is prime"); 
getch();    //It reads a character from the keyword.
}

Write a program to check palindrome number in C Programming?

#include<stdio.h>
#include<conio.h>
main()
{
int n,r,sum=0,temp;
clrscr();
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
printf("palindrome number ");
else
printf("not palindrome");
getch();
}

Mention The Characteristics Of Arrays In C?

An array holds elements that have the same data type.
Array elements are stored in subsequent memory locations.
Two-dimensional array elements are stored row by row in subsequent memory locations.
Array name represents the address of the starting element.
Array size should be mentioned in the declaration. Array size must be a constant expression and not a variable.
Differentiate Between A Linker And Linkage?

A linker converts an object code into an executable code by linking together the necessary build in functions. The form and place of declaration where the variable is declared in a program determine the linkage of variable.

What Are The Advantages Of Auto Variables?

The same auto variable name can be used in different blocks.
There is no side effect by changing the values in the blocks.
The memory is economically used.
Auto variables have inherent protection because of local scope.

What Is Storage Class And What Are Storage Variable?

A storage class is an attribute that changes the behavior of a variable. It controls the lifetime, scope and linkage. There are five types of storage classes.

auto.
static.
extern.
register.
typedef.

Which Expression Always Return True? Which Always Return False?

expression if (a=0) always return false.
expression if (a=1) always return true.

Is It Possible To Execute Code Even After The Program Exits The Main () Function?

The standard C library provides a function named at exit () that can be used to perform “cleanup” operations when your program terminates. You can set up a set of functions you want to perform automatically when your program exits by passing function pointers to the at exit() function.

Why Should I Prototype A Function?

A function prototype tells the compiler what kind of arguments a function is looking to receive and what kind of return value a function is going to give back. This approach helps the compiler ensure that calls to a function are made correctly and that no erroneous type conversions are taking place.

How Do You Print An Address?

The safest way is to use printf () (or fprintf() or sprintf()) with the %P specification. That prints a void pointer (void*). Different compilers might print a pointer with different formats. Your compiler will pick a format that’s right for your environment.

If you have some other kind of pointer (not a void*) and you want to be very safe, cast the pointer to a void*

printf (“%Pn”, (void*) buffer);

Can Math Operations Be Performed On A Void Pointer?

No. Pointer addition and subtraction are based on advancing the pointer by a number of elements. By definition, if you have a void pointer, you don’t know what it’s pointing to, so you don’t know the size of what it’s pointing to. If you want pointer arithmetic to work on raw addresses, use character pointers.

How Can You Determine The Size Of An Allocated Portion Of Memory?

You can’t, really free() can , but there’s no way for your program to know the trick free() uses. Even if you disassemble the library and discover the trick, there’s no guarantee the trick won’t change with the next release of the compiler.

What Is A "null Pointer Assignment" Error? What Are Bus Errors, Memory Faults, And Core Dumps?

These are all serious errors, symptoms of a wild pointer or subscript. Null pointer assignment is a message you might get when an MS-DOS program finishes executing. Some such programs can arrange for a small amount of memory to be available “where the NULL pointer points to” (so to speak). If the program tries to write to that area, it will overwrite the data put there by the compiler. When the program is done, code generated by the compiler examines that area. If that data has been changed, the compiler-generated code complains with null pointer assignment.

This message carries only enough information to get you worried. There’s no way to tell, just from a null pointer assignment message, what part of your program is responsible for the error. Some debuggers, and some compilers, can give you more help in finding the problem. Bus error: core dumped and Memory fault: core dumped are messages you might see from a program running under UNIX. They’re more programmers friendly. Both mean that a pointer or an array subscript was wildly out of bounds. You can get these messages on a read or on a write. They aren’t restricted to null pointer problems.

The core dumped part of the message is telling you about a file, called core that has just been written in your current directory. This is a dump of everything on the stack and in the heap at the time the program was running. With the help of a debugger, you can use the core dump to find where the bad pointer was used. That might not tell you why the pointer was bad, but it’s a step in the right direction. If you don’t have write permission in the current directory, you won’t get a core file, or the core dumped message.

What Is The Heap?

The heap is where malloc(), calloc(), and realloc() get memory.

Getting memory from the heap is much slower than getting it from the stack. On the other hand, the heap is much more flexible than the stack. Memory can be allocated at any time and deallocated in any order. Such memory isn’t deallocated automatically; you have to call free ().

Recursive data structures are almost always implemented with memory from the heap. Strings often come from there too, especially strings that could be very long at runtime. If you can keep data in a local variable (and allocate it from the stack), your code will run faster than if you put the data on the heap. Sometimes you can use a better algorithm if you use the heap—faster, or more robust, or more flexible. It’s a tradeoff.

If memory is allocated from the heap, it’s available until the program ends. That’s great if you remember to deallocate it when you’re done. If you forget, it’s a problem. A “memory leak” is some allocated memory that’s no longer needed but isn’t deallocated. If you have a memory leak inside a loop, you can use up all the memory on the heap and not be able to get any more. (When that happens, the allocation functions return a null pointer.) In some environments, if a program doesn’t deallocate everything it allocated, memory stays unavailable even after the program ends.

Difference Between Null And Nul?

NULL is a macro defined in for the null pointer. NUL is the name of the first character in the ASCII character set. It corresponds to a zero value. There’s no standard macro NUL in C, but some people like to define it.

The digit 0 corresponds to a value of 80, decimal. Don’t confuse the digit 0 with the value of ‘’ (NUL)!

NULL can be defined as ((void*)0), NUL as ‘  ’.

What Is The Stack?

The stack is where all the functions’ local (auto) variables are created. The stack also contains some information used to call and return from functions.

A “stack trace” is a list of which functions have been called, based on this information. When you start using a debugger, one of the first things you should learn is how to get a stack trace. The stack is very inflexible about allocating memory; everything must be deallocated in exactly the reverse order it was allocated in. For implementing function calls, that is all that’s needed. Allocating memory off the stack is extremely efficient. One of the reasons C compilers generate such good code is their heavy use of a simple stack.

There used to be a C function that any programmer could use for allocating memory off the stack. The memory was automatically deallocated when the calling function returned. This was a dangerous function to call; it’s not available anymore.

Post a Comment

Previous Post Next Post