Write First Program: Compile F9 Execute or Press CRT+F9
Write First Program: Compile F9 Execute or Press CRT+F9
Write First Program: Compile F9 Execute or Press CRT+F9
#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
#include<stdio.h>
#include<conio.h>
void main(){
/* variable declaration */
int a, b;
int c;
/* initialization */
a = 10;
b = 20;
c = a + b;
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
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
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 ‘\’
CF‐Lab‐01, prepared by Nauman Shamim
TASK‐05 PRACTICE FOLLOWING PROGRAM
Write The Following Code, Execute It And Compare The Outputs
#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();
}
#include <stdio.h>
#include <conio.h>
main(void){
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
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){
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;
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?
#include <stdio.h>
#include <conio.h>
void main(void)
{
int random_num=10;
random_num= 19;
getch();
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();
}
#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();
}
#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:
Work Experience:
References
(Few lines of your choice)
------------------------------------------------------------------------------------------------
CF‐Lab‐01, prepared by Nauman Shamim