1 Intro to C/C++ #include using namespace std; int main() { cout << “Hello world!” << endl; return 0; } #include int main() { printf(“Hello world!\n”);

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

Programming Languages and Paradigms The C Programming Language.
MPI and C-Language Seminars Seminar Plan (1/3)  Aim: Introduce the ‘C’ Programming Language.  Plan to cover: Basic C, and programming techniques.
Informática II Prof. Dr. Gustavo Patiño MJ
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles C/C++ Emery Berger and Mark Corner University of Massachusetts.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Basics of Linux On linux machine: Login at your home directory Open a “shell” or “terminal” or “xterm” workspace (4) On windows machine Intall linux.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between kloc.
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
Introduction to Linux and Shell Scripting Jacob Chan.
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
Object Oriented Programming in C++ Dr. Hammadi Nait-Charif Media School Bournemouth University
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
ITEC 320 C++ Examples.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
Pointers OVERVIEW.
INTRODUCTION TO CSCE LAB BASIC OF COMPUTERS, C++, UNIX, AND HELLO WORLD.
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
C++ Memory Overview 4 major memory segments Key differences from Java
Dynamic memory allocation and Pointers Lecture 4.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3.
C By Example 1 The assumption is that you know Java and need to extend that knowledge so you can program in C. 1. Hello world 2. declarations 3. pass by.
CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.
Navigating the C++ Development Environment
C++ (intro) Created by Hwansoo Han Edited by Ikjun Yeom.
1 CSC2100B Data Structure Tutorial 1 Online Judge and C.
System Programming Practical Session 7 C++ Memory Handling.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
C:\Temp\Templates 4 5 Use This Main Program 6.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Object Oriented Programming Lecture 2: BallWorld.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
C++ Lesson 1.
UNIX To do work for the class, you will be using the Unix operating system. Once connected to the system, you will be presented with a login screen. Once.
Overview 4 major memory segments Key differences from Java stack
C/C++ Tutorial.
A bit of C programming Lecture 3 Uli Raich.
C Programming Tutorial – Part I
C++ in 90 minutes.
C programming language
C Basics.
C Programming Lecture Series
Overview 4 major memory segments Key differences from Java stack
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Created by Hwansoo Han Edited by Ikjun Yeom
Dynamic Memory A whole heap of fun….
C Programming Getting started Variables Basic C operators Conditionals
C By Example The assumption is that you know Java and need to extend that knowledge so you can program in C. 1. Hello world 2. declarations 3. pass.
Pointers and dynamic objects
The Stack.
COP 3330 Object-oriented Programming in C++
Pointers, Dynamic Data, and Reference Types
C Tutorial Adapted from Wei Qian’s C tutorial slides.
Presentation transcript:

1 Intro to C/C++ #include using namespace std; int main() { cout << “Hello world!” << endl; return 0; } #include int main() { printf(“Hello world!\n”); return 0; } C++ C

2 Compile & run Compile: g++ –g -o hello hello.cpp -g: allow debugging Run: hello or./hello

3 Intrinsic types byte short int long float double char boolean JavaC/C++ char short int long float double char boolean

4 Conditionals bool temp = true; int i = 2; if(temp) cout << “Hello world!” << endl; if(temp==true) cout << “Hello world!” << endl; if(i) cout << “Hello world!” << endl; An non-zero value is true (better to use 1). A zero value is false.

5 Control flow for loop: for(i=1; i<=9; i++){…} while loop: while(cond){…} if, else if(cond){…} else{…} do, switch

6 File I/O Standard I/O cout and cin are special files cout << “hello.” << endl; (print hello on screen) cin >> a; (read a from the console) File I/O Open file Read/write Close file

7 Command-line arguments #include using namespace std; int main(int argc, char * argv[]) { int num; ifstream input; if(argc != 3){ cout " << endl; exit(1); } num = atoi(argv[1]); input.open(argv[2]); input.close(); return 0; }

8 Key differences between C/C++ and Java Pointers Assignment Parameter passing Heap & Stack Arrays

9 Pointers C/C++ Pointer is just an address Address: location of a piece of data in memory &x: the address of x; *ptr: dereference ptr Dereference pointer to a class Someclass *ptr; //declare a pointer cout a << endl; cout << (*ptr).a << endl;

10 Pointers II #include using namespace std; int main(int argc, char * argv[]) { int x; int * ptr; x = 5; ptr = &x; cout << "ptr " << *ptr << endl; //print 5 x=10; cout << "ptr " << *ptr << endl; //print 10 return 0; }

11 Pointer exercises Data structure for an item struct myItem{ int val; myItem * ptr; }; Create a linked list with two items Print the value of items in the list Remove the last item in the list

12 One way to do it #include struct myItem{ int val; myItem * ptr; }; int List_Items(myItem * head) { myItem *cur_item; cur_item = head; while(cur_item != NULL){ printf("cur_item value %d\n", cur_item->val); cur_item = cur_item -> ptr; } return 1; }

