Difference between revisions of "C-and-C++/C2/First-C-Program/English"
(Created page with ''''Title of script:''' First C program '''Author:''' Ashwini Patil '''Keywords: C Program, stdio.h, printf(), int main(), Video Tutorial''' {| style="border-spacing:0;" ! Vi…') |
|||
Line 8: | Line 8: | ||
{| style="border-spacing:0;" | {| style="border-spacing:0;" | ||
− | + | | style="border-top:0.002cm solid #000000;border-bottom:0.002cm solid #000000;border-left:0.002cm solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Cue'''</center> | |
− | + | | style="border:0.002cm solid #000000;padding:0.097cm;"| <center>'''Narration'''</center> | |
|- | |- |
Revision as of 11:35, 3 May 2013
Title of script: First C program
Author: Ashwini Patil
Keywords: C Program, stdio.h, printf(), int main(), Video Tutorial
|
|
Slide 1 | Welcome to the spoken tutorial on First C program. |
Slide 2
Learning Objectives |
In this tutorial you will learn
To write a simple C program To compile it To execute it We will also explain some common errors and their solutions. |
Slide 3
System Requirements
|
To record this tutorial, I am using
Ubuntu operating system version 11.10 and gcc Compiler 4.6.1 on Ubuntu |
Slide 4
Prerequisites
|
To practice this tutorial,
You should be familiar with Ubuntu Operating System an Editor Some editors are vim and gedit I will use gedit in this tutorial For relevant tutorials please visit our website: |
Open the terminal
Open the terminal using Ctrl + Alt + T simultaneously
|
Let me tell you how to write a C program through an example
Open the terminal by pressing Ctrl, Alt and T keys simultaneously
|
At the command prompt type:
|
Now let's open the text editor. So, at the prompt, type
“gedit” space “talk” dot “c” space [ampersand] “&” We use ampersand (&) to free up the prompt |
Highlight the .c extension | Please note that all the C files have the extension dot “c” |
Now Press Enter | |
Now the text editor has opened | |
Let us write a program | |
Type double slash “//” space
“My first C program”. | |
Here, double slash is used to comment the line
Comments are used to understand the flow of program It is useful for documentation It gives us information about the program The double slash is called as single line comment. | |
Now press Enter | |
Type
#include <> |
Type hash “#include” space opening angle bracket “<”, closing angle bracket “>” |
It is always a good practice to complete the brackets first, and then start writing inside it | |
Type
#include <stdio.h> |
Inside the bracket, type
“stdio” dot”.” “h” |
Highlight stdio.h | stdio.h is a header file
A program must contain this header file when it uses standard input/output functions. |
Move cursor to the end of the line. | Move the cursor to the end of the line
and press Enter . |
Type
int main() |
Then type “int” space “main” opening bracket and closing bracket “()”. |
Highlight main | Function main is a special function used by C
It denotes that the execution of the program begins from this line The opening bracket and the closing bracket is called as parenthesis. |
Highlight ()
|
Parenthesis followed by main is to tell the user that main is a function |
Highlight int main() | Here the int main function takes no arguments
It returns a value of type integer. |
We will learn about data types in another tutorial. | |
Switch to the slides | Let us switch to the slides to know more about the main function. |
Slide 5 | Every program will have only one main function
There should NOT be more than one main function Otherwise the compiler cannot locate the beginning of the program The empty pair of parentheses indicates that main has no arguments The concept of arguments will be discussed in detail in the upcoming tutorials. |
Switch to the editor | Now let us go back to our program. |
Type
{ |
Type the opening curly brace “{”
The opening curly bracket marks the beginning of the function main. |
Type
} |
Type the closing brace “}”
The closing curly bracket indicates the end of the function main. |
Press Enter twice within the braces
Move the cursor one line up |
Inside the braces
press Enter twice, and then move the cursor one line up |
Give a space | Indentation makes the code easier to read
It also helps to locate errors faster So let us give three space here |
Type
printf() |
Type “printf” and opening bracket and closing bracket “()”
printf is a standard C function to print the output on the terminal |
Highlight the text in the double quotes
|
Here inside the brackets, type
within double quotes, Anything within the double quotes in the printf statement will be pritned on the terminal. Type “Talk To a Teacher backslash n” |
Highlight \n | Backslash n “\n” signifies newline
As a result, after execution of the printf function, the cursor moves to a new line |
Type semicolon (;) | Every C statement must end with a semicolon “;”
Hence, type it at the end of this line. Semicolon acts as a statement terminator. |
Now press Enter
give three space here | |
Type return 0; | Now type “return” space “0” and a semicolon “;” |
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 executable statements We will learn more about the returned values in another tutorial. | |
Click on Save | 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. |
Type
gcc talk.c -o myoutput |
Let us now compile the program
Please open the terminal Type “gcc” space “talk.c” space hyphen “-o” space “myoutput” |
Highlight or point to each component of the statement | gcc is the compiler
talk.c is the filename on compilation gcc creates an executable -o myoutput says that the executable should go to the file myoutput Press Enter. |
Type ls -lrt | We see that the program is compiled
By typing ls -lrt, we can see that myoutput is the last file to be created
|
Type ./myoutput | To execute the program,
type dot slash “./myoutput” and press Enter. |
Here the output is displayed as “Talk To a Teacher”. | |
Type return 0;
|
As I said before, return is the last statement to be executed
Thus after the return statement nothing will be executed Let us try it out. |
Switch to the editor | Let us come back to our program. |
Type printf("Welcome \n"); | After the return statement, let us include one more printf statement
type printf("Welcome \n"); |
Click on Save icon. | Now save it. |
Type gcc talk.c -o myoutput
|
Let us compile and run the program as before
gcc talk.c -o myoutput ./myoutput. |
We see that the second statement 'Welcome' is not executed. | |
Press up arrow key | On the terminal, you can recall the previously entered commands by using up arrow
That is what I did now We see that the statements after return are not executed |
Shift printf("Welcome \n");
above return 0; |
Now come back to our program
Let us write the 'Welcome' statement above the return statement |
Click on Save | Save it |
On the terminal, use up arrow to get “gcc talk.c -o myoutput”
and press Enter. |
Let us compile and execute
We see that the second printf statement also has been executed |
Errors
|
Now let us see the common errors which we can come across
Let's switch to the editor |
Type
|
Suppose we miss the dot “.” in
<stdio.h> I will retain rest of the code as it is |
Click on Save
Switch to the terminal |
save the program
Let us go to the terminal |
On the terminal, use up arrow to get “gcc talk.c -o myoutput”
and press Enter. |
Now compile the program using the command we used before
We see that There is a fatal error in our talk.c file at line no.1 and column no.18 The compiler cannot find a header file with the name “stdioh” hence the compilation is terminated. |
Reinsert the dot “.”
|
Let us fix the error we made on the header file
Reinsert the dot “.”
|
Click on Save | Save it |
On the terminal, use up arrow to get “gcc talk.c -o myoutput” and press Enter. | Let us now execute it
It is working. |
Switch back to gedit
|
I will show you another common error
Let us switch back to the editor
|
Type:
printf("Talk To a Teacher\n") |
Now, suppose that we miss the semicolon at the end of printf() statement. |
Click on Save | Save it
Switch to the terminal. |
Type clear and press Enter | I will clear the terminal by typing
“clear” command. |
On the terminal, use up arrow to get “gcc talk.c -o myoutput” and press Enter. | Now compile the program
using command that we have used before.
|
Point to line no. 5 and column no. 1 on the screen | we see an error message at line no.5 and column no.1 in talk.c file. |
As i said before semicolon acts as a statement terminator
So it will search for it at the end of the line and also at the beginning of the next line. | |
Bring the cursor to line 5 | Now come back to our program
This is line 5
|
Point to the location | This is the last place where you can put the semicolon
Recall that the compiler also gives the error message on line 5. |
Put the semicolon at the beginning of line 6 | Let us try what happens if we put the semicolon in the next line. |
Click on Save | Save it |
On the terminal, use up arrow to get “gcc talk.c -o myoutput” and press Enter. | Compile it and execute it |
Back to the editor
Put semicolon back in original location
|
Let us now fix the error
|
Click on Save | Click on Save |
On the terminal, use up arrow to get “gcc talk.c -o myoutput” and press Enter. | Now execute it
It is working |
Slide 7
Assignment |
As an Assignment
Write a program to print "Welcome to the World of C" See what happens if we do not write “\n” in the printf statement. |
Slide 8
About the Spoken Tutorial Project
|
Watch the video available at the link shown
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
Spoken Tutorial Workshops
|
The Spoken Tutorial Project Team
Conducts workshops using spoken tutorials Gives certificates to those who pass an online test For more details, please write to contact [at] spoken hyphen tutorial dot org |
lide Number 10
|
Spoken Tutorial Project is a part of the Talk to a Teacher project
It is supported by the National Mission on Education through ICT, MHRD, Government of India More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro |
Remain on previous slide
No slide for this part
|
This is Ashwini Patil from IIT Bombay.
Thank you for joining. |