Lecture+1+Intro+to+C+Programming
Lecture+1+Intro+to+C+Programming
In this very introductory practical session, you will be introduced to the Editor and the Compiler through
which you will have to work the whole semester. The ins and outs of them, what games you can play with
them, the pros and cons of the editor and the compiler and most of all, what types of creations you can
make with them will be presented as you go through this material.
When you write a program, you just write according to some conventions of a programming language.
When you submit that to the software on which you were writing with a view to see your desired output, a
tremendous series of complex tasks is done by your computer by that time. Don’t worry! I am not making
your first class a complex with descriptions of such complex routine works!! At the moment, you need to
know that your computer goes through tremendous pressure when you finish writing your program and
ask it to provide you the output. By the grace of advancement of technology, your compiler does the job
within a second and gives you the output! All these stuffs are accessible with an IDE.
How to install:
Here are some easy steps to install Code::Blocks in your PC;
1. Collect the installable codeblocks-12.11mingw-setup.exe file from any source. You can also
download it from internet
(http://sourceforge.net/projects/codeblocks/files/Binaries/12.11/Windows/codeblocks-
12.11mingw-setup.exe)
2. Double click on the codeblocks-12.11mingw-setup.exe file and installation will start. You will see
the following window
Click next.
3. Then you will find I Agree button. Press it.
4. Proceed with clicking next button.
5. Finally press the Install button and your installation will complete automatically.
6. After completion of the installation press next button and then finish button.
Now Code::Blocks IDE installed in your computer.
You can run the Code::Blocks IDE from your computer by go through start-> all programs->
CodeBlocks-> CodeBlocks.
And you will see
The screenshot here is what you see when you first enter into your IDE.
Page 1 of 4
Figure 1: Main window for Code::Blocks IDE
In the opening face, you can find many useful tabs like File, Edit, Build, Debug, Settings and most
importantly Help.
On File tab, you can find out many useful options like new, open, save file, save file as, save all files and
quit. You need to memorize their shortcut keys for being faster.
Page 2 of 4
Tabs like Build and Debug are mostly useful when you finish your coding and want to see the output.
Debug and Project are more likely tabs that hold options which are useful during programming. We will
see them later on.
In Settings tab, you can find various setting options. Among them now we only focus on Editor.... Here
you can set or change various features of the editor like font color and size of the program codes,
highlighted color etc. Set your own color preferences and font size that suits you.
On Debug tab, you will find many options very useful when you are involving yourself some mastering
level. And you know, Help tab is always providing you the help you need.
Get Started
Ok, now you are familiarized well with the IDE so that you can create your own first program!
Click File->New. What you see, is the editor. Here you write your codes. Now, write the following code-
#include <stdio.h>
void main(){
printf("This is my first C program");
}
Now, click File->Save. You will have to put a name for your piece of code with an extension of .c. Save
the code with appropriate name and remember the location where it has been saved.
Press F9. This will build and run the code. By building the code you can see how many lines of codes
have been checked, how many warnings and errors present with the code. Press any key after taking a
note of them.
And run will take you to the output screen if your code is error/bug free. The procedure should show you-
This is my first C program. Hit any key to go back to your Editor. Now, this piece of code reveals many
important parts of a C program to you.
#include <stdio.h>
causes the file stdio.h (read as standard input output.header) to be read by the C compiler and to be
included with the program. This file contains information about C’s built in function printf().
C programs have functions. Functions can be depicted like as follows. The job of hand is to eat, to shake
a hand, to hold things and so on. So, let us assume that hand() is a function. The job of leg is to walk, to
run, to kick and so on. So, let us assume that leg() is a function. Now, who asks them to do their job? Of
course, brain. We call brain as a function but name it main() as it is the main thing. Every C program must
have a main() function. This is where program execution begins. Void is a keyword that means this
function does not return anything (we will see what is returning and what if a function returns something).
After void main(), there is an opening curly brace {. This marks the beginning of statements that make up
the function.
printf ("This is my first C program"); is a statement. This statement calls the standard library function
named printf() which causes the string This is my first C program to be displayed.
After the end of the segment of code, we put a closing curly brace } saying that this is the end area of the
code.
Class work 1
Display a code that shows I have made my first program in C and now I have created my second
program. Save the code with appropriate name. Can you open a saved program? Try it!
#include <stdio.h>
void main(){
printf("This is ");
printf("another ");
printf("C program");
}
Class work 2
While writing this code, try to use copy and paste option from the Edit tab. Save the program. Run the
program.
Page 3 of 4
Home work 1
After seeing the output of the above program, do you think that statements are executed sequentially
within curly braces? If so, give reasons.
Class work 3
The code you have written in Class work 1-display the same thing using three printf () functions.
Home Work 2
Write down the general form of a C function by taking a look at all the programs used in this material.
Home Work 3
Write down the very first program in the material except make the function name void absurd () {…}
instead of main(). Do you get error message while pressing F9? Give reason(s) behind the error
message.
Home Work 4
#include <stdio.h>
void main(void){
printf("I am playing with C Now!");
}
#include <stdio.h>
void main(){
printf("I am playing with C Now!");
}
#include <stdio.h>
int main(){
printf("I am playing with C Now!");
return 0;
}
Find out the syntactic difference in these three programs. Why are not they different when they provide
the output? You will have to dig into books on C or the Internet to find out the answer.
The Report
The report should contain a proper reflection of what you have learnt from each practical. It also should
contain solutions or arguments if urged to be included as a home work. The report has to be well
structured and well written. The basic structures of any report like Introduction, Body of the Report
(including answering of question, segments of codes, screenshots and so forth), discussion, and
reference (if any) is appropriate for this practical session. The organization of the report and the writing
style of it will play a vital role in case of marking.
The report must be delivered in handwritten format. Printed reports will not be accepted.
You must not copy other people’s work. If you take a reference from the Internet or other source, then
appropriately state them in the end of your report. Otherwise, you will be penalized a resubmission if you
copy any of your classmate’s work (that classmate will be penalized too for this).
Page 4 of 4