Really Basic Data Type Questions

I am new to C, and I have some questions about data types that I would really like answered.

What is the difference between using "const" and not using it, e.g. "const char" versus "char"?

What is the difference between "const" and "static"?

What is the difference between using "const" and "#define"?

Comments

  • : I am new to C, and I have some questions about data types that I would really like answered.
    :
    : What is the difference between using "const" and not using it, e.g. "const char" versus "char"?

    const declares a non-modifiable value, so it can be assigned to rom space rather than ram. Not usually an issue with programs for PCs.
    :
    : What is the difference between "const" and "static"?

    static, when declaring a global variable means it's not visible outside the current module (file). When declared within a function, it means the variable lives in global memory instead of on the stack and retains its value between function calls.
    :
    : What is the difference between using "const" and "#define"?
    :
    #define is just for preprocessor text substitution. Has nothing to do with const.

  • Thanks for the reply. I do have one other question to help clarify something for me.

    : : What is the difference between using "const" and "#define"?
    : :
    : #define is just for preprocessor text substitution. Has nothing to do with const.

    So, when would one use something like

    #define CPUSTRING "Intel - x86"

    versus

    const char CPUSTRING = "Intel - x86";

    ? CPUSTRING is a const char in either case, right?

  • : Thanks for the reply. I do have one other question to help clarify something for me.
    :
    : : : What is the difference between using "const" and "#define"?
    : : :
    : : #define is just for preprocessor text substitution. Has nothing to do with const.
    :
    : So, when would one use something like
    :
    : #define CPUSTRING "Intel - x86"
    :
    : versus
    :
    : const char CPUSTRING = "Intel - x86";
    :
    : ? CPUSTRING is a const char in either case, right?
    :
    Not necessarily. If you use the define, you could write

    const char stuff[] = CPUSTRING;

    or

    char stuff[] = CPUSTRING;

    In the first instance, it would indeed be a const char array, but not in the second case. Again, const is only really useful if you're going to rom the application, for example in an embedded system. For PC apps, it doesn't make any difference, except that the compiler may complain about lack of type casting.

    The statement

    const char CPUSTRING = "Intel - x86";

    will give you a compiler error.

  • Thanks for the reply.

    : The statement
    :
    : const char CPUSTRING = "Intel - x86";
    :
    : will give you a compiler error.

    Yeah, I see now I should have written

    const char CPUSTRING[] = "Intel - x86";

    :Again, const is only really useful if you're going to rom
    :the application, for example in an embedded system. For PC
    :apps, it doesn't make any difference, except that the compiler
    :may complain about lack of type casting.

    One of the reasons I ask about the differences is because the Quake III source code uses "const char" and
    #define "" quite a bit, and I don't understand why it uses #define in some spots and const char in others.
  • const declarations are better style and more typesafe. the compiler can directly check if statements involving it are ok.

    but using #define is more efficient. after the compilation
    there wont be a variable for this but a literal constant.

    and see:
    #define b 3
    a=b;
    after preprocessor run:
    a=3;
    assembler:
    mov a,3;

    on the other hand:
    const int b=3;
    a=b;
    assembler:
    mov ax,b;
    mov a,ax;
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories