0% found this document useful (0 votes)
586 views

CIT 113 Practicals

The document provides steps for installing the CodeBlocks IDE and compiler on Windows: 1. Download the binary release of CodeBlocks from the website and select the installer with GCC for Windows. 2. Run the installer and accept the default options to install GCC. 3. Open the installed CodeBlocks IDE to begin programming.

Uploaded by

Asad Altaf
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
586 views

CIT 113 Practicals

The document provides steps for installing the CodeBlocks IDE and compiler on Windows: 1. Download the binary release of CodeBlocks from the website and select the installer with GCC for Windows. 2. Run the installer and accept the default options to install GCC. 3. Open the installed CodeBlocks IDE to begin programming.

Uploaded by

Asad Altaf
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Practical No 1

INSTALLATION AND STEPS OF C


COMPILER

Step 1 :

Download Binary release Go to


http://www.codeblocks.org/downloads and click
Binary Release.
Step 2 :
Select the installer with GCC for Windows
compiler
Step 3 :
Start installation
Run the downloaded installer and accept the default
options to install GCC Windows file.
Step 4 :
Accept the terms and conditions
Step 5 : Keep default component selection
Keep the component selection default and click Next.

Step 6 : Locate the installation path


You may change the installation folder and click Next.

Step 7: Find and double-click on the CodeBlocks


icon
To launch CodeBlocks double click on the icon.

Step 8: Let it detect the compiler itself


It will detect the GCC compiler for Windows
automatically, set it as default.
Step 9: Open the IDE and start using
You will see the IDE Home screen.
Practical No 2
Navigation Of Integrated Development
Environment (IDE)
Integrated development environments (IDE) are
applications that facilitates the development of other
applications. Designed to encompass all programming
tasks in one application, one of the main benefits of an
IDE is that they offer a central interface with all the
tools a developer needs, including:
 Code editor: Designed for writing and editing
source code, these editors are distinguished from
text editors because work to either simplify or
enhance the process of writing and editing of code
for developers
 Compiler: Compilers transform source code that is
written in a human readable/writable language in a
form that computers can execute.
 Debugger: Debuggers are used during testing and
can help developers debug their application
programs.
 Build automation tools: These can help automate
developer tasks that are more common to save
time.
In addition, some IDEs may also include:
 Class browser: Used to study and reference
properties of an object-oriented class hierarchy.
 Object browser: Used to inspect objects
instantiated in a running application program.
 Class hierarchy diagram: Allows developers to
visualize the structure of object-oriented
programming code.
The IDE may be a stand-alone application, though it
might also be included as part of one or more
compatible applications.
Practical No 3

Use Of Program Editor


You can create user-defined
functions or programs by typing
definition statements on the
Calculator entry line or by using the
Program Editor. The Program Editor
offers some advantages, and it is
covered in this section. For more
information, see Calculator. 

• The editor has programming templates an


and programs using correct syntax.

• The editor lets you enter multiple-line pro


special key sequence to add each line.

• You can easily create private and public li


programs). For more information, see Lib

Launching the Program Editor


▶ To add a new Program Editor page in the

From the toolbar, click Insert >


Program Editor > New.
Handheld: Press ~ and
select Insert > Program Editor
> New.
Note: The editor is also accessible
from the Functions &
Programs menu of a Calculator
page.
À Program Editor menu – This menu is available
anytime you are in the Program Editor work
area using the Normal view mode.

Á Program Editor work area

 Status line shows line-number information and


the name of the function or program being
edited. An asterisk (*) indicates that this
function is “dirty,” which means that it has
changed since the last time its syntax has been
checked and it has been stored.
Practical No 4
Flow of C Program
The C program follows many steps in execution.
To understand the flow of C program well, let us
see a simple program first.
File: simple.c
1.#include <stdio.h>    
2.int main(){    
3.printf("Hello C Language");    
4.return 0;   
5.}  
Execution Flow
Let's try to understand the flow of above program
by the figure given below.
1) C program (source code) is sent to preprocessor
first. The preprocessor is responsible to convert
preprocessor directives into their respective values.
The preprocessor generates an expanded source
code.
Practical No 5

C++ Basic Input/Output


In this tutorial, we will learn to use the cin
object to take input from the user, and the cout
object to display output to the user with the
help of examples.
C++ Output
In C++, cout sends formatted output to
standard output devices, such as the screen.
We use the cout object along with
the << operator for displaying output.

Example 1: String Output

#include <iostream>
using namespace std;

int main() {
// prints the string enclosed in
double quotes
cout << "This is C++
Programming";
return 0;
}

