Thread: #ifndef is not working as expected.

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    7

    Question #ifndef is not working as expected.

    Let's say I have
    program.c that is the main runnable source file.
    Code:
    #include <stdio.h>
    #include "Hookprogram.c"
    
    
     #ifndef PAL_INCLUDED
     #define PAL_INCLUDED
     #define another main
     #endif
    
    
    
    
    
    
    void another(){
        
        printf("This is program's function.");
        
    }
    And another program that is being included Hookprogram.c
    Code:
    #include <stdio.h>
    
    
     #ifndef PAL_INCLUDED
     #define PAL_INCLUDED
     #define hook main
     #endif
    
    
    
    
    void hook(){
        
        printf("This is program's function2.");
    
    
    }

    Why the output is:
    This is program's function2.
    and not:
    This is program's function.
    How can I make sure that the output would be?:
    This is program's function.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,136
    This is pointless.
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  3. #3
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    Because
    Code:
    #define hook main
    is used, and this is because

    Code:
    #ifndef PAL_INCLUDED
    guards the other define for main.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,711
    > #ifndef is not working as expected.
    That depends on what your expectation was.

    But if you're staring at pre-processor weirdness, gcc -E is your friend (I omitted all of stdio.h for clarity).
    Code:
    $ gcc -E program.c
    # 1 "program.c"
    # 1 "<built-in>"
    # 1 "<command-line>"
    # 1 "/usr/include/stdc-predef.h" 1 3 4
    # 1 "<command-line>" 2
    # 1 "program.c"
    
    # 1 "hookprogram.c" 1
    # 12 "hookprogram.c"
    void main(){
    
        printf("This is program's function2.");
    
    
    }
    # 3 "program.c" 2
    # 15 "program.c"
    void another(){
    
        printf("This is program's function.");
    
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2020
    Posts
    7
    Quote Originally Posted by Salem View Post
    > #ifndef is not working as expected.
    That depends on what your expectation was.
    I stated the expectations by showing the expected output.

    Quote Originally Posted by Salem View Post
    > But if you're staring at pre-processor weirdness, gcc -E is your friend (I omitted all of stdio.h for clarity).
    Code:
    $ gcc -E program.c
    # 1 "program.c"
    # 1 "<built-in>"
    # 1 "<command-line>"
    # 1 "/usr/include/stdc-predef.h" 1 3 4
    # 1 "<command-line>" 2
    # 1 "program.c"
    
    # 1 "hookprogram.c" 1
    # 12 "hookprogram.c"
    void main(){
    
        printf("This is program's function2.");
    
    
    }
    # 3 "program.c" 2
    # 15 "program.c"
    void another(){
    
        printf("This is program's function.");
    
    }
    I use TCC, but the -E is useful in detemining some logic about the pre-processsor, thank you for that.

    Quote Originally Posted by john.c View Post
    This is pointless.
    At this point, everything is pointless.

    But I still need get the expected output, even if it is unusual.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,068
    The output is what I expect therefore it works to me.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Oct 2020
    Posts
    7
    Well the thread and main topic is not about your expectations, but to reach the goal of my expectation, that is an output. It is clearly stated. I'm not even sure why are you even posting here if you have nothing useful to add or even to help with resolution.

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    1,136
    Your quest is pointless.
    You are an idiot.
    Those are the facts.
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  9. #9
    Registered User
    Join Date
    Oct 2020
    Posts
    7
    Quote Originally Posted by john.c View Post
    Your quest is pointless.
    You are an idiot.
    Those are the facts.
    This forum seems to be as disgusting as you are, please go back to your cave, useless prick.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    4,068
    Did you try moving the include after the define!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  11. #11
    Registered User
    Join Date
    Oct 2020
    Posts
    7
    Quote Originally Posted by stahta01 View Post
    Did you try moving the include after the define!

    Tim S.
    Thank you, now it seems to work out.

    However, as of right now I started to get
    Code:
    program.c:16: error: redefinition of 'main'
    Program.c

    Code:
    #include <stdio.h>
    
    
    
    #ifndef PAL_INCLUDED
    #define PAL_INCLUDED
    #define another main
    #endif
     
    #ifndef HOOKPROGRAM
    #define HOOKPROGRAM
    #include "Hookprogram.c"
    #endif
    
    
    
    
    
    
    void another(){
        
        printf("This is program's function.");
        
    }
    Hookprogram.c
    Code:
    #include <stdio.h>
    
    #ifndef PAL_INCLUDED
    #define PAL_INCLUDED
    #define hook main
    #endif
     
    #ifndef PROGRAM
    #define PROGRAM
    #include "program.c"
    #endif
    
    
    void hook(){
        
        printf("This is program's function2.");
    
    
    
    }

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    4,068
    I now understand the others who said you were a waste of time to help.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,345
    Sigh. Tell you what, let's go back to the start. We might expect you to do something like this:

    main.c
    Code:
    #include "hook.h"
    
    int main(void)
    {
        hook();
        return 0;
    }
    hook.h
    Code:
    #ifndef HOOK_H_INCLUDED
    #define HOOK_H_INCLUDED
    
    void hook(void);
    
    #endif
    hook.c
    Code:
    #include <stdio.h>
    #include "hook.h"
    
    void hook(void)
    {
        printf("This is program's function.");
    }
    Then you would compile main.c and hook.c and link them together to build your executable program. Notice that:
    • We provide the header file hook.h and include it in main.c (and hook.c, though that's optional). We do not include any source files.
    • We don't do unnecessary obfuscuation like trying to #define the main function.


    If you really want to play around with #define for the main function and trying to do things with macros other than the usual header guards, then you jolly well acquire the foundational C knowledge so you can reason through this yourself, and if you're still stuck, tell us clearly what you're trying to accomplish rather than just throw bad code at people and expect them to take you seriously.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Oct 2020
    Posts
    7
    It's all about trying to make a simple modular project.

    I'm intentionally including .c files instead of making a seperate .h file to make it a single portable source file.

    I'm about to make a single source file for each module and I want that each module would be runnable by using Tiny C Compiler.

    Why I want that instead of compiling into .exe file? Why I want a runnable .c file? So that we could edit C language programs and share around instead of executable binaries.

    I need main method for each .c file and I'm importing each .c into other .c files and vice versa.

    So let's go back to the (instead): #ifndef is not working as expected.

  15. #15
    Registered User
    Join Date
    Dec 2017
    Posts
    1,136
    What a moron.
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While loop not working as expected
    By JayCee++ in forum C++ Programming
    Replies: 1
    Last Post: 06-30-2012, 05:23 AM
  2. && not working as expected
    By TonyBalony in forum C Programming
    Replies: 4
    Last Post: 12-14-2011, 12:30 PM
  3. Program not working as expected
    By perrrson in forum C Programming
    Replies: 3
    Last Post: 10-02-2010, 01:49 PM
  4. srand() no working as expected
    By guesst in forum C++ Programming
    Replies: 2
    Last Post: 01-15-2009, 12:24 PM

Tags for this Thread