Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VC6
Dear sir,
if i m writing one program using showbits() and compile it in vc++ compiler , it showing error.
the program is like:
    #include<stdio.h>
    main()
    {
      int j;
      for(j=0;j<<=5;j++)
      {
        printf("the decimal %d number is",j);
        showbits(j);
      }
    }
    
 
is there any problem in following program.
 

hello sir,
when i execute the above program in vc++ compiler the following error is come out.
i.e; error LNK2001: unresolved external symbol _showbits
Debug/puja.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
 
is showbits() doesnot support in vc++.
 

dear sir plz kindly tell the header file that support showbits().
Posted 8-Jan-10 7:47am
Edited 9-Jan-10 9:21am
v7
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Thank you for adding the error message you are getting to your post. I am fixing an error I made in my original post. I also have some additions based on some stuff I located with the help of Google.
 
First, I have come to the conclusion that the code you posted is not code that you wrote. It appears to be taken from the book "Let Us C" by Yashavant Kanetkar. It is almost identical to the code in an edition of the book that I found online - even including a clear (typographical?) error in the code.
 
Just before the code in the book, the author writes "Throughout this discussion of bitwise operators we are going to use a function called showbits( ), but we are not going to show you the details of the function immediately."
 
What this means is that the book author wrote a function he named showbits(). He uses this function in most (all?) of the examples in that chapter and they will not work without it, but he doesn't provide or explain that code until 20 pages later at the end of the chapter.
 
So, if you look ahead in the book, you will find the code for the showbits() function. I haven't read the book, but since this is 500 pages into it, hopefully you know how to use a function in addition to main() in your program. If not, you might need a better book. (I have the impression that is not a good one.)
 
I will add that the showbits() code in the edition of the book that I saw presumes that an int is 16 bits. This is not necessarily true. I think it is 32 bits in VC 6.0, so you will want to practice your programming and fix up that function.
 


In addition, you did ask:
is there any problem in following program

 

Looking at the program, one line really stands out:
 
wrote:
for(j=0;j<<=5;j++)

 
This is an infinite loop instruction. Yes, that is what is printed in the book. It is, however, a mistake. You can practice your programming by figuring out the problem and fixing it.
 
Note also that main() should be defined as returning int. It is true that the book that you are using does not do that. That does not mean that you shouldn't, it means you should get a better book. Writing function definitions that implicitly return int rather than explicitly specifying the return type was done in K & R C. This practice has not been appropriate for new code for over 20 years.
 
This touches on CPallini's wondering why you aren't getting a compiler error for the use of showbits() without a function prototype. First, you are trying to learn C and using a C Compiler, not C++. As long as it is C and not C++ that you intend, this is fine. Second, you are (unknowingly) exploiting the allowance in the compiler for pre-standard K & R syntax. This is a BAD BAD BAD idea and has been for over 20 years. You should turn up the warning level on the compiler and then you should start getting warnings about this. The book you are using seems to be inconsistent on this issue. You should strive to consistently get it right. Perhaps you should consider finding a better book.
  Permalink  
v3
Comments
CPallini at 16-Dec-11 6:01am
   
Hi Avi, the for statement does NOT produce an infinite loop, it actually produces no loop at all.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You didn't link with the proper library (or object file). What is the library (or object) containing the showbits function?
Anyway, I wonder why you aren't getting a compiler error (the one posted is a linker one), since AFAIK the showbits function isn't defined in stdio.h header.
 
BTW If you need just a function that prints out the binary representation of its int argument, then you can write easily your own.
Smile | :)
  Permalink  
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hello friend, I am Kishore
I want to tell you a simple program to print binary code.
The function showbits() doesn't work directly,we have to define that function.
The function showbits() doesn't have any library function.
 
Try this code, it may help you
 
#include<stdio.h>
main()
{
int i,a,b,c;
printf("Enter a number(Only Integer): ");
scanf("%d",&a);
for(i=15;i>=0;i--)
{
b= 1<<i;
c= a&b;
c==0?printf("0"):printf("1");
}
printf("\n")
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

May depend upon your compiler but for(int j=0;j<16;j++)might not be valid.
Try defining j outside the for statement as:
 
int j;
void dec2bin(char *a,int i)
{
for(j=0;j<16;j++)
 
and see if it compiles.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Please explain more detailed, what you need.
Perhaps you are searching for this (got it from here: [^]):
#include<stdio.h>

 
void dec2bin(char *a,int i)
{
for(int j=0;j<16;j++)
a[j]='0';
 

a[16]='\0';
j=15;
while(i>0)
{
if(i%2)
a[j]='1';
else
a[j]='0';
i/=2;
j--;
}
}
 

void main()
{
int i;
char a[17];
 

printf("Enter a no : ");
scanf("%d",&i);
 

dec2bin(a,i);
 

printf("\n%s",a);
}
 
What a pity it does not even compile!
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

/*Hi..! Frnds...
   Sreenivasulu.P
   vasu.msit@gmail.com
   +919493482801
To print binary code for Given Positive Integer.*/
 
#include<stdio.h>
main(){
       int i,x=5;//if want change 'x' value here
       for(i=(sizeof(int)*8)-1; i>=0; i--)//in the case of 32 bit processor
             (x&(1<<i))?putchar('1'):putchar('0');//Terinay operator
       getch();
       }
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 910
1 OriginalGriff 508
2 thatraja 225
3 Abhinav S 218
4 Emre Ataseven 200
0 Sergey Alexandrovich Kryukov 8,142
1 OriginalGriff 4,665
2 Peter Leow 3,774
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 16 Dec 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid