what is "#" do in C programming ?

M

Mylinux

why are these lines marked with #

eg
panel.h

#define LINE_LENGTH 16



I want to define line_length 20

I should take away "#".

please correct me.
 
W

WW

Mylinux said:
why are these lines marked with #

Preprocessor directives.
eg
panel.h

#define LINE_LENGTH 16



I want to define line_length 20

Do you want to define LINE_LENGTH or line_lenght? C is case sensitive.

If you want to define LINE_LENGTH to be 20 instead of 16 just change 16 to
20.
I should take away "#".

No
 
G

Gianni Mariani

Mylinux said:
why are these lines marked with #

C and C++ have 2 stage processing.

The first stage is the "c/c++ preprocessor" and anything with a # is
munged by the pre-processor.

The pre-processor manages directives like.


#define
#else
#endif
#if
#ifdef
#include
#line
#pragma

The # must be the first (non whitespace ?) character on the line for the
pre-processor to recognize it.

So, "#define" is a macro define directive. *IMPORTANT* - it is a
textual substitutionm - be VERY carful.

Here is an example where it does not do what you expect


#define CONSTANT 1+2 // constant is 1 plus 2

OK

cout << 100 * CONSTANT; // this is 300 --- right ? WRONG

The macro substitution leads to

cout << 100 * 1+2

which as you know will print 102.

Macros are great for some things but I reccomend you use enums and const
values.

e.g.

enum { CONSTANT = 1+2 };

or

const int CONSTANT = 1+2;

eg
panel.h

#define LINE_LENGTH 16



I want to define line_length 20

I should take away "#".

Write this as:

const int LINE_LENGTH=20;

and nuke the #define.
 
J

Jon Bell

why are these lines marked with #

eg
panel.h

#define LINE_LENGTH 16

They're preprocessor directives. You can read about them in any decent C
or C++ book, or try a Google search on "preprocessor directives".
 
M

Mylinux

so "#" is not a remark in C programming.


if I want to define LINE_LENGTH 16 to 20.

I should in panel.h
....
#DEFINE LINE_LENGTH 20

.....
.....


q2. why it is panel.h, what *.h means?
 
M

Mike Wahler

Mylinux said:
so "#" is not a remark in C programming.


if I want to define LINE_LENGTH 16 to 20.

I should in panel.h
...
#DEFINE LINE_LENGTH 20

No. All C identifiers and keywords are case sensitive

#define LINE_LENGTH 20
....
....


q2. why it is panel.h,

Ask whomever gave it that name.
what *.h means?

Anything or nothing. It's just part of a file
name where that code is stored.
C++ doesn't care about source file names.
You can use any names which are valid on
your system. '.h' is a quite common form
for header files.


-Mike
 
W

WW

Mylinux said:
"define" should be small case in c programming?

Lower case, yes. I suggest you get a good C introductory book. Or use
Google to find an online tutorial. Maybe regulars here can even suggest you
one. But I am afraid if you start programming in C without knowing even the
basics you will do more harm than good to yourself. You will develop a lot
of misconceptions which will harm you later when you program.

C is a very simple language compared to C++. The K&R book (describing
todays ANSI C language) is a very short book. Try to get it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top