Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

Similar documents
Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print

Parameter Passing in C. Pointer Arithmetic. Parameter Passing in C. Pointer Arithmetic. Pointer Arithmetic. Tevfik Ko!ar

Environment Variables

CSC Systems Programming Fall Lecture - XVIII Concurrent Programming. Tevfik Ko!ar. Louisiana State University. November 11 th, 2008

Roadmap. Concurrent Programming. Concurrent Programming. Motivation. Why Threads? Tevfik Ko!ar. CSC Systems Programming Fall 2010

Roadmap. Concurrent Programming. Concurrent Programming. Communication Between Tasks. Motivation. Tevfik Ko!ar

CSC Systems Programming Fall Lecture - XV Network Programming - I. Tevfik Ko!ar. Louisiana State University. November 9 th, 2010

Work relative to other classes

Lecture 03 Bits, Bytes and Data Types

CSC Systems Programming Fall Lecture - XIV Concurrent Programming. Tevfik Ko!ar. Louisiana State University. November 2nd, 2010

Lecture 3. More About C

The C language. Introductory course #1

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

BSM540 Basics of C Language

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Using make. Using Variables in Makefiles. Implicit Rules. Static vs Dynamic Libraries. Libraries. Tevfik Ko!ar

EL2310 Scientific Programming

A Quick Look at C for C++ Programmers

Chapter 11 Introduction to Programming in C

SU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator

Chapter 11 Introduction to Programming in C

CSC231 C Tutorial Fall 2018 Introduction to C

PRINCIPLES OF OPERATING SYSTEMS

CSE 303 Lecture 8. Intro to C programming

Lectures 5-6: Introduction to C

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

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

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

F28HS2 Hardware-Software Interface. Lecture 1: Programming in C 1

The Fundamentals. Port Assignments. Common Protocols. Data Encapsulation. Protocol Communication. Tevfik Ko!ar

CSC Systems Programming Fall Lecture - XXIII Final Review. Tevfik Koşar. Louisiana State University

Dynamic memory allocation (malloc)

EL2310 Scientific Programming

Chapter 11 Introduction to Programming in C

CS 241 Data Organization Pointers and Arrays

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

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

SWEN-250 Personal SE. Introduction to C

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

SU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar)

High Performance Programming Programming in C part 1

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Programming Language Basics

CSC 2400: Computer Systems. Arrays and Strings in C

C for C++ Programmers

Saint Louis University. Intro to Linux and C. CSCI 2400/ ECE 3217: Computer Architecture. Instructors: David Ferry

Contents. A Review of C language. Visual C Visual C++ 6.0

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

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

Chapter 11 Introduction to Programming in C

CS 61c: Great Ideas in Computer Architecture

CSE 303: Concepts and Tools for Software Development

Lectures 5-6: Introduction to C

Dynamic Memory Allocation and Command-line Arguments

CSC 2400: Computer Systems. Arrays and Strings in C

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Lecture 7: file I/O, more Unix

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Should you know scanf and printf?

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

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

Continued from previous lecture

CS2141 Software Development using C/C++ C++ Basics

Online Judge and C. Roy Chan. January 12, Outline Information Online Judge Introduction to C. CSC2100B Data Structures Tutorial 1

1. The Mac Environment in Sierra Hall 1242

CSC 1600 Memory Layout for Unix Processes"

Goals of C "" The Goals of C (cont.) "" Goals of this Lecture"" The Design of C: A Rational Reconstruction"

ECE 250 / CPS 250 Computer Architecture. C Programming

SU 2017 May 18/23 LAB 3 Bitwise operations, Program structures, Functions (pass-by-value), local vs. global variables. Debuggers

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

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

Arrays, Strings, & Pointers

211: Computer Architecture Summer 2016

Arrays and Pointers. CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik. University of Toronto Mississauga

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Array Initialization

mith College Computer Science CSC231 Bash Labs Week #10, 11, 12 Spring 2017 Introduction to C Dominique Thiébaut

COMP 2355 Introduction to Systems Programming

ECE 250 / CS 250 Computer Architecture. C Programming

CSC 1107: Structured Programming

Pointers cause EVERYBODY problems at some time or another. char x[10] or char y[8][10] or char z[9][9][9] etc.

Lecture 2: C Programming Basic

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

Arrays and Pointers (part 2) Be extra careful with pointers!

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

C: Introduction, Pointers Instructors: Steven Ho, Nick Riasanovsky

The Design of C: A Rational Reconstruction: Part 2

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

