A History Lesson Adapted from Chapter 1 in C++ for Java Programmers by Weiss and C for Java Programmers: a Primer by McDowell Development of language by.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

Programming Languages and Paradigms The C Programming Language.
Chapter 7: User-Defined Functions II
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Road Map Introduction to object oriented programming. Classes
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Guide To UNIX Using Linux Third Edition
Computer Science 210 Computer Organization Introduction to C.
1 Chapter 9 Scope, Lifetime, and More on Functions.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
CPS120: Introduction to Computer Science Decision Making in Programs.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
C Basics Computer Organization I 1 May 2010 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell Labs.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Numbers in ‘C’ Two general categories: Integers Floats
C++ Lesson 1.
A History Lesson Adapted from Chapter 1 in C++ for Java Programmers by Weiss and C for Java Programmers: a Primer by McDowell Development of language by.
The Machine Model Memory
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
Computer Science 210 Computer Organization
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Introduction to C++ Systems Programming.
Lecture 5: Some more Java!
Java Primer 1: Types, Classes and Operators
C Programming Tutorial – Part I
Programming Languages and Paradigms
Object Oriented Programming
Representation, Syntax, Paradigms, Types
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
Programming Paradigms
The Preprocessor Based on Chapter 1 in C++ for Java Programmers by Weiss When a C compiler is invoked, the first thing that happens is that the code is.
C Basics.
Introduction to C Programming Language
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
User-Defined Functions
Computer Science 210 Computer Organization
Chapter 9 Scope, Lifetime, and More on Functions
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.
Built-In (a.k.a. Native) Types in C++
A History Lesson Adapted from Chapter 1 in C++ for Java Programmers by Weiss and C for Java Programmers: a Primer by McDowell Development of language by.
Representation, Syntax, Paradigms, Types
Introduction to C Topics Compilation Using the gcc Compiler
Classes and Objects.
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Representation, Syntax, Paradigms, Types
Python Primer 1: Types and Operators
WEEK-2.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Introduction to C Topics Compilation Using the gcc Compiler
Representation, Syntax, Paradigms, Types
Java Programming Language
Programming Languages and Paradigms
Variables in C Topics Naming Variables Declaring Variables
C Language B. DHIVYA 17PCA140 II MCA.
The Three Attributes of an Identifier
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
SPL – PS3 C++ Classes.
Presentation transcript:

A History Lesson Adapted from Chapter 1 in C++ for Java Programmers by Weiss and C for Java Programmers: a Primer by McDowell Development of language by Dennis Ritchie at Bell Labs culminated in the C language in 1972. Motivation was to facilitate development of systems software, especially OS development. Traditionally, supports a procedural view of problem analysis. Formal language Standard adopted in 1990; required compromises because of vast body of existing C code based on a more-or-less common understanding of the language. Significant revision, ISO/IEC 9899:1999 or simply C99 if you like, was adopted in 1999; another revision ISO/IEC 9899:2011 or C11 was adopted in 2011. My presentation will be based on the C11 Standard, although the C99 Standard covers everything we will use. A final draft of the current standard is available at: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

The First Program Since tradition demands it: #include <stdio.h> // load declarations of std // library functions for I/O int main() { // mandatory fn printf("Hello, world!\n"); // output to console return 0; // exit fn (& pgm) } Note: #include loads declarations from standard C library (and more) Every C program must have a non-member function called main(). main() must be declared with a return type of int.

The Preprocessor When a C compiler is invoked, the first thing that happens is that the code is parsed and modified by a preprocessor. The preprocessor handles a collection of commands (commonly called directives), which are denoted by the character '#'. #include directives specify an external file (for now a C library file); the preprocessor essentially copies the contents of the specified file in place of the directive. We will see more interesting preprocessor directives later. #include <stdio.h> . . . int main() { printf("Hello, world!\n"); return 0; } Contents of file stdio.h are copied here.

