enum in c programming

WBahn

Joined Mar 31, 2012
32,883
What do you mean by indent your code. does it means include numbers for each lines. I have posted code with lines and numbers. you can see error in line number. can you tell me what you all telling me about indent code?
No, indenting is not the same as including line numbers. Indenting is moving a line of text to the right relative to the line above it.

The line numbers of the code you posted clearly are NOT the same as the line numbers that the errors are using.

This is particularly evident in the two code blocks you posted in Post #8. What line is the main() declaration on? In the top block it is on line 54 while in the bottom block it is on line 1. But neither of these are the line it is on in the file that the compiler is working with, probably because you have stuff in the file that is above the first line that you have in your block.
 

WBahn

Joined Mar 31, 2012
32,883
okay I understood. your code is readable, anyone can understand easily. I think indent code will not work on real design. I think it will only help to understand logic , it will not work on real project. I was reading this link https://www2.cs.arizona.edu/~mccann/indent_c.html I have to write code in that style to understand easily. I am trying to write c program for three LED's using different time delay because I want to learn use of enum, case and switch statement. when I look the example for enum, case and switch statement. I understand but when I try to write in one program for real project I am facing problem.
While it is true that, in C and most languages (not all -- Python is a case in point) indenting is ignored and has no effect on what the compiler produces. But this is NOT the same as saying that indenting will not work on a real design. The purpose of indenting is specifically to communicate logical organization to the human reader and the fact that the compiler ignores it means that it will work in a real design just fine -- it doesn't affect the compiled code one way or the other.
 

WBahn

Joined Mar 31, 2012
32,883
Hello,

MrBool has a page about the use of indent:
http://mrbool.com/importance-of-code-indentation/29079

Bertus
Generally I agree with his guidelines. There's really only three things I take some level of exception to:

1) The requirement to use block delimiters for single statement blocks. I understand the reasoning and it has merit. But since I also recommend writing functions so that they fit entirely on the screen at once (whenever practical, which is most of the time), using three lines for a one-line block is a bit excessive. It's a tradeoff and can be argued either way.

2) The line length limit of 120. I use a line length of 75 since the classic character width of text consoles and printer pages was 80 characters. This isn't as critical today, so it's a loose requirement, and is somewhat at odds with using good, meaningful variable names. But generally if you need to go beyond 75 characters a bit of thought will result in more readable code with things split up a bit. Again, it's a compromise that can be argued either way.

3) The absolutist nature of his stance. There are times when putting things all on one line add to the readability of the code. As a hastily contrived example, consider the following function that returns the value of a piecewise linear function (not necessarily continuous).

Code:
double y(double x)
{
   if (              (x <= -13) ) return ( 3 * x) + 4;
   if ( (-13 < x) && (x <= - 3) ) return (-5 * x) - 9;
   if ( (  3 < x) && (x <=   2) ) return (-7 * x) - 3;
   if ( (  2 < x) && (x <=   9) ) return ( 4 * x) + 7;
   if ( (  9 < x)               ) return ( 9 * x) - 1;

   return NaN; // Should never get here
}
This makes the function definitions closely match what we would expect to see if written out by hand using normal math conventions. It also makes debugging a lot easier. For instance, I made a mistake in the above code and when I previewed the code (which is the first time that I saw it using a fixed-pitch font) the error was glaringly obvious -- I decided to leave the error in place to make that point.

This function is written in a very compact form that, I think, significantly aids its readability over what this author mandates.

Code:
double y(double x)
{

   if (x <= -13)
   {
      return ( 3 * x) + 4;
   }

   if ( (-13 < x) && (x <= - 3) )
   {
      return (-5 * x) - 9;
   }

   if ( (  3 < x) && (x <=   2) )
   {
      return (-7 * x) - 3;
   }

   if ( (  2 < x) && (x <=   9) )
   {
      return ( 4 * x) + 7;
   }

   if ( 9 < x)
   {
      return ( 9 * x) - 1;
   }

   return NaN; // Should never get here
}
Note that the author actually recognizes this when he gives his recommendation for an if-elseif construct, which would be written in a much less readable way if it were treated for what it is, which is nested if-else statements. His indenting does not reflect the true level of block nesting, but it is much more readable.

