C Programming: Datatype Name Datatype Size Datatype Range
C Programming: Datatype Name Datatype Size Datatype Range
Ans: The Datatypes in C Language are broadly classified into 4 categories. They are as
follows:
Basic Datatypes
Derived Datatypes
Enumerated Datatypes
Void Datatypes
Q3. What do you mean by the Scope of the variable? What is the scope of the
variables in C?
Ans: Scope of the variable can be defined as the part of the code area where the
variables declared in the program can be accessed directly. In C, all identifiers are
lexically (or statically) scoped.
Ans: The variables and functions that are declared using the keyword Static are
considered as Static Variable and Static Functions. The variables declared using Static
keyword will have their scope restricted to the function in which they are declared.
Q6. What are the valid places where the programmer can apply Break Control
Statement?
Ans: Break Control statement is valid to be used inside a loop and Switch control
statements.
Ans: To store a negative integer, we need to follow the following steps. Calculate the
two’s complement of the same positive integer.
Q8. Differentiate between Actual Parameters and Formal Parameters.
Ans: The Parameters which are sent from main function to the subdivided function are
called as Actual Parameters and the parameters which are declared a the Subdivided
function end are called as Formal Parameters.
Ans: The program will be compiled but will not be executed. To execute any C program,
main() is required.
Ans: When a data member of one structure is referred by the data member of another
function, then the structure is called a Nested Structure.
Q14. Mention the features of C Programming Language.
Ans:
Ans: printf() is used to print the values on the screen. To print certain values, and on
the other hand, scanf() is used to scan the values. We need an appropriate datatype
format specifier for both printing and scanning purposes. For example,
Ans. The array is a simple data structure that stores multiple elements of the same
datatype in a reserved and sequential manner. There are three types of arrays, namely,
Q18. What is the main difference between the Compiler and the Interpreter?
Ans: Compiler is used in C Language and it translates the complete code into the
Machine Code in one shot. On the other hand, Interpreter is used in Java Programming
Langauge and other high-end programming languages. It is designed to compile code in
line by line fashion.
Ans: No, Integer datatype will support the range between -32768 and 32767. Any value
exceeding that will not be stored. We can either use float or long int.
Ans: Dynamic Memory Allocation is the process of allocating memory to the program
and its variables in runtime. Dynamic Memory Allocation process involves three
functions for allocating memory and one function to free the used memory.
Syntax:
Syntax:
Syntax:
Syntax:
free(ptr);
struct employee
{
char name[10];
int age;
}e1;
int main()
{
printf("Enter the name");
scanf("%s",e1.name);
printf("n");
printf("Enter the age");
scanf("%d",&e1.age);
printf("n");
printf("Name and age of the employee: %s,%d",e1.name,e1.age);
return 0;
}
Ans:
Memory Separate memory locations are created for Actual and Formal arguments share the same
Location actual and formal arguments memory space.
Arguments Copy of actual arguments are sent Actual arguments are passed
#include<stdio.h>
void change(int,int);
int main()
{
int a=25,b=50;
change(a,b);
printf("The value assigned to a is: %d",a);
printf("n");
printf("The value assigned to of b is: %d",b);
return 0;
}
void change(int x,int y)
{
x=100;
y=200;
}
//Output
#include<stdio.h>
void change(int*,int*);
int main()
{
int a=25,b=50;
change(&a,&b);
printf("The value assigned to a is: %d",a);
printf("n");
printf("The value assigned to b is: %d",b);
return 0;
}
void change(int *x,int *y)
{
*x=100;
*y=200;
}
//Output
Ans: Both the functions are designed to read characters from the keyboard and the only
difference is that
getch(): reads characters from the keyboard but it does not use any buffers. Hence,
data is not displayed on the screen.
getche(): reads characters from the keyboard and it uses a buffer. Hence, data is
displayed on the screen.
//Example
#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
printf("Please enter a character ");
ch=getch();
printf("nYour entered character is %c",ch);
printf("nPlease enter another character ");
ch=getche();
printf("nYour new character is %c",ch);
return 0;
}
//Output
//Example
#include<stdio.h>
#include<ctype.h>
int main()
{
char c;
c=a;
printf("%c after conversions %c", c, toupper(c));
c=B;
printf("%c after conversions %c", c, toupper(c));
//Output:
a after conversions A
B after conversions B
Q28. Write a code to generate random numbers in C Language
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b;
for(a=1;a<=10;a++)
{
b=rand();
printf("%dn",b);
}
return 0;
}
//Output
1987384758
2057844389
3475398489
2247357398
1435983905
Ans: It is possible to create a new header file. Create a file with function prototypes that
need to be used in the program. Include the file in the ‘#include’ section in its name.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int* ptr;
int n, i, sum = 0;
n = 5;
printf("Enter the number of elements: %dn", n);
ptr = (int*)malloc(n * sizeof(int));
if (ptr == NULL)
{
printf("Memory not allocated.n");
exit(0);
}
else
{
printf("Memory successfully allocated using malloc.n");
for (i = 0; i<= n; ++i)
{
ptr[i] = i + 1;
}
printf("The elements of the array are: ");
for (i = 0; i<=n; ++i)
{
printf("%d, ", ptr[i]);
}
}
return 0;
}
//Output
#include<stdio.h>
void fun()
{
static int x;
printf("%d ", x);
x = x + 1;
}
int main()
{
fun();
fun();
return 0;
}
//Output
01
Q32. What is the difference between declaring a header file with < > and ” “?
Ans: If the Header File is declared using < > then the compiler searches for the header
file within the Built-in Path. If the Header File is declared using ” ” then the compiler will
search for the Header File in the current working directory and if not found then it
searches for the file in other locations.
Ans: We use Register Storage Specifier if a certain variable is used very frequently.
This helps the compiler to locate the variable as the variable will be declared in one of
the CPU registers.
Ans: x++; is the most efficient statement as it just a single instruction to the compiler
while the other is not.
Q35. Can I declare the same variable name to the variables which have different
scopes?
Ans: Yes, Same variable name can be declared to the variables with different variable
scopes as the following example.
int var;
void function()
{
int variable;
}
int main()
{
int variable;
}
Q36. Which variable can be used to access Union data members if the Union
variable is declared as a pointer variable?
Ans: Arrow Operator( -> ) can be used to access the data members of a Union if the
Union Variable is declared as a pointer variable.
Ans: Basic File Handling Techniques in C, provide the basic functionalities that user
can perform against files in the system.
Function Operation
fopen() To Open a File
fclose() To Close a File
fgets() To Read a File
fprint() To Write into a File
auto
register
static
extern
Ans:
#include<stdio.h>
void main()
{
if(printf("hello world")){}
}
//Output:
hello world
Q41. Write a program to swap two numbers without using the third variable.
Ans:
#include<stdio.h>
#include<conio.h>
main()
{
int a=10, b=20;
clrscr();
printf("Before swapping a=%d b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("nAfter swapping a=%d b=%d",a,b);
getch();
}
//Output
Q42. How can you print a string with the symbol % in it?
1
12
123
1234
12345
Ans: To print the above pattern, the following code can be used.
#include<stdio.h>
int main()
{
for(i=1;i<=5;1++)
{
for(j=1;j<=5;j++)
{
print("%d",j);
}
printf("n");
}
return 0;
}
Q44. Explain the # pragma directive.
Ans: The following program will help you to remove duplicates from an array.
#include <stdio.h>
int main()
{
int n, a[100], b[100], calc = 0, i, j,count;
printf("Enter no. of elements in array.n");
scanf("%d", &n);
printf("Enter %d integersn", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for (i = 0; i<n; i++)
{
for (j = 0; j<calc; j++)
{
if(a[i] == b[j])
break;
}
if (j== calc)
{
b[count] = a[i];
calc++;
}
}
printf("Array obtained after removing duplicate elementsn");
for (i = 0; i<calc; i++)
{
printf("%dn", b[i]);
}
return 0;
}
//Output
Enter 5 integers
12
11
11
10
4
Array obtained after removing duplicate elements
12
11
10
4
Ans: Bubble sort is a simple sorting algorithm that repeatedly steps through the list,
compares adjacent elements and swaps them if they are in the wrong order. The pass
through the list is repeated until the list is sorted.
int main()
{
int array[100], n, i, j, swap;
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d Numbers:n", n);
for(i = 0; i<n; i++)
scanf("%d", &array[i]);
for(i = 0 ; i<n - 1; i++)
{
for(j = 0 ; j < n-i-1; j++) { if(array[j]>array[j+1])
{
swap=array[j];
array[j]=array[j+1];
array[j+1]=swap;
}
}
}
printf("Sorted Array:n");
for(i = 0; i < n; i++)
printf("%dn", array[i]);
return 0;
}
Q47. What is Round-robin algorithm? Write a code for Round Robin Scheduling
#include<stdio.h>
int main()
{
int i, limit, total = 0, x, counter = 0, time_quantum;
int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], temp[10];
float average_wait_time, average_turnaround_time;
printf("nEnter Total Number of Processes:t");
scanf("%d", &limit);
x = limit;
for(i = 0; i<limit; i++)
{
printf("nEnter Details of Process[%d]n", i + 1);
printf("Arrival Time:t");
scanf("%d", &arrival_time[i]);
printf("Burst Time:t");
scanf("%d", &burst_time[i]);
temp[i] = burst_time[i];
}
Q48. Which structure is used to link the program and the operating system?
Q51. Suppose a global variable and local variable have the same name. Is it is
possible to access a global variable from a block where local variables are
defined?
Ans: No. It is not possible in C. It is always the most local variable that gets
preference.