Sei sulla pagina 1di 9

C-FAQ

1.How to use printf function without semicolon?


if(printf("Hello, World!\n")) { }
while(!printf("Hello, World!\n")) { }
switch(printf("Hello, World!\n")) { }

2.How to swap two numbers without using temporary variable ?


//---------------------//
a=a+b;
b=a-b;
a=a-b;
//---------------------//
a=a^b;
b=b^a;
a=a^b;
//---------------------//

3.How to add two numbers without using arithmetic operators ?

C coding in next page.....fata-fati


#include <stdio.h>

int add(int a, int b)


{
//printf("\n%d %d",a,b);// add
if (!a) return b;
else
return add((a & b) << 1, a ^ b);// ‘^’xor
}

nt main()
{
printf("Enter the two numbers: \n");
int a,b;
scanf("%d",&a);
scanf("%d",&b);
printf("Sum is: %d",add(a,b));
}

**Try this also to replace


//--------------------------------------------------------//

unsigned add(unsigned a, unsigned b)


{
return printf("%*s%*s", a, "", b,"");
}
//--------------------------------------------------------//

Explanation:
Enter the two numbers:
38 99

38 99--------00100110,1100011
68 69--------01000100,1000101
136 1--------10001000,1
0 137--------00000000,10001001
Sum is: 137

4.How to find whether a given number is even or odd without using % (modulus)
operator ?
//---------------------//
bool isOdd(int num)
{
return num&1?true:false;
}
//---------------------//
5.How to use malloc?

#include <stdlib.h> //it is necessary to include this header file for reasons I will mention later
#include<stdio.h>
int main(void){
{ int i = 0;
int *a = 0; //a is visible only within this block
const int num = 10;
a = malloc(sizeof(int)*num); //allocates memory large enough to hold 10 int values

//Initializing the allocated memory


for ( i = 0; i < 10; i++)
{
a[i]=i+1;
}
//Use the allocated memory here
//.............
//.............
//Memory is no longer needed. So free it
free(a); //if we forget this step and the pointer "a" goes out of scope then we have a memory leak
a=NULL;//to prevent accidental reuse
//printf("%d", a[i]);// if uncomment it will give segmentation fault
}
}

6.How to allocate a multi-dimensional (2D/3D) array dynamically using malloc()?

????????????????????

7.Can you write a code which compiles in C but not in C++ ?

//---------------------//

#include<stdio.h>
int main()
{
int class;
class=10;
printf("%d",class);
}
//---------------------//
//---------------------//
#include<stdlib.h>
int main()
{
int *array=malloc(sizeof(int)*100);
}

//---------------------//
This is valid in C because a pointer of type void* can be assigned to any other pointer
without cast, but this is not in valid C++ because will have to give an explicit cast to it as
in:
int *array=(int*)malloc(sizeof(int)*100);

/---------------------//
#include<stdio.h>
int main()
{
static int i=5;
if(i>0)
printf("%d\n",i);
else
{
i--;
main();
return 0;
}
}

//---------------------//
This code is valid in C because recursion of main function is legal in C language whereas
it is illegal in C++ language.
//---------------------//

7.What are far, huge and near qualified pointers?


far, huge and near are non-standard qualifiers that were only used in 16 bit compilers
like Turbo C/C++(DOS versions). A near qualified pointer is 16 bits in size and can
access data within the 64kb data segment, while far and huge pointers can access data in
other segments as well. A far pointer is 32 bits in size, of which the first 16 bits is for the
segment address and the other 16 bits is for offset address. A huge pointer is normalized
pointer and so its address can range beyond one segment.

These qualifiers are not available in the new 32 and 64 bit compilers (like VC++ and gcc)
because they use flat memory model∞ where memory is not divided into different
segments, so these compilers do not have or require such qualifiers

8.What are the differences between structure and union ?

The main difference between a structure and union is in allocation of memory. In a


