51. What are header files and what are its uses in C programming?
Header Files in C
In C header files must have the extension as .h, which contains function definitions,
data type definitions, macro, etc. The header is useful to import the above definitions
to the source code using the #include directive. For example, if your source code
needs to take input from the user do some manipulation and print the output on the
terminal, it should have stdio.h file included as #include <stdio.h>, with which we can
take input using scanf() do some manipulation and print using printf().
52. When is the "void" keyword used in a function
The keyword “void” is a data type that literally represents no data at all. The most
obvious use of this is a function that returns nothing:
void PrintHello()
{
printf("Hello\n");
return; // the function does "return", but no value is returned
}
Here we’ve declared a function, and all functions have a return type. In this case,
we’ve said the return type is “void”, and that means, “no data at all” is returned.
The other use for the void keyword is a void pointer. A void pointer points to the
memory location where the data type is undefined at the time of variable definition.
Even you can define a function of return type void* or void pointer meaning “at
compile time we don’t know what it will return” Let’s see an example of that.
void MyMemCopy(void* dst, const void* src, int numBytes)
{
char* dst_c = reinterpret_cast<char*>(dst);
const char* src_c = reinterpret_cast<const char*>(src);
for (int i = 0; i < numBytes; ++i)
dst_c[i] = src_c[i];
}
53. What is dynamic data structure?
A dynamic data structure (DDS) refers to an organization or collection of data in
memory that has the flexibility to grow or shrink in size, enabling a programmer to
control exactly how much memory is utilized. Dynamic data structures change in size
by having unused memory allocated or de-allocated from the heap as needed.
Dynamic data structures play a key role in programming languages like C, C++, and
Java because they provide the programmer with the flexibility to adjust the memory
consumption of soware programs.
54. Add Two Numbers Without Using the Addition Operator
For the sum of two numbers, we use the addition (+) operator. In these tricky C
programs, we will write a C program to add two numbers without using the addition
operator.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x, y;
printf("Enter two number: ");
scanf("%d %d",&x,&y);
// method 1
printf("%d\n", x-(-y));
// method 2
printf("%d\n", -(-x-y));
// method 3
printf("%d\n", abs(-x-y));
// method 4
printf("%d", x-(~y)-1);
return 0;
}
55. Subtract Two Number Without Using Subtraction Operator
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x, y;
printf("Enter two number: ");
scanf("%d %d",&x,&y);
printf("%d", x+(~y)+1);
return 0;
}
The bitwise complement operator is used in this program. The bitwise complement
of number ~y=-(y+1). So, expression will become x+(-(y+1))+1=x-y-1+1=x-y
56. Multiply an Integer Number by 2 Without Using Multiplication Operator
#include<stdio.h>
int main()
{
int x;
printf("Enter a number: ");
scanf("%d",&x);
printf("%d", x<<1);
return 0;
}
The le shi operator shis all bits towards the le by a certain number of specified
bits. The expression x<<1 always returns x*2. Note that the shi operator doesn’t
work on floating-point values.
For multiple of x by 4, use x<<2. Similarly x<<3 multiply x by 8. For multiple of the
number x by 2^n, use x<<n.
57. Check whether the number is EVEN or ODD, without using any arithmetic or relational operators
#include<stdio.h>
int main()
{
int x;
printf("Enter a number: ");
scanf("%d", &x);
(x&1)?printf("Odd"):printf("Even");
return 0;
}
The bitwise and(&) operator can be used to quickly check the number is odd or even.
58. Reverse the Linked List. Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL
Assume that we have linked list 1 → 2 → 3 → Ø, we would like to change it to Ø ← 1 ← 2 ← 3.
While you travel the linked list, change the current node's next pointer to point to its
previous element. reference to the previous nodes should be stored into a temp
variable as shown so that we don’t lose track of the swapped node. You also need
another pointer to store the next node before changing the reference. Also when we
are done return the new head of the reversed list.
/* Function to reverse the linked list */
static void reverse(struct Node** head_ref)
{
struct Node* prev = NULL;
struct Node* current = *head_ref;
struct Node* next;
while (current != NULL)
{
// store next
next = current->next;
// reverse curr node pointer
current->next = prev;
// move pointer one position ahead
prev = current;
current = next;
}
*head_ref = prev;
}
59. Check for Balanced Parentheses using Stack
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the
input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Example 1:
Input: s = "()"
Output: true
Example 2:
Input: s = "()[]{}"
Output: true
Example 3:
Input: s = "(]"
Output: false
Below is the source code for C Program to Check for Balanced Parentheses using
Stack which is successfully compiled and run on Windows System to produce desired
output as shown below :
int check(char exp[] )
{
int i;
char temp;
for(i=0;i<strlen(exp);i++)
{
if(exp[i]=='(' || exp[i]=='{' || exp[i]=='[')
push(exp[i]);
if(exp[i]==')' || exp[i]=='}' || exp[i]==']')
if(top==-1) /*stack empty*/
{
printf("Right parentheses are more than left parenthese
return 0;
}
else
{
temp=pop();
if(!match(temp, exp[i]))
{
printf("Mismatched parentheses are : ");
printf("%c and %c\n",temp,exp[i]);
return 0;
}
}
}
if(top==-1) /*stack empty*/
{
printf("Balanced Parentheses\n");
return 1;
}
else
{
printf("Left parentheses more than right parentheses\n");
return 0;
}
}
60. Program to find n’th Fibonacci number
Fibonacci sequence is characterized by the fact that every number aer the first two
is the sum of the two preceding ones. For example, consider below sequence
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . .. and so on
Where in F{n} = F{n-1} + F{n-2} with base values F(0) = 0 and <code>F(1) = 1
Below is naive implementation for finding the nth member of the Fibonacci
sequence
// Function to find the nth Fibonacci number
int fib(int n)
{
if (n <= 1) {
return n;
}
return fib(n - 1) + fib(n - 2);
}
int main()
{
int n = 8;
printf("nth Fibonacci number is %d", fib(8));
return 0;
}
