Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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
Updated 9-Jan-10 9:21am
v7

SQL
/*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();
       }
 
Share this answer
 
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.
 
Share this answer
 
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")
}
 
Share this answer
 
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.
:)
 
Share this answer
 
v4
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.
 
Share this answer
 
v3
Comments
CPallini 16-Dec-11 6:01am    
Hi Avi, the for statement does NOT produce an infinite loop, it actually produces no loop at all.
Please explain more detailed, what you need.
Perhaps you are searching for this (got it from here: [^]):
C++
#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!
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900