CMSC 216 Introduction to Computer Systems Lecture 2 Introduction to C

Similar documents
Sources: On the Web: Slides will be available on:

The programming language C. sws1 1

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

The C Programming Language course syllabus associate level

Pemrograman Dasar. Basic Elements Of Java

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

How To Write Portable Programs In C

Objective-C Tutorial

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Stacks. Linear data structures

Informatica e Sistemi in Tempo Reale

C++ INTERVIEW QUESTIONS

Lecture 5: Java Fundamentals III

Simple C Programs. Goals for this Lecture. Help you learn about:

C++ Language Tutorial

System Calls Related to File Manipulation

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

So far we have considered only numeric processing, i.e. processing of numeric data represented

Computer Programming Tutorial

Moving from CS 61A Scheme to CS 61B Java

Install Java Development Kit (JDK) 1.8

Lecture 22: C Programming 4 Embedded Systems

Programming Languages CIS 443

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Tutorial on C Language Programming

Java Interview Questions and Answers

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

C++ Essentials. Sharam Hekmat PragSoft Corporation

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

C++ Programming Language

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Java (12 Weeks) Introduction to Java Programming Language

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

C Programming Tutorial

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Chapter 2: Elements of Java

El Dorado Union High School District Educational Services

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

2 ASCII TABLE (DOS) 3 ASCII TABLE (Window)

Getting Started with the Internet Communications Engine

J a v a Quiz (Unit 3, Test 0 Practice)

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Stack Allocation. Run-Time Data Structures. Static Structures

System Calls and Standard I/O

CS 106 Introduction to Computer Science I

Module 816. File Management in C. M. Campbell 1993 Deakin University

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Course Title: Software Development

Introduction to Python

Chapter 13 - The Preprocessor

X86-64 Architecture Guide

Embedded C Programming, Linux, and Vxworks. Synopsis

Introduction to Java

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

CS 111 Classes I 1. Software Organization View to this point:

Parameter passing in LISP

arrays C Programming Language - Arrays

C Interview Questions

CP Lab 2: Writing programs for simple arithmetic problems

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Chapter 7D The Java Virtual Machine

Some Scanner Class Methods

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

VB.NET Programming Fundamentals

CISC 181 Project 3 Designing Classes for Bank Accounts

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

C++FA 5.1 PRACTICE MID-TERM EXAM

Base Conversion written by Cathy Saxton

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall

I PUC - Computer Science. Practical s Syllabus. Contents

Chapter 13 Storage classes

Lexical Analysis and Scanning. Honors Compilers Feb 5 th 2001 Robert Dewar

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Variables, Constants, and Data Types

This section describes how LabVIEW stores data in memory for controls, indicators, wires, and other objects.

Introduction to Python

Programming Microcontrollers in C

DNA Data and Program Representation. Alexandre David

6.1. Example: A Tip Calculator 6-1

5 Arrays and Pointers

GBIL: Generic Binary Instrumentation Language. Andrew Calvano. September 24 th, 2015

C Compiler Targeting the Java Virtual Machine

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

CS 241 Data Organization Coding Standards

Chapter One Introduction to Programming

Object Oriented Software Design

AP Computer Science Java Subset

Transcription:

CMSC 216 Introduction to Computer Systems Lecture 2 Introduction to C

Administrivia Read Chapters 2 and 3 of Reek Class examples available in grace at: /afs/glue.umd.edu/class/summer12011/cmsc/216/0101/public/lectureexamples TA will illustrate how to get to this folder in lab CMSC 216 - Wood, Sussman, Herman, Plane 2

Chapter 2, Reek BASIC CONCEPTS CMSC 216 - Wood, Sussman, Herman, Plane 3

Overview Let s first go over the concepts discuss in this lecture via a set of examples Compilation gcc Wall file.c Executable a.out Fundamentals Example: evenvalues.c Example: computegrade.c CMSC 216 - Wood, Sussman, Herman, Plane 4

Compilation stages Preprocessor used to make sure the program parts see declarations they need (and other purposes too, e.g., macros) directives begin with a # (pound sign) do not end in a ; like C statements do Translation makes sure individual parts are consistent within themselves creates an object (.o) file Linking joins one or more compiled object files together includes matching function call to callee across separate files and matching global variable use to where it s defined makes sure caller/callee consistent with each other creates an executable file (the convention in Unix is no filename extension is used for executable files, although sometimes.x or.exe are used). Default: a.out CMSC 216 - Wood, Sussman, Herman, Plane 5

Basic C syntax is similar to Java Variable declarations format: typename variable1, variable2; each variable can optionally have an initializer examples: int a; float x, y; int m = 8; if appearing in a function, must appear before executable code may be local to a function, or global (remainder of the same file) Legal variable names similar to Java Looping and conditionals if, switch while, do/while, for Function calls format: functionname(argument1, argument2, ) Compound statement can go anywhere a single statement goes surrounded by { } can have block local variables declared at beginning CMSC 216 - Wood, Sussman, Herman, Plane 6

