Difference between revisions of "C-and-C++/C2/First-C++-Program/Gujarati"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 1: Line 1:
{| border=1
+
{| border = 1
|| ''Time'''
+
 
|| '''Narration'''
+
|'''Time'''
 +
 
 +
|'''Narration'''
 +
 
 +
 
 
|-
 
|-
| 00.01
+
| 00.02
| First C++ પ્રોગ્રામ રનાં સ્પોકન ટ્યુટોરીયલમાં સ્વાગત છે. .  
+
| | Welcome to the spoken tutorial on First C program.  
  
 
|-
 
|-
 
| 00.06
 
| 00.06
| આ ટ્યુટોરીયલમાં આપણે શીખીશું,  
+
| In this tutorial, we  will learn
 +
 
 
|-
 
|-
| 00.10
+
| 00.08
| How to write a C++ program
+
| How to write a simple C program  
  
 
|-
 
|-
| 00.12
+
| 00.11
 
| How to compile it
 
| How to compile it
  
 
|-
 
|-
| 00.14
+
| 00.13
 
| How to execute it
 
| How to execute it
  
 
|-
 
|-
| 00.16
+
| 00.14
| We will also explain some common errors and their solution.  
+
|We will also explain some common errors and their solutions.  
 +
 
 +
|-
 +
|00.19
 +
| | To record this tutorial, I am using
  
 
|-
 
|-
 
| 00.21
 
| 00.21
| To record this tutorial, I am using Ubuntu operating system version 11.10 and G++ Compiler version 4.5.2 on Ubuntu.
+
|Ubuntu operating system version 11.10 and gcc Compiler version 4.6.1 on Ubuntu
  
 
|-
 
|-
| 00.34
+
| 00.31
 
|  To practice this tutorial,  
 
|  To practice this tutorial,  
  
 
|-
 
|-
| 00.36
+
| 00.33
| You should be familiar with Ubuntu Operating System and an Editor
+
|You should be familiar with Ubuntu Operating System and an Editor
 +
 
 +
|-
 +
| 00.39
 +
|Some editors are '''vim''' and '''gedit'''
  
 
|-
 
|-
 
| 00.42
 
| 00.42
|Some editors are vim and gedit
+
|I will use gedit in this tutorial
  
 
|-
 
|-
 
| 00.46
 
| 00.46
| I am using gedit in this tutorial
+
|For relevant tutorials please visit our website: [http://spoken-tutorial.org/ http://spoken-tutorial.org]
  
 
|-
 
|-
| 00.49
+
|00.51
| For relevant tutorial Please visit our website: [http://spoken-tutorial.org/ http://spoken-tutorial.org]
+
| Let me tell you how to write a C program through an example
  
 
|-
 
|-
| 00.54
+
| 00.56
| Let me tell you how to write a C++ program through an example
+
|Open the terminal window  by pressing '''Ctrl, Alt and T '''keys simultaneously on your keyboard.
  
 
|-
 
|-
| 00.59
+
| 01.07
| Open the terminal Window using '''Ctrl, Alt and T keys''' simultaneously on your keyboard.
+
Now let's open the text editor. So, at the prompt, type  
 
+
|-
+
| 01.08
+
| To open the text editor, type under terminal.
+
  
 
|-
 
|-
 
| 01.12
 
| 01.12
| '''“gedit”''' space '''“talk”''' dot '''“.cpp”''' space ampersand '''“&”.'''
+
|'''“gedit”''' space '''“talk”''' dot '''“c”''' space “&”'''
  
 
|-
 
|-
 
| 01.20
 
| 01.20
| We use the '''&”''' to free up the prompt.
+
|'''We use ampersand (&) to free up the prompt '''
  
 
|-
 
|-
| 01.24
+
| 01.25
| Please note that all the C++ files will have the extension '''“.cpp”'''
+
| Please note that all the '''C''' files will have extension dot '''“c”'''
  
 
|-
 
|-
| 01.30
+
|01.31
| Now   Press '''Enter'''
+
| Now Press '''Enter'''
  
 
|-
 
|-
| 01.32
+
| 01.33
|the text editor has opened.
+
| the text editor has opened
  
 
|-
 
|-
| 01.35
+
| 01.37
|  Let us start to write a program.
+
|  Let us start to write a program  
  
 
|-
 
|-
| 01.38
+
| 01.39
 
| Type double slash '''“//”''' space  
 
| Type double slash '''“//”''' space  
  
 
|-
 
|-
| 01.41
+
| 01.42
| '''“My first C++ program”.'''
+
|'''“My first C program”.'''
  
 
|-
 
|-
| 01.44
+
|01.48
| Here, double slash is used to comment the line
+
| Here, double slash is used to comment the line
  
 
|-
 
|-
| 01.49
+
| 01.52
| Comments are used to understand the flow of program
+
|Comments are used to understand the flow of program
  
 
|-
 
|-
| 01.53
+
| 01.56
| It is useful for documentation
+
|It is useful for documentation
  
 
|-
 
|-
| 01.55
+
| 01.58
| It gives us information about the program
+
|It gives us information about the program
  
 
|-
 
|-
| 01.58
+
| 02.01
| The double slash is called as single line comment.Now press Enter.
+
|The double slash is called as single line comment.
  
 
|-
 
|-
| 02.04
+
| 02.07
| Type hash '''“#include”''' space opening angle bracket ''' ''closing angle bracket ''' .
+
| Now press '''Enter'''
 
+
  
 
|-
 
|-
| 02.13
+
|02.09
|It is  a good practice to complete the brackets first, and then start writing inside it
+
|Type hash '''“#include”''' space opening bracket , closing bracket'''
  
 
|-
 
|-
| 02.20
+
| 02.17
| Now Inside the bracket, type '''“iostream”''' .
+
| It is always a good practice to complete the brackets first, and then start writing inside it
  
 
|-
 
|-
 
| 02.24
 
| 02.24
| Here '''iostream''' is a '''header file'''  
+
| Now  Inside the bracket, type'''“stdio”''' dot”.” “'''h” '''
  
 
|-
 
|-
| 02.28
+
| 02.30
| This file includes the declaration of standard input output functions  in C++.Now press Enter
+
'''stdio.h''' is a '''header file'''
  
 
|-
 
|-
| 02.36
+
| 02.33
| Type '''“using”''' space '''“namespace”''' space '''“std”''' and a semicolon '''“;”''' .
+
|A '''program''' must contain this header file when it uses standard '''input/output functions'''.Now press Enter
  
 
|-
 
|-
|02.48
+
| 02.43
| The '''using''' statement informs the compiler that you want to use the '''std namespace'''
+
|   type '''“int” '''space''' “main” '''opening bracket,
 +
closing bracket '''“()”. '''
  
 
|-
 
|-
| 02.54
+
| 02.50
| The purpose of '''namespace''' is to avoid name collisions
+
| | ' '''main''' is a special '''function'''
 +
|-
 +
| 02.52
 +
|It denotes that the execution of the program begins from this line
  
 
|-
 
|-
 
| 02.58
 
| 02.58
| It is done by localizing the names of identifiers
+
|The opening bracket and  closing bracket is called as parenthesis.
  
 
|-
 
|-
|03.02
+
| 03.04
| It creates a declarative region and defines a scope
+
| Parenthesis followed by '''main''' is to tell the user that '''main''' is a '''function'''
  
 
|-
 
|-
| 03.05
+
| 03.11
| Anything defined within a '''namespace''' is in the SCOPE of that namespace
+
| Here the '''int''' '''main function''' takes no '''arguments'''  
  
 
|-
 
|-
| 03.11
+
| 03.15
|Here '''std''' is the '''namespace''' in which entire standard C++ library is declared. Now press '''Enter'''.
+
|It returns a value of type '''integer'''.
  
 +
|-
 +
| 03.19
 +
| We will learn about '''data types''' in another tutorial.
  
|-  
+
|-
| 03.20
+
| 03.23
| Type '''“int”''' space '''“main”''' opening bracket '''“(”''' closing bracket '''“)”''' .
+
| Now  Let us switch to the slides to know more about  '''main function'''. Let us go to the next slide.
  
 +
|-
 +
| 03.30
 +
|  Every '''program'''should have  one main function
  
 
|-
 
|-
| 03.28
+
| 03.33
| '''main''' is a special '''function''' 
+
|There should NOT be more than one main function
  
 
|-
 
|-
| 03.31
+
| 03.37
| It denotes that the execution of the program begins from this line.
+
|Otherwise the compiler cannot locate the beginning of the program  
  
|-
 
| 03.36
 
|The opening and the closing bracket is called as'' Parenthesis''.
 
 
|-
 
|-
 
| 03.41
 
| 03.41
| Parenthesis followed by '''main'''  tells the user that '''main''' is a '''function.'''
+
|The empty pair of parentheses indicates that main has no '''arguments'''
  
 
|-
 
|-
| 03.48
+
| 03.46
| Here the '''int''' '''main''' '''function''' takes no '''arguments''' and returns a value of type integer.
+
|The concept of arguments will be discussed in detail in the upcoming tutorials.
  
 
|-
 
|-
| 03.55
+
| 03.52
| We will learn about data types in another tutorial.
+
| Now let us come back to our program. Press Enter.
  
 
|-
 
|-
| 04.00
+
| 03.58
|Now  Let us switch to the slides to know more about  main function.
+
| Type opening curly brace '''“{”'''
  
 
|-
 
|-
| 04.06
+
| 04.00
| Every '''program'''should have  one main function
+
|The opening curly bracket marks the beginning of the '''function''' '''main'''.
  
 
|-
 
|-
| 04.09
+
| 04.05
| There should NOT be more than one “main” function
+
|Then  Type  closing curly  bracket '''“}”'''
 +
 +
|-
 +
| 04.08
 +
|The closing curly bracket indicates the end of the '''function''' '''main'''.
  
 
|-
 
|-
 
| 04.13
 
| 04.13
| Otherwise the compiler cannot locate the beginning of the program
+
| Now Inside the bracket 
  
 
|-
 
|-
| 04.17
+
| 04.14
| The empty pair of parentheses indicates that main has no '''arguments'''
+
|press '''Enter''' twice,  move the cursor one line up
  
 
|-
 
|-
| 04.23
+
| 04.20
| The concept of '''arguments''' will be discussed in  the upcoming tutorials. Now come back to our program. press enter.
+
| Indentation makes the code easier to read
  
 
|-
 
|-
| 04.34
+
| 04.23
| Type opening curly bracket '''“{”'''
+
|It also helps to locate errors faster
  
 
|-
 
|-
| 04.37
+
| 04.26
| The opening  curly  bracket marks the beginning of the '''function main. '''
+
|So let us give three space here
  
 
|-
 
|-
| 04.42
+
| 04.29
|Then Type closing curly bracket '''“}”'''
+
| And  Type''' “printf” ''' opening bracket closing bracket '''“()” '''
  
 
|-
 
|-
| 04.45
+
| 04.34
| The closing bracket indicates the end of the '''function main '''
+
|'''printf '''is a standard C function to print the output on the terminal
  
 
|-
 
|-
| 04.50
+
| 04.39
|Now Inside the bracket press enter twice
+
Here inside the brackets, within double quotes,
  
 
|-
 
|-
| 04.54
+
| 04.44
| Move the cursor one line up.
+
|Anything within the double quotes in the printf statement will be printed on the terminal.
  
 
|-
 
|-
|04.57
+
| 04.50
| Indentation makes the code easier to read
+
|Type'''“Talk To a Teacher '''backslash '''n”'''
  
 
|-
 
|-
 
| 05.00
 
| 05.00
| It also helps to locate errors faster
+
|Backslash n '''“\n” '''signifies newline
  
 
|-
 
|-
 
| 05.03
 
| 05.03
|So let us  give a space here.
+
|As a result, after execution of the '''printf '''function, the cursor moves to the new line
  
 
|-
 
|-
| 05.06
+
| 05.11
|And type '''“cout”''' space two opening angle bracket ''''
+
| Every '''C''' statement must end with a '''semicolon “;”'''
  
 
|-
 
|-
| 05.13
+
| 05.15
| Here '''cout '''is a standard '''C++ function''' to print the output on the terminal.
+
|Hence, type it at the end of this line.  
  
 +
|-
 +
| 05.19
 +
|'''Semicolon''' acts as a statement terminator.
  
 +
|-
 +
|05.24
 +
|Now press '''Enter''' give three space here
  
 
|-
 
|-
| 05.20
+
|05.28
|Now  after the brackets, type within  double quotes '''
+
| And type '''“return”''' space''' “0”''' and a '''semicolon''' '''“;”'''
  
 
|-
 
|-
| 05.26
+
| 05.34
|Anything within the double quotes in the '''cout ''' functions will be printed. Now inside a quote type“Talk to a teacher backslash \n”'''. 
+
| This statement returns the integer zero
  
 
|-
 
|-
| 05.39
+
| 05.38
| Here '''\n''' signifies newline
+
|An integer has to be returned for this function because the function type is '''int'''
  
 
|-
 
|-
| 05.43
+
| 05.45
| As a result, after execution of the '''cout function''', the cursor moves to the new line.
+
|The '''return''' statement marks the end of executable statements
  
 
|-
 
|-
| 05.50
+
| 05.51
| Every C++ statement must end with a '''semicolon'''
+
|We will learn more about the returned values in another tutorial.
 
+
  
 
|-
 
|-
| 05.54
+
| 05.56
| Hence type it at the end of this line.
+
| Now click on"'''Save'''" button to save the file
  
 
|-
 
|-
| 05.57
+
| 06.00
| Semicolon acts as a statement terminator. Now press Enter.
+
|It is a good habit to save files frequently
  
 
|-
 
|-
 
| 06.03
 
| 06.03
| Give a space here and  Type '''“return”''' space '''“0”''' and a semicolon '''“;”.'''
+
|This will protect you from sudden power failures
  
 
|-
 
|-
| 06.10
+
| 06.06
| This statement returns the integer zero
+
|It will also be useful in case the applications were to crash.
 
+
  
 
|-
 
|-
| 06.14
+
| 06.10
| An integer has to be returned for this '''function'''
+
| Let  us now compile the program come back to a  terminal.
 
+
  
 
|-
 
|-
| 06.17
+
| 06.15
| Because the '''function''' type is '''int'''
+
|Type '''“gcc”''' space '''“talk.c”''' space hyphen “-'''o”''' space '''“myoutput”'''
 
+
  
 
|-
 
|-
| 06.21
+
| 06.24
| The '''return''' statement marks the end of executable statements
+
| '''gcc''' is the compiler
 
+
  
 
|-
 
|-
| 06.26
+
| 06.27
| We will learn more about the returned values in another tutorial.
+
|'''talk.c''' is our filename .
 
+
 
|-
 
|-
| 06.31
+
| 06.30
| Now click on '''“Save”''' button to save the file  
+
|'''-o''' '''myoutput''' says that the executable should go to the file '''myoutput'''
 
+
  
 
|-
 
|-
| 06.36
+
| 06.37
| It is a good habit to save files frequently
+
| Now Press''' Enter. '''
 
+
  
 
|-
 
|-
 
| 06.39
 
| 06.39
| This will protect you from sudden power failures
+
|We see that the program is compiled
 
+
  
 
|-
 
|-
 
| 06.42
 
| 06.42
| It will also be useful in case the applications were to crash.
+
|By typing '''ls -lrt''', we can see that '''myoutput''' is the last file to be created
  
 
|-
 
|-
| 06.47
+
| 06.54
| Let us now compile the program.
+
|To execute the program, type dot slash '''“./myoutput” ''' press '''Enter.'''
  
 
|-
 
|-
| 06.50
+
|07.01
| Come back to a terminal
+
Here the output is displayed as '''“Talk To a Teacher”.'''
 
+
  
 
|-
 
|-
| 06.53
+
| 07.06
| Type '''“g++”''' space '''“talk.cpp”''' space hyphen '''“-o”''' space '''“output”.'''
+
| As I said before, return is the last statement to be executed
  
 
|-
 
|-
| 07.05
+
| 07.10
|Here '''g++''' is the compiler used to compile '''C++ '''programs
+
|Thus after the return statement nothing will be executed. Let us try it out.
 
+
  
 
|-
 
|-
| 07.12
+
|07.15
| '''talk.cpp''' is our filename
+
| come back to our program.
  
 
|-
 
|-
| 07.16
+
| 07.17
| '''-o output''' says that the executable should go to the file output. Now press enter
+
| After the '''return''' statement, let us include one more '''printf''' statement give space here type printf("Welcome \n") at the end type a semicolon.
  
 
|-
 
|-
| 07.25
+
| 07.35
| We see that the program is compiled.
+
| Now click on  save.
 
+
|-
+
| 07.27
+
| By typing '''ls -lrt''', we can see that '''output''' is the last file to be created.
+
  
 
|-
 
|-
 
| 07.37
 
| 07.37
| Let us  execute a  program,  type dot slash '''“./output” '''
+
|Let us compile and execute come back to our terminal.
 
+
  
 
|-
 
|-
| 07.43
+
|07.41
|And Press '''Enter.'''
+
you can recall the previously entered commands by using '''up arrow'''key.
  
 
|-
 
|-
 
| 07.46
 
| 07.46
| Here the output is displayed as '''“Talk to a teacher”.'''
+
|That is what I did now
  
 
|-
 
|-
| 07.50
+
| 07.51
| Now let us see the common errors which we can come across
+
|We see that the second  statements  '''welcome''' is  not executed
 
+
  
 
|-
 
|-
| 07.54
+
| 07.58
| switch  back to a editor.
+
| Now come back to our program
  
 
|-
 
|-
| 07.57
+
| 08.00
| Suppose here we miss the '''{'''.
+
|Let us write the 'Welcome' statement above the return statement
|-
+
| 08.01
+
|Now  save the file.
+
  
 
|-
 
|-
| 08.04
+
| 08.07
| Let us execute.Come back to a terminal
+
Click on  Save.
  
 
|-
 
|-
| 08.08
+
| 08.09
| Now compile and run the program using the command we used before.
+
|Let us compile and execute
  
 
|-
 
|-
 
| 08.15
 
| 08.15
| we see that there is an error at line no.7 in our '''talk.cpp''' file
+
|We see that the second '''printf''' statement ''welcome'' has also has been executed
 
+
  
 
|-
 
|-
| 08.22
+
| 08.23
| That Expected curly bracket at the end of input.
+
| Now let us see the common errors which we can come across.  Come back to our program.
  
|-
 
| 08.28
 
|Now  Come back to a  text editor .
 
  
 
|-
 
|-
| 08.31
+
| 08.29
| As i said before the closing curly bracket marks the end of the function main
+
| Suppose here I  will the miss the dot in '''“stdio.h”''' Click on save.
  
  
 
|-
 
|-
 
| 08.36
 
| 08.36
| Hence reinsert the bracket here now Save the file.
+
| Let us  compile and execute .
 
+
  
 
|-
 
|-
 
| 08.41
 
| 08.41
| Let us execute it
+
|We see that
 
+
  
 
|-
 
|-
| 08.44
+
| 08.42
| You can recall the previously entered commands by using up arrow key
+
|There is a fatal error  at line no.2 in our '''talk.c''' file.
 
+
  
 
|-
 
|-
| 08.49
+
| 08.48
| That is what I did now. Yes it is working.
+
|The compiler cannot find a '''header file''' with the name '''“stdioh”'''hence it is giving an error no such file or directory.
  
 
|-
 
|-
|08.56
+
| 08.59
I will show you another common error
+
|And the compilation is terminated.
 +
|-
 +
| 09.03
 +
|Let us now fix the error come back to a  program Reinsert the dot '''“.”'''click on save
  
  
 
|-
 
|-
| 08.59
+
| 09.11
| Let us switch back to our text editor.
+
| Let us compile and  execute.Yes  It is working.
  
 
|-
 
|-
| 09.02
+
| 09.19
| Now, suppose here we missed '''std.'''Let us save the file
+
| I will show you another common error
  
 
|-
 
|-
| 09.08
+
|09.22
| Come back to our  terminal . Let us compile .
+
|Let us switch back to the program.
 
+
  
  
 
|-
 
|-
| 09.13
+
| 09.26
| We see that there is an  errors at line no 3 and  line no 6 in our '''talk.cpp''' file
+
| Now, suppose here i will miss the '''semicolon''' at the end of the line
 
+
  
 
|-
 
|-
| 09.21
+
| 09.31
That expected identifier before '''semicolon''' and '''cout''' was not declared in this scope.
+
| Click on '''Save'''.Let us compile and execute
  
 
|-
 
|-
|09.30
+
| 09.41
As '''cout''' is the standard '''C++ library function'''
+
| We see that there is an error at line no.6 in our talk.c file.  That expected ''semicolon'' before  printf.
 +
|-
 +
| 09.51
 +
|Come back to our program.  As  i said before semicolon acts as a statement terminator
  
  
 
|-
 
|-
| 09.33
+
| 09.59
| and the entire '''C++ library function''' is defined under '''std namespace '''
+
|So it will search for it at the end of the line.5 and at the beginning of the line.6.
  
 +
|-
 +
| 10.07
 +
|  This is line 6
  
 
|-
 
|-
| 09.39
+
| 10.09
| Hence it is giving an error.
+
| This is the last place where you can put the '''semicolon'''
  
 
|-
 
|-
| 09.42
+
| 10.13
| Let us now  fix the error
+
|Recall that the compiler also give the error message on line 6.
  
 +
|-
 +
| 10.18
 +
| Let us try what happens if we put the semicolon here.
  
 
|-
 
|-
| 09.44
+
| 10.24
| Come back to our Text editor  type '''std''' at line  no-3.
+
| Click on '''Save''' .
  
 
|-
 
|-
| 09.50
+
| 10.26
| Let us Save it.
+
|Let us   Compile and execute.Yes it is working
  
 
|-
 
|-
| 09.52
+
| 10.33
| Let us compile it again.Yes it is working. Now let us go back to our slide.
+
|Now come back to our program.  Let us type the ''semicolon'' here  at the end of this line.
  
  
|-
 
| 10.02
 
|  As an assignment,
 
  
  
 
|-
 
|-
| 10.04
+
| 10.41
| Write a program to print your name and city
+
| As it is a conventional practice to type  the ''semicolon'' at the end of the line. Now click on save.
 
+
  
 
|-
 
|-
| 10.07
+
| 10.49
| We used '''single line comment''' in this tutorial
+
|   Let us  Compile and execute. .Yes it is working.
  
  
 
|-
 
|-
| 10.10
+
| 10.49
| Now just try to give a '''multiline comment'''
+
|Now let us go back to our slides.
  
 +
|-
 +
|10.57
 +
| As an Assignment
  
 
|-
 
|-
| 10.14
+
| 10.59
|This brings us to the end of these  tutorial .
+
|Write a program to print "Welcome to the World of C"
  
 
|-
 
|-
| 10.17
+
| 11.03
| Watch the video available at the link shown http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial
+
|See what happens if  '''“\n”''' is not included in the '''printf''' statement.
  
 +
|-
 +
| 11.09
 +
| This brings us to the end of this tutorial
  
 
|-
 
|-
| 10.20
+
| 11.12
| It summarises the Spoken Tutorial project
+
| Watch the video available at the link shown below, http://spokentutorial.org/What\_is\_a\_Spoken\_Tutorial
 
+
  
 
|-
 
|-
| 10.22
+
| 11.15
| If you do not have good bandwidth, you can download and watch it.
+
|It summarises the Spoken Tutorial project
  
 
|-
 
|-
|10.26
+
| 11.18
|The Spoken Tutorial Project Team
+
|If you do not have good bandwidth, you can download and watch it.
 
+
  
 
|-
 
|-
| 10.28
+
| 11.22
| Conducts workshops using spoken tutorials
+
| The Spoken Tutorial Project Team
 
+
  
 
|-
 
|-
| 10.31
+
| 11.24
| Gives certificates to those who pass an online test
+
|Conducts workshops using spoken tutorials
 
+
  
 
|-
 
|-
| 10.35
+
| 11.28
| For more details, please  write to contact @spoken-tutorial.org
+
|Gives certificates to those who pass an online test
  
 
|-
 
|-
|10.43
+
| 11.32
| Spoken Tutorial Project is a part of the Talk to a Teacher project
+
|For more details, please write to contact [at] spoken hyphen tutorial dot org
  
 
|-
 
|-
| 10.47
+
|11.38
| It is supported by the National Mission on Education through ICT, MHRD, Government of India
+
| Spoken Tutorial Project is a part of the Talk to a Teacher project
 
+
  
 
|-
 
|-
| 10.53
+
| 11.42
| More information on this Mission is available at: [http://spoken-tutorial.org/ http://spoken-tutorial.org]\NMEICT-Intro
+
|It is supported by the National Mission on Education through ICT, MHRD, Government of India
  
 
|-
 
|-
| 10.58
+
| 11.48
| This is Ashwini Patil from IIT Bombay signing off
+
|More information on this Mission is available at  the link shown below: http://spoken-tutorial.org\NMEICT-Intro
 
+
  
 
|-
 
|-
| 11.01
+
| 11.51
| Thank you for watching
+
| This is Ashwini Patil from IIT Bombay. Thank you for joining.
  
 
|}
 
|}

Revision as of 12:33, 10 October 2013

Time Narration


00.02 Welcome to the spoken tutorial on First C program.
00.06 In this tutorial, we will learn
00.08 How to write a simple C program
00.11 How to compile it
00.13 How to execute it
00.14 We will also explain some common errors and their solutions.
00.19 To record this tutorial, I am using
00.21 Ubuntu operating system version 11.10 and gcc Compiler version 4.6.1 on Ubuntu
00.31 To practice this tutorial,
00.33 You should be familiar with Ubuntu Operating System and an Editor
00.39 Some editors are vim and gedit
00.42 I will use gedit in this tutorial
00.46 For relevant tutorials please visit our website: http://spoken-tutorial.org
00.51 Let me tell you how to write a C program through an example
00.56 Open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard.
01.07 Now let's open the text editor. So, at the prompt, type
01.12 “gedit” space “talk” dot “c” space “&”
01.20 We use ampersand (&) to free up the prompt
01.25 Please note that all the C files will have extension dot “c”
01.31 Now Press Enter
01.33 the text editor has opened
01.37 Let us start to write a program
01.39 Type double slash “//” space
01.42 “My first C program”.
01.48 Here, double slash is used to comment the line
01.52 Comments are used to understand the flow of program
01.56 It is useful for documentation
01.58 It gives us information about the program
02.01 The double slash is called as single line comment.
02.07 Now press Enter
02.09 Type hash “#include” space opening bracket , closing bracket
02.17 It is always a good practice to complete the brackets first, and then start writing inside it
02.24 Now Inside the bracket, type“stdio” dot”.” “h”
02.30 stdio.h is a header file
02.33 A program must contain this header file when it uses standard input/output functions.Now press Enter
02.43 type “int” space “main” opening bracket,
closing bracket “()”. 
02.50 ' main is a special function
02.52 It denotes that the execution of the program begins from this line
02.58 The opening bracket and closing bracket is called as parenthesis.
03.04 Parenthesis followed by main is to tell the user that main is a function
03.11 Here the int main function takes no arguments
03.15 It returns a value of type integer.
03.19 We will learn about data types in another tutorial.
03.23 Now Let us switch to the slides to know more about main function. Let us go to the next slide.
03.30 Every programshould have one main function
03.33 There should NOT be more than one main function
03.37 Otherwise the compiler cannot locate the beginning of the program
03.41 The empty pair of parentheses indicates that main has no arguments
03.46 The concept of arguments will be discussed in detail in the upcoming tutorials.
03.52 Now let us come back to our program. Press Enter.
03.58 Type opening curly brace “{”
04.00 The opening curly bracket marks the beginning of the function main.
04.05 Then Type closing curly bracket “}”
04.08 The closing curly bracket indicates the end of the function main.
04.13 Now Inside the bracket
04.14 press Enter twice, move the cursor one line up
04.20 Indentation makes the code easier to read
04.23 It also helps to locate errors faster
04.26 So let us give three space here
04.29 And Type “printf” opening bracket closing bracket “()”
04.34 printf is a standard C function to print the output on the terminal
04.39 Here inside the brackets, within double quotes,
04.44 Anything within the double quotes in the printf statement will be printed on the terminal.
04.50 Type“Talk To a Teacher backslash n”
05.00 Backslash n “\n” signifies newline
05.03 As a result, after execution of the printf function, the cursor moves to the new line
05.11 Every C statement must end with a semicolon “;”
05.15 Hence, type it at the end of this line.
05.19 Semicolon acts as a statement terminator.
05.24 Now press Enter give three space here
05.28 And type “return” space “0” and a semicolon “;”
05.34 This statement returns the integer zero
05.38 An integer has to be returned for this function because the function type is int
05.45 The return statement marks the end of executable statements
05.51 We will learn more about the returned values in another tutorial.
05.56 Now click on"Save" button to save the file
06.00 It is a good habit to save files frequently
06.03 This will protect you from sudden power failures
06.06 It will also be useful in case the applications were to crash.
06.10 Let us now compile the program come back to a terminal.
06.15 Type “gcc” space “talk.c” space hyphen “-o” space “myoutput”
06.24 gcc is the compiler
06.27 talk.c is our filename .
06.30 -o myoutput says that the executable should go to the file myoutput
06.37 Now Press Enter.
06.39 We see that the program is compiled
06.42 By typing ls -lrt, we can see that myoutput is the last file to be created
06.54 To execute the program, type dot slash “./myoutput” press Enter.
07.01 Here the output is displayed as “Talk To a Teacher”.
07.06 As I said before, return is the last statement to be executed
07.10 Thus after the return statement nothing will be executed. Let us try it out.
07.15 come back to our program.
07.17 After the return statement, let us include one more printf statement give space here type printf("Welcome \n") at the end type a semicolon.
07.35 Now click on save.
07.37 Let us compile and execute come back to our terminal.
07.41 you can recall the previously entered commands by using up arrowkey.
07.46 That is what I did now
07.51 We see that the second statements welcome is not executed
07.58 Now come back to our program
08.00 Let us write the 'Welcome' statement above the return statement
08.07 Click on Save.
08.09 Let us compile and execute
08.15 We see that the second printf statement welcome has also has been executed
08.23 Now let us see the common errors which we can come across. Come back to our program.


08.29 Suppose here I will the miss the dot in “stdio.h” Click on save.


08.36 Let us compile and execute .
08.41 We see that
08.42 There is a fatal error at line no.2 in our talk.c file.
08.48 The compiler cannot find a header file with the name “stdioh”hence it is giving an error no such file or directory.
08.59 And the compilation is terminated.
09.03 Let us now fix the error come back to a program Reinsert the dot “.”click on save


09.11 Let us compile and execute.Yes It is working.
09.19 I will show you another common error
09.22 Let us switch back to the program.


09.26 Now, suppose here i will miss the semicolon at the end of the line
09.31 Click on Save.Let us compile and execute
09.41 We see that there is an error at line no.6 in our talk.c file. That expected semicolon before printf.
09.51 Come back to our program. As i said before semicolon acts as a statement terminator


09.59 So it will search for it at the end of the line.5 and at the beginning of the line.6.
10.07 This is line 6
10.09 This is the last place where you can put the semicolon
10.13 Recall that the compiler also give the error message on line 6.
10.18 Let us try what happens if we put the semicolon here.
10.24 Click on Save .
10.26 Let us Compile and execute.Yes it is working
10.33 Now come back to our program. Let us type the semicolon here at the end of this line.



10.41 As it is a conventional practice to type the semicolon at the end of the line. Now click on save.
10.49 Let us Compile and execute. .Yes it is working.


10.49 Now let us go back to our slides.
10.57 As an Assignment
10.59 Write a program to print "Welcome to the World of C"
11.03 See what happens if “\n” is not included in the printf statement.
11.09 This brings us to the end of this tutorial
11.12 Watch the video available at the link shown below, http://spokentutorial.org/What\_is\_a\_Spoken\_Tutorial
11.15 It summarises the Spoken Tutorial project
11.18 If you do not have good bandwidth, you can download and watch it.
11.22 The Spoken Tutorial Project Team
11.24 Conducts workshops using spoken tutorials
11.28 Gives certificates to those who pass an online test
11.32 For more details, please write to contact [at] spoken hyphen tutorial dot org
11.38 Spoken Tutorial Project is a part of the Talk to a Teacher project
11.42 It is supported by the National Mission on Education through ICT, MHRD, Government of India
11.48 More information on this Mission is available at the link shown below: http://spoken-tutorial.org\NMEICT-Intro
11.51 This is Ashwini Patil from IIT Bombay. Thank you for joining.

Contributors and Content Editors

Gaurav, Jyotisolanki, Krupali, PoojaMoolya, Pratik kamble