C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, 2000 Introduction to C Muldner, Chapters 1, 2.

Slides:



Advertisements
Similar presentations
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Advertisements

Programming Languages and Paradigms
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 3: Beginning Problem Solving Concepts for the Computer Programming Computer Programming Skills /1436 Department of Computer Science.
Chapter 1: Introduction
Lab#1 (14/3/1431h) Introduction To java programming cs425
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
Unix Continuum of Tools Do something once: use the command line Do something many times: –Use an alias –Use a shell script Do something that is complex.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Guide To UNIX Using Linux Third Edition
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
C Programming. C vs C++ C syntax and C++ syntax are the same but... C is not object oriented * There is no string class * There are no stream objects.
C programming Language and Data Structure For DIT Students.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
COMPUTER PROGRAMMING. Introduction to C++ History Merges notions from Smalltalk and notions from C The class concept was borrowed from Simular67 Developed.
Computer Science 210 Computer Organization Introduction to C.
“C” Programming Language CIS 218. Description C is a procedural languages designed to provide lowlevel access to computer system resources, provide language.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
“C” Programming Language What is language ? Language is medium of communication. If two persons want to communicate with each other, they have to use.
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
The Java Programming Language
Programming With C.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 1 Section 1.1 Introduction to Java Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
First Compilation Rudra Dutta CSC Spring 2007, Section 001.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001.
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
An overview of C Language. Overview of C C language is a general purpose and structured programming language developed by 'Dennis Ritchie' at AT &T's.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, 2000 Tomasz Müldner C for Java Programmers.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
CSE 374 Programming Concepts & Tools
Computer Science 210 Computer Organization
CSE 374 Programming Concepts & Tools
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Chapter 18 I/O in C.
C Basics.
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
More About Data Types & Functions
C Language B. DHIVYA 17PCA140 II MCA.
SPL – PS1 Introduction to C++.
SPL – PS3 C++ Classes.
Introduction to Classes and Objects
Presentation transcript:

C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, 2000 Introduction to C Muldner, Chapters 1, 2

C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, 2000 Introduction: Preview u History of C. u Two programming paradigms; objected oriented and procedural. u Comparison of Java and C. u Software development process. u Programming idiom.

C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, 2000 Introduction: About C u 1972: The C programming language developed by Dennis Ritchie u 1973: C used to write the Unix operating system. u 1978: Kernighan and Ritchie set the first standard of C K&R standard u 1989: the American National Standards Institute adopted the ANSI C standard

C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, 2000 Introduction: Programming paradigms u object-oriented programming paradigm, based on the following design rule: decide which classes you need, provide a full set of operations for each class, and make commonality explicit by using inheritance. u procedural programming paradigm, based on the following design rule: decide which procedures and data structures you want and use the best algorithms.

C for Java Programmers Copyright: Rudra Dutta, NCSU, Spring 2006 Object-Oriented and Procedural: A Pictorial View

C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, 2000 Introduction: Comparison of C and Java u primitive data types: character, integer, and real In C, they are of different sizes, there is no Unicode 16-bit character set u structured data types: arrays, structures and unions. In C, arrays are static there are no classes u Control structures are similar u Functions are similar

C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, 2000 Introduction: Comparison of C and Java u Java references are called pointers in C. u Java constructs missing in C: packages threads exception handling garbage collection standard Graphical User Interface (GUI) built-in definition of a string standard support for networking support for program safety.

C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, 2000 Introduction: Why is C still useful? C provides: u efficiency u flexibility and power u many high-level and low-level operations u stability C is used: u data compression, graphics and computational geometry u databases, operating systems u there are zillions of lines of C legacy code

C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, 2000 Introduction: Development Process Four stages u editing u preprocessing u compiling:translates source code -> object code u linking: produces executable code u Portable programs will run on any machine. u Program correctness and robustness are most important than program efficiency

C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, 2000 Introduction: Programming Style and Idioms u Programming style includes recommendations for: lexical conventions a convention for writing comments u In a natural language, an idiom is a phrase that has a specific meaning such as “don’t pull my leg” while(*p++ = *q++) ; You'll learn how to identify, create and use idioms. Alternatively called “chunks”.

C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, 2000 Sample Small C Program

