ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Computer Keyboard Piano in C Programming Language

Updated on July 14, 2018
rajkishor09 profile image

Raj is an ardent coder who loves exploring new technology. He is an IT pro with 9 years of exp in C#, Angular, React, Vue.

To accomplish this task you don’t need to buy any expensive software or hardware, only a little knowledge of C program will do and you can build your own musical keyboard software. Before we begin you need to digest some basic concept and logic we are going to use in this program. If you are one of them who spent money to learn basic of computer or any programming language then you must have heard about ASCII (American Standard Code for Information Interchange) characters or codes. We are going to use ASCII codes here. This sounds a bit difficult but it is not so.

Logic is that every alphabets on our PC keyboard has some ASCII value like capital A has ASCII value 66, B is equal to 67 and so on. So we will use these values as a frequency range like original musical keyboard has.

The C inbuilt “sound()” function will do this task for us. This “sound()” function instead of using external speaker uses internal speaker located in CPU cabinet. This internal speaker makes beep sound when we boot our PC.

Keyboard Piano Program


void main()
char ch='y';
printf("Press X to exit......");
while(ch !='X')
ch = getch();


Line 1-3: Very first three lines of source code include header files, among three header files “dos.h” header file has vital function which will do our task.

Line-4: Forth line is “void main()” which is the main function of our program and program execution will begin from here.

Line-5: Fifth line is opening brace of main program.

Line-6: In sixth line a character variable has been declared and initialized with character ‘y’.

Line-7: Next line clears the any previous output from output screen.

Line-8: The eighth line prints a line informing user that if he needs to close this program then he/she should press Capital X.

Line-9: Next line is a while loop which keeps looping four lines in it until we press X.

Line-10: Beginning of while loop.

Line-11: In this line of code we store the ASCII value of key pressed by user during runtime. For example if user presses ‘p’ key on keyboard then its corresponding ASCII value, 113 gets store in ‘ch’ variable.

Line-12: The “sound()” will produce sound from pc speaker and in bracket we need to set frequency at which it will buzz. Here we are multiplying 10 with ASCII value available in ch variable. You can change the constant ‘10’ in this line to different integer value and as a result it will change the sound output. Do experiment only with this value.

Line-13: Here “delay()” function determines how long the sound will last (Note: actually “ delay()” function suspends the execution of program). This is determined by value provided in bracket. In our example I have set 75 milliseconds. If you change it to 3000 then sound will be played for 3 seconds.

Line-14: The “nosound()” function works opposite of “sound()” function. It turns off PC speaker. If we won’t use this in our program then speaker will keep buzzing until we close program.

Line-15: End of while loop.

Line-16: End of “main()” function.

You can download 100% working code from Here.




    0 of 8192 characters used
    Post Comment
    • profile image


      5 years ago

      superb... i like it very much.. can u post more similar prgms. it might be useful for us !!

    • profile image

      Manu rajput from bijnor 

      6 years ago

      It is really awesome code I was inventing for this type of code.... I was very happy after knowing this...

    • profile image

      vinay tiwari 

      7 years ago

      yes...its very useful for me...

    • rajkishor09 profile imageAUTHOR


      8 years ago from Bangalore, Karnataka, INDIA

      I wrote my program using Turbo C on windows XP. You have check which compiler you are using and that compiler have all required header file.

    • profile image


      8 years ago

      would you plz tell me how to run in ubuntu (linux) in thisdos header file not working also i got errors like sound.c:(.text+0x1e): undefined reference to `getch' plz resole my problem

    • Java Programs profile image

      Java Programs 

      8 years ago from India

      Great hub in C programming language .... Nice explanation of the program in line by line way .... I like the approach ..... keep up the good work ....

    • profile image


      8 years ago

      i will definetly try this keep it up friendz all the best...............

    • profile image

      Divyanshu Dhruw 

      8 years ago

      It's fabolous man it's incredible. . . i can't believe that this simple small c program can make a musical instrument

    • rajkishor09 profile imageAUTHOR


      9 years ago from Bangalore, Karnataka, INDIA

      @Unknown : if program complied successfully and didn't produce any sound then i doubt there is no internal speaker (which beeps when PC starts). I am not talking about external sound speaker you used with media player.

    • profile image

      Preston Champion 

      9 years ago

      In 1876 a man by the name of Elisha Gray invented a musical instrument called the "musical telegraph" which was the precursor to all modern electronic musical keyboards. It was, essentially, the first electronic synthesizer, and it created sounds throught the electromagnetic oscillation of steel reeds and transmitted these sounds over a telephone line.

    • profile image


      9 years ago

      its great

    • profile image


      9 years ago

      really its very gud material..u done a great job sir....

      keep it up..its very useful for us

    • profile image


      10 years ago

      Can someone teach me how to search for a file in a folder using find first() and find next() i wrote a program on my own but it finds only for the files inside the current directory. But its not searching in other drives or directories. Please help me if you know this stuff. Thanks in advance.

    • profile image


      11 years ago

      not so bad... can u pls postsometing related to hacking..

    • profile image


      11 years ago

      did you don't get any prototype errors in this ,

      next error return statement missing.

      your program i have not tested yet , but i always get this errors ,

      & i always fail how to solve this error.

      & program is not working on vista.

    • sophieqd profile image


      11 years ago

      Turn Your PC Keyboard to Musical Keyboard

      Great info I really will think about this. Thanks for sharing.

    • rajkishor09 profile imageAUTHOR


      11 years ago from Bangalore, Karnataka, INDIA

      First of all thanks for appreciation anjalichugh. Actually I want instigate new C programmer psyche that they can do much more thing than doing regular programming. I think this kind of example encourages and spice up their learning process.

    • anjalichugh profile image


      11 years ago from New York

      Although it sounded like 'Greek' to me, yet I spent some time in learning these tips. It seemed so interesting. Great work...and welcome to Hub Pages.


    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at:

    Show Details
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the or domains, for performance and efficiency reasons. (Privacy Policy)
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
    ClickscoThis is a data management platform studying reader behavior (Privacy Policy)