469,626 Members | 889 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

pls #ifdef,#ifndef

#include <stdio.h>
#include <ctype.h>
#define DEBUG 1
main()
{
char inbuf[BUFSIZ];
int i = 0;
int lcnt = 0;
gets(inbuf);
while(*(inbuf+i))
{
#ifdef DEBUG
printf("Character %d Value %c(%o)\n",
i,*(inbuf+i),*(inbuf+i));
#endif
if(isalpha(*(inbuf+i)))lcnt++;
#ifdef DEBUG
printf("Letter Count %d\n",lcnt);
#endif
i++;
}
printf("Total letters %d\n",lcnt);
}

GUYS SORRY FOR DISTURBING YOU A LOT.. AGAIN, I HAVE NO IDEA ABT #ifdef,#ifndef. I CHECKED NET AND LEARNT FEW.. BUT I GOT THIS CODE FROM ONE SITE.. HOWEVER, IF I DELETE THE #define DEBUG 1 , OR ANY #ifdef ... THE CODE IS EXECUTING AS SAME. NO CHANGES..

WATS UP WITH THAT..
??? ALSO PLS TELL ME WHICH SITUATION CAN I USE THEM
IF CAN, ONE SNIPPET ON THIS...
Oct 4 '06 #1
5 3240
Just tried it - if you comment out your #define DEBUG 1 pre-processor directive, the ouput for character "1" is:

Total letters 0

If you leave the #define DEBUG 1 in there, the output for "1" is:

Character 0 Value 1(61)
Letter Count 0
Total letter 0

Basically, the #ifdef tag looks to see if you have the condition defined in your code. If so, it will execute everything up through the #endif tag.

It looks like this code is working correctly - hope this helps, let me know if there is something that I'm missing.
Oct 4 '06 #2
D_C
293 100+
Actually, I think it's just
Expand|Select|Wrap|Line Numbers
  1. #define DEBUG
  2. ...
  3. #ifdef DEBUG
  4. // do debug stuff
  5. #endif
  6. ...
Oct 4 '06 #3
Just tried it - if you comment out your #define DEBUG 1 pre-processor directive, the ouput for character "1" is:

Total letters 0

If you leave the #define DEBUG 1 in there, the output for "1" is:

Character 0 Value 1(61)
Letter Count 0
Total letter 0

Basically, the #ifdef tag looks to see if you have the condition defined in your code. If so, it will execute everything up through the #endif tag.

It looks like this code is working correctly - hope this helps, let me know if there is something that I'm missing.
thank you man.. could be specify me the places where can i use these and how it is working..
Oct 5 '06 #4
Hey there,

One use for the #define #ifdef pre-processor directions is in header files.

Because you don't want to include a header file more than once, it's a good idea to put a #ifndef SOMETHING, then a #define SOMETHING, so that once the file is included one time it doesn't appear again.

For example:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #ifndef _SOMESUCH_HEADER_
  3. #define _SOMESUCH_HEADER_
  4.  
  5. // header file information here, class/function definitions, etc...
  6.  
  7. #endif
  8.  
  9.  
Hope that makes sense. Thanks!

Anthony
Oct 10 '06 #5
thanks a lot man...
Oct 10 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by lovecreatesbeauty | last post: by
6 posts views Thread by Michael B Allen | last post: by
1 post views Thread by Michael Sgier | last post: by
6 posts views Thread by canoewhiteh2o | last post: by
6 posts views Thread by anirbid.banerjee | last post: by
10 posts views Thread by David W | last post: by
46 posts views Thread by mattia | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.