Output
This is C++ Programming
How does this program work?
 We first include the iostream header file

that allows us to display output.


 The cout object is defined inside

the std namespace. To use
the stdnamespace, we used the using
namespace std; statement.
 Every C++ program starts with

the main() function. The code execution


begins from the start of
the main() function.
 cout is an object that prints the string
inside quotation marks " ". It is followed
by the << operator.
 return 0; is the "exit status" of

the main() function. The program ends


with this statement, however, this
statement is not mandatory.
Note: If we don't include the using
namespace std; statement, we need to
use std::cout instead of cout.
This is the preferred method as using
the std namespace can create potential
problems.
However, we have used the std namespace in
our tutorials in order to make the codes more
readable.

#include <iostream>

int main() {
// prints the string enclosed in
double quotes
std::cout << "This is C++
Programming";
return 0;
}

Example 2: Numbers and Characters


Output
To print the numbers and character variables,
we use the same cout object but without using
quotation marks.

#include <iostream>
using namespace std;

int main() {
int num1 = 70;
double num2 = 256.783;
char ch = 'A';
cout << num1 << endl; // print
integer
cout << num2 << endl; // print
double
cout << "character: " << ch <<
endl; // print char
return 0;
}
Output
70
256.783
character: A
Notes:
 The endl manipulator is used to insert a

new line. That's why each output is


displayed in a new line.
 The << operator can be used more than

once if we want to print different


variables, strings and so on in a single
statement. For example:
cout << "character: " << ch << endl;
C++ Input
In C++, cin takes formatted input from
standard input devices such as the keyboard.
We use the cin object along with
the >> operator for taking input.

Example 3: Integer Input/Output

#include <iostream>
using namespace std;

int main() {
int num;
cout << "Enter an integer: ";
cin >> num; // Taking input
cout << "The number is: " << num;
return 0;
}
Output
Enter an integer: 70
The number is: 70
In the program, we used

cin >> num;


to take input from the user. The input is stored
in the variable num. We use the >>operator
with cin to take input.
Note: If we don't include the using
namespace std; statement, we need to
use std::cin instead of cin.

C++ Taking Multiple Inputs

#include <iostream>
using namespace std;

int main() {
char a;
int num;
cout << "Enter a character and an
integer: ";
cin >> a >> num;

cout << "Character: " << a <<


endl;
cout << "Number: " << num;

return 0;
}
Output
Enter a character and an integer: F
23
Character: F
Number: 23
Practical No 6
C Arithmetic Operators
An arithmetic operator performs mathematical
operations such as addition, subtraction,
multiplication, division etc on numerical values
(constants and variables).

Operator Meaning of Operator

+ addition or unary plus

- subtraction or unary minus

* multiplication

/ division

% remainder after division (modulo division)


Example 1: Arithmetic Operators

// Working of arithmetic operators


#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b
= %d \n",c);
return 0;
}
Output
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
The operators +, - and * computes addition,
subtraction, and multiplication respectively as you
might have expected.
In normal calculation, 9/4 = 2.25. However, the
output is 2 in the program.
It is because both the variables a and b are
integers. Hence, the output is also an integer. The
compiler neglects the term after the decimal point
and shows answer 2instead of 2.25.
The modulo operator % computes the remainder.
When a=9 is divided by b=4, the remainder is 1.
The % operator can only be used with integers.
Suppose a = 5.0, b = 2.0, c = 5 and d = 2.
Then in C programming,
// Either one of the operands is a
floating-point number
a/b = 2.5
a/d = 2.5
c/b = 2.5

// Both operands are integers


c/d = 2

C Increment and Decrement Operators


C programming has two operators increment +
+ and decrement -- to change the value of an
operand (constant or variable) by 1.
Increment ++ increases the value by 1 whereas
decrement -- decreases the value by 1. These two
operators are unary operators, meaning they only
operate on a single operand.
Example 2: Increment and Decrement
Operators

// Working of increment and decrement


operators
#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;

printf("++a = %d \n", ++a);


printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);

return 0;
}
Output
++a = 11
--b = 99
++c = 11.500000
--d = 99.500000
Here, the operators ++ and -- are used as prefixes.
These two operators can also be used as postfixes
like a++ and a--. Visit this page to learn more
about how increment and decrement operators
work when used as postfix.

C Assignment Operators
An assignment operator is used for assigning a
value to a variable. The most common assignment
operator is =
Operator Example Same as

= a=b a=b

+= a += b a = a+b

-= a -= b a = a-b

