Funny C Questions

1.Write a C program which prints Hello World! without using a semicolon?
a) #include
int main()
{if(printf(“HelloWorld”)){}

2. Write a small C program to determine whether a machine’s type is little-endian or big-endian.?
Big endian order:-Here the “big end” (most significant value in the sequence) is stored first, at the lowest storage address. The most significant byte is stored in the leftmost position
a)#include
int main()
{
unsigned int number = 10;
char *ptr;
ptr = (char *)&number;
if(*ptr)
Printf(“little-endian \n”);
else
Printf(“big-endian \n”);
return 0;
}

3. What is the format specifiers for printf to print double and float values?
a)printf(“%lf , %f”);
%lf – double
%f – float

4. What is the difference between memcpy and memmove?
memcpy memmove
Takes 3 arguments a Void *ptr, const void *cptr , and an int n. Takes 3 arguments a Void *ptr, const void *cptr , and an int.
Copies ‘n’ characters from const void ptr cptr to void ptr ptr and returns void ptr. Copies ‘n’ characters from const void ptr cptr to void ptr ptr and returns void ptr.
Not reliable Reliable when an overlap.like objects cptr and ptr overlap.

4. Write a C program to find the smallest of three integers, without using any of the comparision operators.
a) #include
#include
int main() {
int x = 2;
int y = 1;
int z = 3;
int r;

r = y + ((x – y) & ((x – y) >> (sizeof(int) * CHAR_BIT – 1)));
r = z + ((r – z) & ((r – z) >> (sizeof(int) * CHAR_BIT – 1)));

printf(“%d\n”, r);
}

5.What does the format specifier %n of printf function do?
a)Tthe format specifier “ %n” will give the number of characters actually formatted in the printf
function.
Int num = 10;
Char *a= “helloworld”;
Printf(“ %3x,%n ” ,num, &a);
Printf(“%d no of bytes are formatted here into hex”,a);

6. What’s the difference between the following two C statements?
const char *p;
char* const p;
a) const char *p; p is a pointer to a constant character;
b) char* const p; p is a constant character pointer;
Exp–In the first case, the character pointed to by p is const. So, *p is const and you can’t do something like *p = ‘a’;. But p itself is not constant so you can change the value of p – i.e. have it point to some other char object: p = &c1; p = &c2;
In the second case, p is const, but *p is not. So *p = ‘a’; is valid, but p = &c2 is not valid. In C++, you’ll have to initialise p since it’s a const. C is not very strict in this case.

7. How do you print I can print % using the printf function?
a) using %% in the format specifier.
Printf(“%d %% %d” ,a ,b);
Output is a % b;

8.Write a program that repeatedly accepts user input and then prints the numbers between 0 and the entered number. If the user enters a number less than or equal to zero, the program will exit?

a)#include<stdio.h>
void main()
{
int a= 0,i;
for(;;)
{
printf(“\nEnter a number\n”);
scanf(“%d”,&a);
if(a<=0){exit(0);}
for(i=0;i<=a;i++)
printf(“\nThe numbers between 0 and the entered number are %d \t “,i);
}
}

Follow

Get every new post delivered to your Inbox.