structure, every member will be allocated its own memory space. Whereas for a union,
the total memory allocated will be the memory required to accommodate the largest
member. Suppose, a structure and union are declared as given below :

struct tmpStruct {
char name[20];
int id;
float f;
}varStruct;

union tmpUnion {
char name[20];
int id;
float f;
}varUnion;

varUnion.id = 50;
varUnion.f = 100.00f;
printf("%d\n", varUnion.id); // Gives undefined result as last assigned member was
varUnion.f.

varStruct.id = 50;
varStruct.f = 100.00f;
printf("%d\n", varStruct.id); // Prints 50

Here the variable 'varStruct' will be allocated with total number of bytes required by its
members, along with padding bits if it is required (ie. sizeof(name) + sizeof(id) +
sizeof(f) + Padding). So here each member will be having its own memory location. On
the other hand, the variable 'varUnion' will be allocated only with the largest member of
it(ie. sizeof(name) + Padding). As a result, for a union only the member which was last
assigned can be accessed. For example, the following code may give undefined results:

9.How to find whether the given number is a power of 2 in single a single step
(without using loops) ?
bool isPowerOf2(int num){return ((num>0) && (num & (num­1))==0);}
10.How can we return more than one value from a function?
??????????????????????????????????????

11. Out put of the following program.


#define f(g,g2) g##g2
int main()
{
int var12=100;
printf("%d",f(var,12));
return 0;
}
Ans:100
Some comment from ORKUT C expert:

shrinivas

# and ## are called token passing orgument


# is used for showing teh name of variable and ## is used for joining two variable in to
one as var##12 become var12 which value is 100 get printed think about this.

PREM

## is called glue operator

12.Out put of the following prog. Ans: 61

int a=10;
int main()
{      int b=20; 
 static int c=30; 
 int *p; 
 p = &c; 
 p­­; 
 *p = a++ + b + c; 
 printf("%d", a); 

}
13. Add two no with out using ‘+’.

#include<stdio.h>
#include<conio.h>
void main()
{
int num,num1;
printf("Enter the two numbers : ");
scanf("%d %d",&num,&num1);
while(num1>0)
{
num1--;
num++;
}
printf("The sum of these two number is = ",num);
}

Comment:This ans is previously discussed nicely.

14.Difference between exit(0) n exit(1)

in view of programmer:
exit(0) : normal termination
exit(1): abnormal termination
exit(0) terminates ur application with writing all updated inforamation to the OS but
exit(1) terminates ur application without updating the info to OS

15.what's the difference between system call and functions?

a)System calls are the operating systems routine used internally to interact in low
level, While funtions are generally used to return the values required by the user
at user level.

Q.Swap two value?

A:#include<stdio.h>

int main()
{
int a,b;
printf("\n Enter the 2 numbers:");
scanf("%d%d",&a,&b);
b=(a+b)-(a=b);

printf("\n A = %d \n B = %d\n",a,b);

}
Q.What is the difference between null pointer and void pointer?

A.
a)Null pointers returns null value and void pointer returns no
value.

b)NULL POINTER :

the pointer which dosent point to any memory location is


called NULL POINTER.

VOID POINTER :
the pointer is a one which can only point to a particular
memory location of it's own type.... but when the pointer
is given void it can be mde to point any location of
different type

Note:Delete user, remove user on Linux Fedora, using userdel command

Command use:

# finger [username] <-- Verify user account on the Linux system


# userdel [username] <-- Remove user account from Linux system
# userdel -r [username] <-- Delete user account, remove home directory
including their files and remove mail spool

Add user on Linux Fedora, using useradd command

Command use to add new user:

# useradd [username] <-- Add user account to Linux system


# passwd [username] <-- Create password for user
# finger [username] <-- User datails, to verify user created on the system
# adduser [username] <-- Note: The adduser is useradd command... you can
use either one to add user account on the system...

Potrebbero piacerti anche