Common & Uncommon in C'

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 32

Common & Uncommon in

‘C’
Company References
• SATYAM 2007
• WIPRO 2009/2010
• ADITI SYSTEMS 2009
• IBM 2008/2009
• I-FLEX SOLUTIONS 2009/2010
• TCS 2008/2009
• HP 2009
Which is the Latest C Standard

ANSI-ISO C 9x (C 99)
Define the following

12.36

Answer : A Double Constant


How to convert a integer to a string ?

char *ss;
int num = 40;
sprintf(ss, "%d", num);
What is correct in C: "void main()" or "int main()"?

According to the ANSI/ISO standard, the correct


forms are two:

int main() { /* ... */ }


int main(int argc, char* argv[]) { /* ... */ }

Even "void main()" will probably compile (depends on


the compiler), it is not correct!
int main()
 {
   printf("%d"+1,123);
   return(0);
 }
Answer : d

"%d"+1 (or > 0) in the printf statement affects the program output
by considering "%d" as string and ignores the excess argument i.e.
123. Here 1 refers to the index i.e. 2nd character in the array or
string "%d"
int main()
 {
   printf("Hi Friends"+3);
   return(0);
 }

Ans: Friends

(base adress of the string 'Hi Friends')+0 points to the value


'H'. Now the NEW (base address) equals (base address)+3
that points to the character 'F'. Thus it prints the string from
'F' onwards.
 int main()
 {
   int main=7;
   {
      printf("%d",main);
      return main;
   }
   printf("Bye");
   return(0);
 }
 Answer : 7

Prints 7 and returns the same to the Operating System. NOTE:


Last printf statement will not be executed.
int main()
 {
   char str[]="%d";
   int val=25;
   printf(str,val);
   return(0);
 }
Answer: 25

First parameter contains the format specifier & the


Second parameter contains the actual value 25.
int main()
 {
   int val=10;
   printf("%d",val+1,"%d",val--);
   return(0);
 }
Answer : 10

R->L priority. The second format specifier


'%d' is an excess argument and it is ignored.
 #define Compute(x,y,z) (x+y-z)
 int main()
 {
   int x=2,y=3,z=4;
   cout<Compute(y,z,(-x+y)) * Compute(z,x,(-y+z));
   return(0);
 }

Answer : 30

During pre-processing the macro calls are merely replaced by the


replacement text. Be careful while analyzing macro calls.
int main()
 {
   int val=97;
   "Printing..."+printf("%c",val);
   return(0);
 }

Answer : a
 int main()
 {
   int val=5;
   printf("%*d",val,val);
   return(0);
 }

Answer : bbbbb5 (where b means blank space)

Here '*' specifies the precision. First val is the precision and
second val is the actual value. Thus it prints 5 BLANK SPACES &
then the value 5.
int main()
 {
   cout<<!(100==100)+1;
   return(0);
 }
Answer : 1

Negation (!) of non-zero value always returns 0


(zero). Thus 0 + 1 = 1
int main()
 {
   int a=1,b=2,c=3;
   scanf("%d %*d %d",&a,&b,&c);
   printf("a=%d b=%d c=%d",a,b,c);
   return(0);
 }
Answer : 100 300 3

The * (asterisk) in the scanf statement indicates an


assignment suppression operator that skips the related
value and picks the next value in the input series.
THE USER INPUT IS :ABC DEF GHI
int main()
 {
   char a,b,c;
   scanf("%c %c %c",&a,&b,&c);
   printf("a=%c b=%c c=%c",a,b,c);
   return(0);
 }
Answer : a=A b=B c=C

We're expecting character inputs separated by white


spaces but the actual input contains string of
characters.
THE USER INPUT IS :123456 44 544
int main()
 {
   int a,b,c;
   scanf("%1d %2d %3d",&a,&b,&c);
   printf("Sum=%d",a+b+c);
   return(0);

}
Answer : Sum=480
Here access format specifier is used in the scanf
statement similar to the access format specifier usage
in the printf statements.
int main()
 {
     int a=1,b=2,c=3;
     c=(--a,b++)-c;
     printf("%d %d %d",a,b,c);
   return(0);
 }
