Vous êtes sur la page 1sur 24

CST1811

Structured Programming Laboratory Session 02


ConstantsVariables

Thevalueofaconstantcannotbechanged afteraninitialvalueisassignedtoit

declaredconstants constfloatpi=3.141; definedconstants #definepi3.141

Excercise01

Writeasmallprogrametocalculatethesumof twointegernumberswhicharegivenas keyboardinputs. Tips sum=num1+num2

SampleAnswer
1 /* Fig. 2.5: fig02_05.c 2 Addition program */ 3 #include <stdio.h> 4 5 int main() 6 { 7 int integer1, integer2, sum; 8 9 printf( "Enter first integer\n" ); 10 scanf( "%d", &integer1 ); 11 printf( "Enter second integer\n" ); 12 scanf( "%d", &integer2 ); 13 sum = integer1 + integer2; 14 printf( "Sum is %d\n", sum ); 15 16 return 0; /* indicate that program 17}
Enter first integer 45 Enter second integer 72 Sum is 117

/* declaration */ /* /* /* /* /* /* prompt */ read an integer */ prompt */ read an integer */ assignment of sum */ print sum */

ended successfully */

OperatorsinC

Assignmentoperator Arithmeticoperators Relationaloperators Logicaloperators Bitwiseoperators Specialoperators Pointeroperators

AssignmentOperator

Theassignmentoperatoristhesimpleequal sign(=)
a=5;//valueofvariableabecomes5 a=5+10;//valueofvariableabecomes15 a=5+b;//valueofvariableabecomes5+valueof b a=b+c;//valueofvariableabecomesvalueofb+ valueofc a=(x*x+y*y)/2;

compoundassignmentoperators
a=b;//issameasa=ab; a*=b;//issameasa=a*b; a/=b;//issameasa=a/b; a%=b;//issameasa=a%b;

Arithmeticoperations
Operator + * / % ++

Action Addition Subtraction Multiplication Division Modulodivision Increment(extended) Decrement(extended)

ArithmeticoperationsinC

exp1+exp2 Addsexp1andexp2.Anexpcanbeany integerorfloatingpointexpression.

exp1exp2 Subtractsexp2fromexp1. exp1*exp2 Multipliesexp1byexp2. exp1/exp2 Dividesexp1byexp2.

ArithmeticoperationsinC

exp1%exp2 Findsmoduloofexp1dividedbyexp2.(Thatis, findstheremainderofanintegerdivision.)An expressioncanbeanyintegerexpression.

exp Negatesthevalueofexp. +exp Identity(unaryplus).

ArithmeticoperationsExample
#include<stdio.h> intmain() { inta,b; printf("Entera:"); scanf("%d",&a);//readvalueofa printf("Enterb:"); scanf("%d",&b);//readvalueofb printf("\na+b=%d",a+b);//displaysumofa&b printf("\nab=%d",ab);//displaysubtractionofbfroma printf("\na*b=%d",a*b);//displaymultiplicationofa&b printf("\na/b=%d",a/b);//displaydivisionofabyb

printf("\na%%b=%d",a%b);//displaymodulusofadividedbyb

return0;}

Developingaprograminvolvesa setofsteps:
1.Definetheproblem 2.Outlinethesolution 3.Developanalgorithm1 4.Testthealgorithmforcorrectness 5.Codethealgorithmusingasuitable programminglanguage 6.Compileandcorrectionofcompileerrors 7.Runtheprogramonthecomputer
8.Test,documentandmaintaintheprogram

Excercise02

ToWriteacprogrammetofindthe circumferenceofacircle.Youcangettheradius astheuserinputvalue.


TipTheratioofacircle'scircumferencetoitsdiameteris (pi),anirrationalconstantapproximatelyequalto 3.141592654.ThusthelengthofthecircumferenceCis relatedtotheradiusranddiameterdby:
C=2pir=pid,

Assignment01

Writeaprogramtofindtheaverageofthree numberswhicharegivenasuserinputs. (Commentsshouldbeincluded)

IncrementandDecrement Operators
incrementavariable: a=a+1; youmayuse: a+=1;ora++; Alsoexpression: a=a1; canbewrittenas: a=1;ora;

Exercise03

Predictthevaluesofvariablesa,b,sum1 andsum2ifthefollowingcodesegmentis executed. inta,b; a=b=2; sum1=a+(++b); sum2=a+(b++);

PrecedenceandAssociativelyof ArithmeticOperators

RelationalandLogicalOperators

PrecedenceofRelationaland LogicalOperators

PrecedenceofRelationaland LogicalOperators

Homework
BitwiseOperators ShiftOperators

Bestpractices
Testyourcodewhileyouareprogramming!

Questions??

Thankyou!

Vous aimerez peut-être aussi