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

Screenshots from Windows 1.01

Windows 1.0 is a graphical personal computer operating environment developed by Microsoft, released on November 20, 1985, as the first version of the Microsoft Windows line. Version 1.01 , also released in 1985, was the first point-release after Windows 1.00.   Screenshots from Windows 1.01: ⇰ Desktop  First Run Empty Desktop Desktop With Applications ⇰  Office Applications Notepad Text Editor Calculator Calendar Clock Address Book ⇰  Multimedia Applications Media player, CD player, Volume level, and Sound: This GUI doesn’t have these features. ⇰  Networking Applications Terminal Phone Dialer: This GUI doesn’t have this feature. ⇰  Internet Applications Browser, and Mail: This GUI doesn’t have these features. ⇰  Accessibility Applications Keyboard Map:  This GUI doesn’t have this feature. ⇰  Settings Desktop themes,  Display,  S...

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 Implement Casino Number Guessing Game.

#include <iostream> #include <string> #include <cstdlib> #include <ctime> using namespace std; void drawLine(int n, char symbol); void rules(); int main() { string playerName; int amount; int bettingAmount; int guess; int dice; char choice; srand(time(0)); drawLine(70,'_'); cout << "\n\n\n\t\tCASINO GAME\n\n\n\n"; drawLine(70,'_'); cout << "\n\nEnter Your Name : "; getline(cin, playerName); cout << "\n\nEnter Deposit Amount To Play Game : $"; cin >> amount;

Java: The Complete Reference, 9th Edition

This is Herb's most popular book on Java, fully updated and expanded to cover Java SE 8 (JDK 8).    Whether you're an experienced pro or just starting out, this one-stop guide will help you master this important language.  Inside you'll find comprehensive coverage of the Java language, its keywords, syntax, and fundamental programming principles.  Of course, descriptions of Java's newest features, such as lambda expressions, default interface methods, and the stream API are included. This lasting resource also describes key elements of the Java API library, such as the Collections Framework, concurrency, applets, servlets, Beans, event handling,  AWT,  Swing, and more. Coverage of JavaFX, Java's newest GUI framework, is also included. *** TO REVIEW BOOK ***  (click below) *** TO REVIEW SOURCE CODE PROBLEM  SOLUTIONS, VISIT   THIS   LINK ***