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;
}
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. */