โค้ดโปรแกรมภาษาซี รับค่าตัวเลข 1-12 จากนั้นทำการแปลงตัวเลขเป็นชื่อเดือน January – December
การทำงานของโปรแกรม
รับตัวเลขที่กรอกเข้ามาเป็นชนิด int แล้วเก็บค่าไว้ในตัวแปร input จากนั้นนำค่าที่ได้ เป็น index (ตำแหน่งใน *month[]) โดยจะต้องลบด้วย 1(หนึ่ง) เนื่องจาก array ของ month เริ่มต้น index ที่ 0 (ศูนย์)
ตัวอย่างโค้ด
/***************************************************
* Author : CS Developers
* Author URI: https://www.comscidev.com
* Facebook : https://www.facebook.com/CSDevelopers
***************************************************/
#include<stdio.h>
int main()
{
char *month[] = {"January", "February", "March", "April", "May", "June","July",
"August", "September", "October", "November", "December"};
int input;
printf("\n Enter number 1-12 of Month : ");
scanf("%d", &input);
printf("\n Month name is %s \n\n", month[input-1]);
return 0;
}
ผลลัพธ์
