Hi,

I am trying to print the values in enum. Here is the code:

#include<stdio.h>

int main(int argc , char* argv[])
{
char played[] = "This old man, he played ";
int i=0;
enum days{one,two,three,four,five,six,seven,eight,nine,ten};
 enum days d;
for(i=0;i<10;i++)
	{
printf("%s %s\n ",played,d);
	}
return 0;
}

Can Anyone help how to print the values.
I want to print like:
This old man,he played one
This old man,he played two
This old man,he played three
This old man,he played four

and so on.

Recommended Answers

All 3 Replies

Enumerations constants are symbolic constants representing integral values. If you want the string representation of the symbol, you must create it manually:

#include<stdio.h>

int main(void)
{
    enum days { 
        one, two, three, four, five,
        six, seven, eight, nine, ten 
    };
    const char *days_str[] = {
        "one", "two", "three", "four", "five",
        "six", "seven", "eight", "nine", "ten"
    };
    int i;
    
    for (i = one; i <= ten; i++)
        printf("This old man, he played %s\n", days_str[i]);
    
    return 0;
}

One issue with this particular approach is that enumerations need not be consecutive values:

enum days { 
    one = 23, two = 11, three = 6, four = 2, five = 199,
    six = 42, seven = 0, eight /* = 1 */, nine /* = 2 */, ten = 17
};

But presumably if you know that, you won't try to do something stupid with a parallel array of strings. ;)

Hi,

Thanks for the reply.I tried doing something like this:

#include<stdio.h>
#include<string.h>


int main()
{
    char common1[] ="Knick-knack paddywhack, give your dog a bone";
    char com[] ="This old man came rolling home";
    char played[] = "This old man, he played ";
    char ply[50];
    strcpy(ply,played);

    int i=0;

     char *days[10]={"one","two","three","four","five","six","seven","eight","nine","ten"};
    printf("************** %s \n \n",com);
    for(i=0;i<10;i++)
        {   
    printf("%s\n ",strcat(played,days[i]));
    printf("%s\n ",common1);
    fflush(stdout);
    printf("%s \n \n",com);
    strcpy(played,ply);

        }
return 0;


}

But I dont know why I am not getting correct values for String "Com". It is giving garbage value.

It prints like this:

This old man, he played one
 Knick-knack paddywhack, give your dog a bone
 ne

This old man, he played two
 Knick-knack paddywhack, give your dog a bone
 wo

This old man, he played three
 Knick-knack paddywhack, give your dog a bone
 hree

This old man, he played four
 Knick-knack paddywhack, give your dog a bone
 our

Am I doing something wrong??

Thanks.

You're playing dangerous games with strcpy(), and it's manifesting as string corruption. Why not simply do this?

#include <stdio.h>
#include <string.h>

int main()
{
    char common1[] ="Knick-knack paddywhack, give your dog a bone";
    char com[] ="This old man came rolling home";
    char played[] = "This old man, he played ";
    int i=0;

    char *days[10]= {"one","two","three","four","five","six","seven","eight","nine","ten"};
    
    printf("************** %s\n\n",com);
    
    for(i=0; i<10; i++) {
        printf("%s%s\n", played, days[i]);
        printf("%s\n",common1);
        printf("%s\n\n",com);
    }
    
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.