const modifier

This is a discussion on const modifier within the C Programming forums, part of the General Programming Boards category; I am working my way through a c programming book and I am a little confused with the const modifier. ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    28

    const modifier

    I am working my way through a c programming book and I am a little confused with the const modifier. It says in my book that after you declare a variable with the const modifier, the content of a variable cannot be changed. But if i run this program:

    #include <stdio.h>

    int main(int argc, char *argv[])
    {
    int const x = 10;
    x = 54;
    printf("%i",x);

    system("PAUSE");
    return 0;
    }


    It compiles fine and I was able to change the contents of the variable. The only thing that happend was my compiler (bloodshed dev c++) give me a warning about trying to change a read only variable. So is that the only purpose of the const modifier, to give you a warning if you try to change it?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    The compile is doing its job by warning you of the problem. Turn up the warning/error levels to get it to halt during the compile. The behaviour of the program when run is undefined. It would be allowable for the const variable to be put into read-only memory, any attempt to write to it would result in a crash. In your case, this isn't happening.... that's the magic of "undefined".
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Extra: I recomend using
    Code:
    puts("Press any key...");getch();
    instead of system("pause"); because it's portable, and it doesn't have to run a command line interpreter, which is more inefficient.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >because it's portable
    Bwahahaha!

    >which is more inefficient
    You also forgot safer. If pause happens to be a malicious program than you just hosed your system.
    My best code is written with the delete key.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    puts("Press any key..."); getchar(); would be much better (read: portable).
    If you understand what you're doing, you're not learning anything.

  6. #6
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by Prelude
    >because it's portable
    Bwahahaha!.
    I didn't knew that old amiga or spectrum also have pause command lines...

  7. #7
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by itsme86
    puts("Press any key..."); getchar(); would be much better (read: portable).
    getchar() waits for a character key... followed by enter (I think). getch() returns after any key press except keys like alt, ctrl, shift, caps lock, num lock, scroll lock, windows logo... bla bla bla.
    e.g. getchar(); doesn't return the F** keys.

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    http://www.delorie.com/djgpp/doc/libc/libc_383.html

    Highlights are:
    Code:
    Syntax 
    #include <conio.h>
    
    int getch(void);
    
    Portability
    ANSI/ISO C No 
    POSIX     No
    If you understand what you're doing, you're not learning anything.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Quote Originally Posted by xErath
    getchar() waits for a character key... followed by enter (I think). getch() returns after any key press except keys like alt, ctrl, shift, caps lock, num lock, scroll lock, windows logo... bla bla bla.
    e.g. getchar(); doesn't return the F** keys.
    That really has nothing to do with the getchar() function. Putting the terminal in "raw" mode before calling getchar() will cause getchar() to return even key press as it's typed. In fact, I imagine that that's all getch() does is put the terminal in raw mode, call getchar(), and then return the terminal to its previous state.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21