C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, : General Documentation Comments in C are similar to those in Java. There is no standard tool such as javadoc to produce documentation in HTML form. An “in-line” comment, starting with //, is not supported *. The heading comment that starts the sample program follows a particular style convention used throughout this book. It always describes the intended meaning of the program, any known bugs, etc. * read “not standardized”

C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, 2000 documentation /* Author: Tomasz Muldner * Date: August, 1999 * Version: 2.0 * File: Sample.c * Program that expects one or two filenames on the * command line and produces a hexadecimal dump of the * file whose name is passed as the first argument. If * the second argument is present, the dump is stored in * the file whose name is passed as this argument. * Otherwise, the dump is displayed on the screen. * The format of the dump is as follows: * Each line contains 16 hexadecimal ASCII codes of the * corresponding 16 bytes read from the file; separated * by a blank. This line is followed by a line containing * the 16 corresponding characters, again separated by a * blank, with each non-printable character displayed * as a dot and other characters unchanged. */

C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, : Run-time Libraries and Function Declarations Include directives, starting with #include, are needed in C programs to include files, called header files, which typically have the “.h ” extension. In my example, three files are included, respectively called stdio.h, ctype.h and stdlib.h. These header files contain declarations of various standard functions that are defined in the run-time libraries.

C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, 2000 memory 2: Global Definitions Global definitions define entities (such as variables), which are available to all the code that follows this definition. In my example there is a single global definition: FILE *outFile; that defines a file handle, available to all the code that follows it, both the hex() and main() functions. This is called “scope” of the variable.

C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, 2000 header /* include files */ #include /* global definitions */ FILE *outFile; /* output file */

C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, : Function Declaration and Definition A declaration merely provides a function prototype: void hex(unsigned char *p, int max); The declaration does not say anything about the implementation The definition of a function includes both the function prototype and the function body, where the body is the implementation of the function

C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, 2000 definition /* Function declaration */ /* * Function: hex(p, max) * Purpose: writes to the global output file * outFile the hexadecimal codes of max number of * characters originating at the address given by the * pointer p. This line is followed by the * corresponding chars. * Assumes that the file outFile has been * opened for output. * Inputs: p, max (parameters) * outFile (global variable) * Returns: nothing * Modifies: outFile * Error checking: none */ void hex(unsigned char *p, int max);

C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, : The main Function Every C program must include a function called main : int main(int argc, char *argv[]) main() is an integer function, and returns one of two standard return codes: EXIT_FAILURE EXIT_SUCCESS.

C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, 2000 main int main(int argc, char *argv[]) { FILE *inFile; /* input file handle */ int i, toFile; const int SIZE = 16; unsigned char line[SIZE]; /* local buffer */ if(argc > 3 || argc < 2) { fprintf(stderr, "usage: %s filename [filename2]\n", argv[0]); return EXIT_FAILURE; } outFile = stdout; /* set default output stream */ toFile = (argc == 3); /* is there an output file */ /* open I/O files */ if((inFile = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "Cannot open file %s\n", argv[1]); return EXIT_FAILURE; }

C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, 2000 main if(toFile && (outFile = fopen(argv[2], "w")) == NULL) { fprintf(stderr, "Cannot open file %s\n", argv[2]); fclose(inFile); return EXIT_FAILURE; } /* main loop; reads SIZE bytes at a time; * stores them in line, and calls hex() */ while((i = fread(line, 1, SIZE, inFile)) > 0) hex(line, i); /* close I/O */ if(fclose(inFile) == EOF) { fprintf(stderr, "Cannot close file %s\n", argv[1]); if(toFile) fclose(outFile); return EXIT_FAILURE; }

C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, 2000 main if(toFile && fclose(outFile) == EOF) { fprintf(stderr, "Cannot close file %s\n", argv[2]); return EXIT_FAILURE; } return EXIT_SUCCESS; }

C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, 2000 main /* Definition of hex() */ void hex(unsigned char *p, int max) { int i; unsigned char *paux; for(i = 0, paux = p; i < max; i++, paux++) fprintf(outFile, "%02x ", *paux); fputc('\n', outFile); for(i = 0, paux = p; i < max; i++, paux++) fprintf(outFile, "%c ", isprint(*paux) ? *paux : '.'); fputc('\n', outFile); }