Week0 Differences (1)

download Week0 Differences (1)

of 24

description

awey

Transcript of Week0 Differences (1)

  • Differences between Java and CCS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and from C: How to Program, 5th and 6th editions, by Deitel and Deitel)

    Differences between Java and C

  • Java and CJava is derived from CMany of its syntactic characteristics are similar to C

    However, there are some huge differences

    Differences between Java and C

  • ExpressionsArithmetic operators are the same:+, , *, /, %, ++,

    Numerical type conversion is mostly the sameJava spells out divide by zero, NaN (not a number, etc.)C & C++ are machine dependent

    Check the textbooks for details

    Differences between Java and C

  • Relational OperatorsRelational operators work the same way but return different results:>, >=,
  • Conditional and Bitwise OperatorsConditional execution operators are same in Java and C/C++:||, &&, ? followed by :

    Bitwise operators are same in Java and C/C++:|, &, ^ for bit-by-bit operations with a wordShift operators differ a little bit> (right shift) is machine dependent in C/C++I.e., whether to fill from left with zeros or sign bits

    Differences between Java and C

  • Assignment and Unary OperatorsAssignment operators work the same:=, +=, -=, *=, /=, &=, |=, ^=

    The following unary operators are available C/C++ but not in Java~invert the bits of a word*pointer dereference&pointer creation(type)cast (i.e., forceable type conversion)sizeof# of bytes in operand or data type->pointer dereference with field selection

    Differences between Java and C

  • Summary about Expressions and OperatorsPretty much the same in C/C++ and Java

    Be sure to check details in textbook

    Be sure to check operator precedenceTable 2-1 in K&RDistributed throughout text in D&D

    Differences between Java and C

  • StatementsStatements in C/C++:Labeled statementExpression statementCompound statementSelection statementIteration statementJump statement

    Differences between Java and C

  • StatementsStatements in C/C++:Labeled statementExpression statementCompound statementSelection statementIteration statementJump statement

    Differences between Java and C

  • StatementsStatements in C/C++:Labeled statementExpression statementCompound statementSelection statementIteration statementJump statement

    Differences between Java and C

  • StatementsStatements in C/C++:Labeled statementExpression statementCompound statementSelection statementIteration statementJump statement

    Differences between Java and C

  • StatementsStatements in C/C++:Labeled statementExpression statementCompound statementSelection statementIteration statementJump statement

    Differences between Java and C

  • StatementsStatements in C/C++:Labeled statementExpression statementCompound statementSelection statementIteration statementJump statement

    Differences between Java and C

  • StatementsStatements in C/C++:Labeled statementExpression statementCompound statementSelection statementIteration statementJump statement

    Differences between Java and C

  • Summary about StatementsPretty much the same in C/C++ and Java

    Be sure to check details in textbooks

    Differences between Java and C

  • Formatted Input & OutputVery different between C and JavaVery different between C and C++

    Handled by library functions in Cprintf()scanf()getc()putc()Many others!

    Differences between Java and C

  • printf() Print formatted dataprintf("string containing '%' specifiers", expr1, expr2, expr3, );Copy the string, character-by-character, to the output.When the ith '%' is encountered, treat it as a conversion specifier for converting the value of expriCopy the converted value to the output per instructions encoded in the conversion specifierReturn number of characters printed

    Differences between Java and C

  • printf() conversion specifiersSee K&R, p 154 & 244 or D&D Chapter 9%d or %iTreat expression as a decimal number (with sign)%uTreat expression as unsigned decimal number%fTreat expression as double precision floating point number; print without exponent %e or %ETreat expression as double precision floating point number; print with exponent (base 10) scientific notation%cTreat value of expression as the code for a single character%sTreat expression as a pointer to a stringLater in this course

    Differences between Java and C

  • printf() conversion specifiers (continued)Conversion specifiers may optionally containRight or left alignment in the fieldMinimum field width (padded on right or left)Precision i.e.,Maximum length of stringNumber of decimal places of floating point valueExamples%6d print signed decimal number in 6-char field%8.4f print floating point number with four places after decimal point, field width of 8 characters

    Differences between Java and C

  • scanf() Scan formatted datascanf("string containing '%' specifiers", &var1, &var2, &var3, );Scan the input, matching the string character by character.When the ith '%' is encountered, treat as a conversion specifier for converting next sequence of characters and storing result in variCopy the converted value to the output per instructions encoded in the conversion specifierStop if input does not match string or conversion not successfulReturn number of successful conversions.

    Differences between Java and C

  • scanf() Typical Usageint j;double x;scanf("%d%f", &j, &x);Scan the input, skipping blanks and tabsTry to match a signed integer; if successful, store result in jContinue scanning, skipping blanks and tabsTry to match a floating point number. If successful, store in xReturn number of items stored.

    Differences between Java and C

  • scanf() DetailsK&R Table B-2 (p. 246), D&D Chapter 9printf() and scanf() are both needed for Lab #1 Programming Assignment #1

    Differences between Java and C

  • SummaryDifferences and similarities between Java and CExpressionsStatementsThere are lots of other differencesWill be covered during the course

    Differences between Java and C

  • Questions?

    Differences between Java and C