Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Write First Program: Compile F9 Execute or Press CRT+F9

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

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 , however the value part is optional i.e. when 
declaring variable it is not necessary to provide a value as well, this can be done later in the program. 
type       identifier   value  ;  

Example

Int    x =    100  ; 


float    y =    2.5 ;   
Example-1 simple declaration;

#include<stdio.h>
#include<conio.h>

void main(){
/* variable declaration */
int a, b;
int c;

/* initialization */
a = 10;
b = 20;
c = a + b;

printf("value of c : %d \n", c);


getch(); }

CF‐Lab‐01, prepared by Nauman Shamim 
Example-2 Declaring and Initializing

#include<stdio.h>
#include<conio.h>

void main(){
int a=10,b=20;
printf(“Starting Values of a and b are \n”);
printf(“a=%d,b=%d \n”,a,b);
//Re-Initializing
a=30;
b=40;
printf(“\nNew values of a and b are \n”);
printf(“a=%d,b=%d\n”,a,b);
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;
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> printf is an out put function, it only
void main(){ displays data on the screen
int length,width,area;
length=20;
width=30;
area=length*width;
printf(“Area of rectangle is =%d”,area);
getch();
}

CF‐Lab‐01, prepared by Nauman Shamim 
Data Input from keyboard
To get input from the keyboard C provide a simple function called scanf, the function has two parts
1. Format string: Specifies which type of data to be input , uses %d, %c, %f, etc format sepcifiers
2. Address of variable/s to store value: One or more variables addresses to store the data entered from the
keyboard
Format string

scanf(“%d”,&a) ;

Variable address
Example scanf
Lets rewrite our previous program to find the area of a rectangle, this time we will get the length and width from the
user using scanf
#include<stdio.h>
#include<conio.h>
void 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();
}
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 (in old computers)
\\ Prints backslash
\” Prints double quote
\’ Prints a single quote

CF‐Lab‐01, prepared by Nauman Shamim 
  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();
}

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

CF‐Lab‐01, prepared by Nauman Shamim 
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();

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>

void 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>
void main(void){
int num_days=365;
printf(“There are number of days in a year %d “, num_days);
getch();
}

CF‐Lab‐01, prepared by Nauman Shamim 
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();

Followings are basic format specifiers in C (only %d is covered in this lab)

1. %d   is used for int (integers) 


2. %c   is used for char (characters) 
3. %f   is used for float (fractions) 
4. %s   is used for 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)
{

CF‐Lab‐01, prepared by Nauman Shamim 
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;
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 City / School / College / Univeristy


Matriculation ABC School etc
Intermediate ABC College etc
Graduation ABC University etc
Masters ABC University etc

Work Experience:

(Few lines of your choice)

References
(Few lines of your choice)
------------------------------------------------------------------------------------------------

CF‐Lab‐01, prepared by Nauman Shamim 

You might also like