Your first C and C++ programs

Similar documents
Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Classes, Constructors, etc., in C++

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Linked Lists in C and C++

Exception Handling in C++

Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts

Programming Assignment #4 Binary Trees in C++

Containers and the Standard Template Library (STL)

A Deeper Look at Classes

Derived Classes in C++

KYC - Know your compiler. Introduction to GCC

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Iterators. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Arrays and Pointers in C & C++

CE221 Programming in C++ Part 1 Introduction

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

Introduction to C++ Introduction to C++ 1

Binary Trees (and Big O notation)

ENERGY 211 / CME 211. Evolution

CS240: Programming in C

Structures, Unions, and Typedefs

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CS11 Intro C++ Spring 2018 Lecture 1

Strings in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

6.S096 Lecture 1 Introduction to C

CSE 303: Concepts and Tools for Software Development

6.S096 Lecture 4 Style and Structure

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Spring 2018 NENG 202 Introduction to Computer Programming

(Extract from the slides by Terrance E. Boult

CS 61C: Great Ideas in Computer Architecture Introduction to C

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

Crash Course into. Prof. Dr. Renato Pajarola

EL2310 Scientific Programming

Programmazione. Prof. Marco Bertini

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

C - Basics, Bitwise Operator. Zhaoguo Wang

CS240: Programming in C. Lecture 2: Overview

EP241 Computer Programming

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

C, C++, Fortran: Basics

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

CS 376b Computer Vision

Course Information and Introduction

Chapter 1 Introduction to Computers and C++ Programming

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Welcome to... CS113: Introduction to C

COSC350 System Software

EC 413 Computer Organization

ANSI C. Data Analysis in Geophysics Demián D. Gómez November 2013

CS 253: Intro to Systems Programming 1/21

Tutorial-2a: First steps with C++ programming

CSE 303 Lecture 8. Intro to C programming

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

Cours de C++ Introduction

Software Lesson 1 Outline

Work relative to other classes

ENCE Computer Organization and Architecture. Chapter 1. Software Perspective

CPS1011. Program Structure {C};

CS 241 Data Organization. August 21, 2018

Multimedia-Programmierung Übung 3

Introduction to Programming

CS201 - Introduction to Programming FAQs By

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

CS133 C Programming. Instructor: Jialiang Lu Office: Information Center 703

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

Computers and Computation. The Modern Computer. The Operating System. The Operating System

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

Lab 1: First Steps in C++ - Eclipse

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Intermediate Programming, Spring 2017*

University of Colorado at Colorado Springs CS4500/ Fall 2018 Operating Systems Project 1 - System Calls and Processes

EL2310 Scientific Programming

Exercise 1.1 Hello world

Programming in C and C++

by Pearson Education, Inc. All Rights Reserved.

Programming in C and C++

Compiler Drivers = GCC

Embedded Systems Programming

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 1. Types Variables Expressions & Statements 2/23

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

Beyond this course. Machine code. Readings: CP:AMA 2.1, 15.4

COMP322 - Introduction to C++ Lecture 01 - Introduction

Problem Set 1: Unix Commands 1

CSI33 Data Structures

PROGRAMMAZIONE I A.A. 2017/2018

EECS2031 Software Tools

Introduction. Instructor: Jia Xu CSCI-135

COMP1917: Computing 1 1. Introduction

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

COMP1917: Computing 1 1. Introduction

A software view. Computer Systems. The Compilation system. How it works. 1. Preprocesser. 1. Preprocessor (cpp)

2 Compiling a C program

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

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

Transcription:

Your first C and C++ programs Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by Bjarne Stroustrup, and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel) 1

Your first C program /* * helloworld.c - "hello, world" program */ #include <stdio.h> int main (void) { printf( Hello, World!\n ); return 0; } CS-2303, A-Term 2012 Your first C and C++ programs 2

