473,320 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 3519
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

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

Similar topics

1
by: Christopher M. Lusardi | last post by:
Hello, How is this possible. If I have two separate files that include the same dot h file as described below why am I allowed to access the same value for a constant. I compiled this program...
5
by: lovecreatesbeauty | last post by:
Do #ifdef or #ifndef have some defects? I ever heard that some people use #if defined() or #if !defined() instead of using #ifdef or #ifndef in header file.
6
by: Michael B Allen | last post by:
Which is the preferred method for preprocessor tests and why? #ifdef XYZ or #if XYZ or #if defined(XYZ) and
1
by: Michael Sgier | last post by:
Hi I get the error: No case-independent string comparison (stricmp, strcasecmp) with the code below. Why...where should stricmp be defined? And how do i get rid of the error on Linux? // //...
1
by: ajaypatel19 | last post by:
I have code like #ifndef ABC #define ABC ... ... #ifdef XYZ ... ..
6
by: canoewhiteh2o | last post by:
I am converting a couple of C header files to C#. It is mainly just a bunch C structs but I am not sure how to handle the #ifdef and #ifndef in C#. For example: #ifndef DATE_TIME #define...
6
by: anirbid.banerjee | last post by:
Hi, I need to write a macro which would have a lot many conditional #ifdef ... #endif blocks in it. #define _xx_macro (x) { ... \ ... \ /* some code (); */ #ifdef _SOME_STMT \ ... \ ... \
10
by: David W | last post by:
Hello, In a C++ MFC application I need to conditionally #include one of two additional resource files in my main resource file because different forms of the application have different names. I...
46
by: mattia | last post by:
I've see in some code: #ifdef __cplusplus extern "C" { #endif what does it mean? Thanks
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.