I am doing an exercise which has to do with International country codes.The user must give a code and the programm will display the corresponding country.
Code:
#include <stdio.h>
#define COUNTRY_COUNT \
((int) (sizeof(country_codes) / sizeof(country_codes[0])))
struct dialing_code {
char *country;
int code;
};
const struct dialing_code country_codes[] =
{{"Argentina", 54}, {"Bangladesh", 880},
{"Brazil", 55}, {"Burma (Myanmar)", 95},
{"China", 86}, {"Colombia", 57},
{"Congo, Dem. Rep. of", 243}, {"Egypt", 20},
{"Ethiopia", 251}, {"France", 33},
{"Germany", 49}, {"India", 91},
{"Indonesia", 62}, {"Iran", 98},
{"Italy", 39}, {"Japan", 81},
{"Mexico", 52}, {"Nigeria", 234},
{"Pakistan", 92}, {"Philippines", 63},
{"Poland", 48}, {"Russia", 7},
{"South Africa", 27}, {"South Korea", 82},
{"Spain", 34}, {"Sudan", 249},
{"Thailand", 66}, {"Turkey", 90},
{"Ukraine", 380}, {"United Kingdom", 44},
{"United States", 1}, {"Vietnam", 84},
{"Venezuela", 58}, {"Greece", 30},
{"Ireland" , 353}, };
int main(void)
{
int code, i;
printf("Enter dialing code: ");
scanf("%d", &code);
for (i = 0; i < COUNTRY_COUNT; i++)
if (code == country_codes[i].code) {
printf("The country with dialing code %d is %s\n",
code, country_codes[i].country);
return 0;
}
printf("No corresponding country found\n");
return 0;
}
This solution comes from : Answers to Selected Exercises in Chapter 16 of C Programming: A Modern Approach - Second Edition
1. What is the significance of int cast? I mean i know what it does ... but I tried to put many countries and the result of sizeof(country_codes)/sizeof(country_codes[0]) was always an integer not a double or something that can make us to think about an int casting.
2. Hence the macro is not parameterized the last pair of extra braces it comes due to int cast? and in my implementation I didn't put the array of structures as global. Here is my implementation of this exercise :
Code:
#include <stdio.h>
#include <stdbool.h>
#define LEN 33
struct dialing_code {
char *country ;
int code;
} ;
int main(void)
{
const struct dialing_code country_codes[LEN] =
{ {"Argentina" , 54} , {"Bangladesh" , 880} ,
{"Brasil" , 55} , {"Burma (Myanmar)" , 95} ,
{"China" , 86} , {"Colombia" , 57} ,
{"Congo" , 243} , {"Egypt" , 20} ,
{"Egypt" , 20} , {"Ethiopia" , 251} ,
{"France" , 33} , {"Germany" , 49} ,
{"India" , 91} , {"Indonesia" , 62} ,
{"Iran" , 98} , {"Italy" , 39} ,
{"Japan" , 81} , {"Mexico" , 52} ,
{"Nigeria" , 234} , {"Pakistan" , 92} ,
{"Philippines" , 63} , {"Poland" , 48} ,
{"Russia" , 7} , {"South Africa" , 27} ,
{"South Korea" , 82} , {"Spain" , 34} ,
{"Sudan" , 249} , {"Thailand" , 66} ,
{"Turkey" , 90} , {"Ukraine" , 380} ,
{"United Kingdom" , 44} , {"United States" , 1 } ,
{"Vietnam" , 84} };
int choice , i;
bool sentinel = false;
for(i=0; i<LEN; i++)
printf(" %s: %d \n" , country_codes[i].country , country_codes[i].code);
for(;;) {
putchar('\n');
printf("Give the code (press 0 to terminate): ");
scanf("%d" , &choice);
if( !choice )
return 0;
for(i=0; i<LEN; i++){
if( choice == country_codes[i].code ){
printf("The country is: %s " , country_codes[i].country);
sentinel = true;
break;
}
sentinel = false;
}
if( !sentinel )
printf(" There is no such a country\t");
}
return 0;
}
Thank you in advance.