IndiaBIX.com
Arithmetic Aptitude Data Interpretation
Logical Reasoning Verbal Reasoning Non Verbal Reasoning
General Knowledge
Sudoku Number puzzles Missing letters puzzles Logical puzzles Playing cards puzzles Clock puzzles
C Programming C# Programming Java Programming
Networking Database Questions Computer Science Basic Electronics Digital Electronics Electronic Devices Circuit Simulation Electrical Enigneering Engineering Mechanics Technical Drawing
Placement Papers Group Disucssion HR Interview Technical Interview Body Language
Aptitude Test Verbal Ability Test Verbal Reasoning Test Logical Reasoning Test C Programming Test Java Programming Test Data Interpretation Test General Knowledge Test
Data Structures Operating Systems Networking DATABASE Database Basics SQL Server Basics SQL Server Advanced SQL Server 2008 JAVA Core Java Java Basics Advanced Java UNIX Unix File Management Unix Memory Management Unix Process Managemnt C Interview Questions The C Language Basics .NET Interview Questions .NET Framework ADO.NET ASP.NET Software Testing

C Programming - Bitwise Operators - Discussion

@ : Home > C Programming > Bitwise Operators > Point Out Correct Statements - Discussion

3. 

Which of the following statements are correct about the program?

#include<stdio.h>
char *fun(unsigned int num, int base);

int main()
{
    char *s;
    s=fun(128, 2);
    s=fun(128, 16);
    printf("%s\n",s);
    return 0;
}
char *fun(unsigned int num, int base)
{
    static char buff[33];
    char *ptr = &buff[sizeof(buff)-1];
    *ptr = '\0';
    do
    {
        *--ptr = "0123456789abcdef"[num %base];
        num /=base;
    }while(num!=0);
    return ptr;
}

[A]. It converts a number to a given base.
[B]. It converts a number to its equivalent binary.
[C]. It converts a number to its equivalent hexadecimal.
[D]. It converts a number to its equivalent octal.

Answer: Option E

Explanation:

No answer description available for this question.


Shyamala said: (Tue, Jun 7, 2011 03:48:02 PM)    
 
Please give explanation for this program. Me too a basic c learner.

Prudhvi said: (Sun, Jul 17, 2011 07:51:45 AM)    
 
Please guys any one let me know this explanation very soon.

Needprayers4Me. said: (Wed, Aug 3, 2011 11:30:50 PM)    
 
char *ptr = &buff[sizeof(buff)-1]; makes ptr point to 32nd index of buff.
buff[32] is made null in the next statement. (*ptr='\0'). Reason being strings are supposed to be terminated with null.

Now considering 128,2

128%2=0 so "0123456789abcdef"[num %base]; returns 0. ptr is decremented and then the value 0 is assigned. This value 0 is written on to the next six places from the right towards left till num becomes 1. Now when num becomes 1, "0123456789abcdef"[num %base] returns 1. and hence the binary rep of 128.

Similar analysis will lead one to see that with 128,16 the value returned is 80, the hexadecimal rep pf 128.

Saurav said: (Mon, Sep 5, 2011 02:34:42 AM)    
 
The question asked is based on the function and not the main program's output.....
For the given program it will print the binaray and hexadecimal equivalent of 128.

Write your comments here:
Name *:     Email:


© 2008-2011 by IndiaBIX™ Technologies. All Rights Reserved | Copyright | Terms of Use & Privacy Policy

Advertise     Contact us: info@indiabix.com     Follow us on twitter!