The C Standard Library The C Standard Library includes a fairly large collection of types and functions. The declarations of these are placed into a collection of header files, which are part of the distribution of every C compiler. The implementations are placed into a collection of C source files, which are then pre-compiled into binary library files (also part of every C compiler distribution). C programmers incorporate portions of the Standard Library into their programs by making use of #include directives.

What's the Same as Java (more or less) Naming rules are the same. But… customary conventions differ. Declaration syntax is the same. Scoping rules are similar, within a file at least. Many reserved words are the same, with the same meanings, but ALL (almost) reserved words in C and ALL (almost) Standard Library identifiers are purely lower-case. Operator symbols and expressions are generally the same. The basic control structures (if, for, while, . . .) have same syntax and semantics. Function call/return syntax and semantics are the same; as with Java, function parameters can only be passed into a function by value.

Conditionals and Loops C includes the same set of conditional and loop statement forms as Java: if… if…else… switch… while… for… do…while… C also has a goto statement for unconditional branching. Thou shalt not goto.

C Philosophy The stated goal of the designers of the C language is: Correct code should execute as fast as possible on the underlying hardware. Of course, good programmers write only correct code… … and only good programmers should be writing code.

Core Differences vs Java All built-in C types are primitives; there are no class types in the language. In C there is no notion of a member function. A C program is a collection of functions that call one another, not a collection of classes and objects that use one another's services. In C, every variable may be allocated dynamically, or not; it's up to you to decide. Scope rules are slightly different; a name declared within a block is strictly local to the block. In most cases, C variables are not automatically initialized at all; you may initialize them yourself when you declare them.

Variable Declarations All declared objects are (by default) statically allocated (not dynamically). Thus, the following declaration results in X and Y being objects of type int, not references to objects: int X = 6, Y = 28; Memory X 6 Y 28 This has many consequences: - assigning X to Y does not result in an alias; rather X becomes a copy of Y, but is still an entirely different object; just like Java primitives, and unlike Java objects - using X as a parameter to a function does not allow the function to modify X - logically, you can only initialize declared objects to 0 if they are numeric types Memory X 6 Y

Automatic Variable Initialization Variables are not (usually) automatically initialized. The compiler will not check for use of a variable before it has been initialized. int X, Y; Y = 2*X + 1; This is a common source of errors in C programs and is easily avoided. Memory X ?? Y ???? Note: - when Linux allocates memory to a process, it may write zeros into that memory, which has the effect of initializing variables stored within that memory to 0; you should never count on that to save you. - static globals (explained later) are initialized to 0 if you do not initialize them

Boolean Variables C initially did not have a Boolean type. The code example gets a warning from gcc, if –Wall is used: T06Slide11.c:5:4: warning: suggest parentheses around assignment used as truth value C initially did not have a Boolean type. Integer values can be used as Booleans; zero is interpreted as false and all other values are interpreted as true. Modern C includes a _Bool type which is aliased to bool. Every expression in C has a value (well-defined or not). Hence, the following is valid code: if ( x = 42 ) // always executes the if-clause

C Primitive Types Standard C provides a plethora of primitive types. These store single values, and are most definitely not objects in the Java sense. In particular, there is no guarantee of automatic initialization. Integer types Probable characteristics int 32-bits unsigned int 32-bits short (int) 16-bits unsigned short (int) 16-bits long (int) 32-bits unsigned long (int) 32-bits #include <stdint.h> int8_t uint8_t int16_t uint16_t int32_t uint32_t int64_t uint64_t Floating-point types Conforming implementations provide: float 32-bit IEEE single-precision type double 64-bit IEEE double-precision type

Ranges of C Primitive Integer Types For integer types, the range of representation depends on the number of bits used, and whether the type is signed or unsigned: type min max int8_t -128 127 uint8_t 255 int16_t -32,768 32,767 uint16_t 65,355 int32_t -2,147,483,648 2,147,483,647 uint32_t 4,294,967,295 int64_t -9,223,372,036,854,775,808 9,223,372,036,854,775,807 uint64_t 18,446,744,073,709,551,615 The choice matters: - too narrow and you risk numeric overflow and incorrect results - unnecessarily wide and you use more memory than is logically needed

