Motivation was to facilitate development of systems software, especially OS development.

Similar documents
Motivation was to facilitate development of systems software, especially OS development.

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

C Introduction. Comparison w/ Java, Memory Model, and Pointers

CS201 - Introduction to Programming Glossary By

Short Notes of CS201

Presented By : Gaurav Juneja

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Review of the C Programming Language for Principles of Operating Systems

In Java we have the keyword null, which is the value of an uninitialized reference type

CMPE-013/L. Introduction to C Programming

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Review of the C Programming Language

Introduction to Programming Using Java (98-388)

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Lectures 5-6: Introduction to C

Interview Questions of C++

C - Basics, Bitwise Operator. Zhaoguo Wang

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

CE221 Programming in C++ Part 1 Introduction

6.096 Introduction to C++ January (IAP) 2009

CSE 303 Lecture 8. Intro to C programming

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

Lectures 5-6: Introduction to C

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

C Language, Token, Keywords, Constant, variable

printf( Please enter another number: ); scanf( %d, &num2);

COMP 2355 Introduction to Systems Programming

Pointers, Dynamic Data, and Reference Types

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Topic 6: A Quick Intro To C

Compiling and Running a C Program in Unix

PRINCIPLES OF OPERATING SYSTEMS

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

struct Properties C struct Types

CSCI 171 Chapter Outlines

EL2310 Scientific Programming

A brief introduction to C programming for Java programmers

a data type is Types

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Outline. 1 About the course

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Fundamental of Programming (C)

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

An Introduction to C++

Variables. Data Types.

Programming in C and C++

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Object-Oriented Programming

Recitation #11 Malloc Lab. November 7th, 2017

C Programming Review CSC 4320/6320

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

C: How to Program. Week /Mar/05

CSE 374 Programming Concepts & Tools

Data Structure Series

G Programming Languages - Fall 2012

Programming refresher and intro to C programming

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

Course Coding Standards

377 Student Guide to C++

Preview from Notesale.co.uk Page 6 of 52

Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

Lexical Considerations

PESIT-BSC Department of Science & Humanities

Fundamentals of Programming

Intermediate Code Generation

Creating a C++ Program

10. Abstract Data Types

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

Chapter 2 - Introduction to C Programming

Informatica 3 Syntax and Semantics

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Appendix G: Writing Managed C++ Code for the.net Framework

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

A brief introduction to C++

Programming in C and C++

IAR Embedded Workbench MISRA C:2004. Reference Guide

Dynamic Allocation in C

Procedures, Parameters, Values and Variables. Steven R. Bagley

Kurt Schmidt. October 30, 2018

Getting started with Java

Final CSE 131B Spring 2004

COP 3330 Final Exam Review

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Structure of this course. C and C++ Past Exam Questions. Text books

Administration. Literature. Lectures. Lab assignments. Text book coverage. Realtime systems D0003E. Also need: some additional book for C

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Programming. Data Structure

Transcription:

A History Lesson C Basics 1 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. My presentation will be based on the C99 Standard most C compilers now support most of that Standard.

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

The Preprocessor C Basics 3 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"); Contents of file stdio.h are copied here. return 0;

The C Standard Library C Basics 4 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 precompiled 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) C Basics 5 Naming rules are the same. But customary conventions differ. Declaration syntax is the same but semantics are different. 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 Basics 6 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 C Basics 7 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 C Basics 8 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. (Linux, however )

Variable Declarations C Basics 9 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; 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 28 Memory X 6 Y 6

Automatic Variable Initialization C Basics 10 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; X Memory?? Y???? This is a common source of errors in C programs and is easily avoided. 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.

Boolean Variables C Basics 11 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 C Basics 12 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 #include <stdint.h> unsigned int 32-bits int8_t uint8_t short (int) 16-bits int16_t uint16_t unsigned short (int) 16-bits int32_t uint32_t long (int) 32-bits int64_t uint64_t unsigned long (int) 32-bits Floating-point types Conforming implementations provide: float 32-bit IEEE single-precision type double 64-bit IEEE double-precision type

C Primitive Types C Basics 13 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 C Basics 14 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 C Basics 15 Java public class Rational { private int top; private int bottom; public Rational() { C struct _Rational { int top; int bottom; ; typedef struct _Rational Rational; Rational Rational_Create() { Classes: - data and fn 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

Memory Management C Basics 16 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.

Java Object Creation C Basics 17 public class Rational { private int top; private int bottom; public Rational() { public void Calculate () { Rational r1 = new Rational(); // MUST alloc with new r1 = new Rational(); // Just discard old object // Dynamically-allocated objects are automatically reclaimed // (eventually) by the Java GC system no worries!

C Object Creation I C Basics 18 struct _Rational { int top; int bottom; ; typedef struct _Rational Rational; Rational Rational_Create() { void Calculate () { Rational r1 = Rational_Create(); // CAN create statically r1 = Rational_Create(); // Just discard old object // Statically-allocated objects are automatically reclaimed // when the function terminates no worries!

Aside: Pointer Variables C Basics 19 A pointer is simply a variable whose value is the address of something. When we allocate an object dynamically, we get an address: 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: 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.

C Object Creation II C Basics 20 struct _Rational { int top; int bottom; ; typedef struct _Rational Rational; Rational Rational_Create() { void Calculate () { Rational* r1 = malloc(); // CAN create dynamically free(r1); // MUST explicitly destroy dynamic object r1 = malloc(); // before losing access to it // Dynamically-allocated objects are never automatically // reclaimed when fn terminates worries!