Advance C Project Chapter 1 (Introduction To CodeBlocks)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 71

C PROJECT BATCH

CHAPTER 1
CODE BLOCKS BASICS
Topics Covered
• What Is Code Blocks ?
• Why Not To Use Turbo C++ ?
• Differences With Turbo C++
• Configuring Code Blocks
• Running A C Program In Code Blocks
• Creating Our Own Header Files
• Adding Support For “conio.h” In CodeBlocks
What Is Code Blocks ?
• Code::Blocks is a free, open-source cross-platform IDE
(not a compiler) which was designed to support C
and C++ languages.

• However now it also supports other languages


like Fortran and D.
Which Compiler
Code::Blocks Uses ?
Code::Blocks supports multiple compilers like:

GCC,
MinGW,
Digital Mars,
Microsoft Visual C++,
Borland C++,
LLVM Clang,
Watcom,
LCC and
the Intel C++ compiler.
What Are GCC
And MinGW ?

• GCC stands for GNU Compiler Collection, and is a collection


of programming compilers including C, C++, Objective-C,
Fortran, Java, and Ada.

• It’s latest version is GCC 8.2 lauched on 26th –Jul-2018

• MinGW stands for Minimalist GNU for Windows.

• It is GCC compiler for Windows platform


About Turbo C++

• Turbo C++ was released on somewhere between


1990-1992 (almost 27 years ago).

• Back then it was a very good IDE and it was used


a lot to code C and C++.

• But things have changed today in 2019


What Is Wrong
With Turbo C++ ?
• OutDated Compiler :
– Turbo C++ uses a old 16-bit compiler which is outdated
and is not in compliance with the latest standards,
while the latest compilers are 32-bit or 64-bit and comply
to the standards.

– Thus many applications built using Turbo C++ are not


compatible with our 64 bit OS.

• No AutoComplete :
– Another problem is that Turbo C++ does not support
autocomplete features which is really helpful on longer
projects and in minimizing errors related to syntax.

– In Turbo C++ we will have to write every bit of code and


we will have to close every single function.
So What Are
The Alternates ?
• Today we have numerous IDE’s available
which can be used to migrate from Turbo C++ .

• Some popular of them are:


– Code Blocks
– Dev C++
– Code Lite
– C-Free
– Microsoft Visual Studio
– Apple’s X-Code

• We will be using Code Blocks in this project batch


Turbo C++
V/s Code Blocks
1. No “conio.h” Present

– There's no header file named as 'conio.h' in


Code Blocks. Including such header file would
result in compilation error.

– Since conio.h header file doesn't exist, the


latest standard does not support functions
like clrscr(), getch(), getche(), gotoxy() etc.
Turbo C++
V/s Code Blocks
• So how do we clear the screen then?

• We don't need to. Because whenever we will run the


application , Code Blocks automatically launches a
fresh console window to display the output
Turbo C++
V/s Code Blocks
• What if we want to clear the screen during execution of
our application ?

• If we want to clear the screen during runtime then we have


to use the function system( ) available in the header file
<stdlib.h>

• The statement will be:


system(“cls”);
Turbo C++
V/s Code Blocks
• What about getch( )?

• As mentioned , getch( ) is not supported . However we have


2 alternates to it :

– We can use system(“pause”)

– We can use the function _getch( )


Turbo C++
V/s Code Blocks
2. No “dos.h” Present

– There's no header file named as ‘dos.h' in Code Blocks.


And so we cannot call the functions like delay( )

– But we have an alternate to this also, which the


function Sleep() available in the header file
“windows.h”

– The statement will be:


• Sleep(1000);
Turbo C++
V/s Code Blocks
3. The main( ) function has return type int

– The main( ) function has the default return type of int

– Just like C++ , here also we write return 0 as our last


statement in main( ) which indicates successful
termination

– Although we can make it as void , but we will get a


warning during compilation :

• warning: return type of 'main' is not 'int'


Data Types
Differences
With TurboC++

In Turbo In GCC
int short int
unsigned int unsigned short int
long int int or long int
unsigned long int unsigned int or unsigned long int
long double (10 B) long double (12 B)
New Data Type:
long long int (8B) and unsigned long
long int(8B)

