Advance C Project Chapter 1 (Introduction To CodeBlocks)
Advance C Project Chapter 1 (Introduction To CodeBlocks)
Advance C Project Chapter 1 (Introduction To CodeBlocks)
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.
GCC,
MinGW,
Digital Mars,
Microsoft Visual C++,
Borland C++,
LLVM Clang,
Watcom,
LCC and
the Intel C++ compiler.
What Are GCC
And MinGW ?
• 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 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
• It is a 3 step process:
– 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 NewFile
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
#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 NewFile
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
– 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
• 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