Thread: What are macros and why build your own c library?

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    74

    What are macros and why build your own c library?

    Hello guys, I came across a word MACRO, what does it mean in programming? And why people create their own c libraries?

    Thanks.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Sankait Laroiya View Post
    Hello guys, I came across a word MACRO, what does it mean in programming?
    In C macros are divided into MACRO constants and MACRO that take parameters.

    The MACRO constants are used to avoid doing magic number programming [constants].
    Magic number (programming) - Wikipedia, the free encyclopedia

    Edit2: Look up the "#define" for more info on MACROs

    Edit3: http://en.wikipedia.org/wiki/C_preprocessor

    MACRO that take parameters are too complex for me to explain to anyone; esp. to a newbie C programmer.

    Quote Originally Posted by Sankait Laroiya View Post
    And why people create their own c libraries?
    They do it in order to make useful programs that are NOT tiny.
    They do it in order to reuse the code/libraries.
    They do it to avoid using other people library they do NOT wish to use.
    They do it to avoid re-inventing the wheel in every program they write.

    Tim S.
    Last edited by stahta01; 09-24-2014 at 05:49 AM.
    "...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

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    74
    Well, I didn't understant it. Anyone else with a simple explanation?

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Which part didn't you understand? Maybe you could try one question per thread to make it easier to tackle them? For the library question, a library is nothing more than a collection of routines that are useful. If you write a few programs, you will probably notice saying to yourself, "Oh, I can use the fn_foo on this problem, which I created a few days ago while working on project bar." If you reuse fn_foo more than once or twice, then you may as well add it to your library so that you can use it whenever you feel like it.

    Same goes for macros, which in C can be used to create text substitutions that behave like functions. However, if you can write it as a function you should do so, there's no reason to write it as a macro in that case.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Why would I want to write my own library...

    It might be easier to have an example.

    Say I have an encryption algorithm -> I've written a VERY basic one here for you (note that the code could be a lot smaller, but I'm guessing that you are quite novice). However, this could represent a complicated and large algorithm.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    
    #define MYKEY 0x01
    
    void HideString(char inputString[], size_t inputSize);
    void FindString(char inputString[], size_t inputSize);
    
    int main(void)
    {
        char myString[16] = "Test String";
    
        //Encrypt String
        HideString(myString, sizeof(myString));
        printf("<Hidden> %s\n", myString);
    
        //Decrypt String
        FindString(myString, sizeof(myString));
        printf("<Found> %s", myString);
    
        return EXIT_SUCCESS;
    }
    
    void HideString(char inputString[], size_t inputSize)
    {
        int i;
    
        for (i=0; i<inputSize; i++)
        {
            char inputChar = inputString[i];
    
            //Skip if not a number, or not part of the alphabet
            if(!isalnum(inputChar))
            {
                continue;
            }
    
            inputChar = inputChar ^ MYKEY;
    
            inputString[i] = inputChar;
        }
    }
    
    void FindString(char inputString[], size_t inputSize)
    {
        int i;
    
        for (i=0; i<inputSize; i++)
        {
            char inputChar = inputString[i];
    
            //Skip if not number, or not part of the alphabet
            if(!isalnum(inputChar))
            {
                continue;
            }
    
            inputChar = inputChar ^ MYKEY;
    
            inputString[i] = inputChar;
        }
    }
    Let's say that I want to get some data from a microcontroller programmed in C and send it it a program written on my desktop also written in C.

    I could just copy and paste the code from one program to another: But what if I need to change the algorithm? What if your encryption has been cracked?

    If both programs used the same header file, I could just edit the one header file and then recompile the other programs (rather than open the programs, find the subroutines, copy and paste the new routines, and then recompile). This is the better way to build projects with common routines.

    Another thing that you can see is that I used a MACRO "MYKEY". What will happen is that before my program is compiled, every instance of "MYKEY" is swapped for what I defined it as at the top (0x01) -> Imagine if I had use MYKEY 50 times and I had to change it to 0x02! Wouldn't it be better to just change the definition rather than going through the code and changing it one instance at a time?

    Other small points that you may get stuck on
    -> The word "continue" means go and do the next loop (i.e. inputChar is not change, the code just goes to the next loop)
    -> "isalnum" returns the integer value of "1" if its input is either part of the alphabet, or is a number ("alphanumeric"). The "!" before it means "not" - i.e. true only if the char is not alphanumeric. This is from the library ctype.h.
    -> "EXIT_SUCCESS" is a macro defined in stdlib.h -> It is more self-documenting than "0". Note that you should always use the "EXIT_FAILURE" if you need to end your program because you detected an error: The standard says that 0 is always the number returned on a successful ending of your program, but failure is just a non-zero number. "stdlib" should always have the correct return number for your OS.

    If you have any further questions, feel free to ask.
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by Click_here View Post
    Another thing that you can see is that I used a MACRO "MYKEY". What will happen is that before my program is compiled, every instance of "MYKEY" is swapped for what I defined it as at the top (0x01) -> Imagine if I had use MYKEY 50 times and I had to change it to 0x02! Wouldn't it be better to just change the definition rather than going through the code and changing it one instance at a time?
    I thought something like MYKEY was called a "symbolic constant", and that a "macro" was something with a function type form (?) :

    Code:
    #define MYKEY(ch, lshift) (ch << lshift) ^ 0xF0 
    
    /// Or whatever it would look like in real life :P
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Alpo View Post
    I thought something like MYKEY was called a "symbolic constant", and that a "macro" was something with a function type form (?) :

    Code:
    #define MYKEY(ch, lshift) (ch << lshift) ^ 0xF0 
    
    /// Or whatever it would look like in real life :P
    Nope, both are called macros. In the standard (see C11 6.10.3 for more info), they refer to them as object-like macros for the symbolic constant (and other non-function-like uses) and function-like macros for the function type. Note that your example seems to contradict your understanding since MYKEY is a function-like macro -- it takes parameters ch and lshift.

    A "symbolic constant" or "named constant" is just a term for a constant value that has been given a name of some sort, regardless of how it was declared. This is in contrast to magic numbers. For example
    Code:
    const int ANOTHER_SYMBOLIC_CONSTANT = 42;
    #define ONE_SYMBOLIC_CONSTANT 17
    // not symbolic/named constants
    char string[666];  // unnamed constants are the devil

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I'm not sure of the proper semantics regarding the word "macro" in C programming - but I think Click_here might be correct. For instance, I know NULL is referred to as a macro.

    The standard also seems to differentiate between "object-like macros" and "function-like macros" (6.10.3).

    [edit] Confirmed by anduril462 (thanks!)

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Don't quote me on this, but I've always seen the word "macro" not refering to the nature of the #define, whether it be a constant or a little code, but the process of going through the code and changing it before compiling. i.e. The doing part, rather than the content.
    Fact - Beethoven wrote his first symphony in C

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    See the "Text substitution macro" part here for what I mean Macro (computer science) - Wikipedia, the free encyclopedia
    Fact - Beethoven wrote his first symphony in C

  11. #11
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by anduril462 View Post
    Note that your example seems to contradict your understanding since MYKEY is a function-like macro -- it takes parameters ch and lshift.

    A "symbolic constant" or "named constant" is just a term for a constant value that has been given a name of some sort, regardless of how it was declared. This is in contrast to magic numbers. For example
    Code:
    const int ANOTHER_SYMBOLIC_CONSTANT = 42;
    #define ONE_SYMBOLIC_CONSTANT 17
    // not symbolic/named constants
    char string[666];  // unnamed constants are the devil
    I understand, I was demonstrating what I was talking about by giving an example (I just named it the same as the other MYKEY). I suppose enum's are considered a symbolic constant as well. I didn't realize there was an overlap of the definitions.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  12. #12
    Registered User
    Join Date
    Sep 2014
    Posts
    74
    That means libraries are collection of your own defined functions and macros? Thanks.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Sankait Laroiya View Post
    That means libraries are collection of your own defined functions and macros? Thanks.
    Of course, you could always Google something like "software library" or "programming library", which might give you a helpful result like this one: Library (computing) - Wikipedia, the free encyclopedia.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 02-08-2012, 10:23 PM
  2. Error in Exceptions.h file during build of QuickFix library
    By arupsarkar in forum C++ Programming
    Replies: 3
    Last Post: 07-16-2010, 10:30 AM
  3. Macros inside of macros
    By Chewie8 in forum C Programming
    Replies: 2
    Last Post: 02-24-2008, 03:51 AM
  4. Replies: 19
    Last Post: 01-12-2006, 11:04 AM
  5. Library build path
    By aker_y3k in forum C++ Programming
    Replies: 7
    Last Post: 10-05-2002, 12:46 AM