Difference between revisions of "C-and-Cpp/C2/First-C-Program/English-USA"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "'''Title of script:''' First C program '''Author:''' Ashwini Patil '''Keywords: C Program, printf(), int main(), Video Tutorial''' {| border=1 |<center>'''Visual Cue'''</...")
 
Line 1: Line 1:
'''Title of script:''' First C program
+
{| border = 1
  
'''Author:''' Ashwini Patil
+
|'''Time'''
 +
|'''Narration'''
  
'''Keywords: C Program, printf(), int main(), Video Tutorial'''
+
|-
 +
| 00:01
 +
|  Welcome to the spoken tutorial on '''First C program'''.
  
 +
|-
 +
| 00:05
 +
|  In this tutorial, we  will learn
  
 +
|-
 +
| 00:08
 +
| *How to write a simple C program
  
{| border=1
+
|-
|<center>'''Visual Cue'''</center>
+
| 00:11
| <center>'''Narration'''</center>
+
| *How to compile it
  
 
|-
 
|-
| Slide 1
+
| 00:13
| Welcome to the spoken tutorial on First C program.
+
| *How to execute it
  
 
|-
 
|-
| Slide 2
+
| 00:14
 +
|We will also explain some common errors and their solutions.
  
Learning Objectives
+
|-
| In this tutorial we will learn,  
+
|00:18
 +
| To record this tutorial, I am using
  
*How to write a simple C program
+
|-
*How to compile it
+
| 00:21
*How to execute it
+
|Ubuntu operating system version 11.10 and gcc Compiler version 4.6.1 on Ubuntu.
  
We will also explain some common errors and their solution.
+
|-
 +
| 00:31
 +
|  To practice this tutorial,
  
 
|-
 
|-
| Slide 3
+
| 00:33
 +
|You should be familiar with Ubuntu Operating System and an Editor.
  
System Requirements
+
|-
 +
| 00:38
 +
|Some editors are '''vim''' and '''gedit'''.
  
 +
|-
 +
| 00:42
 +
|I will use 'gedit' in this tutorial.
  
 +
|-
 +
| 00:45
 +
|For relevant tutorials please visit our website which is as shown. http://spoken-tutorial.org
  
| To record this tutorial, I am using
+
|-
 +
|00:51
 +
| Let me tell you how to write a C program through an example.
  
*Ubuntu operating system version 11.10
+
|-
 +
| 00:55
 +
|Open the terminal window  by pressing '''Ctrl, Alt and T '''keys simultaneously on your keyboard.
  
*and GCC Compiler version 4.6.1 on Ubuntu.  
+
|-
 +
| 01:07
 +
|  Now let's open the text editor. So, at the prompt, type
  
 
|-
 
|-
| Slide 4
+
| 01:12
 +
|'''“gedit”''' space '''“talk”''' dot '''“c”''' space “&”''' sign.
  
Prerequisites
+
|-
 +
| 01:20
 +
|'''We use ampersand (&) to free up the prompt. '''
  
 +
|-
 +
| 01:24
 +
| Please note that all the '''C''' files will  have  extension  '''“.c”'''(dot C).
  
 +
|-
 +
|01:30
 +
|  Now Press '''Enter''',
  
| To practice this tutorial,
+
|-
 +
| 01:32
 +
|  the text editor has opened.
  
You should be familiar with '''Ubuntu Operating System.'''
+
|-
 +
| 01:36
 +
|  Let us start to write a program.  
  
And an '''editor'''.
+
|-
 +
| 01:39
 +
| Type double slash '''“//”''' space
  
 +
|-
 +
| 01:42
 +
|'''“My first C program”.'''
  
Some '''editors''' are '''vim''' and '''gedit'''
+
|-
 +
|01:48
 +
| Here, double slash is used to comment the line.
  
I will use '''gedit''' in this tutorial.  
+
|-
 +
| 01:52
 +
|Comments are used to understand the flow of program.
  
 +
|-
 +
| 01:56
 +
|It is useful for documentation.
  
For relevant tutorials please visit our website which is as shown:  
+
|-
 +
| 01:58
 +
|It gives us information about the program.
  
[http://spoken-tutorial.org/ http://spoken-tutorial.org]
+
|-
 +
| 02:01
 +
|The double slash is called as single line comment.
  
 
|-
 
|-
| /*Switch to Terminal*/
+
| 02:07
 +
| Now press '''Enter'''.
  
Open the terminal using Ctrl + Alt + T or alternately,  
+
|-
 +
|02:09
 +
|Type (hash) '''“#include”''' space opening bracket , closing bracket.
  
Click on Applications-> Accessories -> Terminal
+
|-
 +
| 02:17
 +
| It is always a good practice to complete the brackets first, and then start writing inside it.
  
At the command prompt type: gedit firstprog.c &
+
|-
 +
| 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'''.
  
| Let me tell you how to write a C program through an example
+
|-
 +
| 02:41
 +
|Now press Enter.
  
Open the terminal Window by pressing '''Ctrl, Alt '''and '''T''' keys Simultaneously on your keyboard.  
+
|-
 +
| 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.
  
Now lets open the text editor.
+
|-
 +
| 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'''.
  
So at the prompt type:  
+
|-
 +
| 03:11
 +
| Here the '''int''' '''main function''' takes no '''arguments'''.
  
'''"gedit"''' space '''"talk"''' dot '''"c"''' space '''&'''(ampersand sign)
+
|-
 +
| 03:15
 +
|It returns a value of type '''integer'''.
  
 +
|-
 +
| 03:18
 +
| 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.
  
We used the ampersand'''(&)''' to free up the prompt
+
|-
 +
| 03:29
 +
|  Every '''program''' should have one main() function.
  
 +
|-
 +
| 03:33
 +
|There should NOT be more than one main function.
  
 +
|-
 +
| 03:36
 +
|Otherwise the compiler cannot locate the beginning of the program.
  
Please note that all the '''C''' files will have extension dot '''"c"'''  
+
|-
 +
| 03:41
 +
|The empty pair of parentheses indicates that main has no '''arguments'''.
  
Now Press '''Enter. '''
+
|-
 +
| 03:46
 +
|The concept of arguments will be discussed in detail in the upcoming tutorials.
  
 
|-
 
|-
|Type:  
+
| 03:52
 +
| Now let us come back to our program.
  
'''//My first C program'''  
+
|-
|
+
| 03:55
 +
| Press '''Enter.'''
  
The text editor has opened.  
+
|-
 +
| 03:58
 +
| Type opening curly bracket '''“{”'''.
  
Let us start to write a program.  
+
|-
 +
| 04:00
 +
|The opening curly bracket marks the beginning of the '''function''' '''main'''.
  
Type double slash “'''//”''' space
+
|-
 +
| 04:04
 +
|Then  Type closing curly  bracket '''“}”'''.
 +
 +
|-
 +
| 04:08
 +
|The closing curly bracket indicates the end of the '''function''' '''main'''.
  
'''My first C program”.'''  
+
|-
 +
| 04:13
 +
| Now Inside the bracket  press '''Enter''' twice, 
  
 
|-
 
|-
|'''//My first C program'''
+
| 04:16
| Here, double slash is used to comment the line.  
+
|move the cursor one line up.
  
Comments are used to understand the flow of program.  
+
|-
 +
| 04:20
 +
| Indentation makes the code easier to read.
  
 +
|-
 +
| 04:23
 +
|It also helps to locate errors faster.
  
It is useful for documentation.
+
|-
 +
| 04:25
 +
|So let us give three space here
  
 +
|-
 +
| 04:29
 +
| And  Type''' “printf” ''' opening bracket closing bracket '''“()” '''.
  
It gives us information about the program.  
+
|-
 +
| 04:34
 +
|'''printf '''is a standard C function to print the output on the terminal.  
  
 +
|-
 +
| 04:39
 +
|  Here, inside the brackets, within the double quotes.
  
The double slash is called as '''single line comment.'''
+
|-
 +
| 04:43
 +
|Anything within the double quotes, in the printf statement, will be printed on the terminal.
  
 
|-
 
|-
|Highlight '''//'''  
+
| 04:50
 +
|Type'''“Talk To a Teacher '''backslash '''n”'''.
  
'''<nowiki>#include <stdio.h> </nowiki>'''
+
|-
 +
| 04:59
 +
|Backslash n '''“\n” '''signifies newline.
  
| Now press '''Enter'''  
+
|-
 +
| 05:03
 +
|As a result, after execution of the '''printf '''function, the cursor moves to the new line.
  
Type "hash '''(#)''' '''include''' (space) opening bracket, closing bracket.  
+
|-
 +
| 05:10
 +
| Every '''C''' statement must end with a '''semicolon “;”'''.
  
It is always a good practice to complete the brackets first, and then start writing inside it.  
+
|-
 +
| 05:15
 +
|Hence, type it at the end of this line.  
  
Now, Inside the bracket, type:  
+
|-
 +
| 05:19
 +
|'''Semicolon''' acts as a statement terminator.
  
'''"stdio"''' dot '''"h" '''
+
|-
 +
|05:24
 +
|Now press '''Enter''' give three space here.
  
'''stdio.h''' is a '''header file'''  
+
|-
 +
|05:27
 +
| And type '''“return”''' space''' “0”''' and a '''semicolon'''.
  
A program must contain this header file when it uses standard input/output functions
+
|-
 +
| 05:34
 +
| This statement returns the integer zero.
  
Now press '''Enter '''
+
|-
 +
| 05:38
 +
|An integer has to be returned for this function because the function type is '''int'''.
  
 
|-
 
|-
| <nowiki>#include<stdio.h> </nowiki>
+
| 05:45
 +
|The '''return''' statement marks the end of executable statements.
  
int main()
+
|-
 +
| 05:51
 +
|We will learn more about the returned values in another tutorial.
  
 +
|-
 +
| 05:55
 +
| Now click on"'''Save'''" button to save the file.
  
 +
|-
 +
| 06:00
 +
|It is a good habit to save files frequently.
  
| Type: "'''int''' (space) '''main()'''" (opening bracket and closing bracket)
+
|-
 +
| 06:03
 +
|This will protect you from sudden power failures.
  
'''main()''' is a special function.  
+
|-
 +
| 06:05
 +
|It will also be useful in case the applications were to crash.
  
It denotes that the execution of the program begins from this line.  
+
|-
 +
| 06:10
 +
|  Let  us now compile the program, come back to a  terminal.
  
The opening bracket and closing bracket is called as paranthesis.  
+
|-
 +
| 06:15
 +
|Type '''“gcc”''' space '''“talk.c”''' space hyphen “-'''o”''' space '''“myoutput”''',
  
Paranthesis followed by '''main()''' is to tell the user that main is a function.
+
|-
 +
| 06:24
 +
| '''gcc''' is the compiler,
  
Here the '''int main()''' function takes no arguments. It returns a value of type integer.  
+
|-
 +
| 06:27
 +
|'''talk.c''' is our filename .
 +
|-
 +
| 06:30
 +
|'''-o''' '''myoutput''' says that the executable should go to the file '''myoutput'''.
  
We will learn about data types in another tutorial.  
+
|-
 +
| 06:37
 +
|  Now Press''' Enter'''.
  
Now let us switch to the slides to know more about the '''main()''' function.  
+
|-
 +
| 06:39
 +
|We see that the program is compiled.
  
Let us go tot he next slide.  
+
|-
 +
| 06:42
 +
|By typing '''ls space (hypen) -lrt''', we can see that '''myoutput''' is the last file to be created.
  
 
|-
 
|-
| Slide 5
+
| 06:54
| Every program should have one '''main''' function.  
+
|To execute the program, type (dot slash)'''“./myoutput” ''', press '''Enter.'''
  
There should NOT be more than one main function.  
+
|-
 +
|07:01
 +
|  Here the output is displayed as '''“Talk To a Teacher”.'''
  
Otherwise, the compiler cannot locate the beginning of the program.  
+
|-
 +
| 07:06
 +
|  As I said before, return is the last statement to be executed.
  
The empty pair of parentheses indicate that main has no arguments.  
+
|-
 +
| 07:10
 +
|Thus, after the return statement nothing will be executed. Let us try it out.
  
The concept of arguments will be discussed in detail in the upcoming tutorials.  
+
|-
 +
|07:15
 +
| come back to our program.
  
 
|-
 
|-
| <nowiki>#include<stdio.h> </nowiki>
+
| 07:17
 +
| After the '''return''' statement, let us include one more '''printf''' statement,
  
int main()
+
|-
 +
| 07:22
 +
| give space here, type printf opening bracket, closing bracket.
  
{
+
|-
 +
| 07:27
 +
| Inside the bracket within the double quotes type Welcome backslash 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 arrow'''key.
  
| Now let us come back to our program,
+
|-
 +
| 07:46
 +
|That is what I did now.
  
Press '''Enter'''  
+
|-
 +
| 07:51
 +
|We see that the second  statement  '''welcome''' is  not executed.
  
type: '''{''' (opening curly bracket)
+
|-
 +
| 07:58
 +
| Now come back to our program.
  
The opening curly bracket marks the beginning of the function '''main. '''
+
|-
 +
| 08:00
 +
|Let us write the 'Welcome' statement above the return statement.
  
 
|-
 
|-
| <nowiki>#include<stdio.h> </nowiki>
+
| 08:06
 +
|  Click on ''' Save.'''
  
int main()
+
|-
 +
| 08:09
 +
|Let us compile and execute.
  
{
+
|-
 +
| 08:15
 +
|We see that the second '''printf''' statement ''welcome'' has also been executed.
  
}
+
|-
| Then
+
| 08:23
 +
| Now let us see the common errors which we can come across.  Come back to our program.
  
Type: '''}''' (Closing curly bracket)
+
|-
 +
| 08:29
 +
| Suppose here I  will miss the dot in '''“stdio.h”''', click on '''Save.'''
  
The Closing bracket indicates the end of the function '''main.'''
+
|-
 +
| 08:36
 +
| Let us  compile and execute .
  
 
|-
 
|-
|  
+
| 08:41
| Now inside the bracket
+
|We see that
  
press enter twice, move the cursor one line up
+
|-
 +
| 08:42
 +
|there is a fatal error  at line no.2 in our '''talk.c''' file.
  
 
|-
 
|-
|  
+
| 08:48
| Indentation makes the code easier to read
+
|The compiler cannot find a '''header file''' with the name '''“stdioh”'''. Hence it is giving an error "no such file or directory".
  
It also helps to locate errors faster
+
|-
 +
| 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.
  
So let us give three space here.  
+
|-
 +
| 09:11
 +
| Let us compile and  execute. Yes, it is working.
  
 
|-
 
|-
|<nowiki>#include<stdio.h> </nowiki>
+
| 09:19
 
+
| I will show you another common error.
int main()
+
 
+
{
+
 
+
printf("Talk To a Teacher \n");
+
 
+
 
+
 
+
| And Type:
+
 
+
'''printf''' opening bracket closing bracket '''()'''
+
 
+
'''printf()''' is a standard '''C''' function to print the output on the terminal.
+
 
+
Here inside the brackets, within double quotes,
+
 
+
Anything within the double quotes in the '''printf''' statement will be printed on the terminal.
+
 
+
Type:
+
 
+
'''Talk To a Teacher''' backward slash '''(\)''' and '''"n"'''
+
 
+
'''\n''' (BackSlash n) signifies newline,
+
 
+
As a result after the execution of the printf function the cursor moves to the new line.
+
 
+
Every '''C''' statement must end with a semicolon'''(;)'''
+
 
+
Hence Type it at the end of this line.
+
 
+
Semicolon'''(;)''' acts as a statement terminator.  
+
  
 
|-
 
|-
| <nowiki>#include<stdio.h> </nowiki>
+
|09:22
 
+
|Let us switch back to the program.
int main()
+
 
+
{
+
 
+
printf("Talk To a Teacher \n");
+
 
+
return 0;
+
 
+
}
+
| Now press '''Enter'''
+
 
+
Give three space here.
+
 
+
And type ''''return''' (space)'''0'''' and a semicolon ''''<nowiki>;'</nowiki>'''
+
 
+
This statement returns the integer zero.
+
 
+
An integer has to be returned for this function.
+
 
+
Because the function type is int.
+
 
+
The return statement marks the end of the executable statements.
+
 
+
We will learn more about the returned values in another tutorial.  
+
  
 
|-
 
|-
| <nowiki>#include<stdio.h> </nowiki>
+
| 09:25
 +
| Now, suppose here I will miss the '''semicolon'''  at the end of the line.  
  
int main()
+
|-
 
+
| 09:31
{
+
| Click on '''Save'''. Let us compile and execute.
 
+
printf("Talk To a Teacher \n");
+
 
+
return 0;
+
 
+
}
+
 
+
'''Type:'''
+
 
+
'''gcc talk.c -o myoutput'''
+
 
+
'''Highlight'''
+
 
+
'''gcc'''
+
 
+
'''talk.c'''
+
 
+
'''-o myoutput'''
+
| Now Click on the '''"Save"''' button to save the file.  
+
 
+
It is a good habit to save files frequently.
+
 
+
This will protect you from sudden power failures.
+
 
+
It will also be useful in case the applications were to crash.
+
 
+
Let us now compile the program
+
 
+
Come back to our terminal
+
 
+
Type: '''"gcc"''' space “'''talk.c”''' space hyphen '''o''' space “'''myoutput"'''
+
 
+
'''gcc''' is the compiler.
+
 
+
'''talk.c''' is the filename.
+
 
+
'''-o''' '''myoutput''' says that the executable should go to the file myoutput.
+
 
+
Now press '''Enter'''
+
 
+
We see that the program is compiled.  
+
  
 
|-
 
|-
| Type:  
+
| 09:41
 
+
| We see that there is  an error at line no.6 in our talk.c file. that "expected ';' (semicolon) before 'printf'".
'''ls -lrt'''
+
| By typing '''ls -lrt''', we can see that '''myoutput''' is the last file to be created.  
+
 
+
 
|-
 
|-
| '''Type:'''
+
| 09:51
 
+
|Come back to our program.
'''./myoutput'''
+
| To execute the program,
+
 
+
type '''./myoutput''' (dot slash "myoutput")
+
 
+
press '''Enter '''
+
 
+
Here the output is displayed as: "Talk To a Teacher".  
+
  
 
|-
 
|-
|  
+
| 09:54
| As I said before, return is the last statement to be executed.
+
|As I said before, semicolon acts as a statement terminator.
 
+
Thus after the return statement nothing will be executed.
+
 
+
Let us try it out.
+
 
+
Come back to our program.  
+
  
 
|-
 
|-
|  
+
| 09:58
| After the return statement let us include one more printf statment.  
+
|So it will search for it at the end of the line.5 and at the beginning of the line.6.
 
+
Give space here.
+
 
+
'''printf''' opening bracket closing bracket '''()'''
+
 
+
inside the brackets, within double quotes,
+
 
+
type: “'''Welcome”''' backslash '''n'''
+
 
+
At the end type a semicolon '''<nowiki>;</nowiki>'''
+
  
 
|-
 
|-
|  
+
| 10:06
| Click on '''Save'''
+
| This is line 6.
 
+
Let us compile and execute.
+
 
+
Come back to our terminal.
+
 
+
You can recall the previously entered command by using up arrow key.
+
 
+
That is what I did now.
+
 
+
We see that the second statement '''Welcome''' is not executed.  
+
  
 
|-
 
|-
|  
+
| 10:09
| Now come back to our program.
+
| This is the last place where you can put the '''semicolon'''.
 
+
Let us write the Welcome statement above the return statement.  
+
  
 
|-
 
|-
|  
+
| 10:12
| Click on Save
+
|Recall that the compiler also gives the error message on line 6.
 
+
Let us compile and execute.  
+
  
 
|-
 
|-
|  
+
| 10:18
| We see that the second printf statement Welcome has also been executed.  
+
| Let us try what happens if we put the semicolon here.
  
 
|-
 
|-
| Errors
+
| 10:23
 
+
| Click on '''Save''' .
Type:  
+
 
+
<nowiki><stdioh> </nowiki>
+
| Now,let us see the common errors which we can come across
+
 
+
Come back to our program.
+
 
+
Now suppose here I will miss the dot ''''.'''' in
+
 
+
'''<nowiki><stdio.h></nowiki>'''
+
 
+
Click on '''Save '''
+
 
+
Let us compile and execute.
+
 
+
we see that there is an fatal error at line no.2 in our '''talk.c''' file.
+
 
+
The compiler cannot find a header file with the name “'''stdioh”'''
+
 
+
Hence it is giving an error "No such file or directory" and the compilation is terminated.  
+
  
 
|-
 
|-
|  
+
| 10:26
| Let us now fix the error.  
+
|Let us Compile and execute.
 
+
Come back to our program.
+
 
+
Reinsert the “'''.”'''
+
  
 
|-
 
|-
|  
+
| 10:30
| '''Click on Save'''
+
|Yes, it is working.  
 
+
Let us compile and execute.
+
 
+
Yes it is working.  
+
  
 
|-
 
|-
| Error 2
+
| 10:32
 
+
|Now come back to our program. Let us type the ''semicolon'' here at the end of this line
Type:  
+
 
+
printf("Hello World \n")
+
 
+
 
+
 
+
| I will show you another common error.
+
 
+
Let us switch back to the program.  
+
 
+
Now, suppose here I will miss the semicolon at the end of the line.
+
 
+
Click on '''Save'''
+
 
+
Let us compile and execute.
+
 
+
we see that there is an error at line no.6 in our '''talk.c''' file.
+
 
+
That expected semicolon before '''printf.'''
+
 
+
Come back to our program.
+
 
+
As I said before, semicolon acts as a statement terminator.
+
 
+
So it will search for it at the end of the line 5 and at the begning of the line 6.
+
 
+
This is line 6.
+
 
+
This is the last place where you can put the semicolon.
+
 
+
Recall that compiler also gave the error message on line 6.
+
  
 
|-
 
|-
|  
+
| 10:40
| Let us try what happens if you put the semicolon here.
+
| as it is a conventional practice to type  the ''semicolon'' at the end of the line.
 
+
Click on '''Save.'''
+
 
+
 
|-
 
|-
|  
+
| 10:46
| Let us compile and execute.
+
| Now click on '''Save.'''
 
+
Yes it is working.  
+
  
 
|-
 
|-
|  
+
| 10:49
| Now come back to our program.
+
| Let us Compile and execute. Yes it is working.
 
+
Let us type the semicolon here at the end of this line.  
+
 
+
As it is the conventional practice to type the semicolon at the end of the line.  
+
  
 
|-
 
|-
|  
+
| 10:54
| Now click on '''Save.'''
+
|Now let us go back to our slides.  
 
+
Let us compile and execute.
+
 
+
Yes it is working.
+
 
+
Now let us move back to our slides.  
+
  
 
|-
 
|-
| Slide 7
+
|10:57
 
+
Assignment
+
 
| As an Assignment  
 
| As an Assignment  
  
Write a program to print '''"Welcome to the World of C" '''
+
|-
 +
| 10:59
 +
|Write a program to print "Welcome to the World of C"
  
See what happens if \n is not included in the '''printf''' statement.  
+
|-
 +
| 11:02
 +
|See what happens if '''“\n”''' is not included in the '''printf''' statement.
  
 
|-
 
|-
|  
+
| 11:08
| This brings us to the end of this tutorial  
+
| This brings us to the end of this tutorial.
  
 
|-
 
|-
| Slide 8
+
| 11:12
 +
| Watch the video available at the link shown below,
  
About the Spoken Tutorial Project
+
|-
 +
| 11:15
 +
|It summarises the Spoken Tutorial project.
  
 
+
|-
 
+
| 11:18
| Watch the video available at the link shown below
+
|If you do not have good bandwidth, you can download and watch it.
 
+
[http://spoken-tutorial.org/ http://spoken-tutorial.org] /What\_is\_a\_Spoken\_Tutorial
+
 
+
It summarises the Spoken Tutorial project
+
 
+
If you do not have good bandwidth, you can download and watch it.  
+
  
 
|-
 
|-
| Slide Number 9
+
| 11:22
 
+
Spoken Tutorial Workshops
+
 
+
 
+
 
+
 
| The Spoken Tutorial Project Team  
 
| The Spoken Tutorial Project Team  
  
Conducts workshops using spoken tutorials  
+
|-
 +
| 11:24
 +
|Conducts workshops using spoken tutorials.
  
Gives certificates to those who pass an online test  
+
|-
 +
| 11:28
 +
|Gives certificates to those who pass an online test.
  
For more details,please write to contact@spoken-tutorial.org  
+
|-
 +
| 11:31
 +
|For more details, please write to contact [at] spoken hyphen tutorial dot org
  
 
|-
 
|-
| Slide Number 10
+
|11:38
 +
| Spoken Tutorial Project is a part of the Talk to a Teacher project.
  
Acknowledgement
+
|-
| 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.
  
It is supported by the National Mission on Education through ICT, MHRD, Government of India
+
|-
 
+
| 11:47
More information on this Mission is available at the link shown below: http://spoken-tutorial.org\NMEICT-Intro
+
|More information on this Mission is available at   the link shown below:  
  
 
|-
 
|-
| Slide Number 11  
+
| 11:51
 
+
| This is Jean Bonnet from IIT Bombay signing off. Thank you for watching.  
About the contributor
+
| This is Ashwini Patil from IIT Bombay signing off  
+
 
+
Thank You for joining.  
+
  
 
|}
 
|}

Revision as of 14:49, 10 April 2015

Time Narration
00:01 Welcome to the spoken tutorial on First C program.
00:05 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:18 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:38 Some editors are vim and gedit.
00:42 I will use 'gedit' in this tutorial.
00:45 For relevant tutorials please visit our website which is as shown. http://spoken-tutorial.org
00:51 Let me tell you how to write a C program through an example.
00:55 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 “&” sign.
01:20 We use ampersand (&) to free up the prompt.
01:24 Please note that all the C files will have extension “.c”(dot C).
01:30 Now Press Enter,
01:32 the text editor has opened.
01:36 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.
02:41 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:18 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:29 Every program should have one main() function.
03:33 There should NOT be more than one main function.
03:36 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.
03:55 Press Enter.
03:58 Type opening curly bracket “{”.
04:00 The opening curly bracket marks the beginning of the function main.
04:04 Then Type closing curly bracket “}”.
04:08 The closing curly bracket indicates the end of the function main.
04:13 Now Inside the bracket press Enter twice,
04:16 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:25 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 the double quotes.
04:43 Anything within the double quotes, in the printf statement, will be printed on the terminal.
04:50 Type“Talk To a Teacher backslash n”.
04:59 Backslash n “\n” signifies newline.
05:03 As a result, after execution of the printf function, the cursor moves to the new line.
05:10 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:27 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:55 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:05 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 space (hypen) -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,
07:22 give space here, type printf opening bracket, closing bracket.
07:27 Inside the bracket within the double quotes type Welcome backslash 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 statement 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:06 Click on Save.
08:09 Let us compile and execute.
08:15 We see that the second printf statement welcome has also 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 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:25 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.
09:54 As I said before, semicolon acts as a statement terminator.
09:58 So it will search for it at the end of the line.5 and at the beginning of the line.6.
10:06 This is line 6.
10:09 This is the last place where you can put the semicolon.
10:12 Recall that the compiler also gives the error message on line 6.
10:18 Let us try what happens if we put the semicolon here.
10:23 Click on Save .
10:26 Let us Compile and execute.
10:30 Yes, it is working.
10:32 Now come back to our program. Let us type the semicolon here at the end of this line
10:40 as it is a conventional practice to type the semicolon at the end of the line.
10:46 Now click on Save.
10:49 Let us Compile and execute. Yes it is working.
10:54 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:02 See what happens if “\n” is not included in the printf statement.
11:08 This brings us to the end of this tutorial.
11:12 Watch the video available at the link shown below,
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:31 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:47 More information on this Mission is available at the link shown below:
11:51 This is Jean Bonnet from IIT Bombay signing off. Thank you for watching.

Contributors and Content Editors

Nancyvarkey