Answer : 0 3 -1
Expressions within the parenthesis are evaluated from
L->R but the rightmost value will be considered for
computation and the rest will be ignored.
int main()
 {
     int val=50;
     void *ptr;
     ptr=&val;
     printf("%d %d",val,*(int *)ptr);
   return(0);
 }
Answer : 50 50
Here the void pointer is used to access the contents of
a integer variable. Note: void pointer can point to any
variable of any data type. Care should be taken during
type casting it.
void main() {
int a, b, c, abc = 0;
a = b = c = 40;
if (c) {
int abc;
abc = a*b+c;
}
printf ("c = %d, abc = %d\n", c, abc);
}
Answer : c = 40 and abc = 0

Because the scope of the variable 'abc' inside


if(c) {.. } is not valid out side that if (.) { .. }.
main()
{
char *p;
printf("%d %d ",sizeof(*p),sizeof(p));
}
Answer: 14

The sizeof() operator gives the number of bytes taken by its


operand. P is a character pointer, which needs one byte for
storing its value (a character). Hence sizeof(*p) gives a
value of 1. Since it needs two bytes to store the address of
the character pointer sizeof(p) gives 4.
main()
{
char string[]="Hello World";
display(string);
}
void display(char *string)
{
printf("%s",string);
} Answer:
Compiler Error : Type mismatch in re-declaration of function
display
In third line, when the function display is encountered, the compiler
doesn't know anything about the function display. It assumes the
arguments and return types to be integers, (which is the default type).
When it sees the actual function display, the arguments and type
contradicts with what it has assumed previously. Hence a compile time
error occurs.
main()
{
printf("%p",main);
}
Answer : will Print the address where
main function program memory area is
created
What is the difference between NULL and
NUL(nul)?

NULL is a macro defined in for the null


pointer.

NUL(nul) is the name of the first character in


the ASCII character set. It corresponds to a
zero value. There’s no standard macro NUL in
C, but some people like to define it.
What is the purpose of main( ) function?

The function main( ) invokes other functions within it.It is the first function
to be called when the program starts execution.

Ø It is the starting function


Ø It returns an int value to the environment that called the program
Ø Recursive call is allowed for main( ) also.
Ø It is a user-defined function
Ø Program execution ends when the closing brace of the function main( )
is reached.
Ø It has two arguments 1)argument count and 2) argument vector
(represents strings passed).
What is the difference between malloc and calloc
Both malloc and calloc do the same thing with almost the same results,
they allocate a block of n * sizeof ( T ) bytes of memory. The difference is
in how the functions are called and how the memory block is initialized.
malloc is called like this :
p = malloc ( n * sizeof ( T ) );

calloc takes two arguments:


p = calloc ( n, sizeof ( T ) );

malloc leaves the block of memory uninitialized, but calloc zero fills the
memory.

Because of this zero fill, calloc can be slightly less efficient than
malloc.
Why does C give better run-time performance then
Java?

That's because the Java bytecode is interpreted, not


compiled. Programs written in C are compiled into binaries
which can be executed by a specific computer processor.

Programs written in Java require one more step -- they


must be interpreted by the Java "virtual machine" before
running on a particular computer architecture. As a result,
a computer running a Java program has to execute more
machine-language instructions to do the same amount of
work than a computer running an equivalent program
written in C.
What is the quickest sorting method to use?

The answer depends on what you mean by quickest. For


most sorting problems, it just doesn’t matter how quick the
sort is because it is done infrequently or other operations
take significantly more time anyway. Even in cases in
which sorting speed is of the essence, there is no one
answer. It depends on not only the size and nature of the
data, but also the likely order.

No algorithm is best in all cases.

There are three sorting methods all very fast and that are
useful in different situations. Those methods are quick sort,
merge sort, and radix sort.
 Which is the parameter that is added
to every non-static member function
when it is called?

Ans : this Pointer


What will be the output ?
class some
{
public:

~some()
{
cout<<"some's destructor"<<endl;
}
};
  Ans:
void main() some’s destructor
{
some s; some’s destructor
s.~some();
}
Define a way other than using the
keyword inline to make a function
inline.

Answer:
The function must be defined inside
the class.

You might also like