Académique Documents
Professionnel Documents
Culture Documents
USINGSPECIALCHARACTERS
TherearespecialcharactersinCthatmodifiestheoutputofprintf,somethemaregivenbelow,eachspecial
characterstartswith\
\n
\t
Prints a tab
\a
Produces a beep
\\
Prints backslash
TASK04
PRACTICEFOLLOWINGPROGRAM
WriteTheFollowingCode,ExecuteItAndCompareTheOutputs
Program-01 (understanding new line)
#include <stdio.h>
#include <conio.h>
main(void)
{
clrscr();
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>
main(void)
{
clrscr();
printf(This is not a \n single line );
getch();
}
Q: Why the line is printed on two different lines ?
CFLab01,preparedbyNaumanShamim
#include <stdio.h>
#include <conio.h>
main(void)
{
clrscr();
printf(Characters in capitals will be printed far from each Other\n A\t\t\t\tB\t\t\tC\t\t\tD);
getch();
}
Activity01Useprintf,newlinecharacter\n,tabcharacter\tand*(stars,useshift+8ornumpadtotypestarcharacter)
toProduceTheFollowingOutputs
Output 1
Name
: (Your Name)
Roll No : (Your Roll No)
Session : (Your Session)
Press any key to terminate
Output 2
*******************
*
*
*
*
*
*
*
*
*******************
TASK05
PRACTICEFOLLOWINGPROGRAM
Format Specifies
Formatspecifiersareusedinprintffunctionforprintingnumbersandcharacters,Aformatspecifieractslikeaplaceholder,it
reservesaplaceinastringfornumbersandcharacters,formoreunderstandingseetheexamplebelow
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>
main(void)
{
clrscr();
printf(There are %d number of days in a week,7);
getch();
CFLab01,preparedbyNaumanShamim
The%disaformatspecifierorplaceholderfornumber7,
thisisusefulwhenwehavetoprintthevaluesstoredinvariables,seetheexamplebelow
Example-2
#include <stdio.h>
#include <conio.h>
main(void)
{
clrscr();
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)
{
clrscr();
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
#include <stdio.h>
#include <conio.h>
main(void)
{
clrscr();
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);
CFLab01,preparedbyNaumanShamim
getch();
}
%d
%c
%f
%s
for
for
for
for
int(integers)
char(characters)
float(fractions)
string(sequenceofcharacters)
[Activity02]TrytoPredictTheOutputOfTheFollowingProgramsAndTryToAnswerTheQuestionIfAny.
Program-03
#include <stdio.h>
#include <conio.h>
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?
CFLab01,preparedbyNaumanShamim
b=20;
main(void)
printf(A = %d \n B = %d \n C = %d ,
a,b,c);;
int a,b,c;
getch();
a=10;
c=30;
Program-05
#include <stdio.h>
#include <conio.h>
main(void)
{
int a;
printf(Enter a number \n );
scanf(%d, &a)
printf(Value of a =%d ,a);
getch();
}
Second version of program-05
#include <stdio.h>
#include <conio.h>
main(void)
{
int a;
a=10;
printf(Initial value of variable a =%d,a);
printf(Enter a number \n);
scanf(%d,&a);
printf(New walue of a = %d,a);
CFLab01,preparedbyNaumanShamim
getch();
}
Program-06
#include <stdio.h>
#include <conio.h>
main(void)
{
int a,b,c;
printf(Enter value for a \n);
scanf(%d,&a);
printf(Enter value for b \n);
scanf(%d,&b);
c=a+b;
printf(Sum of a and b =%d ,c);
getch();
}
CompoundStatements
program-07
#include <stdio.h>
#include <conio.h>
main(void)
{
int a=10;
printf(value of a = %d \n,a);
a=a+1;
printf(Updated value of a = %d \n,a);
a+=1;
printf(Updated again value of a = %d,a);
getch();
}
Using increment and decrement operators
program-08
#include <stdio.h>
#include <conio.h>
main(void)
{
CFLab01,preparedbyNaumanShamim