13 One way to do it (cont’d I) int main() { myItem *head; myItem *item1, *item2; item1 = new(myItem); item1->val = 1; item1->ptr = NULL; item2 = new(myItem); item2->val = 2; item2->ptr = NULL; //now link the items head = item1; item1->ptr = item2; //list the items List_Items(head);

14 One way to do it (cont’d II) //now delete the last item in the list (last item ptr==NULL) myItem *cur_item, *next_item; cur_item = head; next_item = cur_item->ptr; while(next_item->ptr != NULL){ cur_item = next_item; next_item = next_item->ptr; } delete(next_item); cur_item->ptr = NULL; printf("after remove the last item:\n"); List_Items(head); return 1; }

15 Assignment Java assignment: makes reference C++ assignment: makes copy SomeClass x, y; Someclass *a; x=y; //this copies y to x; changing x does not change y a = &y; //copies the reference (address) of y to a; a->val += 10; //this changes y too

16 Parameter passing: default by value #include using namespace std; void foo(int para){ // para += 10; } int main(int argc, char * argv[]) { int i=5; foo(i); //all parameters copied by default cout << "i " << i << endl; }

17 Parameter passing: how to change value? (pass pointer or call by references) #include using namespace std; void foo(int * para){ *para += 10; } void bar(int & para){ para += 10; } int main(int argc, char * argv[]){ int i=5; foo(&i); cout << “after foo: i " << i << endl; bar(i); cout << “after bar: i " << i << endl; return 0;}

18 Stack & heap Stack: regions of memory for temporaries Stack pointer pushed in on function entry Stack pointer pushed out on function exit Heap: regions of memory for persistent data C/C++: explicitly managed

19 Big stack mistake (never return pointer to a stack!) #include using namespace std; int * foo( ) { int b=10; return &b; } int main(int argc, char * argv[]) { int *a; a = foo(); cout << "a: " << *a << endl; //print 10? return 1; }

20 Return value from heap (Good!) #include using namespace std; int * foo() { int *b = new (int); *b = 10; return b; } int main(int argc, char * argv[]) { int *a; a = foo(); cout << "a: " << *a << endl; //print 10? return 1; }

21 Explicit memory management You must delete items (or memory leak) Deleting items too soon – crash Deleting items twice – crash Good practice: int * a = new(int); After deleting a, do a=NULL; if(a!=NULL) delete a;

22 Struct: class with everything public #include using namespace std; struct foo { int mem1, mem2; }; int main(int argc, char * argv[]) { foo a, *b; a.mem1 = 1; a.mem2 = 2; b = &a; b->mem1 += 10; cout mem1 << endl; //print ? return 1; }

23 Class declaration: similar to Java class IntCell { public: IntCell (int initVal = 0){ storedVal = initVal; } int getVal() {return storedVal;} int setVal(int val) {storedVal = val;} private: int storedVal; } ;

24 Arrays Arrays do not have to be allocated with new Array bounds not checked (careful!) Avoid pointer arithmetic item[12]=0; Don’t use: v=12, *(item+v)=0; Multiple dimensional array int item[10][20];

25 Other features - I Assert (good style) int *ptr = NULL; … assert(ptr != NULL); Typedef typedef int * p_int; p_int a; //a is a pointer to an integer comments Same syntax as in java

26 Other features II #define #define MAX 100 int myArray[MAX]; Static May not mean the same thing as in java Declaring a global variable or function as static means that it is only visible in this file.

27 Basics of Linux On linux machine: Login at your home directory Open a “shell” or “terminal” or “xterm” workspace (4) On windows machine Install linux terminals (xmanager, putty, cygwin) Login

28 Basic commands of Linux I Command format: [command] [arguments] Search information on a command: man [command] e.g., man pwd Print current directory: pwd List files and directories in a directory: ls ls –l Only list directories: ls -ld Current directory:. upper-level directory:..

29 Basics Commands of Linux II File/directory operations: Copy file x to y: cp x y Remove file x: rm x be very careful, rm –i x Move file x to y (rename): mv x y Create new directory x: mkdir x Change directory: Go to upper level: cd.. Go to directory x: cd x Remove directory: rm –r x Rename directory x to y: mv x y

30 Basics Commands of Linux III State of processes: ps (process state) ps –aux ps –aux | grep bing Kill First use ps to find the ID of a process kill pid Pipeline command: e.g., ps -aux | more

31 Basics Commands of Linux IV Mode of a program: list mode of f: ls –l f 3 components: for owner, group member, others 3 bits in each component: r, w, x e.g., mode of 755 means? Change the mode of a file: chmod E.g., chmod 755 f Change owner of a program: chown

32 Programming on Linux Write your code (using e.g., emacs, xemacs, vi, or eclipse) Compile: e.g., g++ –g -o hello hello.cpp Run: e.g., hello or./hello Debug: print messages using gdb( locate segmentation fault) or xxgdb (better interface)