His code if he followed his basic rules:

Code:
// if- else if - else conditional statements

if (Cond1 == val1)
{
   System.out.println("Cond1 is val1");
}
else
{
   if (Cond1 == val2)
   {
      System.out.println("Cond2 is val2");
   }
   else
   {
      System.out.println("No condition is satisfied");
   } 
}
But he clearly recognizes that this actually detracts from the logic behind this construct, and so he gives it a special form, which is quite appropriate. But he fails to recognize that there are many ad hoc logical constructs that would suffer similar loss in readability if an indentation style is adhered to too dogmatically.
 

Thread Starter

vead

Joined Nov 24, 2011
629
I saw some link and try to learn indent coding style. does following program look easy and readable
Code:
#include<reg51.h>    //header file

sbit   LED1     = P2^0;    //LED 1 conncted to port P2 pin 0
sbit   LED2     = P2^1;    //LED 2 conncted to port P2 pin 1
sbit   LED3     = P2^2;    //LED 3 conncted to port P2 pin 2

typedef enum
{
    task_1 =0,
    task_2,
    task_3
}
my_task_t;
my_task_t task = task_1;

// prototype function

DoSomething0();
DoSomething1();
DoSomething1();

int main()
{
    unsigned int task;

  switch  (task)
  {
        case task_1:
        DoSomething0();
        break;
    
        case task_2:
        DoSomething1();
        break;
    
        case task_3:
        DoSomething2();
        break;
    
        default:
        DoSomethingElse();
  }

}


     DoSomething0()
       {
            blink LED1;
       }
     DoSomething1()
       {
             blink LED2;
       }
     DoSomething2()
       {
             blink LED2;
       }
this is not original code. I posted this code just to show you that I am going on correct way or not, if I am on write track than I will try to make original program.
 
Last edited:

WBahn

Joined Mar 31, 2012
32,883
I saw some link and try to learn indent coding style. does following program look easy and readable
It's certainly an improvement over what you've been posting, but it still needs work.

Look at the following:

Code:
#include<reg51.h>    //header file

sbit   LED1     = P2^0;    //LED 1 conncted to port P2 pin 0
sbit   LED2     = P2^1;    //LED 2 conncted to port P2 pin 1
sbit   LED3     = P2^2;    //LED 3 conncted to port P2 pin 2

typedef enum
{
    task_1 =0,
    task_2,
    task_3
} my_task_t;

my_task_t task = task_1;

// prototype function

void DoSomething0(void);
void DoSomething1(void);
void DoSomething1(void);

int main(void)
{
   unsigned int task;

   switch  (task)
   {
      case task_1:
         DoSomething0();
         break;
   
      case task_2:
         DoSomething1();
         break;
   
      case task_3:
         DoSomething2();
         break;
   
      default:
         DoSomethingElse();
   }

}

void DoSomething0(void)
{
   blink LED1;
}

void DoSomething1(void)
{
   blink LED2;
}

void DoSomething2(void)
{
   blink LED2;
}
I've only cleaned up the indenting and declared function return types and argument lists to be void where needed. Using no return type or an empty argument list is strongly deprecated.

You still have issues.

First, you are declaring a global variable, task, to be of your enumeration type. First off, why it is global? Second, it doesn't really matter because it can't be seen from your main, where you declare a local variable, of type unsigned int, with the same name.

In your switch statement, you are using the local variable named task, which is not initialized.

Then you have two prototypes for DoSomething1() and no prototype for DoSomething2() or DoSomethingElse().

You also don't have a function definition for DoSomethingElse().

Then there is this "blink" that is used in the functions but it is never defined. What is it? It isn't a function.
 

Thread Starter

vead

