User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 456,233 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,767 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 1600 | Replies: 13 | Solved
Reply
Join Date: Oct 2007
Posts: 11
Reputation: piscean123 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
piscean123's Avatar
piscean123 piscean123 is offline Offline
Newbie Poster

Help problem in printing Hexadecimal no's

  #1  
Nov 7th, 2007
Need your help in this code too..
this is the program to convert any entered number into its hexadecimal equivalent but the problem is that same result is printed for any number...
int main (void)
{
	int n;
	printf("Enter Number :");
	scanf("%d", n);
	printf("Hexadecimal Equivalent %x", n);
	getch();
	return 0;

}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: problem in printing Hexadecimal no's

  #2  
Nov 7th, 2007
>>but the problem is that same result is printed for any number
The scanf() line needs a pointer to n.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Sep 2004
Posts: 6,510
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 487
Super Moderator
Narue's Avatar
Narue Narue is online now Online
Expert Meanie

Re: problem in printing Hexadecimal no's

  #3  
Nov 7th, 2007
>printf("Enter Number :");
You should call fflush ( stdout ) after this. The stream buffer is only guaranteed to be flushed when it's full (which you can't portably determine), when a newline is printed (which you aren't doing for this prompt), or when fflush is called. If stdout isn't flushed before the scanf call, your user may not see the prompt.

You also need to include stdio.h. Your code presently exhibits undefined behavior.

>scanf("%d", n);
scanf takes a pointer to int so that it can store the value in your variable. Because n isn't already a pointer, you need to pass the address of n. You should also be checking the return value of scanf in case the user can't type or follow instructions.

>getch();
You need to include conio.h, but getchar is better because it's standard while getch is not.

Here's a better example:"
  1. #include <stdio.h>
  2.  
  3. int main ( void )
  4. {
  5. int n;
  6.  
  7. printf ( "Enter Number: " );
  8. fflush ( stdout );
  9.  
  10. if ( scanf ( "%d", &n ) == 1 )
  11. printf ( "Hexadecimal Equivalent %x\n", n );
  12.  
  13. while ( getchar() != '\n' )
  14. ;
  15. getchar();
  16.  
  17. return 0;
  18. }
I'm here to prove you wrong.
Reply With Quote  
Join Date: Dec 2005
Posts: 3,834
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 436
Colleague
Salem's Avatar
Salem Salem is offline Offline
banned

Re: problem in printing Hexadecimal no's

  #4  
Nov 7th, 2007
> scanf("%d", n);
Are you using gcc?
Have you considered gcc -W -Wall prog.c
You forgot the &, which the above compiler/warning options would tell you.
Reply With Quote  
Join Date: Oct 2007
Posts: 11
Reputation: piscean123 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
piscean123's Avatar
piscean123 piscean123 is offline Offline
Newbie Poster

Re: problem in printing Hexadecimal no's

  #5  
Nov 7th, 2007
Yes salem you r rite........
Im now wondering that how i forgot to put &

While compiling the following
#include <stdio.h>

int main ( void )
{
int n;

printf ( "Enter Number: " );
fflush ( stdout );

if ( scanf ( "%d", &n ) == 1 )
printf ( "Hexadecimal Equivalent %x\n", n );

while ( getchar() != '\n' )
;
getchar();

return 0;
}

compiler shows a warning..."code has no effect"
Reply With Quote  
Join Date: Sep 2004
Posts: 6,510
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 487
Super Moderator
Narue's Avatar
Narue Narue is online now Online
Expert Meanie

Re: problem in printing Hexadecimal no's

  #6  
Nov 7th, 2007
>compiler shows a warning..."code has no effect"
What line and what compiler?
I'm here to prove you wrong.
Reply With Quote  
Join Date: Oct 2007
Posts: 11
Reputation: piscean123 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
piscean123's Avatar
piscean123 piscean123 is offline Offline
Newbie Poster

Re: problem in printing Hexadecimal no's

  #7  
Nov 7th, 2007
getchar();

Borland C++
Reply With Quote  
Join Date: Sep 2004
Posts: 6,510
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 487
Super Moderator
Narue's Avatar
Narue Narue is online now Online
Expert Meanie

Re: problem in printing Hexadecimal no's

  #8  
Nov 7th, 2007
>Borland C++
That makes sense. Borland likes to complain about unused results. You can safely ignore the warning (because getchar really does something here), or you can cast the result to void to silence the warning:
(void)getchar();
I'm here to prove you wrong.
Reply With Quote  
Join Date: Oct 2007
Posts: 11
Reputation: piscean123 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
piscean123's Avatar
piscean123 piscean123 is offline Offline
Newbie Poster

Re: problem in printing Hexadecimal no's

  #9  
Nov 7th, 2007
The problem is solved by replacing getchar(); with getch();
Reply With Quote  
Join Date: Nov 2007
Posts: 1
Reputation: venkata is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
venkata venkata is offline Offline
Newbie Poster

Re: problem in printing Hexadecimal no's

  #10  
Nov 7th, 2007
how to print /n
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 5:04 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC