C Program(Month Name) to demonstrate use array of string

C Program(Month Name) to demonstrate use array of string                       



 

Code: 

#include<stdio.h>
int main()
{
 char month[12][10]={"January","February","March","April","May","June",
 "July","August","September","October","November","December"};
 int m;
 printf("Enter a Month Number:");
 scanf("%d",&m);
 printf("The month name=%s",month[m-1]);
 
    return 0;
}


 

Sample Input and Output:

Enter a Month Number:5
The month name=May


In this article we will see how we can use array of string to print month name based on month number passed.So Let's start

In the first line of code we have included our most important header file i.e. stdio.h which help us to use some of the most important inbuilt function like printf() and scanf() which we use for I/O Operation,We can say that stdio header file is hardcore of c programming language.whose who don't the full form of stdio.h then I would like to tell stdio stands for standard input output.

In the next line of code we have defined main function,main function is a function from which the execution of c program is started.C program can have n number of user defined function but only one main function,if we try to define multiple main function then the compiler will show us the error that c program can contain only one main function.

In the next line we have declared one multidimensional array of type char which we will use to store month name.The reason behind declaring multi-dimensional array is first array is used to store total no.of element we can store in array like January-December(12) and second array is used to defined the maximum size of particular month name for eg January has 7 length March has 5 length etc.So we can say that in above array we cannot initialize more than 12 month and each month name should be less than of 10 character.

In the next line we declared one more variable m of type int(integer) which we will use to store month number of which we have to find month name.So for that we have printed one statement in the next line 'enter a Month number'.

In the next line we have used scanf() function to take user input and used %d format specifier because we are storing input value in int(Integer) type variable i.e. m.

In the next line of code we are printing the month using some logic Let's understand it.

printf("The month name=%s",month[m-1]);

In above print statement 'The month name' will be printed as it is and after that we are printing month[m-1] here we are printing the element of month array with index of m-1,the value of m is coming from user so user will get month name based on the number provided by them.Most of you must be thinking that why we are using month[m-1] and not month[m] because the index of array starts from 0 to n-1 so if want 6 month then the month[5] will give that month.

If you any have doubt,suggestion regarding this post or website then feel free to share with us.

If you have learned something new from this post then share this post with your family and

friends.

 

Happy Learning :)😎  

 

 

Previous Next

Post a Comment

Previous Post Next Post