Joined Nov 24, 2011
629
You still have issues.
n.
I am too long to write original program. I think I don't understand exact use of enum statement and switch statement in program. so first I am trying to understand use of those statement
enum statement
Code:
typedef enum
{
    task_1 =0,
    task_2,
    task_3
} my_task;
I have seen that type of code in c programming. In C, enumerations assign related names to a set of integer values. does it store constant value of integers?. such as task_1 = than task_2=2 than task_3=3.
switch statement
Code:
switch (task)
{
   case task_1:
    {
       Code to execute if <task> == task_1
      break;
    }
   case task_2:
    {
       Code to execute if <task> == task_2

      break;
    }
  
        case task_3:
    {
       Code to execute if <task> == task_3

      break;
    }
 

spinnaker

Joined Oct 29, 2009
7,830
I am too long to write original program. I think I don't understand exact use of enum statement and switch statement in program. so first I am trying to understand use of those statement
enum statement
Code:
typedef enum
{
    task_1 =0,
    task_2,
    task_3
} my_task;
I have seen that type of code in c programming. In C, enumerations assign related names to a set of integer values. does it store constant value of integers?. such as task_1 = than task_2=2 than task_3=3.
switch statement
Code:
switch (task)
{
   case task_1:
    {
       Code to execute if <task> == task_1
      break;
    }
   case task_2:
    {
       Code to execute if <task> == task_2

      break;
    }

        case task_3:
    {
       Code to execute if <task> == task_3

      break;
    }

You are making this enum thing WAY more complicated than it needs to be. enum is just a way to give something a name rather than a number so it is easier for humans to read.

So instead of 0,1,2 it is horse,chicken,pig. To the computer there is no difference.
 

Thread Starter

vead

Joined Nov 24, 2011
629
You are making this enum thing WAY more complicated than it needs to be. enum is just a way to give something a name rather than a number so it is easier for humans to read.

So instead of 0,1,2 it is horse,chicken,pig. To the computer there is no difference.
I don't want to make complicated but its happen. that's why I am trying to complete step by step. now how to use switch statement in main function. I tried to make in previous posts , but I am mistaking there, if all fine than I will do part of LED. I can write program for LED. so I am just trying to insert switch statement in main function.
 
Last edited:

spinnaker

Joined Oct 29, 2009
7,830
I don't want to make complicated but its happen. that's why I am trying to complete step by step. now how to use switch statement in main function. I tried to make in previous posts , but I am mistaking there, if all fine than I will do part of LED. I can write program for LED. so I am just trying to insert switch statement in main function.

You are not posting all of your code. Assuming you instantiated as a enumn then you are doing it correcly from what I see.
 

spinnaker

Joined Oct 29, 2009
7,830
This
typedef enum
{
task_1 =0,
task_2,
task_3
} my_task_t;


is not necessarily btw. task_1 should automatically be assigned a zero and it does not matter any way assuming you other code does not require it be a certain value.
 

MrChips

Joined Oct 2, 2009
34,829
Why are you making things more complicated than it needs to be?
enum has some usefulness but you do not have to use it.

Simply:

#define TASK1 1
#define TASK2 2
#define TASK3 3

will be just as effective
 

ErnieM

Joined Apr 24, 2011
8,415
Why are you making things more complicated than it needs to be?
enum has some usefulness but you do not have to use it.

Simply:

#define TASK1 1
#define TASK2 2
#define TASK3 3

will be just as effective
Complete disagreement here. Enumeration are there as a programming aid. They help you avoid mistakes.

What if as the work develops you find the need to add a new task between 2 and 3? Do you stick it at the end, or in between 2 and 3 as 2A? Do you call it 4 or do you change all the numbers?

It may seem trivial here with only 3 or 4 tasks, but what happens when Yu have dozens to track?

All these questions are side stepped when you use an enumeration. All the references get changed automatically.
 

WBahn

Joined Mar 31, 2012
32,883
While I agree that enumerations are quite useful and valuable, we need to consider that the TS is still struggling mightily with basic concepts such as variable declarations, proper indenting, for() statements, and switch() statements. There's a reason that no textbook or tutorial that I am aware of introduces enumerations until well after these and other basic concepts are well established. I think the TS is getting overwhelmed with trying to get a handle on too many concepts at once. He needs to step back and focus on building the basic foundation first.
 

spinnaker

Joined Oct 29, 2009
7,830
any way I want to become good in programming. so I will be focus on basic concept. I know I need to learn small things. should I create new thread to learn basics ?

We will answer specific question but no one here is going to teach you C programming through a thread. You need to do that by reading and then doing. If reading does not work for you ten take a class at you local school.

But mostly it takes patience. You need to go out and start writing programs. and you don't start by trying to write an autonomous drone program. Start by writing some code that blinks an LED.
 

ErnieM

Joined Apr 24, 2011
8,415
...I think there is more difference between C programming and Embedded C programming.
The basics of C programming are exactly the same for embedded work or any other larger system such as programming for Windows.

That is what C is, it a standard programming language.

There may be other differences, such as standard library routines like sprint() either don't work or are not very useful as there is no keyboard, mouse, or video display.
 

Thread Starter

vead

Joined Nov 24, 2011
629
You are not posting all of your code. Assuming you instantiated as a enumn then you are doing it correcly from what I see.
I understand my basic is not very good and I am trying to Improve . you all gave advice to learn basics. defiantly I will do ,but before I have taken one chance. I wrote this code If you are not happy with this program and you want to me go back I will follow your advice. please look at this code compiled with no errors
Code:
#include<reg51.h>    //header file

#define  LED1_ON   1
#define  LED1_OFF  0
#define  LED2_ON   1
#define  LED2_OFF  0
#define  LED3_ON   1
#define  LED4_OFF  0

sbit   LED1     = P2^0;    //LED 1 conncted to port P2 pin 0
sbit   LED2     = P2^1;    //LED 2 conncted to port P2 pin 1
sbit   LED3     = P2^2;    //LED 3 conncted to port P2 pin 2

typedef enum
{
    task1,
    task2,
    task3
};
// prototype function
void LED1_Blink(void);
void LED2_Blink(void);
void LED3_Blink(void);
int main(void)
{
   unsigned int task=0;

    while(1)
  {
   switch  (task)
   {
      case task1:
                 if (task == task1)
                 {
                    LED1 = LED1_ON;
                    LED1_Blink();
                    LED1 = LED1_OFF;
                 }
         break;

      case task2:
                 if (task == task2)
                 {
                      LED2 = LED2_ON;
                      LED2_Blink();
                      LED2 = LED2_OFF;
                 }
         break;

      case task3:
                 if (task == task3)
                 {
                    LED3 = LED3_ON;
                    LED3_Blink();
                    LED3 = LED4_OFF;
                 }
         break; 
   }
}
}

void LED1_Blink ()
{
    unsigned int i;
    for (i = 0; i < 1000; i++);
}

void LED2_Blink()
{
    unsigned int j;
    for (j = 0; j < 1500; j++);
}

void LED3_Blink()
{
   unsigned int k;
    for (k = 0; k < 1200; k++);
}
 
Last edited:

WBahn

Joined Mar 31, 2012
32,883
I apologies and agree I have downloaded books also I am reading books and materials. most of example I see in c programming. I think there is more difference between C programming and Embedded C programming.
There's some difference, but it is mostly in the non-standard libraries that support the microcontroller you are using. But this can be hard to see clearly if you are trying to absorb everything all at once.

any way I want to become good in programming. so I will be focus on basic concept. I know I need to learn small things. should I create new thread to learn basics ?
No. You can't learn programming basics via a forum thread. You learn programming by writing lots of programs that progressively move from the ultra simple "Hello World!" (or, blinking LED in the embedded world) to more sophisticated programs incrementally. In your case I'd recommend divorcing this process from the embedded issues for a while -- at least until you get the basic control structures under your -- and just write programs that run in a console window on a PC or Mac or whatever. Start with Hello World. Then write a program that uses a for loop to print the numbers 1 through 10. Then have it print them in reverse order. Then use in if() statement to print the negative of any number that is greater than five. Then change the if() statement only print the numbers that are divisible by three. Then use a switch() statement to print only the prime numbers. Keep adding just a bit of complexity at a time, making sure that you are completely comfortable with everything you have done up to that point. There are lots and lots of online tutorials out there to guide you through this process.
 
Last edited:

WBahn

Joined Mar 31, 2012
32,883
I understand my basic is not very good and I am trying to Improve . you all gave advice to learn basics. defiantly I will do ,but before I have taken one chance. I wrote this code If you are not happy with this program and you want to me go back I will follow your advice. please look at this code compiled with no errors
The code still shows that you don't understand what a switch statement is or the basics of how to use it. Can the value of the variable 'task' every change? What do the if() statements do within the switch() statement? Learn the basics!
 
Top