Differences between Java and C

Slides:



Advertisements
Similar presentations
Introduction to C Systems Programming Concepts. Introduction to C A simple C Program A simple C Program –Variable Declarations –printf ( ) Compiling and.
Advertisements

Dale Roberts Basic I/O – scanf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Numerical Computation Review and Continuation CS-2301, B-Term Numerical Computation in C Review and Continuation CS-2301, System Programming for.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
COMP Primitive and Class Types Yi Hong May 14, 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 1.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
CS 1704 Introduction to Data Structures and Software Engineering.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Chapter 9 - Formatted Input/Output
Numbers in ‘C’ Two general categories: Integers Floats
C Formatted Input/Output
Chapter 2 Variables.
Formatted Input and Output
Topics Designing a Program Input, Processing, and Output
ECE Application Programming
Week 3 - Friday CS222.
Tokens in C Keywords Identifiers Constants
Input/output.
TMF1414 Introduction to Programming
Introduction to C CSE 2031 Fall /3/ :33 AM.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
CSCI206 - Computer Organization & Programming
Input and Output Lecture 4.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Introduction to Programming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Java - Data Types, Variables, and Arrays
More about Numerical Computation
Chapter 9 - Formatted Input/Output
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Lectures on Numerical Methods
Structures, Unions, and Typedefs
Bit Fields & Bitwise Operations
Programming Assignment #1 12-Month Calendar—
Symbolic Constants in C
Programming Assignment #5
Your first C and C++ programs
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Conversion Check your class notes and given examples at class.
WEEK-2.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Introduction to C EECS May 2019.
Module 2 Variables, Data Types and Arithmetic
Chapter 2 Variables.
DATA TYPES There are four basic data types associated with variables:
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

Differences between Java and C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by Bjarne Stroustrup, and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) CS-2303, A-Term 2012 Differences between Java and C

Differences between Java and C Java is derived from C++, hence from C Many of its syntactic characteristics are similar to C However, there are some huge differences CS-2303, A-Term 2012 Differences between Java and C

Differences between Java and C Expressions Arithmetic operators are the same:– +, –, *, /, %, ++, – – Numerical type conversion is mostly the same Java spells out divide by zero, NaN (not a number, etc.) C & C++ are machine dependent Check the textbooks for details CS-2303, A-Term 2012 Differences between Java and C

Differences between Java and C Relational Operators Relational operators work the same way but return different results:– >, >=, <, <=, ==, != In Java, they return values FALSE and TRUE C/C++ operators return values 0 and 1 In C/C++, a value of zero means “false” any value that is not zero means “true” E.g., 1, 5, -1000000, 3.14159, 6.626068 × 10-34 Very important! CS-2303, A-Term 2012 Differences between Java and C

Conditional and Bitwise Operators Conditional 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 within a word Shift operators differ a little bit << (left shift) is the same >> (right shift) is machine dependent in C/C++ I.e., whether to fill from left with zeros or sign bits CS-2303, A-Term 2012 Differences between Java and C

Assignment and Unary Operators Assignment 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 CS-2303, A-Term 2012 Differences between Java and C

Summary about Expressions and Operators Pretty much the same in C/C++ and Java Be sure to check details in textbook Be sure to check operator precedence Table 2-1 in K&R (p. 53) Figure 2.3 in Absolute C++ Note: Absolute C++ uses the term “Display” to mean “figure” CS-2303, A-Term 2012 Differences between Java and C

Differences between Java and C Statements Statements in C/C++:– Labeled statement Expression statement Compound statement Selection statement Iteration statement Jump statement CS-2303, A-Term 2012 Differences between Java and C

Differences between Java and C Statements Statements in C/C++:– Labeled statement Expression statement Compound statement Selection statement Iteration statement Jump statement E.g., cases of a switch statement Similar to Java CS-2303, A-Term 2012 Differences between Java and C

