![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
|
A small code help
#include <stdio.h>
#include <string.h>
#define mxstr 200
main(){
char lighter[mxstr];
strcpy(lighter,"TEST");
printf("%s",lighter);
system("PAUSE");
}Ok i have no idea why that wont work. it's suppose to write "TEST" on the screen but all i see is "Press any key to continue..." >_> It's a C code and i use DevC++ on WindowsXP pro just in case you needed that :p |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
This works on my machine. It prints:
"TESTPress any key to continue." |
|
|
|
|
|
#3 | |
|
Newbie
|
Quote:
works now ^_^ thanks |
|
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
I'm glad to hear it.
|
|
|
|
|
|
#5 |
|
Programmer
Join Date: Feb 2005
Location: Limbo
Posts: 39
Rep Power: 0
![]() |
*steps up onto soap-box*
#define should not be used, because a typed constant is better and easier to debug. 200? It's nice to have a buffer, I know, but try using malloc with the actual string size instead; this will save memory (and we all know that 200 chars will really make a new machine creep )*steps off of soap-box* Sorry. I'm feeling giddy tonight.
__________________
The meek will inherit the earth. -WDaquell |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
While I prefer the use of a typed constant myself, it isn't always the prefered choice. Using #define will cause a textual substitution that will allow a dimension to be used for entities of differing types. I probably also wouldn't use malloc for a small buffer. If I KNEW the size (e.g. "TEST"), I would probably use something like
char myString [] = "TEST"; Whether to define a buffer on the stack, as a global, or from the free store, is a design decision and the criteria may vary from case to case. I certainly wouldn't issue a dictum to a newbie on the point without elucidating the various design considerations. Just my own opinion, of course.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Quote:
The real fix is to add a line; [PHP] fflush(stdout); [/PHP] immediately before the system() call. Out of curiosity, why use a system("PAUSE") at all? It is non-standard, so a simpler solution would be to use (depending on your compiler) [PHP] #include <stdio.h> #include <string.h> #include <conio.h> #define mxstr 200 main(){ char lighter[mxstr]; int byte; strcpy(lighter,"TEST"); printf("%s",lighter); printf("\n Hit any key to continue ...."); fflush(stdout); byte = getch(); /* do whatever you want based on what key is hit by the user */ } [/PHP] getch() is non-standard, but is supported by several compilers under windows when building console mode applications. On other compilers, it may have a different name and be in a different header file --- but that's true of system() as well. Also, I agree with Dawei that using typed constants is not always preferred over #define's. I agree that, given a choice, I will rarely use macros because they can cause lots of hard-to-find problems in code. In practice, particularly with older (pre-standard) versions of C, #define's were the only way of doing such things. And there are still lots of old compilers around. Last edited by grumpy; Jun 12th, 2005 at 1:55 AM. Reason: Fix typos |
|
|
|
|
|
|
#8 |
|
Programmer
Join Date: Feb 2005
Location: Limbo
Posts: 39
Rep Power: 0
![]() |
DaWei and co.
I was just kidding around. These things seemed to me to be of slight importance, but CrAzY_J, please don't be insulted by me. I'm harmless.
__________________
The meek will inherit the earth. -WDaquell |
|
|
|
|
|
#9 |
|
Newbie
|
ok...........
well most of it comes from my book (the C programming language edition 4) the only thing not from there is the system("PAUSE") which i got from devshed. but ill try to use it like grumpy showed ![]() |
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
"getch" will cause the program to wait for any keystroke before proceeding (assuming there are no unused characters buffered up). If you're willing to use ENTER, you may use a standard character input mechanism such as "getchar ()" or (for C++) "cin.get ()".
@wraith: My apologies. I, myself, have an obsession; it's trying to guard the world against self-proclaimed high-priest gurus who tell the acolytes to shut up and just do it "because we got it from on high, and we say so."
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|