Presentation1.ppt

26
1 REVIEW OF C-LANGUAGE BASICS TYPES 1 byte Signed character signed char 2 bytes Unsigned integer unsigned int 4 bytes Long integer longint 8 bytes Double precision F.Pt.No. double 4 bytes Floating point no. float 1 byte Single character char 2 bytes integer int Memory requirement Def. Data type

Transcript of Presentation1.ppt

  • REVIEW OF C-LANGUAGE BASICSTYPES1 byteSigned charactersigned char2 bytesUnsigned integerunsigned int4 bytesLong integerlongint8 bytesDouble precision F.Pt.No.double4 bytesFloating point no.float1 byteSingle characterchar2 bytesintegerintMemory requirementDef.Data type

  • Constants An immediate absolute value4 main types

    A,b,HelloString constantsA,bCharacter constants0.12, 12.3, 6.6e6, 6.7E5, 5.5e-7Floating point constants6, 7Integer constantsexampleType

  • Character escapesFound within character and string constantsThese are characters not represented on keyboardBegin with backslash \

    A single slash\\Single quote\Double quote\Carriage return\rbackspace\bNew line\ndefinitionCharacter escape

  • Declarations- tells the compiler the name and type of variable to be usedSimplest Form consists of

    typeVariable name;char c;int I;float f; For several variablestypeVariable name,Variable name;char c1, c2, c3;

  • Declarations continuedWe can initialise our variables when they are declared; this consists of

    typeVariable name=constant;e.g float n1 = 0.78e-3; For multiple variables we havetype,;e.g. char c1 = A, c2 =b;

  • Declarations ContinuedReasons why we declare variables before useThe compiler knows what kind/size of storage to allocateIt forces the programmer to pick the appropriate variables

  • Variable namesVariable names/identifiers need to reflect what they representC is case sensitive so approx and Approx will be treated as separate variablesThe identifiers can consist of numbers, letters and the underscore, but must always begin with a letter

  • Arithmetic Operators5 main operators :-modulus%division/multiplication*subtraction-addition+definitionoperator

  • Operators ContinuedC follows the standard operator hierarchy which can be controlled by parenthesis e.gY = (a + b) * (c d)(a + b) and (c d) will be executed 1stThe results of which will be multiplied and stored in the variable y

  • Assignment OperatorsThe assignment operator = assigns a value to a variable e.g.x = 1; a = b = c; a = b; i = i + 2; this sets x to 1 this copies the value of b into a this increments the current value of i by 2the value of c is copied into a and b

  • Type CastingThe value of an expression can be converted into a different data typeSyntax :- ((data type) expression)

    b = 3;a = 6.542;y = ((int) a + b) % 4The result of y would be 1 9.5429Remainder of 9 divided by 4

  • Unary OperatorThese operators act on a single operand to generate a new valueUnary Minus --(x + y)Sum x and yReverse the sign

  • Increment operatorThe increment operator causes its operand to increment by 1

    Post incrementVariable ++This increments the variable by 1 after the statement is executedPre increment++Variable This increments the variable by 1 before the statement is executed

  • Increment operator continuedi = 1;printf(i = %d \n, i);i = 1OUTPUTprintf(i = %d \n, ++i);i = 2printf(i = %d \n, i);i = 2THIS IS AN EXAMPLE OF THE PRE-INCREMENT UNARY OPERATOR

  • Increment operator continuedi = 1;printf(i = %d \n, i);i = 1OUTPUTprintf(i = %d \n, i++);i = 1printf(i = %d \n, i);i = 2THIS IS AN EXAMPLE OF THE POST-INCREMENT UNARY OPERATOR

  • The decrement operator --The increment operator causes its operand to decrease by 1

    Post decrementVariable This decreases the variable by 1 after the statement is executedPre decrementVariable This decreases the variable by 1 before the statement is executed----

  • Decrement operator continuedi = 5;printf(i = %d \n, i);i = 5OUTPUTprintf(i = %d \n, --i);i = 4printf(i = %d \n, i);i = 4THIS IS AN EXAMPLE OF THE PRE-DECREMENT UNARY OPERATOR

  • Decrement operator continuedi = 5;printf(i = %d \n, i);i = 5OUTPUTprintf(i = %d \n, i--);i = 5printf(i = %d \n, i);i = 4THIS IS AN EXAMPLE OF THE POST-DECREMENT UNARY OPERATOR

  • Relational and Logical OperatorsThere are 4 relational operators in c

    OperatorDefinitionGreater than=Greater than or equal to

  • Relational and Logical OperatorsThere are two equality operators in c

    OperatorDefinition= =Equal to!=Not equal to

  • Relational and Logical OperatorsThere are two logical operators in c

    The Relational and Logical Operators are found in conditional statements

    OperatorDefinition&&AND||OR

  • Other Assignment Operatorsj = j % cj %= c%=b = b/3b /= 3/=a = a * (i 3)a *= (i 3)*=j = j b j -= b-=j = j + 7j += 7 +=Definition ExampleOperator

  • The Conditional OperatorThe ? Operator requires 3 operands and takes the general form:Expression-1?Expression-2:Expression-3If Expression-1 is trueexecute

  • The Conditional OperatorThe ? Operator requires 3 operands and takes the general form:Expression-1?Expression-2:Expression-3If Expression-1 is Falseexecute

  • The Conditional Operator continuedFalseAs expression-1 is false y is assigned the value 200

  • Operator HierarchyHierarchyHighestLowest

    Operator CategoryOperatorsAssociativityUnary- ++ -- ! R LArithmetic * / % L RArithmetic + - L RRelational< >= L REquality == != L RLogical And && L RLogical Or || L RConditional ?: R LAssignment= += -= *= /= %= R L