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

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

Programming. Data Structure

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

ECET 264 C Programming Language with Applications

ECET 264 C Programming Language with Applications

Environment Variables

Principles of C and Memory Management

Work relative to other classes

Introduction to C. Systems Programming Concepts

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

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

BSM540 Basics of C Language

Lecture 03 Bits, Bytes and Data Types

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

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

PRINCIPLES OF OPERATING SYSTEMS

CSE 303 Lecture 8. Intro to C programming

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

EL2310 Scientific Programming

Pointers (2) Applications

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

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

A Quick Look at C for C++ Programmers

High Performance Programming Programming in C part 1

Dynamic memory allocation (malloc)

Chapter 11 Introduction to Programming in C

CSC231 C Tutorial Fall 2018 Introduction to C

EL2310 Scientific Programming

The C language. Introductory course #1

Chapter 11 Introduction to Programming in C

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

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

Lecture 3. More About C

Dynamic Memory Allocation and Command-line Arguments

CS 241 Data Organization Pointers and Arrays

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

THE FUNDAMENTAL DATA TYPES

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

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

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

A Short Course for REU Students Summer Instructor: Ben Ransford

INTRODUCTION TO C-PROGRAMING LANGUAGE

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

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

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

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

Chapter 11 Introduction to Programming in C

Arrays and Pointers in C. Alan L. Cox

Lectures 5-6: Introduction to C

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

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

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

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

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

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

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

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

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

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

Programming Language Basics

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

C Pointers. sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then

Pointers (continued), arrays and strings

CS 61c: Great Ideas in Computer Architecture

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

COMP 2355 Introduction to Systems Programming

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

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

Incoming Exam. CS 201 Introduction to Pointers. What is a Pointer? Pointers and Addresses. High Speed Memory (RAM) Size of Variable Types.

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

ISA 563 : Fundamentals of Systems Programming

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

Basic Types and Formatted I/O

Continued from previous lecture

Chapter 11 Introduction to Programming in C

Princeton University Computer Science 217: Introduction to Programming Systems The C Programming Language Part 1

2/12/17. Goals of this Lecture. Historical context Princeton University Computer Science 217: Introduction to Programming Systems

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

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University


First of all, it is a variable, just like other variables you studied

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

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

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

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

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

Programming. Elementary Concepts

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

Pointers (continued), arrays and strings

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

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

ELEC 377 C Programming Tutorial. ELEC Operating Systems

Transcription:

CSC 4304 - Systems Programming Fall 2010 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 directory operations file/dir permissions and changing them process monitoring and manipulation Tevfik Ko!ar Louisiana State University August 26 th, 2010 1 2 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 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! 4 C vs. Java (cont.) C can be quite complex A Java program consists of: Several classes, one class per file. A main method in one of these classes. External class libraries (jar files). 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. 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); 5 6

Basic C Program Basic C Program: Print to stdout printf("hello, CSC4304 Class!\n ); 7 8 Basic C Program: Print to stdout Header Files printf("hello, CSC4304 Class!\n"); gcc prog1.c ==> a.out gcc prog1.c -o prog1 ==> prog1 make prog1 ==> prog1 9 10 Read argument and print Read argument and print // take arguments from stdin main(int argc, char* argv[]) printf("hello, %s!\n", argv[1]); main(int argc, char* argv[]) if (argc < 2) printf("usage: %s <your name>\n", argv[0]); else printf("hello, %s!\n", argv[1]); 11 12

Read from stdin and print Basic Data Types char name[64]; printf("what s your name?"); scanf("%s", name); printf("hello, %s!\n", name); 13 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 14 Test Size of Data Types printf("sizeof(char): %d\n", sizeof(char)); printf("sizeof(short): %d\n", sizeof(short)); printf("sizeof(int): %d\n", sizeof(int)); printf("sizeof(long): %d\n", sizeof(long)); printf("sizeof(float): %d\n", sizeof(float)); printf("sizeof(double): %d\n", sizeof(double)); 15 char var1; float f; Formatting printf(" Enter a character:"); scanf("%c", &var1); printf("you have entered character:%c \n ASCII value=%d \n Address=%x\n", var1, var1, &var1); printf(" And its float value would be: %.2f\n", (float)var1); 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-axes=%d, y=%d\n", x, y); 23 24

Classical Bugs Loops while (x>0)... do... while (x>0); Exercise: for (x=0; X<3;x++)... - (7 & 8) vs (7 && 8) - (7 8) vs (7 8) 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