*= a *= b a = a*b

/= a /= b a = a/b

%= a %= b a = a%b
Example 3: Assignment Operators

// Working of assignment operators


#include <stdio.h>
int main()
{
int a = 5, c;

c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);

return 0;
}
Output
c = 5
c = 10
c = 5
c = 25
c = 5
c = 0

C Relational Operators
A relational operator checks the relationship
between two operands. If the relation is true, it
returns 1; if the relation is false, it returns value 0.
Relational operators are used in decision
making and loops.
Meaning of
Operator Example
Operator

5 == 3 is evaluated
== Equal to
to 0

5 > 3 is evaluated
> Greater than
to 1

5 < 3 is evaluated
< Less than
to 0

!= Not equal to 5 != 3 is evaluated


Meaning of
Operator Example
Operator

to 1

Greater than or 5 >= 3 is evaluated


>=
equal to to 1

Less than or equal 5 <= 3 is evaluated


<=
to to 0
Example 4: Relational Operators

// Working of relational operators


#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;

printf("%d == %d is %d \n", a, b, a
== b);
printf("%d == %d is %d \n", a, c, a
== c);
printf("%d > %d is %d \n", a, b, a >
b);
printf("%d > %d is %d \n", a, c, a >
c);
printf("%d < %d is %d \n", a, b, a <
b);
printf("%d < %d is %d \n", a, c, a <
c);
printf("%d != %d is %d \n", a, b, a !
= b);
printf("%d != %d is %d \n", a, c, a !
= c);
printf("%d >= %d is %d \n", a, b, a
>= b);
printf("%d >= %d is %d \n", a, c, a
>= c);
printf("%d <= %d is %d \n", a, b, a
<= b);
printf("%d <= %d is %d \n", a, c, a
<= c);

return 0;
}
Output
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1

C Logical Operators
An expression containing logical operator returns
either 0 or 1 depending upon whether expression
results true or false. Logical operators are
commonly used in decision making in C
programming.
Operator Meaning Example

If c = 5 and d = 2 then,
Logical AND.
expression ((c==5)
&& True only if all
&& (d>5)) equals to
operands are true
0.

|| Logical OR. True If c = 5 and d = 2 then,


only if either one expression ((c==5)
operand is true || (d>5)) equals to
Operator Meaning Example

1.

Logical NOT. If c = 5 then,


! True only if the expression !(c==5)
operand is 0 equals to 0.
Example 5: Logical Operators

// Working of logical operators

#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;

result = (a == b) && (c > b);


printf("(a == b) && (c > b) is %d \
n", result);

result = (a == b) && (c < b);


printf("(a == b) && (c < b) is %d \
n", result);

result = (a == b) || (c < b);


printf("(a == b) || (c < b) is %d \
n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \
n", result);

result = !(a != b);


printf("!(a != b) is %d \n", result);

result = !(a == b);


printf("!(a == b) is %d \n", result);

return 0;
}
Output
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
Explanation of logical operator program
 (a == b) && (c > 5) evaluates to 1 because

both operands (a == b) and (c > b)is 1


(true).
 (a == b) && (c < b) evaluates to 0 because

operand (c < b) is 0 (false).


 (a == b) || (c < b) evaluates to 1
because (a = b) is 1 (true).
 (a != b) || (c < b) evaluates to 0 because
both operand (a != b) and (c < b)are 0
(false).
 !(a != b) evaluates to 1 because operand (a
!= b) is 0 (false). Hence, !(a != b) is 1 (true).
 !(a == b) evaluates to 0 because (a ==
b) is 1 (true). Hence, !(a == b) is 0 (false).

C Bitwise Operators
During computation, mathematical operations like:
addition, subtraction, multiplication, division, etc
are converted to bit-level which makes processing
faster and saves power.
Bitwise operators are used in C programming to
perform bit-level operations.

Operators Meaning of operators

& Bitwise AND

| Bitwise OR
Operators Meaning of operators

^ Bitwise exclusive OR

~ Bitwise complement

<< Shift left

>> Shift right

Other Operators

Comma Operator
Comma operators are used to link related
expressions together. For example:

int a, c = 5, d;

The sizeof operator


The sizeof is a unary operator that returns the
size of data (constants, variables, array, structure,
etc).
Example 6: sizeof Operator

#include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\
n",sizeof(a));
printf("Size of float=%lu bytes\
n",sizeof(b));
printf("Size of double=%lu bytes\
n",sizeof(c));
printf("Size of char=%lu byte\
n",sizeof(d));

return 0;
}
Output
Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte

You might also like