char , unsigned signed char , float and double are exactly same as Turbo C++
Differences With
Turbo C++
• What will be the output of the following code in Turbo ?

#include <stdio.h>
void main()
{
char ch=‘AB’;
printf(“%c”,ch);
}
Output:
A
Differences With
Turbo C++
• In CodeBlocks however this code will output B?

#include <stdio.h>
int main()
{
char ch=‘AB’;
printf(“%c”,ch);
return 0;
}
Output:
B
Differences With
Turbo C++
• What will be the output of the following code in Turbo ?

#include <stdio.h>
void main()
{
char ch=‘ABC’;
printf(“%c”,ch);
}
Output:
Syntax Error: character constants cannot be more than 2
characters long
Differences With
Turbo C++
• In CodeBlocks however this code will output C?

#include <stdio.h>
int main()
{
char ch=‘ABC’;
printf(“%c”,ch);
return 0;
}
Output:
C
Downloading And
Installing CodeBlocks
• Open http://www.codeblocks.org/downloads/26

Click on
CodeBlocks
17.02 mingw-
setup.exe
Downloading And
Installing CodeBlocks
• Now double click on downloaded installer.
The process is simple hit next couple of times.
• Make sure installation path C:\Program
Files\CodeBlocks (default location).
• Now launch CodeBlocks
Developing First Application
In CodeBlocks
• Open CodeBlocks
• Go to File –> New –> Project..
Developing First Application
In CodeBlocks
• Select Console application and hit on Go.
Developing First Application
In CodeBlocks
• Hit on Next
Developing First Application
In CodeBlocks
• Select C and click on Next
Developing First Application
In CodeBlocks
• Fill in the name of your Project( In CodeBlocks ,
every program is called a Project)
• Also set the folder where you want to save
all your projects. In my case it is D:\C programs
Developing First Application
In CodeBlocks
• Hit on Finish
Developing First Application
In CodeBlocks
• CodeBlocks will automatically generate a file
called main.c shown in left pane under the
project name
• You can now open the main.c file.

This entire
code is
automatically
generated by
CodeBlocks
Compiling And Running
The Program

• To compile the program, click Build → Build

• To run the program, click BuildRun

• Alternatively we can also select Build and Run


icon located in the toolbar on the top.

• The icon has a yellow gear and a green play logo.


Compiling And Running
The Program

• Finally we will get the following output ,


as shown below.
Adding A New File

• A project contains related files such as


source codes, header files, and relevant resources.

• Although a project may contain many source files,


there can only be one main() function
among all the source files.
Adding A New File

• That is, we cannot keep two programs (each having


a main() function) in one project (we will get
the error "multiple definition of 'main'" when
we try to build the project).

• We need to create one project for each program.

• However , there is a solution to this , but first we


will learn how to add a new file/program to our
project
Adding A New File

• Select C/C++ Source from the window that appears


Adding A New File

• Continue through the following dialogs very


much like the original project creation and
select C when prompted for a language.
• On the final page, we will have to mention the
new filename and location. We have given
the name addnos.c
Adding A New File

• Doing this will add a blank file called addnos.c


to the project MyFirstApp
Writing The Code

• Let’s add the code to find sum of 2 integers


inputted by the user:
#include <stdio.h>
int main()
{
int a,b,c;
printf("Enter 2 int:");
scanf("%d %d",&a,&b);
c=a+b;
printf("Nos are %d and %d",a,b);
printf("\nTheir sum is %d",c);
return 0;
}
Writing The Code

• The screenshot of the code is as below


Multiple main( ) Error !

• Now , when we will compile the code , we will


