Writing and executing your first program: C Programming Tutorial 03


In our previous lesson, we very briefly described how programs get stored in
computers memory and how they get executed and then we drew analogy between learning a natural
language like English and a programming language like C. Now in
this lesson, we are going to have some real action. We’re going to the right our
first program in C. it’s a tradition that whenever
you learn a new programming language the first program that you’re right in
that language is to print hello world on-screen. So let’s
write this program the first thing that we will need is an
editor in which we should be able to write our program. What I’ll do is I will not
use any special editor I’m on Windows machine. So I’ll just open notepad and if you are on some other systems
like Linux or Mac you can open an editor of your own
choice and now I can start writing my program the first line that I’ll write in my
C program will be this pound sign and then include
and then this less than sign and then STDIO dot H and then you have a greater
than sign so stdio.h is within angular bracket things are case sensitive here, so make
sure you write everything in small letters lower case letters. This line is saying
that I want to use the functionalities of
a C library that we call standard input and output
library or just studio.h. I will come
back to explaining what a library is I’ll just move on for now. The next line
that I’ll write is int, then space and then I’ll write mean and then I’ll open up a parenthesis and
close a parenthesis and then I’ll go to the next line and
and type an opening curly brace and then on the
next line I’ll draw I’ll type a closing curly brace. We
will write this much of code in all our programs. This is basic skeleton and now I’ll quickly explain what we
have really done here a ‘C’ program is organized in entities that we call functions. You must be
knowing of functions in mathematics Let’s take example of a mathematical
function F(x)=x^2 is my function. You can
imagine a function to be a box that will take something inside and give back something else it will give something else applying some
rules. To this function F(x)=x^2 if we will give this
function 2, it will give back four. if we will give five it will
give back 25. it will give back square of the
number You can think of a function in
programming something like this You can give a nice name to your function
and then you can define what all the function will accept inside
it whatever you give to a function in C is
called argument and you can have more than one
argument you can have arguments and whatever you get back from the function is called return In our program whatever we have
written so far these lines after hash include studio.h are basic structure of a function. First
line of a function contains definition of the function the first part in definition will tell
you what the function will return. int here
means that the function will return integer, an integer. Before the
start of parentheses what we have is name of the function so this function has name main and right
now there is nothing inside the parenthesis but inside the parenthesis, you define
what all the function will accept inside it, what all the function
with accept as arguments. A function may not need arguments and a function sometimes
may not return anything after the definition of the function, we
must-have an opening curly brace and then we can
have some statements to perform some task we can have as many statements as we
want and after all the statements we must have a closing curly brace. Closing curly brace will mark the end of function whatever is in between the opening brace
and the closing brace is what we can call the body of the
function. Statements are executed sequentially a function in a program is basically an
entity that can perform a specific sub-task in a C program you must always have a
function named main the execution of your ‘C’ program always
starts with first statement in the main function. You can
write a function of your own but there are some pre-written and pre-
implemented functions in C to perform some important tasks and these functions are organized in
what we call libraries this first line #include is basically saying that I’m going
to use some functions that are defined in stdio.h
library which we also call standard input and
output library this library contains a function named
printf that will print something. Now I’m
writing a statement that will mean making a call to printf
function. To call a function you first need to write the name of the function
then within parentheses, you must give arguments, if any. To printf you can
give argument like this. Within double quotes write whatever you want to print. I want
to print “Hello World” Every statement in ‘C’ must terminate
with a semicolon its a rule, so we have put a semicolon
here. Thre is one last statement that we need to write and we will be done main is defined to return integer. To
return something from a function you use the keyword return and then after a space you
write a constant or variable name that will
contain the value to be returned. At the end of your main function, you
must write the statement return 0; For any other function that is
defined to return integer, if you would not have
an explicit return statement like this you will error, but for main function
even if you will skip writing this return 0 your program will work. it is good
to write this for the sake of consistency okay, so this is all we need to print “Hello World”
Now, what I’m gonna do is I’ll save this file. In the Save dialog
select file type as “All Files” and then give a
filename something DOT C, I’ll give file name HelloWorld.C so I just created a ‘C’ source file with
a program to print hello world and now we need to compile and run this
program. To compile we will need a compiler. I’m going to
use a compiler named GCC which is a standard compiler for ‘C’. On a Linux
machine you do not need to install gcc it’s already there. On Windows, you will
have to install gcc you can check the description of this
video for a link on how to install and configure gcc for Windows. I am on a
Windows machine and gcc is configured for me to compile using gcc, I first need to
open command prompt in Windows press windows plus ‘R’ key to open this
run dialogue and type in cmd and then click OK, command
prompt window open for you Type in gcc and press enter if you get a
message like gcc no input files, then gcc is configured for you if you get a message like command not
recognized gcc is not correctly configured. Now you
must be remembering, in which directory you had saved your source fine For me it’s in MyCode directory. In ‘C’
drive, there is this file “Helloworld.C” in command prompt I first need to go to
this directory,so ‘ll say CD for change directory and type
in the path now I’m in my code directory and now I
type in gcc and the name of the C file for me, its
“HelloWorld.C” You can type this same command on a
shell in Linux if you hit ENTER and no message comes,
your program has compiled and an executable has been generated By default, an executable named ‘a’ is
generated this is a.exe on Windows. On Linux, a file named a.out will be generated in Windows, you can double-click and run
this fight but if you will double click something will
flash and go away this is because programs starts execution and before you can see the output it
finishes. For your machine, running a simple print
statement is a task of milliseconds if you want to see the output, you can
run the executable from command prompt itself. Type in a.exe
here on Linux, you can run a.out and when you’ll
press Enter you have the output here. if you are able
to do this much Congratulations, you ran your first
program in ‘C’ 🙂 Please note that there are a lot of
compilers that give you very nice UI in which you will have an
editor and within the UI itself, you will be able to
compile and run your program and there will be a lot of cool features
to detect errors in your program We call these compilers integrated
development environments or IDEs. We will show you how to work with
some of the IDEs in our next lesson. This is it for this
lesson. Thanks for watching !

