Thread: Fairly new to C, got a few problems.

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    7

    Talking Fairly new to C, got a few problems.

    Wahoo, first post here! Good day all!
    I hope this is in the right place - being about C - but as some things relate to using Cygwin, it might be better elsewhere. I'm not sure.
    Anyway, I don't know a lot of C or of programming in general, so I come here seeking help for some problems I've enountered. These are probably pretty obvious/easy/whatever, but my search-fu is weak so I haven't found any solutions.

    1) Running a program in a window.
    So far I've been using Cygwin as a compiler for my code, which works great. However, so far it is also the only thing my code will output to. How do I output to a dedicated window?

    2) Colours!
    As I've only been outputting to the Cygwin window, so far everything has been in black and white text. I don't have a problem with the 'text' part of it, but I'd like to be able to control how *each character* looks in terms of colour when it's outputted - blue text on a green background for the first character, for instance, but then to be able to change that for subsequent ones. You get the idea.

    3) Charactersets.
    I was previously just using the default cygwin characterset, which I understand is 'standard' ASCII. Some hours of research lead me to adding 'set LANG=en_US.CP437' to cygwin.bat, which gives 'extended' ASCII. However, it doesn't want to display some of the earlier characters, only showing the hearts/clubs/diamonds/spades. I'd like the smily faces/etc. as well!
    Either within Cygwin or within its own window, I'd like to be able to use these not-shown characters - how do I do that? Additionally, is it possible to define my own character set? (potentially so I could use rudimentary graphics)
    Throughout all this I've been wanting to be able to customise like you can in Dwarf Fortress, for those that know of that.

    And thus ends my first, verbose, post.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok, you're on windows... with CYGWIN... this isn't what I would consider your best learning environment.
    I know the others think I'm running an ad campaign my now but you should give Pelles C a try... it's C-99 standard, has a great IDE and comes with the best help file I've ever seen... and it's free. I think you'll be a lot happier learning there.

    As for the "dedicated window", that's called a "console window" and it's what you get when writing C programs with main() in them, it's part of Windows, not part of CYGWIN.... It's the same window you get if you go WIN+R and type cmd into the dialog... This is also called "command line" programming since it is not intended to operate in GUI (Windowed) mode. It's your best place to learn...

    The thing for you to do, if you're serious about becoming a decent "C Dabbler" (and I'm it's Rogue leader .. LOL) is to sit yourself down with a C textbook or extended tutorial... Start on page 1, read, type up the examples and exercises and play with them, learn how they work, learn what doesn't work... now turn to page 2 ... repeat as necessary until you get to the back cover of the book.

    At that point you will be "qualified" to write a few console mode programs on your own... and you should, lots of them, learn the craft and get yourself fairly good at it...

    Then, once you are confident in console mode you can make the switch over to GUI mode by following the tutorials on TheForgers site. Pelles C is fully GUI capable and comes with a complete set of Windows headers and libraries as well as some of the best Resource editors you'll find anywhere.

    However --and I can't stress this enough-- there are no shortcuts. Do the work, step by step, learn incrementally and learn by doing.... anything else is just going to leave you frustrated and confused.

    Ok... we now return you to our regularly scheduled deluge of meaingless drivel...

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Woobly View Post
    2) Colours!
    As I've only been outputting to the Cygwin window, so far everything has been in black and white text. I don't have a problem with the 'text' part of it, but I'd like to be able to control how *each character* looks in terms of colour when it's outputted - blue text on a green background for the first character, for instance, but then to be able to change that for subsequent ones. You get the idea.
    I know of two ways to do that. I think cygwin includes the bash shell, so you should be able to use ANSI "escape sequences":

    ANSI escape code - Wikipedia, the free encyclopedia

    If this works, you are ANSI compatible:

    Code:
    #include <stdio.h>
    
    #define BOLDMAGENTA "\033[1;35m"
    #define GREEN "\033[0;32m"
    
    int main(void) {
    	printf("%sHello %sworld!\n", BOLDMAGENTA, GREEN);
    
    	return 0;
    }
    The other option is some kind of console library such as ncurses, but I think you will find that is not worth it just for some color.

    3) Charactersets.
    I was previously just using the default cygwin characterset, which I understand is 'standard' ASCII.
    Cygwin should be able to provide unicode (aka, UTF-8) support, which is, eg, the norm on the web. Pretty much every character you've ever seen on a screen is available in unicode.

    If this code gives you ☺Hello↔world! you have UTF8 available (it has to be compiled std=c99):

    Code:
    #include <stdio.h>
    
    #define HAPPYFACE "\u263a"
    #define LEFTRIGHT_ARROW "\u2194"
    
    int main(void) {
    	printf("%sHello%sworld!\n", HAPPYFACE, LEFTRIGHT_ARROW);
    
    	return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Strong first post.

    1) I assume you mean you're running gcc and the resulting executable through the command prompt but want a dedicated window for your program to run in. While you could create your own console window manually and output to it, a vastly simpler way to do that would be to run your compiler through an IDE such as CodeLite or Code::Blocks. When running or debugging from an IDE, a dedicated window will be created for you. Note that while Cygwin is cool, if all you're looking for is the gcc compiler (and related tools), MinGW is often the better choice.

    2) I haven't used Cygwin in a while, so take this with a grain of salt. You should be able to use the VT100 escape sequences to set the color. However, learning C in a bastardized environment (ie. a not-quite-Linux environment plopped on top of a Windows host) may be more frustrating than simply working with Windows directly. It's not like you can't use standard and portable C on Windows.

    3) Extended ASCII provides suitable characters for 80s-style graphics. Some of the "early" characters are actually control characters without a display representation, so what gets displayed when you print them is somewhat unpredictable. But it should be consistent for the same translation driver, so provided you map out what happens beforehand, you can use those graphics in your programs.

    Additionally, is it possible to define my own character set?
    It's possible, but not exactly trivial. Best to avoid that until you're a little further along.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    7
    Wow, quick responses!

    @CommonTater: Ok, thanks for the links! Investigating them now.
    I've been meaning to find a good C tutorial - I used to have one but lost it - so time to try Googling something, methinks.

    @MK27: The first one works! I looked at the Wikipedia link, but I'm afraid most of it went straight over my head.
    Fiddling with the values after #define seems to mean that 0 or 1 just before the semicolon is dark/light, and the last number (before the m) controls the colour, corresponding to the first table here. Fiddling with the second to last number seems to be doing something to the colour of the text and that of the background, but I'm not sure what. Hmm, time for some more digging!
    The second one, however, doesn't work. On compile it gives me two 'universal character names are only valid in C++ and C99' warnings, and running it produces 'Gÿ||HelloGåöworld!' The warnings lead me to think this is something to do with your note 'it has to be compiled std=c99'. Wikipedia tells me that C99 is just a (the latest?) version of C, so given that I'm compiling for 'c', I'm not sure what I'm doing wrong.

    @Prelude Thanks!
    1) I'm running gcc through Cygwin, which I understand to be a Linux Terminal emulator. So, 'through the command prompt' is correct... I think? Thanks for the links, now to find out what an IDE is... (damn I feel stupid >.<) *Goes onto Wikipedia*
    2) Yup, CommonTater and your posts lead me to think I ought to be seeking something a bit different.
    3) The sort of graphics I'm after would be like this - where the 'earlier' characters do print something. I'm linking what Dwarf Fortress uses, which seems like a pretty good characterset. The page notes that it's CP437, however using CP437 in Cygwin isn't giving me those 'earlier' characters - so I guess they're being used as control characters not little pictures.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Woobly View Post
    Fiddling with the second to last number seems to be doing something to the colour of the text and that of the background, but I'm not sure what. Hmm, time for some more digging!
    Yeah, YMMV with that stuff. I don't use it myself much, I'm just aware of it.

    The second one, however, doesn't work. On compile it gives me two 'universal character names are only valid in C++ and C99' warnings, and running it produces 'Gÿ||HelloGåöworld!' The warnings lead me to think this is something to do with your note 'it has to be compiled std=c99'.
    Ie, gcc -std=c99 mycode.c. It actually doesn't make any difference in this case, it just gets rid of the warning.

    You need to have your environment set to use UTF8. I would think that because Cygwin is supposed to provide a "POSIX compatible environment" that it could do this, but I don't know for sure, or how. I believe windows itself is not very unicode friendly so that might actually be impossible.

    It also doesn't support ANSI natively, so beware if you give up the cygwin shell you'll probably lose it (but don't let that be a serious motive).

    Really, as Common Tater first said, I think you should just accept that for the time being you can't do everything you want, and focus on learning the fundamentals of programming. Don't worry about superficial details like color etc.
    Last edited by MK27; 08-11-2011 at 09:28 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    As everyone else has pointed out the best thing for you to do is to get a better grasp of the C language before trying to play with console graphics. However, once you feel you are ready to move on take a look at adrian's console tutorial. It will show you how to really play around with console programming.

    I also second Prelude, excellent first post.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Woobly View Post
    Wow, quick responses!

    @CommonTater: Ok, thanks for the links! Investigating them now.
    I've been meaning to find a good C tutorial - I used to have one but lost it - so time to try Googling something, methinks.
    You could start with Teach Yourself C in 21 Days not everyone likes it but I found it perfectly adequate first step. To do much more than poke your toe in the water you will need more extensive tutelage... Of course your best bet is the old standby Amazon.com: C Programming Language not free but worth every penny.

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    7
    @MK27: Alrighty, figured out colours! But.. but...
    'don't worry about superficial details like colors'
    colours are important! For creating pwetty pictures... (or in my case, probably how part of the information will be conveyed.)

    @Andrew Hunter: Thanks for the link. Between you lot I've got about 10 cluttering up my tabs now! *puts on reading glasses*
    Also, thanks!

    @CommonTater: ooh, that looks like a good tutorial. Took a quick flip through, looks like I know what's it's going over at the start already, but the later stuff is definitely new. Thanks! (Also, pdf'd and saved to my iPod xD)
    As for shelling out some money on a book... I'm not adverse to it, but I'd rather see what I can get for free first!

    So let's see, colours sorted, charactersets are too tricky, hmm, now to figure out getting my program in its own window.
    (btw, when I say that I mean that I'd like it not outputting to the command line/equivalent, but so that when you run the .exe it'll open up in its own window. Not proper graphical mind, just something which is effectively a grid where you have control over what characters are printed on the grid. A bit like the command line.)

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Some useful C resources:

    Steve Summit's C Tutorial
    C-FAQ
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Woobly View Post
    So let's see, colours sorted, charactersets are too tricky, hmm, now to figure out getting my program in its own window.
    (btw, when I say that I mean that I'd like it not outputting to the command line/equivalent, but so that when you run the .exe it'll open up in its own window. Not proper graphical mind, just something which is effectively a grid where you have control over what characters are printed on the grid. A bit like the command line.)
    Assuming you are working in Windows, once you compile your program and produce the exe all you need to do is find the program using your file explorer and open it. Windows takes care of loading the program to include opening up a console window for it.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Woobly View Post
    So let's see, colours sorted, charactersets are too tricky, hmm, now to figure out getting my program in its own window.
    (btw, when I say that I mean that I'd like it not outputting to the command line/equivalent, but so that when you run the .exe it'll open up in its own window. Not proper graphical mind, just something which is effectively a grid where you have control over what characters are printed on the grid. A bit like the command line.)
    Get through the tutorial first... Seriously, you'll be glad you did...

    Then when you move up to TheForger's stuff, you will learn how to make full graphic programs in GUI mode... and yes you can make full screen windows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help in a fairly simple C prog
    By CodeSlow in forum C Programming
    Replies: 4
    Last Post: 01-04-2008, 02:49 AM
  2. Fairly simple problem
    By fatdunky in forum C Programming
    Replies: 1
    Last Post: 11-14-2005, 11:34 PM
  3. Fairly simple classes help please
    By sammacs in forum C++ Programming
    Replies: 13
    Last Post: 12-01-2004, 10:42 AM
  4. Should be a fairly easy question
    By lpmrhahn in forum C Programming
    Replies: 5
    Last Post: 03-21-2004, 12:55 PM
  5. This should be fairly simple to fix.
    By Lancer in forum C++ Programming
    Replies: 3
    Last Post: 03-04-2004, 08:57 PM