Skip to main content

C Interview Questions And Answers - Part 1.

What is the difference between pass by value and pass by reference?
Answer: We can pass the parameters in a function in two different ways in C programming.

Pass by value: In this approach we pass the copy of actual variables in function as a parameter. Hence any modification on parameters inside the function will not reflect in the actual variable. For example:

#include<stdio.h>
int main() {
    int a=5,b=10;
    swap(a,b);
    printf("%d %d",a,b);
    return 0;
} 
void swap(int a,int b) {
    int temp;
    temp =a;
    a=b;
    b=temp;
}
Output: 5  10

Pass by reference: In this approach we pass the memory address of actual variables in function as a parameter. Hence any modification on parameters inside the function will reflect in the actual variable. For example:

#incude<stdio.h>
int main() {
    int a=5,b=10;
    swap(&a,&b);
    printf("%d %d",a,b);
    return 0;
}
void swap(int *a,int *b) {
    int  *temp;
    *temp =*a;
    *a=*b;
    *b=*temp;
}
Output: 10  5

What is the modulus operator?
Answer: The modulus operator outputs the remainder of a division. We use the percentage (%) symbol to represent modulus operator. For example:
10 % 3 = 1,
5 % 3 = 2, and so on.
  
Convert the following statement in the while loop format?
for (a=1; a<=100; a++)
printf ("%d\n", a * a);
a=1;
while (a<=100)
{
printf ("%d\n", a * a);
a++;
}

What is header file and what are its uses in C programming?
Answer: A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.

Both the user and the system header files are included using the preprocessing directive #include. It has the following two forms −

#include <file>
This form is used for system header files. It searches for a file named 'file' in a standard list of system directories. For example, stdio.h is a header file that comes along with the compiler, and it contains definition and prototypes of commands like printf and scanf.

#include "file"
This form is used for header files of your own program. It searches for a file named 'file' in the directory containing the current file. For example, if you have a header file header.h as follows − char *test (void);
and a main program called program.c that uses the header file, like this −

int x;
#include "header.h"
int main (void) { puts (test ()); }

the compiler will see the same token stream as it would if program.c read.
int x;
char *test (void);
int main (void) { puts (test ()); }

Including a header file is equal to copying the content of the header file but we do not do it because it will be error-prone and it is not a good idea to copy the content of a header file in the source files, especially if we have multiple source files in a program.

What is syntax error?
Answer: Syntax errors are associated with mistakes in the use of a programming language. It may be a command that was misspelled or a command that must was entered in lowercase mode but was instead entered with an upper case character. A misplaced symbol, or lack of symbol, somewhere within a line of code can also lead to syntax error.

Write a loop statement that will show the following output:
1
12
123
1234
12345

for (a=1; a<=5; i++)
{
for (b=1; b<=a; b++)
printf("%d",b);
printf("\n");
}

Can I use “int” data type to store the value 32768? Why?
Answer: No. “int” data type is capable of storing values from -32768 to 32767. To store 32768, you can use “long int” instead. You can also use “unsigned int”, assuming you don’t intend to store negative values.

Is C language case sensitive?
Answer: Yes. C language instructions/commands/functions and everything used in C program are case sensitive.

What is NULL pointer?
Answer: 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. 

Example: char *p=NULL;  

What is Dangling pointer?
Answer: 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. Following are examples.

Example 1:
int *ptr = (int *)malloc(sizeof(int));
.............
.............
free(ptr);

// ptr is a dangling pointer now and operations like following are invalid
*ptr = 10;  // or printf("%d", *ptr);

Example 2:
int *ptr = NULL {
int x  = 10;
ptr = &x;
}
/* x goes out of scope and memory allocated to x is free now, so ptr is a dangling pointer now.  */

Popular posts from this blog

Introduction To Algorithms, 3rd Edition

Before there were computers, there were algorithms. But now that there are computers, there are even more algorithms, and algorithms lie at the heart of computing. This book provides a comprehensive introduction to the modern study of computer algorithms. It presents many algorithms and covers them in considerable depth, yet makes their design and analysis accessible to all levels of readers. In this book, the authors tried to keep explanations elementary without sacrificing depth of coverage or mathematical rigor. Each chapter presents an algorithm, a design technique, an application area, or a related topic. Algorithms are described in English and in a pseudocode designed to be readable by anyone who has done a little programming. The book contains 244 figures — many with multiple parts — illustrating how the algorithms work. It also includes careful analysis of the running times of all algorithms. In this third edition, the entire book once again updated including changes cove...

C Program To Check Whether A Number Is Palindrome Or Not.

This program takes an integer from user and the integer is reversed. If the reversed integer is equal to the integer entered by user then that number is a palindrome. If not that number is not a palindrome.   #include <stdio.h> int main()  { int num, temp, remainder, reverse = 0; printf("Enter an integer: "); scanf("%d", &num); /*  original number is stored at temp */ temp = num; while (num > 0)  { remainder = num % 10; reverse = reverse * 10 + remainder; num /= 10;   }

The Basics Of C Programming - Part 3.

There are a number of different C input commands, the most useful of which is the scanf command. To read a single integer value into the variable called a you can use: scanf("%d",&a); When the program reaches the scanf statement it pauses to give the user time to type something on the keyboard and continues only when users press Enter or Return, to signal that he, or she, has finished entering the value. Then the program continues with the new value stored in a. In this way, each time the program is run the user gets a chance to type in a different value to the variable and the program also gets the chance to produce a different result! The final missing piece in the jigsaw is using the printf function, the one we use to print the value currently being stored in a variable. To display the value stored in the variable a you can use: printf("The value stored in a is %d",a); Note: the scanf function does not prompt for an input. You should ge...

C++ Program To Implement Bank Management System.

#include<iostream> #include<fstream> #include<cctype> #include<iomanip> #include <cstdlib> using namespace std; class account { int acno; char name[50]; int deposit; char type; public: void create_account(); //function to get data from user void show_account() const; //function to show data on screen void modify(); //function to add new data void dep(int); //function to accept amount and add to balance amount void draw(int); //function to accept amount and subtract from balance amount void report() const; //function to show data in tabular format int retacno() const; //function to return account number int retdeposit() const; //function to return balance amount char rettype() const;  //function to return type of account }; void account::create_account() { cout<<"\nEnter The Account No. : "; cin>>acno; cout<<"\n\nEnter The Name Of The Account Holder : "; cin.ig...