Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7401073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:22:19+00:00 2026-05-29T04:22:19+00:00

How can I structure some C code so that I don’t have to laboriously

  • 0

How can I structure some C code so that I don’t have to laboriously go back and
redefine (i.e., #define) things when I need to add a new item in the middle.
Here is a code sample (in the real code, there are about 200 different
defines):

 #define CREAM   1
 #define SALT    2
 #define BUTTER  3
 #define SUGAR   4
 #define FAT     5

void healthyDiet()
{
    int length = 10;
    int menu[500];
    void consume(int, int);

    //   ******  these must be called in the following order only  
    consume( menu[ CREAM ],  length);
    consume( menu[ SALT ],   length);
    consume( menu[ BUTTER ], length);
    consume( menu[ SUGAR ],  length);
    consume( menu[ FAT ],    length);
}

But now I need to #define and add LARD, which would be straightforward if the sequence
here were not important. But LARD must come before SUGAR and after BUTTER. So
I now need to edit the defines:

#define CREAM    1
#define SALT     2
#define BUTTER  3
#define LARD    4
#define SUGAR   5   // changed from 4
#define FAT     6   // changed from 5

So how
can I structure things so that each time I want to add something in the middle, I
don’t have to go back and manually change the define value for each item?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:22:22+00:00Added an answer on May 29, 2026 at 4:22 am

    You’re looking for enum

    enum {CREAM, SALT, BUTTER, LARD, SUGAR};
    

    To add anoter element, just add it:

    enum {CREAM, SALT, BUTTER, LARD, BACON, SUGAR};
    

    You can even use this like a psudeo-iterator:

    enum {FIRST_ONE, CREAM=0, SALT, BUTTER, LARD, BACON, SUGAR, LAST_ONE};
    
    void healthyDiet()
    {
        int length = 10;
        int menu[500];
        void consume(int, int);
    
        int x;
        for(x=FIRST_ONE; x<LAST_ONE; x++)
            consume( menu[ x ],  length);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I serve some static content in liferay? I have a directory structure
Can anybody guide me in the right direction... I have some folder strucktures that
I have a tree structure that can be n-levels deep, without restriction. That means
I have to convert some C code (that ran in a Linux machine) to
I have a given XML document (structure can not be changed) and want to
How can we access variables of a structure? I have a struct: typedef struct
Can anyone help? I have the following structure using associations, and as you can
I have a Sitecore content structure where any single item can have a number
Okay, I need some solid OOP advice. Here's the setup: I have a player
We have a project where a vendor produces some custom code for us and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.