Differences between Java and C Statements Statements in C/C++:– Labeled statement Expression statement Compound statement Selection statement Iteration statement Jump statement Any expression followed by ';' Much like to Java CS-2303, A-Term 2012 Differences between Java and C

Differences between Java and C Statements Statements in C/C++:– Labeled statement Expression statement Compound statement Selection statement Iteration statement Jump statement Sequence of statements enclosed in "{}" Called a “block” in Java CS-2303, A-Term 2012 Differences between Java and C

Differences between Java and C Statements Statements in C/C++:– Labeled statement Expression statement Compound statement Selection statement Iteration statement Jump statement switch (expr) {cases} if (expr) statement else Same as in Java CS-2303, A-Term 2012 Differences between Java and C

Differences between Java and C Statements Statements in C/C++:– Labeled statement Expression statement Compound statement Selection statement Iteration statement Jump statement while (expr) statement do statement while (expr); for (exp1; exp2, exp3) statement Very similar to Java CS-2303, A-Term 2012 Differences between Java and C

Differences between Java and C Statements Statements in C/C++:– Labeled statement Expression statement Compound statement Selection statement Iteration statement Jump statement break; continue; return expr; Very similar to Java goto Not present in Java! Not allowed in this course CS-2303, A-Term 2012 Differences between Java and C

Summary about Statements Pretty much the same in C/C++ and Java Be sure to check details in textbooks and/or reference manuals CS-2303, A-Term 2012 Differences between Java and C

Formatted Input & Output Very different between C and Java Very different between C and C++ Handled by library functions in C printf() scanf() getc() putc() Many others! CS-2303, A-Term 2012 Differences between Java and C

printf() – Print formatted data printf("string containing '%' specifiers", expr1, expr2, expr3, …); Copy the string, character-by-character, to the output stream When the ith '%' is encountered, treat it as a conversion specifier for converting the value of expri Copy the converted value to the output per instructions encoded in the conversion specifier Return number of characters printed CS-2303, A-Term 2012 Differences between Java and C

printf() conversion specifiers K&R, pp 154 & 244 %d or %i Treat expression as a decimal number (with sign) %u Treat expression as unsigned decimal number %f Treat expression as double precision floating point number; print without exponent %e or %E Treat expression as double precision floating point number; print with exponent (base 10) — scientific notation %c Treat value of expression as the code for a single character %s Treat expression as a pointer to a string Later in this course CS-2303, A-Term 2012 Differences between Java and C

printf() conversion specifiers (continued) Conversion specifiers may optionally contain Right or left alignment in the field Minimum field width (padded on right or left) Precision – i.e., Maximum length of string Number of decimal places of floating point value Size of data type E.g., short, double, float, etc. Examples %6d – print signed decimal number in 6-char field %8.4f – print 64-bit floating point number with four places after decimal point, field width of 8 characters %hu – print an unsigned short integer (i.e., 16 bits in Pentium) CS-2303, A-Term 2012 Differences between Java and C

scanf() — Scan formatted data scanf("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 vari Copy the converted value to the output per instructions encoded in the conversion specifier Stop if input does not match string OR if conversion was not successful Return number of successful conversions Note ampersands CS-2303, A-Term 2012 Differences between Java and C

scanf() – Typical Usage int j; double x; scanf("%d%f", &j, &x); Scan the input, skipping blanks and tabs Try to match a signed integer; if successful, store result in j and continue Continue scanning, skipping blanks and tabs Try to match a floating point number. If successful, store in x Stop if unsuccessful or new-line encountered Return number of items stored CS-2303, A-Term 2012 Differences between Java and C

Differences between Java and C scanf() Details K&R Table B-2 (p. 246) printf() and scanf() are both needed for Lab #1 and Programming Assignment #1 CS-2303, A-Term 2012 Differences between Java and C

Differences between Java and C Summary Differences and similarities between Java and C Expressions Statements There are lots of other differences Will be covered during the course CS-2303, A-Term 2012 Differences between Java and C

Differences between Java and C Questions? Next Topic CS-2303, A-Term 2012 Differences between Java and C