Newbiest of Newbie Questions:

This is a discussion on Newbiest of Newbie Questions: within the C++ Programming forums, part of the General Programming Boards category; Ok, folks. I'm decent with C++, but there's some C things that confuse and anger me. So, just incase I ...

  1. #1
    People Love Me
    Join Date
    Jan 2003
    Posts
    412

    Question Newbiest of Newbie Questions:

    Ok, folks. I'm decent with C++, but there's some C things that confuse and anger me. So, just incase I ever need them, how do #define and printf work? I haven't had a use for either, yet. I'm pretty sure #define is important if you wanna make your own function libraries and such. Little help?

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    hmm...i really don't know C, but the most common ways I have seen #define are with classes (C++ obviously), and similar to how constants work.

    i.e
    #define MAX = 20;
    instead of
    const int = 20;

    I could be wrong though.

    printf I have seen to be similar to cout...

  3. #3
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Code:
    #include <stdio.h>
    
    #define THE_WORLD_IS_ROUND (1)
    
    int main()
    {
    	if THE_WORLD_IS_ROUND
    		printf("Hello World!\n");
    
    	return 0;
    }
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  4. #4
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Originally posted by abrege
    Code:
    #include <stdio.h>
    
    #define THE_WORLD_IS_ROUND (1)
    
    int main()
    {
    	if THE_WORLD_IS_ROUND
    		printf("Hello World!\n");
    
    	return 0;
    }
    Eh? I'm a #define Newbie. You'll have to explain that.

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    It's basically search and replace
    Replace all occourances of THE_WORLD_IS_ROUND with (1)

    The only time I ever use defines in C++ are for class header files... like

    #ifndef _KRAKS_CLASS_HPP_
    #define _KRAKS_CLASS_HPP_

    class Krak {};

    #endif

  6. #6
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Originally posted by Eibro
    It's basically search and replace
    Replace all occourances of THE_WORLD_IS_ROUND with (1)

    The only time I ever use defines in C++ are for class header files... like

    #ifndef _KRAKS_CLASS_HPP_
    #define _KRAKS_CLASS_HPP_


    class Krak {};

    #endif
    Oy, I still dont exactly get it. ifndef?

    Can someone define #define?

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    are you being a troll?


    #define CODEPLACEMENT cout << "this is defined stuff" << endl;

    int main (void)
    {
    CODEPLACEMENT
    return 0;
    }

    all it does is replace the tag with the code
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    go read a book instead!

  9. #9
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    So.....

    Code:
    #define KRAK printf ("\nHello!");
    
    int main(void)
    {
    KRAK;
    return 0;
    }
    Would print "Hello!" to the screen?

  10. #10
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    whats wrong with compiling it yourself?

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Originally posted by Krak
    So.....

    Code:
    #define KRAK printf ("\nHello!");
    
    int main(void)
    {
    KRAK;
    return 0;
    }
    Would print "Hello!" to the screen?
    Remove the ';' behind KRAK because it's already in your define.

  12. #12
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    printf()

    Code:
    int x = 2;
    printf("Hello %d \n Hello %d \n", 1, x )     // %d gets replaced with variables
    This would display
    Hello 1
    Hello 2

    There's a shipload of formatting characters that you can use in place of %d. for example %s is for a string.

    sprintf() is similar. It builds a C-style string which can be stored, rather than being displayed.

    [edit]
    There was a post recently about #define being evil!! (Because it is global.)

    So, this is preferred:
    const int MAX = 20;
    Last edited by DougDbug; 02-04-2003 at 06:29 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory handling functions (newbie questions)
    By PaulBlay in forum C Programming
    Replies: 6
    Last Post: 03-10-2009, 07:37 AM
  2. newbie questions
    By raptorx in forum C Programming
    Replies: 2
    Last Post: 10-10-2007, 10:30 PM
  3. Newbie Questions
    By snoopy1 in forum C++ Programming
    Replies: 3
    Last Post: 11-19-2006, 02:00 PM
  4. Real newbie with VC6 questions
    By MagiZedd in forum Windows Programming
    Replies: 8
    Last Post: 10-15-2001, 09:27 PM
  5. newbie questions again
    By dune911 in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2001, 03:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21