get the error “multiple definition of `main’ ”
Reason And Solution !
• This is because our project contains 2 files
(main.c and addnos.c) and both have the
main( ) function

• To solve this problem , we can remove main.c


from the project .

• The removed files are not deleted and remain


in the folder
Solution

• To remove main.c , right-click on “main.c" ⇒ "remove file


from project".
Error Removed !

• Now we will only have 1 file with main( ) function i.e.


addnos.c so the code will compile and run
Output

• Following is the output shown by the code


Creating Programmer Defined
Header File

• CodeBlocks , makes it very simple to create our own header


files containing declaration of our own functions.

• It is a 3 step process:

– First we create our own header file containing function prototypes

– Then we define these functions in a separate .c file

– Finally we call these functions from our main( ) function


Creating Programmer Defined
Header File

• To do this follow the below mentioned steps:

– Create a new project called MySecondApp

– Follow the previous steps which will add a file called main.c in our
project containing the main( ) function
Creating Programmer Defined
Header File
• Now select File menu NewFile
Creating Programmer Defined
Header File
• From the window that appears , select C/C++ Header and
select Go
Creating Programmer Defined
Header File
• In the next window type the header file name as mymath.h
after selecting the location and click on Finish
• Also select debug and release options
Creating Programmer Defined
Header File
• After this we will get the header file by the name mymath.h
with a default header guard code

• We will also get a new element called Headers added to our


project containing the header file mymath.h

• The screenshot is on next page


Creating Programmer Defined
Header File
Creating Programmer Defined
Header File
• Now we will have to declare the prototype of the functions
we want to keep in this header file.

• Let’s create a function called add( ) in this header file

#ifndef MYMATH_H_INCLUDED
#define MYMATH_H_INCLUDED

void add(int,int);

#endif
Creating Programmer Defined
Header File
Creating Programmer Defined
Header File
• Now after declaring the function prototype , our next job is
to define the function in a separate .c file
• For this , as before select File menu NewFile
Creating Programmer Defined
Header File
• From the window that appears , select C/C++ Source and
select Go
Creating Programmer Defined
Header File
• Select C and click on Next
Creating Programmer Defined
Header File
• In the next window , as before provide the name and
location of the file and hit Finish
• For example ,in this app we have named it mymath.c
Creating Programmer Defined
Header File
• Doing this will add a blank file called mymath.c to the
project MySecondApp
Code Of mymath.c

• Let’s add the definition of the function add( ) in the file


mymath.c

We have to attach the


#include <stdio.h> header file <stdio.h>
because we are calling
void add(int a,int b) the function printf( ) here
{ in this code
int c;
c=a+b;
printf("Nos are %d and %d",a,b);
printf("Their sum is %d",c);
}
Creating Programmer Defined
Header File
Creating Programmer Defined
Header File
• Finally we will have to update the file main.c by doing the
following changes in it:

– Include the header file “mymath.h” along with <stdio.h>

– In function main( ) accept input from the user and call the function
add( ) appropriately
Code Of main.c

#include <stdio.h>
#include "mymath.h"

int main()
{
int a,b;
printf("Enter 2 int");
scanf("%d %d",&a,&b);
add(a,b);
return 0;
}
Code Of main.c

• The screenshot of the code is as below


Output

• Following is the output shown by the code


Adding conio.h
To CodeBlocks
• CodeBlocks/gcc don’t natively support conio.h.

• However , there is an implementation of conio.h for gcc as a


third party library.

• This implementation can be downloaded from


https://sourceforge.net/projects/conio/

• This link will give us a file called conio21.zip


Adding conio.h
To CodeBlocks
• After extracting conio21.zip , we will get 2 files called
conio2.h and conio.c

• Copy and paste conio2.h and conio.c in our Project Folder

• Now open the project in CodeBlocks by selecting


FileOpen and selecting the Project File from the
Workspace Folder
Adding conio.h
To CodeBlocks
• Now right click the Project in Workspace in the left pane
and select Add Files
Adding conio.h
To CodeBlocks
• In the window that opens , select the files conio2.h and
conio.c
Adding conio.h
To CodeBlocks
• Doing this will automatically place conio2.h in Headers and
conio.c in Sources
Adding conio.h
To CodeBlocks
• Now open the source file “main.c” and add the statement
#include “conio2.h”.

• Now we can use all the functions which are present in conio.h in
Turbo C++ like clrscr( ), textcolor( ), gotoxy( ) etc

#include <stdio.h>
#include "conio2.h"
int main(){
gotoxy(40,12);
printf("Hello");
textcolor(GREEN);
gotoxy(40,13);
printf("Bye");
getch();
return 0;
}
Adding conio.h
To CodeBlocks
The Output

You might also like