C Homework Write mypaste.c and mycomm.c – mypaste is like paste & mycomm is like comm – Both take two files as arguments – Paste reads a line from each.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
C Intro.
C Programming Day 1 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Command Line arguments Main function: int main(argc, char *argv[]) –argc is the count of command line arguments –argv is an array of strings argv[0] is.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
1 Homework Assignments Turn in HW1 (If not done yet, catch up!) Questions about HW1? Anyone still stuck on apply / UNIX account? Everyone have the books?
CS1061 C Programming Lecture 17: Steams and Character I/O A. O’Riordan, 2004.
Character Input and Output C and Data Structures Baojian Hua
Imperative Programming Prof. Béat Hirsbrunner Amine Tafat, PhD Student Matthias Buchs and Raphaël Lesceux, Graduate Students Department of Informatics.
The little language that could Remember C is a “small language”
Prof. Béat Hirsbrunner Fulvio Frapolli, PhD Student (exercises) Bachelor students : - Major in computer science (first year, 2nd term) - Major in information.
Character Input and Output
CPSC 441 Tutorial TA: Fang Wang Introduction to C.
C Programming A Modern Approach
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
Advanced UNIX Shell Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Strlen() implementation /* strlen : return length of string s */ int strlen(char *s) { int n; for (n = 0 ; s[n] != ‘\0’ ; n++) ; return n; } /* strlen.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
Outline Symbolic Constants Character Input and Output if … else switch…case.
#include int main(void) { printf("Hello, world!\n"); return 0; } entry point called on program start only one main( ) in any program # for preprocessor.
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
5. Arrays, Pointers and Strings 7 th September IIT Kanpur C Course, Programming club, Fall
File IO and command line input CSE 2451 Rong Shi.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
1 Homework HW6 On line – due next class Starting K&R Chapter 7 and Appendix B Also, UNIX – Various chapters in Glass.
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
A Quick Look at C for C++ Programmers Noah Mendelsohn Tufts University Web: COMP.
Chapter 1 Basic C Programming
Announcements Assignment 1 will be regraded for all who’s score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
C is a high level language (HLL)
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
Hank Childs, University of Oregon April 15 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
 Dynamic Memory Allocation  Linked Lists  void main() {{ ◦ FILE *file; ◦ char file_name[] = “file.txt”; ◦ if ((file = fopen(file_name, "w")) ==
A bit of C programming Lecture 3 Uli Raich.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Lecture 8 String 1. Concept of strings String and pointers
Lecture 11 File input/output
C Programming Tutorial – Part I
Command Line Arguments
CSE 351 Section 1: HW 0 + Intro to C
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
C programming language
Computer Science 210 Computer Organization
CS111 Computer Programming
5. Arrays, Pointers and Strings
Lecture 8b: Strings BJ Furman 15OCT2012.
CSC215 Lecture Input and Output.
Govt. Polytechnic,Dhangar
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
Introduction to C Topics Compilation Using the gcc Compiler
Your questions from last session
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
C Programming Getting started Variables Basic C operators Conditionals
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C EECS May 2019.
File I/O & UNIX System Interface
I/O CS580U - Fall 2018.
Extra C Material Based on material in: The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie. Prentice Hall, Inc., 1988. 
Presentation transcript:

C Homework Write mypaste.c and mycomm.c – mypaste is like paste & mycomm is like comm – Both take two files as arguments – Paste reads a line from each file and outputs them to stdout on the same line – Comm compares the two lines with strcmp and outputs the one that comes first in alphabetical order (with the appropriate number of tabs)strcmp

hello.c #include main() { printf("hello, world\n"); } New Concepts – C is a compiled Interpreted languages: python, javascript, R, lisp – cc –o hello hello.c –./hello

Make make clean make all Makefile

Input/Output: mycat.c (My Cat) #include main() { int c; while((c = getchar()) != EOF) putchar(c); } New Concepts – Stdio: getchar/putchar – Variable declarations

mywc.c (My Word Count) #include #include main() { int c, bytes=0, words=0, lines=0; int prev_state=0; while((c = getchar()) != EOF) { bytes++; if(c == '\n') lines++; int current_state = isalnum(c) || ispunct(c); if(!prev_state && current_state) words++; prev_state = current_state; } printf("%d\t%d\t%d\n", lines, words, bytes); } New Concepts – ++ – ‘\n’ – printf – isalnum, ispunct – man isalnum INOUT words++

mywc2.c: ac & av #include #include void wc_fd(FILE *fd, char *filename) { int c, bytes=0, words=0, lines=0; int prev_state=0; while((c = getc(fd)) != EOF) { bytes++; if(c == '\n') lines++; int current_state = isalnum(c) || ispunct(c); if(!prev_state && current_state) words++; prev_state = current_state; } printf("%d\t%d\t%d\t%s\n", lines, words, bytes, filename); if(fd && (fd != stdin)) fclose(fd); } void wc(char *filename) { if(strcmp(filename, "-") == 0) { wc_fd(stdin, filename); return; } FILE *fd = fopen(filename, "r"); if(!fd) printf("NA\tNA\tNA\t%s\n", filename); else wc_fd(fd, filename); } int main(int ac, char **av) { int i; for(i=0;i<ac;i++) fprintf(stderr, "av[%d] = %s\n", i, av[i]); if(ac <= 1) wc("-"); else for(i = 1; i<ac; i++) wc(av[i]); }

Pointer Addition #include main() { char *str = "hello, world\n"; for( ; *str; str++) printf("%s", str); }

hello3 #include #define MAXLINE 1024 int main() { char line[MAXLINE]; while(fgets(line, MAXLINE, stdin) != NULL) { char *str = line; for( ; *str; str++) if(str == line || (isblank(str[-1]) && !isblank(str[0]))) printf("%s", str); } } Subscripting – x[i] – (x+i)[0] – *(x+i)

Concepts so far… Interpreted v. Compiled – cc, gcc, make stdio Variable declarations Strings: pointers to sequences of characters Pointer addition Subscripting by positive & negative numbers – x[i] – (x+i)[0] – *(x+i)

Structures struct box { int ptr:30; unsigned int type:2; }; struct pair { struct box first, rest; } heap[HEAPSIZE];

Boxes: Pointer + Type 3 Types – SYMBOL nil, +, *, quote – LIST first + rest – NUMBER int

first(cons(left, right))  left rest(cons(left, right))  right

Pname: Symbol  String Intern: String  Symbol

Read, Eval, Print

Eval Done: addition To do: subtraction, multiplication, division