Programming in C. What is C?... What is C?

Transcription:

CSC 4304 - Systems Programming Fall 2008 Lecture - II Basics of C Programming Summary of Last Class Basics of UNIX: logging in, changing password text editing with vi, emacs and pico file and director operations file/dir permissions and changing them Tevfik Ko!ar Louisiana State University August 28 th, 2008 1 2 Processes ps : list currently active user processes ps aux: list all active processes in long format kill n : kill process with id=n kill -9 n : force to kill CTRL-z : push to background fg : bring to foreground (also fg n: bring nth process) top: system utilization information time command : calculate time for a given command C vs. Java C is procedural, not object oriented C has no objects, interfaces or packages A program only consists of functions and data C is compiled, not interpreted -Translated directly to assembly language -Faster, less portable and very hard to debug. C has no array bounds, null pointer or cast checks -You have to detect and handle all problems yourself C has no garbage collector -You have to do all of the memory management yourself 3 4 C vs. Java (cont.) C has pointers Similar to Java references but......they can be used in calculations (pointer arithmetic) Allows you to use the location of data in computations (not just the value) Useful, powerful and a debugging nightmare! Compared to Java, C is a low-level language - You can and must do everything yourself - Suitable for low-level software like device-drivers, communication libraries, operating systems, etc. S You can implement anything in C! No limits! 5 A Java program consists of: Several classes, one class per file. A main method in one of these classes. External class libraries (jar files). C vs. Java (cont.) A C program consists of: - Several functions in any number of files. - A main function in one of these files. - Possibly some header files. - External libraries with their own header files. 6

C can be quite complex Basic C Program This program computes and prints the first 800 decimals of PI: long a=10000,b,c=2800,d,e,f[2801],g; int for(;b-c;)f[++b]=a/5; for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a) for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b); 7 8 Basic C Program Print to the screen (stdout) prog1.c: printf( Hello, CSC4304 Class!\n ); gcc prog1.c ==> a.out gcc prog1.c -o prog1 ==> prog1 make prog1 ==> prog1 9 10 Header Files Read argument and print // take arguments from stdin main(int argc, char* argv[]) printf( Hello, %s!\n, argv[1]); 11 12

Read argument and print Read from stdin and print main(int argc, char* argv[]) if (argc < 2) printf("usage: %s <your name>\n", argv[0]); else printf("hello, %s!\n", argv[1]); char name[64]; printf( What s your name? ); scanf( %s, name); printf( Hello, %s!\n, name); 13 14 Basic Data Types Basic Types char : character - 1 byte short: short integer - 2 bytes int: integer - 4 bytes long: long integer - 4 bytes float: floating point - 4 bytes double - double precision floating point - 8 bytes Formatting Template %d: integers %f: floating point %c: characters %s: string %x: hexadecimal %u: unsigned int 15 char var1; float f; Formatting printf("enter a character:"); scanf("%c", &var1); printf("you have entered character:%c. ASCII value=%d, Address=%x\n", var1, var1, &var1); f = (float)var1; printf(".. And its float value would be: %.2f\n", f); 16 Formatting (cont.) Arrays 17 18

Strings Manipulating Arrays 19 20 Manipulating Strings Manipulating Strings (cont.) 21 22 Comparison Operators Example int x = 5; int y = 3; if (x=y) printf("x is equal to y, x =%d, y=%d\n", x, y); else printf("x is not equal to y, x =%d, y=%d\n", x, y); 23 24

Classical Bugs Loops while (x>0)... do... while (x>0); for (x=0; X<3;x++)... 25 26 Functions Exercises 1. Write a program which defines an integer, a float, a character and a string, then displays their values and their sizes on screen. /*use the sizeof() function*/ 2. Write a program which computes and displays fib(n), where n is a parameter taken from command line: fib(0) = 0, fib(1) = 1 If n > 1 then fib(n) = fib(n - 1) + fib(n - 2) S 27 28 Summary Acknowledgments C Basics C vs Java Writing to stdout Taking arguments Reading from stdio Basic data types Formatting Arrays and Strings Comparison Operators Loops Functions Hmm.. Advanced Programming in the Unix Environment by R. Stevens The C Programming Language by B. Kernighan and D. Ritchie Understanding Unix/Linux Programming by B. Molay Lecture notes from B. Molay (Harvard), T. Kuo (UT- Austin), G. Pierre (Vrije), M. Matthews (SC), and B. Knicki (WPI). 29 30