CIS 190: C/C++ Programming. Lecture 1 Introduction and Getting Started

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

CS 106 Introduction to Computer Science I

Informatica e Sistemi in Tempo Reale

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

An Incomplete C++ Primer. University of Wyoming MA 5310

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

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

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

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

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

COSC282 BIG DATA ANALYTICS FALL 2015 LECTURE 2 - SEP 9

5 Arrays and Pointers

The C Programming Language course syllabus associate level

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

Java Interview Questions and Answers

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

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

public static void main(string[] args) { System.out.println("hello, world"); } }

Java Crash Course Part I

Computer Programming Tutorial

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Lecture 5: Java Fundamentals III

CP Lab 2: Writing programs for simple arithmetic problems

Number Representation

The programming language C. sws1 1

Introduction to Java

C++ Programming Language

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

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

El Dorado Union High School District Educational Services

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

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

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

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

Comp151. Definitions & Declarations

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Pemrograman Dasar. Basic Elements Of Java

Introduction to Programming II Winter, 2014 Assignment 2

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Install Java Development Kit (JDK) 1.8

Crash Course in Java

Java Basics: Data Types, Variables, and Loops

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

Introduction to Python

6.s096. Introduction to C and C++

arrays C Programming Language - Arrays

CS 253: Intro to Systems Programming

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Secrets of printf. 1 Background. 2 Simple Printing. Professor Don Colton. Brigham Young University Hawaii. 2.1 Naturally Special Characters

Programming Languages CIS 443

In this Chapter you ll learn:

Moving from CS 61A Scheme to CS 61B Java

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

1 Abstract Data Types Information Hiding

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

Vim, Emacs, and JUnit Testing. Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor

ECE 122. Engineering Problem Solving with Java

Introduction to Java. CS 3: Computer Programming in Java

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Illustration 1: Diagram of program function and data flow

CS 6371: Advanced Programming Languages

CMSC 202H. ArrayList, Multidimensional Arrays

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

Semantic Analysis: Types and Type Checking

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

java Features Version April 19, 2013 by Thorsten Kracht

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

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

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

Programming with Data Structures

Java from a C perspective. Plan

Lecture 22: C Programming 4 Embedded Systems

Object-Oriented Programming in Java

Chapter 13 - The Preprocessor

CS106A, Stanford Handout #38. Strings and Chars

How To Write Portable Programs In C

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

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

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

El Dorado Union High School District Educational Services

Lab Experience 17. Programming Language Translation

Basics of I/O Streams and File I/O

Stacks. Linear data structures

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

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

6.S096 Lecture 1 Introduction to C

Passing 1D arrays to functions.

6.1. Example: A Tip Calculator 6-1

Pseudo code Tutorial and Exercises Teacher s Version

CmpSci 187: Programming with Data Structures Spring 2015

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Object Oriented Software Design

Chapter 13 Storage classes

Fundamentals of Programming

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

Sample CSE8A midterm Multiple Choice (circle one)

Static Code Analysis Procedures in the Development Cycle

Answers to Review Questions Chapter 7

Transcription:

CIS 190: C/C++ Programming Lecture 1 Introduction and Getting Started

This course will teach you the basics of C and C++ give you more programming experience be appropriate for majors and non-majors not make you an expert in C or C++ not, by itself, make you ready to take on a C/C++ programming job, or design and write a professional C/C++ application enable you to learn more independently

Why learn C/C++? ubiquitous in software engineering today and into the future see the Tiobe Index helps you understand computers better close to the metal why start with C instead of C++? under the hood

Word of warning C is a language that values speed and programmer control over guaranteed safety. You are expected to develop habits that will keep you safe rather than doing random things and hoping to be caught. Kate Gregory

Administrivia grades based on homework (70%) and final project (30%) curved at end of semester this is recitation but it s actually the class Academic Honesty: work must be yours do not share code

Getting started editor xemacs, emacs, pico, vim setup guide for Windows machines on website compiler run in terminal; gcc using libraries: #include <libname.h> stdio.h, string.h, stdlib.h C and C++ require main()

hello_world.c #include <stdio.h> int main() { printf( Hello world!\n ); } return 0;

Compiling with C: gcc to compile a file to an executable called hello: gcc hello_world.c o hello to run the executable output:./hello to compile a file with warnings: gcc hello_world.c Wall -o hello

