Vous êtes sur la page 1sur 6

TASK03

USINGSPECIALCHARACTERS
TherearespecialcharactersinCthatmodifiestheoutputofprintf,somethemaregivenbelow,eachspecial
characterstartswith\
\n

Prints new line

\t

Prints a tab

\a

Produces a beep

\\

Prints backslash

Prints double quote

Prints a single quote

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

Program -02 (Special Character Tab \t)

#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();
}

Followings are basic format specifiers in C

%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

Alternate way for program 04


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

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;

Q: Why values are printed on different lines ?


User input from keyboard
A computer program can take input from the user (from keyboard), to read the input of the user a basic function is scanf , the
scanf function reads integers , floats, characters and other data from the user, the format of the function is given below

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

Vous aimerez peut-être aussi