Output the printf() function Definition its declaration is included with #include <stdio.h> then just use it because its definition is in the C standard I/O library that the compiler always links for you Syntax: printf("formatstring", expression1, expression2, expression3,...); a function with a variable number of arguments! Within the format string, normal characters print as themselves, and: to print values of expressions use format specifiers, for example: %d print an integer in decimal %f print a floating point value %c print a character %s print a character string there are also special escape sequences, similar to Java: \n print a newline \t print a tab each format specifier in the formatstring is substituted by the corresponding expression, and the formatstring is printed CMSC 216 - Wood, Sussman, Herman, Plane 7

Input the scanf() function Definition its declaration is also included with #include <stdio.h> (it's also in the C standard I/O library) Syntax: scanf("formatstring", &variable1, &variable2, &variable3,...); the format string has the same basic structure as in printf() to match data entered in the input, use the same kinds of formatting expressions and format specifiers in the format string as for printf(): normal characters match themselves %d match a decimal integer %f match a floating point value %c match a character arguments (variable names) indicate where converted input is to be stored, and their types should match the format specifiers be careful that the variable names are all preceded by & (we'll discuss why) The format string matches against characters in the input stream until the last specifier is matched, or until matching fails Returns number of specifiers that are matched and assigns to variables CMSC 216 - Wood, Sussman, Herman, Plane 8

Chapter 3, Reek DATA CMSC 216 - Wood, Sussman, Herman 9

Data types in C C has four basic types: integers, floating point numbers, pointers, and aggregate types (e.g., arrays and structures) The first three are considered scalar types Integers include characters as well as the numbers you commonly think of Integer types have signed and unsigned versions Floating point types have (mostly) the same names as in Java: float and double CMSC 216 - Wood, Sussman, Herman 10

Data sizes in C Type name Minimum size Size on linux.grace char 1 1 short 2 2 int 2 4 long 4 8 float 4 4 double 8 8 long double 10 12 All sizes are in bytes. Note that variables of type char are guaranteed to always be one byte. There is no maximum size for a type, but the following relationships must hold: sizeof(short) <= sizeof(int) <= sizeof(long) sizeof(float) <= sizeof(double) <= sizeof(long double) CMSC 216 - Wood, Sussman, Herman 11

Numeric literals Suffixes allow you to specify a number is of a given type: 30000 is of type int, but 30000L is of type long 2.5 is of type double, but 2.5f is of type float Can also give numbers in different bases: Precede with a zero for octal: 017 == 15 Precede with 0x for hexadecimal: 0x1f == 31 CMSC 216 - Wood, Sussman, Herman 12

Can use these to represent things that only take on certain values Values equal to 0, 1, 2... based on order of definition Values can be set by programmer to things other than 0, 1, 2... Based on integers, but don't mix enums with integers unless absolutely necessary Enumerated types #include <stdio.h> int main() { enum Suit { }; SPADES, HEARTS, DIAMONDS = 42, CLUBS enum Suit suit1, suit2; suit1 = SPADES; suit2 = CLUBS; if (suit1 < suit2) else printf("spades are first.\n"); printf("clubs are first.\n"); printf("spades = %d, Clubs = %d\n", return 0; suit1, suit2); } CMSC 216 - Wood, Sussman, Herman 13

Variable declaration, initialization Declaring a variable provides space for it in memory "int x;" will cause 4 bytes on the stack to be reserved for x No initialization takes place! And unlike Java nothing will prevent you from using that random value. Consider this code: int x; printf("x: %d\n", x); It might print X: 0 It might also print X: -2349235, or X: 82373 It's up to you to initialize your variables properly Initialization can be done at the same time as variable declaration: int x = 42; CMSC 216 - Wood, Sussman, Herman 14

Identifier scope C has two main types of scope Block scope: a variable declared inside a block is visible only within the block (includes nested blocks inside that block) File scope: an identifier declared outside of any block is visible everywhere in the file after the declaration applies both to global variables and function names Example: global.c CMSC 216 - Wood, Sussman, Herman 15

Identifier linkage What happens if we encounter two instances of the same identifier across different files? A function named foo() in file1.c can cause problems if there's a function named foo() in file2.c We can resolve these types of conflicts by changing the linkage of the functions Linkage: a property of an identifier that determines if multiple declarations of that identifier refer to the same object Example: linkageexample folder CMSC 216 - Wood, Sussman, Herman 16

Three types of linkage Identifier linkage, cont. none: all declarations of an identifier refer to different entities (i.e., one copy per declaration) internal: all declarations of an identifier inside a given file refer to the same entity, but declarations across files refer to different entities (i.e., one copy per file) external: all declarations of an identifier refer to the same entity (i.e., one copy per program) Default linkage is different for different types of identifiers all functions and file scope variables default to external linkage variables with block scope default to no linkage Use extern and static to modify linkage CMSC 216 - Wood, Sussman, Herman 17

Storage How long is memory allocated for an object? After this function returns, is there any guarantee that the value 5 will stay at the spot in memory at which it was stored? int example(int i) { int j = 5; return i + j; } There are two types of storage: static and automatic; the variable j above has automatic storage, meaning it is no longer maintained after its function returns Example: static.c CMSC 216 - Wood, Sussman, Herman 18

Storage, cont. Static storage means that the variable exists throughout the entire life of the program global variables have this kind of storage Automatic storage is the default for blockscoped variables, but this can be changed with the static keyword Initializations to static variables occur only once CMSC 216 - Wood, Sussman, Herman 19