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.

Dani AI

Generated

The root cause of the weird output in 's run is buffer overflow from modifying played in place with strcat. played was sized exactly to hold the literal, so appending writes past its end and corrupts neighbouring data (for example, the com string). was right to suggest mapping enum values to text, and that mapping must be guarded if enum values are not a contiguous 0..N range.

Safer patterns:

  • Build each output line into a properly sized buffer with snprintf (it truncates safely and reports needed size).
  • Or map an enum to a string with a switch and never assume consecutive integer values.

Example: compose into a safe buffer with snprintf.

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

int main(void) {
    const char played[] = "This old man, he played ";
    const char *day = "three"; /* from mapping */
    char out[80];

    /* snprintf returns needed size; compare to buffer to detect truncation */
    int needed = snprintf(out, sizeof out, "%s%s", played, day);
    if (needed >= (int)sizeof out) {
        /* handle overflow: enlarge buffer, skip, or log an error */
    }
    puts(out);
    return 0;
}

Example: robust enum-to-string mapping when values might be sparse.

const char *day_to_string(enum days d) {
    switch (d) {
    case one:   return "one";
    case two:   return "two";
    case three: return "three";
    /* ... */
    default:    return "unknown";
    }
}

Quick troubleshooting checklist:

  • Do not call strcat on a buffer with no spare room; prefer snprintf or strncat with careful length checks.
  • Use sizeof and strlen to compute required sizes: needed = strlen(base) + strlen(suffix) + 1.
  • If enum values are not contiguous, use a switch (or a lookup table plus bounds check).
  • Use const char * for string literals; avoid writing into them.

These points expand on 's mapping idea and explain the corruption you saw in 's code.

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.