In our previous lesson, we very brieflydescribed how programs get stored incomputers memoryand how they get executed and then we drewanalogy between learning a naturallanguage like Englishand a programming language like C. Now inthis lesson, we are going to have somereal action. We’re going to the right ourfirstprogram in C. it’s a tradition that wheneveryou learn a new programming languagethe first program that you’re right inthat language is toprint hello world on-screen. So let’swrite this programthe first thing that we will need is aneditor in which we should be able towriteour program. What I’ll do is I will notuse any special editorI’m on Windows machine. So I’ll just open notepad and if you are on some other systemslike Linux or Macyou can open an editor of your ownchoice and now I can start writing myprogramthe first line that I’ll write in myC programwill be this pound sign and then includeand then thisless than sign and then STDIOdot H and then you have a greaterthan sign sostdio.

h  is within angularbracketthings are case sensitive here, so makesure you write everything in smallletterslower case letters. This line is sayingthatI want to use the functionalities ofa C librarythat we call standard input and outputlibraryor just studio.h. I will comeback to explaining what a library isI’ll just move on for now. The next linethat I’ll writeis int, then space and then I’ll write meanand then I’ll open up a parenthesis andclose a parenthesisand then I’ll go to the next line andand typean opening curly brace and then on thenext line I’ll drawI’ll type a closing curly brace. Wewill write this much of code in allour programs. This is basic skeletonand now I’ll quickly explain what wehave really done herea ‘C’ program is organized in entitiesthat we call  functions. Youmust beknowing of functions in mathematicsLet’s take example of a mathematicalfunction F(x) = x^2 is my function.

We Will Write a Custom Essay Specifically
For You For Only $13.90/page!

order now

You canimagine a functionto be a box that will take somethinginside and give back something elseit will give something else applying somerules. To this function F(x) = x^2if we will give thisfunction 2, it will giveback four. if we will give five it willgive back25. it will give back square of thenumberYou can think of a function inprogramming something like thisYou can give a nice name to your functionand then you can definewhat all the function will accept insideitwhatever you give to a function in C iscalledargument and you can have more than oneargumentyou can have arguments and whateveryou get back from the function is called returnIn our program whatever we havewritten so farthese lines after hash include studio.hare basic structure of a function.

Firstline of a function contains definitionof the functionthe first part in definition will tellyouwhat the function will return. int heremeans that the function will returninteger, an integer. Before thestart of parentheseswhat we have is name of the functionso this function has name main and rightnow there is nothing inside theparenthesisbut inside the parenthesis, you definewhat all the function willaccept inside it, what all the functionwith accept asarguments.

A function may notneed arguments and a function sometimesmay not return anythingafter the definition of the function, wemust-have anopening curly brace and then we canhave somestatements to perform some taskwe can have as many statements as wewant and after all the statements wemust havea closing curly brace. Closing curly brace will mark the end of functionwhatever is in between the opening braceand the closing braceis what we can call the body of thefunction. Statements are executedsequentiallya function in a program is basically anentitythat can perform a specific sub-taskin a C program you must always have afunction named mainthe execution of your ‘C’ program alwaysstarts with firststatement in the main function.

You canwrite a function of your ownbut there are some pre-written and pre-implemented functionsin C to perform some important tasksand these functions are organized inwhat we call librariesthis first line #include is basically saying thatI’m goingto use some functionsthat are defined in stdio.h librarywhich we also call standard input andoutput librarythis library contains a function namedprintf that will print something.

Now I’mwriting a statementthat will mean making a call to printffunction. To call a function you firstneed to write the name of the functionthen within parentheses, you must givearguments, if any. To printf you cangiveargument like this. Within double quoteswrite whatever you want to print. I wantto print “Hello World”Every statement in ‘C’ must terminatewith a semicolonits a rule, so we have put a semicolonhere.

Thre is one last statement that weneed to write and we will be donemain is defined to return integer. Toreturn something from a function youuse the keywordreturn and then after a space youwritea constant or variable name that willcontain the value to be returned.At the end of your main function, youmust write the statementreturn 0; For any other function that isdefinedto return integer, if you would not havean explicit return statement like thisyou will error, but for main functioneven if you will skip writing thisreturn 0your program will work. it is goodto write this for the sake of consistencyokay, so this is all we need to print “Hello World”Now, what I’m gonna do isI’ll save this file. In the Save dialogselect filetype as “All Files” and then give afilenamesomething DOT C, I’ll give file name HelloWorld.

Cso I just created a ‘C’ source file witha program to print hello worldand now we need to compile and run thisprogram. To compilewe will need a compiler. I’m going touse a compiler named GCC which isa standard compiler for ‘C’. On a Linuxmachine you do not need to install gccit’s already there. On Windows, you willhave to install gccyou can check the description of thisvideo for a link on how to installand configure gcc  forWindows. I am on aWindows machine and gcc is configuredfor meto compile using gcc, I first need toopen command prompt in Windowspress windows plus ‘R’ key to open thisrun dialogue andtype in cmd and then click OK, commandprompt window open for youType in gcc and press enter if you get amessage like gcc no input files, then gcc isconfigured for youif you get a message like command notrecognizedgcc is not correctly configured. Now youmust be remembering, in which directoryyou had saved your source fineFor me it’s in MyCode directory. In ‘C’drive, there is this file “Helloworld.

C” in command prompt I first need to go tothis directory,so’ll say CD for change directory and typein the pathnow I’m in my code directory and now Itype in gccand the name of the C file for me, its”HelloWorld.C”You can type this same command on ashell in Linuxif you hit ENTER and no message comes,your program has compiled and anexecutable has been generatedBy default, an executable named ‘a’ isgeneratedthis is  a.exe on Windows.

On Linux, a file named a.out will be generatedin Windows, you can double-click and runthis fightbut if you will double click something willflash and go awaythis is because programs starts executionand before you can see the output itfinishes.For your machine, running a simple printstatement is a task of millisecondsif you want to see the output, you canrun the executable fromcommand prompt itself.  Typein a.

exehereon Linux, you can run a.out and when you’ll press Enteryou have the output here. if you are ableto do this muchCongratulations, you ran your firstprogram in ‘C’ :)Please note that there are a lot ofcompilers that give youvery nice UI in which you will have aneditor andwithin the UI itself, you will be able tocompile and run your programand there will be a lot of cool featuresto detect errors in your programWe call these compilers integrateddevelopment environments orIDEs.

  We will show you howto work withsome of the IDEsin our next lesson. This is it for thislesson. 


I'm Erica!

Would you like to get a custom essay? How about receiving a customized one?

Check it out