C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28691
Number of posts: 94711

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Really Basic Data Type Questions Posted by StuPedful on 11 Dec 2001 at 12:37 PM
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"?
Report
Re: Really Basic Data Type Questions Posted by jeffpost on 11 Dec 2001 at 1:32 PM
: 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.

Report
Re: Really Basic Data Type Questions Posted by StuPedful on 11 Dec 2001 at 1:56 PM
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?

Report
Re: Really Basic Data Type Questions Posted by jeffpost on 11 Dec 2001 at 9:52 PM
: 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.

Report
Re: Really Basic Data Type Questions Posted by StuPedful on 12 Dec 2001 at 6:35 AM
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 <variablename> "<data>" quite a bit, and I don't understand why it uses #define in some spots and const char in others.
Report
Re: Really Basic Data Type Questions Posted by PeterTheMaster on 12 Dec 2001 at 8:09 AM
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;



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.