Anda di halaman 1dari 9

Lab-01

CF-Zero Semester
Instructions
1.
2.
3.
4.
5.

Use Borland C
Create a folder CF_Labs
Create new folder for each lab e.g. create folder lab-01 and save all the files in that folder for this lab
Create new file for each program
For any help contact lab staff or faculty member available in the lab

Programming Tool
For programming in C we need
1. An editor to create files to write code
2. A Compiler that will check our C code and will translate the code to machine code
3. A program that will indicate errors in our program
4. A program that will let us check the output of our program , in this lab we are using Borland C IDE, IDE stands for
Integrated Development Environment, it is a program that provides us all the tools necessary for developing code
5.

Can we use a different IDE?


There are many IDEs available for programming in C/C++, Borland C, C Builder, MS Visual Studio, Turbo C etc.

TASK-01

Borland C, Write Your First Program


Steps to develop program in Borland C
1.

Launch Borland C++ IDE

Whether from the Start Program or any shortcut menu on your desktop or from c:\program files, the following IDE will be
launched

CF-Lab-01, prepared by Nauman Shamim

2.

Create File

Next, click File menu, select New and select Text Edit , this will allow you to create a new text file, save this file with c or cpp
extension.

3.

Write first program

Now you are ready to write your first program. Copy and paste the following code into the file, which appears in the center of
the screen.
#include<stdio.h>
#include<conio.h>
void main()
{
printf("hello to winter");
getch();
}
Step 4: Compile and run the code
To compile the program press F9 or click Debug menu, select compile
To execute the program, click the Debug menu, select Run. Or press CRT+F9 key

Declaring variables

The general format to declare any variable in c is as follows


type

identifier value ;

Example

int

x=

float

y=

100 ;

Example-1 simple declaration;

#include<stdio.h>
#include<conio.h>
int main ()
{
/* variable definition: */
int
a, b;
int
c;
float f;

CF-Lab-01, prepared by Nauman Shamim

2.5

/* actual initialization */
a = 10;
b = 20;
c = a + b;
printf("value of c : %d \n", c);
f = 70.0/3.0;
printf("value of f : %f \n", f);
getch();
return 0;
}
Example-2 Declaring and Initializing

#include<stdio.h>
#include<conio.h>
int main ()
{
int a=10,b=20;
printf(Values of a and b are \n);
printf(a=%d,b=%d \n,a,b);
//Re-Initializing
a=30;
b=40;
printf(New values of a and b are \n);
printf(a=%d,b=%d\n,a,b);
return 0;
getch();}
Task: Try to write the output of the following program, note down your answer, execute the program and compare
your answer with program output
#include<stdio.h>
#include<conio.h>
int main ()
{
int a=10,b=20,c=30;
printf(Value of A = %d, value of B=%d,b,a);
printf(\nB+C=%d,a+b);
a=b;
b=c;
c=a+b;

CF-Lab-01, prepared by Nauman Shamim

printf(\nThe values of a,b and c are\n a= %d \nb=%d \nc=%d,a,b,c);


getch();
return 0;}

TASK-03

SOLVING PROBLEMS

In the following section a number of example problems will be introduced along with their solution, read the problems and try
to understand the solution. If you can understand the concept try to solve the problems given in this section
Example-3 Sample Problem
Write a program to calculate the area of rectangle whose length and widths are 20 and 30 respectively
Solution

#include<stdio.h>
#include<conio.h>
int main()
{
int length,width,area;
length=20;
width=30;
area=length*width;
printf(Area of rectangle is =%d,area);
getch();
return 0;
}
Extension Problem
Allow the user to specify the length and width of the rectangle.
#include<stdio.h>
#include<conio.h>
int main()
{
int length,width,area;
printf(Enter Length = );
scanf(%d,&length);
printf(Enter Width = );
scanf(%d,&width);
area=length*width;
printf(Area of rectangle is =%d,area);
getch();
return 0;

CF-Lab-01, prepared by Nauman Shamim

Solve Yourself
Problem-01
Write a program to solve the following problem.
a) What will be sale price of an item after 2 years if its value decreases 10% each year.
b) What will the 10% of 20% of 45678

TASK-04

USING SPECIAL CHARACTERS


There are special characters in C that modifies the output of printf, some them are given below, each special
character starts with \
\n

