Reputation: 11
When the code is compiled, it doesn't show the values inserted to the enum variable in structure
This code only shows the keyboard input 0 or 1
#include<stdio.h>
#define Max_CHARS_NAME 100
struct gps_point{
double latitude;
enum latitude_pole {North,South} pole;
double longitude;
enum longitude {East,West} dire;
char location_name[Max_CHARS_NAME];
}g;
int main(){
char arr[Max_CHARS_NAME];
int i = 0;
printf("Enter location\n");
scanf("%s", &g.location_name);
strcpy(arr,g.location_name);
printf("Enter latitude\n");
scanf("%lf", &g.latitude);
printf("Enter latitude pole North - 0, South - 1\n");
scanf("%d",&g.pole);
//g.latitude_pole = g.pole;
printf("Enter longitude dire East - 0, West - 1\n");
scanf("%d", &g.dire);
printf("Enter longitude\n");
scanf("%s", &g.longitude);
printf("%s is situated at (Latitude : %s %lf , Longitude: %s %lf). \n",arr,g.pole,g.latitude,g.dire,g.longitude);
return 0;
}
Upvotes: 1
Views: 162
Reputation: 685
See the below example,
enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
int main()
{
enum week day;
day = Wed;
printf("%d",day);
return 0;
}
Here we declare a variable day of type week and assign one(numerical) value in the list to the variable using a "string" but it isn't possible to do the other way.
Upvotes: 4
Reputation: 15813
This is equivalent to
enum latitude_pole {
North = 0,
South = 1
} pole;
This defines the variable pole
whose possible values are North(0) and South(1).
It is equivalent to writing
enum latitude_pole {
North = 0,
South
} pole;
because in case you initialize one member the next one will be +1
(previous member).
In your code the missing of initialization will have it insert by default the value 0 for the first member and 1 for the second member.
Upvotes: 1
Reputation: 215245
If you don't give the enumeration constants any values, the compiler will do it silently.
The first one will always be given value 0
, and every following enumeration constant will get the value of previous constant + 1.
In your case {North,South}
, North
is guaranteed to be 0 and South
guaranteed to be 1. The type of these enumeration constants is guaranteed to be 100% compatible with int
.
Had you typed only some values explicitly, the rule where every constant getting the value of the previous + 1 is still applicable. So if you would do {North,South,East=5,West}
, they would get the values 0, 1, 5, 6
.
Upvotes: 0