Your first C program (continued) /* * helloworld.c - "hello, world" program */ #include <stdio.h> int main (void) { printf( Hello, World!\n ); return 0; } A comment: Surrounded by /* and */ or // to end of line Same as in Java CS-2303, A-Term 2012 Your first C and C++ programs 3

Your first C program (continued) /* * helloworld.c - "hello, world" program */ #include <stdio.h> int main (void) { printf( Hello, World!\n ); return 0; } main must return zero if successful, otherwise some non-zero value so OS can recognize an error A function definition Note: by convention, operating system invokes function named main to start a program. CS-2303, A-Term 2012 Your first C and C++ programs 4

Your first C program (continued) /* * helloworld.c - "hello, world" program */ #include <stdio.h> int main (void) { printf( Hello, World!\n ); return 0; } main must return zero if successful, otherwise some non-zero value so OS can recognize an error (arguments to main to be discussed later in course) A function definition Note: by convention, operating system invokes function named main to start a program. CS-2303, A-Term 2012 Your first C and C++ programs 5

Your first C program (continued) /* * helloworld.c - "hello, world" program */ #include <stdio.h> int main (void) { printf( Hello, World!\n ); return 0; } Question: where is printf declared? printf A call to the C library function to format and print a string of text See K & R 1.1, 1.2, 1.5.3, and 7.2 CS-2303, A-Term 2012 Your first C and C++ programs 6

Your first C program (continued) /* * helloworld.c - "hello, world" program */ #include <stdio.h> Answer: In this file! int main (void) { printf( Hello, World!\n ); return 0; } Major difference from Java An include file in C or C++ (a.k.a. a header file ) is a text file declaring a bunch of stuff that is provided elsewhere! o E.g., in libraries OR in other C programs (i.e., modules) that you create Copied bodily into your program as first step of compilation! CS-2303, A-Term 2012 Your first C and C++ programs 7

Questions? 8

Your first C++ program Note file extension An include file (as in C) // helloworld.cpp #include <iostream> int main(void) { std::cout << Hello World! << std::endl; return 0; } A (static) object of the standard class ostream which is a subclass of iostream CS-2303, A-Term 2012 Your first C and C++ programs 9

Your first C++ program (continued) // helloworld.cpp #include <iostream> int main(void) { std::cout << Hello World! << std::endl; return 0; } The left shift operator applied to an ostream object and a string constant Another left shift operator, applied to the constant endl (defined in iostream) CS-2303, A-Term 2012 Your first C and C++ programs 10

Your first C++ program (continued) // helloworld.cpp #include <iostream> int main(void) { std::cout << Hello World! << std::endl; return 0; } The scope resolution operator Provides access to names declared in a class CS-2303, A-Term 2012 Your first C and C++ programs 11

Questions? 12

What happens to your code after it has been compiled but before it runs? 13

Compilation (part 1) HelloWorld.c Preprocessor Modifies the source code Creates internal data structure representing the program Language translator Code generator & optimizer Generates machine code HelloWorld.o However, program is not yet ready to execute! 14

Compilation (continued) gcc HelloWorld.c Preprocessor Translator Code gen & opt. HelloWorld.o gcc printf.c Preprocessor Translator Code gen & opt. printf.o ar (i.e., archive) Linker (part of gcc) Library of.o files for common routines a.out (or file name of your command or program) Note: this topic is developed in greater detail in CS-2011 Loader (part of OS) Memory 15

Questions? 16

Laboratory Assignment #1 Wednesday, August 29 Edit, compile, and run both Hello, World! programs I.e., in C and C++ On CCC Linux Use gcc for compiler Also, write a short program to do a simple numerical calculation Based on your knowledge of Java If time, work on Programming Assignment #1 Due Friday, August 31 17

Extra Laboratory Credit IF you already know how to do the laboratory AND and IF you have already submitted the required solution to Turnin you may earn extra laboratory credit by helping the TAs help others Speak to the TAs to take advantage of this 18

Questions? 19