•
•
•
•
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
![]() |
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...
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;
}•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation:
Rep Power: 40
Solved Threads: 972
>>but the problem is that same result is printed for any number
The scanf() line needs a pointer to n.
The scanf() line needs a pointer to n.
>printf("Enter Number :");
You should call
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:"
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:"
c Syntax (Toggle Plain Text)
#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; }
I'm here to prove you wrong.
Yes salem you r rite........
Im now wondering that how i forgot to put &
While compiling the following
compiler shows a warning..."code has no effect"
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"
>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:
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- problem in printing in VB (Visual Basic 4 / 5 / 6)
- Printing problem with Qbasic (Legacy and Other Languages)
- Printing from java (Java)
- Computer freezes while printing from certain programs (Windows 9x / Me)
- printing a linked list (C++)
- Burner Drive, Printing, PDFs (Apple Hardware)
- Problem printing from IE6 (Web Browsers)
Other Threads in the C Forum
- Previous Thread: C serial communication in Windows
- Next Thread: Help



Linear Mode