59 Replies to “Writing and executing your first program: C Programming Tutorial 03”

  1. Why did you go to such basic things from what you were foing before?..aren't you supposed to go to more complex things?…there are tons of beginner c tutorials out there…yours was a little more advanced…why did you stop that?..anyone can explain the basics but you know how to explain more important staff..

  2. Hi Sebinechita,
    We are not going to stop publishing tutorials on advanced stuff. Its just that, we want to create a completeness in experience. So, trying to put some basic stuff as well. Anyway, feedback taken. 🙂 We will try to push more on advanced stuff.

  3. i have a problem, please help me. I did everything but 'a' flie wouldn't create
    this is what i get
    c:usersRondell>gcc
    gcc: fatal error: no input files
    compilation terminated.

    c:usersRondell>cd c:my code

    c:my code>gcc HelloWorld.C
    HelloWorld.c:In function 'int main()' :
    HelloWorld.C:6:10: error: '0' was not declared in this scope

  4. your tutorial is awesome……. really i've gotten deep knowledge in this ..
    where have you posted other tutorials next to 3 ,

  5. when I press "enter" after writing "gcc (the name of my program)" i get an error message. may this be because the program has an error?

  6. I personally think the Tenacious C IDE is the best C programming environment for beginning C programmers. That being said, good video, well thought out. 

  7. when i'm trying to compile my program it shows as "The program can't start because zlib1.dll is missing from your computer.Try reinstalling the program to fix the problem."
    i tired 3 or 4 times but it always shoes like that.please Tell me how to install gcc correctly for windows7

  8. my program doesn't flash when I doble click it instead, the cursor just keep charging indefinitely like waiting for something to happens. And the same happen on cmd, it doesn't print anything the cursor just keep blinking indefinitely and nothing happens. I'm pretty sure there are no mistakes on the program what else can be? help! 🙁

  9. sir! return 0; is use to c++, in c language we are use to getch() and getche()
    plz sir tell me, if you know that different them.

  10. hello sir,after typing GCC in cmd I'm getting GCC:fatal error: no input files so can u help me in finding the problem and correcting it

  11. would the print function not work without the main function?
    what was the need for it here?
    I'm just learning c so please bear with my incapability

Leave a Reply

Your email address will not be published. Required fields are marked *