Anatomy of hello_world.c /* includes functions from the standard library, like printf */ #include <stdio.h> /* main entry point to our program */ int main() { /* prints Hello World! and a newline to screen*/ printf( Hello world!\n ); } /* returns the result code for the program 0 means there was no error */ return 0;

printf printf(<format string>, <arguments>); prints the given format string to the console <format string> is text you want to print and specifiers (like %s) for additional arguments the <arguments> are handled in order unlike System.out.println, need to insert line breaks manually: \n

printf format string specifiers there are lots the basic ones: %d: int (e.g., 7) %c: char (e.g., a ) %s: string (e.g., hello ) %f: float (e.g., 6.5) printf( It was %f %s at %d%c.\n, 72.5, degrees, 221, B ); > It was 72.5 degrees at 221B.

Precision with printf printf can also format the text it prints out: printf( _%4d_ or %6s or _%-6s_, 18, dog, a ); > _ 18_ or _ dog_ or _a _ place the precision number after the % for floats/doubles, you can specify both before and after the decimal: %4.2f spaces take up extra <precision> characters use a negative sign to left-align

scanf reads information from the console (user) need to know details about input (formatting) ignores whitespace characters information read in is stored in a variable, referenced by a pointer to that variable int x; scanf( %d, &x); same format specifiers as printf

for loops and local variables for loops work as expected, but local variables must be declared at the start of the function int main() { int i; for (i = 0; i < 14; i++) { printf( %d, i); } } > 0 1 2 3 4 5 6 7 8 9 10 11 12 13

Variable types in C unlike Java, there is no boolean or String in C, 0 = false, and everything else = true variables are also not initialized for you! int, char, float, double long, short, etc. signed and unsigned also, there are no vectors, only arrays there is no new keyword either

Arrays in C like in Java: Declaration: <type> <name> [size]; int xarray [10]; /* creates array of 10 ints */ Indexing: starts at 0: xarray[4]; /* 5th element*/ unlike Java: no bounds checking/built-in knowledge of size can t return an array from a function!

Declaring arrays in C arrays are static once they re created, their size is fixed size needs to be pre-determined at compile time for example, this means you cannot make it a value that is read in from the user unless you allocate the memory manually

EXAMPLE: Sieve of Eratosthenes algorithm to quickly identify prime numbers first, assume all numbers >= 2 are prime: X X 2 3 4 5 6 7 8 9 10 11 12 13 then, take the first prime number and mark all of its multiples as composite: 0 1 2 3 X 5 X 7 X 9 X 11 X 13 and so on: 0 1 2 3 4 5 X 7 8 X 10 11 X 13

Functions in C similar to in Java, but not exactly: <return value> <name> (<parameters>) no public static necessary three parts to using a function in C: function prototype function definition function call

Function Parts Prototype: function must be declared before it can be used int SquareNumber (int n); Definition: function must be defined int SquareNumber (int n) { return (n * n); } Call: int answer, input = 3; answer = SquareNumber(input);

Functions in hello_world.c #include <stdio.h> void PrintHelloWorld(); /* function prototype */ int main() { PrintHelloWorld(); /* function call */ return 0; } void PrintHelloWorld() /* function definition */ { printf( Hello world!\n ); }

C-style strings no native String type in C represented as an array of characters terminated by the NULL character, \0 a C-string joke: Two strings walk into a bar. The bartender says, "What'll it be?". The first string says, "I'll have a gin and tonic#mv*()>sdk+!^&@p&]jea&#65535". The second string says, "You'll have to excuse my friend, he's not null-terminated."

C-style strings three ways to declare a string (statically) char *str1 = dog ; char str2 [] = cat ; char str3 [5]; first two ways require initial value; length is set at that initial string s length third way creates string of length 5

C strings are arrays of characters char *str1 = dog ; element 0 1 2 3 char d o g \0 char str2 [] = cat ; element 0 1 2 3 char c a t \0 char str3 [5]; element 0 1 2 3 4 char. N = 8 str3 was only declared, not initialized, so it s filled with garbage and has no null terminator

C strings are arrays! just like you can t compare two whole arrays, you can t just compare strings str1 == str2 will not do what you think library of string functions #include <string.h> strcmp will compare two strings: int same = strcmp(str1, str2); strcpy will copy the second string into the first strcpy(str1, success! );

Homework 1 Hello, World! www.cis.upenn.edu/~cis190 make sure that you have a working setup SEAS account editor eniac.seas.upenn.edu compiler check that you can submit no mercy! hello_world.c, answers.txt take credit for your code comment at top!