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

The C Programming Language, 2nd Edition*

This book is meant to help the reader learn how to program in C. It is the definitive reference guide, now in a second edition. Although the first edition was written in 1978, it continues to be a worldwide best-seller. This second edition brings the classic original up to date to include the ANSI standard. For evolution of the planet earth and our modern understanding of biology, there was Darwin's Origin of the Species. For mathematics, there was Newton's PhilosophiƦ Naturalis Principia Mathematica. Well, for the internet, for Facebook, for LinkedIn, Twitter, Instgram, Snapchat, WhatsApp, Pornhub and even the odious website for Justin Bieber would never have existed without Kernigan and Ritchie (more affectionately known as K&R)'s classic, The C Programming Language. What language was TCP/IP written in? C. What language inspired both C++ and Java (and the abominable C#)? C. What language are most libraries on most operating systems written in if not assembler? C. ...

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 ***

Java: A Beginner's Guide, 6th Edition

This is Herb's step-by-step introduction to Java, updated for Java SE 8 (JDK 8). If you are just learning Java, then this is the book for you.  It starts at the beginning, explaining the history of Java, why it's important to the Web, and how it relates to the world of programming at large.  You then learn how to obtain the Java Development Kit (JDK) and write your first Java program. Next, it's on to the Java fundamentals, including data types, operators, control statements, classes, objects, and methods.  You'll then progress to more advanced topics, such as inheritance, exception handling, the I/O system, multithreading,  applets, and lambda expressions. Also included is coverage of some of Java's most powerful features, such as generics, autoboxing, enumerations, and static import.  An introduction to JavaFX, Java's newest GUI framework, is also included. *** TO REVIEW BOOK *** (click below) *** TO REVIEW SOURCE CODE PROBLEM  SO...