Write a C program to print hello world without using a semicolon (;).
#include
void main()
{
if(printf("hello world")){}
}
Output:
hello world
How can you print a string with the symbol % in it?
There is no escape sequence provided for the symbol % in C. So, to print % we should use ‘%%’ as shown below.
printf(“there are 90%% chances of rain tonight”);
Explain the # pragma directive.
The following points explain the Pragma Directive.
Which structure is used to link the program and the operating system?
The answer can be explained through the following points,
Differentiate between the macros and the functions.
The differences between macros and functions can be explained as follows:
What are the limitations of scanf() and how can it be avoided?
The Limitations of scanf() are as follows:
Explain toupper() with an example.
toupper() is a function designed to convert lowercase words/characters into upper case.
Example:
#include
#include
int main()
{
char c;
c=a;
printf("?fter conversions %c", c, toupper(c));
c=B;
printf("?fter conversions %c", c, toupper(c));
}
Output:
a after conversions A
B after conversions B
What is Dynamic Memory allocation? Mention the syntax.
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.
malloc() – Allocates memory
Syntax:
ptr = (cast-type*) malloc(byte-size);
calloc() – Allocates memory
Syntax:
ptr = (cast-type*)calloc(n, element-size);
realloc() – Allocates memory
Syntax:
ptr = realloc(ptr, newsize);
free() – Deallocates the used memory
Syntax:
free(ptr);
Which built-in library function can be used to re-size the allocated dynamic memory?
realloc().
What do you mean by Memory Leak?
Memory Leak can be defined as a situation where programmer allocates dynamic memory to the program but fails to free or delete the used memory after the completion of the code. This is harmful if daemons and servers are included in the program.
#include
#include
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:
Enter the number of elements: 5
Memory successfully allocated using malloc.
The elements of the array are: 1, 2, 3, 4, 5,