Prints new line

\t

Prints a tab

\a

Produces a beep

\\

Prints backslash

Prints double quote

Prints a single quote

TASK-05

PRACTICE FOLLOWING PROGRAM


Write The Following Code, Execute It And Compare The Outputs
Program-01 (understanding new line)

#include <stdio.h>
#include <conio.h>
main(void)
{
printf(This is a single sentence );
printf(and will not be printed on two lines );
getch();
}

Learning: printf itself do not print a new line , programmer has to add suitable command for new lines
Program-01A

#include <stdio.h>
#include <conio.h>
void main(void)
{
printf(This is not a \n single line );
getch();
}

CF-Lab-01, prepared by Nauman Shamim

Q: Why the line is printed on two different lines ?

Program -02 (Special Character Tab \t)

#include <stdio.h>
#include <conio.h>
main(void)
{
printf(Characters in capitals will be printed far from each Other\n A\t\t\t\tB\t\t\tC\t\t\tD);
getch();
}

Activity- 01
Use printf , new line character \n, tab character \t and * (stars, use shift+8 or num-pad to type star character ) to Produce
The Following Outputs
Output 1
Name
: (Your Name)
Roll No : (Your Roll No)
Session : (Your Session)
Press any key to terminate

Output 2

*******************
*
*
*
*
*
*
*
*
*******************

TASK-06

PRACTICE FOLLOWING PROGRAM


Format Specifies

Format specifiers are used in printf function for printing numbers and characters, A format specifier acts like a place holder, it
reserves a place in a string for numbers and characters, for more understanding see the example below
Type of the following example programs and see the output, by comparing the results of example understand the use of
format specifiers.
Example-1

#include <stdio.h>
#include <conio.h>
void main(void)
{
printf(There are %d number of days in a week,7);
getch();

CF-Lab-01, prepared by Nauman Shamim

The %d is a format specifier or place holder for number 7, this is useful when we have to print the values stored in variables,
see the example below

Example-2

#include <stdio.h>
#include <conio.h>
main(void)
{
int num_days=365;
printf(There are %d number of days in a year, num_days);
getch();
}
Example-2B

#include <stdio.h>
#include <conio.h>
main(void)
{
int num_days=365;
printf(There are number of days in a year %d , num_days);
getch();
}
Q: What is the difference between the output o f example 2 and 2b? Do you know the reason?
Example-3 (Using Format Specifier)

#include <stdio.h>
#include <conio.h>
void main(void)
{
int random_num=10;
printf(The value of random variable is = %d \n , random_num);
random_num= 19;
printf(The new value of random variable is = %d , random_num);
getch();
}

CF-Lab-01, prepared by Nauman Shamim

Followings are basic format specifiers in C

%d
%c
%f
%s

for
for
for
for

int (integers)
char (characters)
float (fractions)
string (sequence of characters)

Activity-02
Try to Predict The Output Of The Following Programs And Try To Answer The Question If Any.
Program-03

#include <stdio.h>
#include <conio.h>
void main(void)
{
int a,b,c;
a=10;
b=40;
c=a+b;
printf(The sum of a and b = %d, c)
getch();
}
Program-04 (printing multiple values)

#include <stdio.h>
#include <conio.h>
main(void)
{
int a,b,c;
a=10;
b=20;
c=30;
printf( A = %d,a);
printf( B = %d,b);
printf( C = %d ,c);
getch();
}
Q: Why all values are printed on the same line?
Alternate way for program 04

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

CF-Lab-01, prepared by Nauman Shamim

b=20;
c=30;
printf(A = %d \n B = %d \n C = %d , a,b,c);
getch();

Q: Why values are printed on different


lines ?

Activity-03
Write code to develop the following programs
1) Write a program that displays your bio-data, the output of the program should be like

-----------------------------------------------------------------------------------------------Name :
Email:
City:
Education:
Degree/Certificate
Matriculation
Intermediate
Graduation
Masters

City / School / College / Univeristy


ABC School etc
ABC College etc
ABC University etc
ABC University etc

Work Experience:
(Few lines of your choice)
References
(Few lines of your choice)
------------------------------------------------------------------------------------------------

CF-Lab-01, prepared by Nauman Shamim

Anda mungkin juga menyukai