C Primitive Types Character types Probable characteristics char 1-byte, ASCII code unsigned char 1-byte, unsigned integer Logical types Probable characteristics bool 1-byte, value either true or false <stdbool.h> (really _Bool, but standard macro provides alias) The primitive types, except as noted, are all available without any inclusions from the Standard Library.

C Arithmetic Operators Again, with –Wall gcc warns: operation on ‘x’ may be undefined Same syntax as Java. Semantics are generally the same as well, although the C Standard leaves the result of a number of unwise constructs undefined. For example: int x = 5; x = x++ * x++; Now, the C Standard leaves the result of executing that statement undefined. If you want a very detailed and interesting discussion of why this is so, take a look at: http://c-faq.com/~scs/readings/undef.950321.html My take on the issue is that such expressions are generally "stupid" and unlikely to be used in real code… Precedence rules are the same as Java. Precedence can be forced (and disambiguated) by use of parentheses.

Creating User-defined Types Java C public class Rational { private int top; private int bottom; public Rational(...) { ... } struct _Rational { int top; int bottom; }; typedef struct _Rational Rational; Rational Rational_Create(...) { ... } Classes: - data and function members - member access control enforced by compiler - automatic initialization (constructor must be invoked when object is created) struct types: - data members only - member access control enforced by programmer discipline (or not) - initialization only if programmer remembers to do it

Code Organization for User-defined Type Java C // Function definitions go // inside the body of the // class definition. // The code for the type goes // into a single file. public class Rational { private int top; private int bottom; public Rational(...) { . . . } // Type definition typically goes // in a header file (Rational.h). struct _Rational { int top; int bottom; }; typedef struct _Rational Rational; . . . // Function definitions typically // go in a C file (Rational.c). Rational Rational_Create(...) { . . . } A collection of related functions (and types) is called a module.

Function Interfaces for User-defined Types // No notion of a member function. Rational Rational_Create(int top, int bottom) { . . . } // So objects the function will "work on" must be passed // to the function via parameters. Rational Rational_Add(Rational left, Rational right) {

Memory Management In C, objects which are allocated dynamically are not automatically deallocated (at least, not until the program terminates execution). Deallocating them efficiently is the responsibility of the programmer. For now, we’ll examine one simple case to illustrate the difference.

Object Creation Java C public class Rational { struct _Rational { ... public Rational(...) { } struct _Rational { int top; int bottom; }; typedef struct _Rational Rational; ... // CAN alloc statically Rational R1; // MUST alloc dynamically // (exception: primitives) Rational r1 = new Rational(...); // OR alloc dynamically Rational* R1 = malloc(sizeof(Rational)); Java C

Dynamic vs Static Allocation Dynamic allocation - memory for object is requested by explicit call in code, executed at runtime - decision whether to actually create object can be deferred until runtime - requires call to OS function; may be slow; request may be denied - raises issue of disposing of the memory once object is no longer needed Static allocation - memory for object is requested by presence of an object declaration in code - decision whether to create object is made when code is written - memory allocation is automatic; only denied if program is out of memory - disposing of the memory is handled automatically; no burden on the programmer

Aside: Pointer Variables A pointer is simply a variable whose value is the address of something. When we allocate an object dynamically, we get an address: Java Rational r1 = new Rational(...); In the Java fragment above, r1 is a reference variable, which is a kind of pointer, and the operator new returns the address of a chunk of memory which will hold the object being allocated. In C, pointer variables are declared by using some "syntactic sugar" after the type specifier: C Rational* r1 = malloc(...); The symbol '*' after the type specifier means that r1 is a pointer. The C function malloc() returns the address of a chunk of memory which will hold the object being allocated.