C programming basics T3-1 -

Similar documents
File IO and command line input CSE 2451

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

File I/O. Arash Rafiey. November 7, 2017

Standard C Library Functions

ENG120. Misc. Topics

211: Computer Architecture Summer 2016

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

8. Characters, Strings and Files

CSCI 171 Chapter Outlines

C mini reference. 5 Binary numbers 12

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

Pointers and File Handling

Introduction to file management

Standard File Pointers

Organization of a file

Input / Output Functions

UNIT IV-2. The I/O library functions can be classified into two broad categories:

Programming & Data Structure

C Language: Review. INFO High Performance Scientific Computing. 26 septembre 2017

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

C PROGRAMMING. Characters and Strings File Processing Exercise

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

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

PRINCIPLES OF OPERATING SYSTEMS

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

C Basics And Concepts Input And Output

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

PROGRAMMAZIONE I A.A. 2017/2018

The Design of C: A Rational Reconstruction: Part 2

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

Lectures 5-6: Introduction to C

Continued from previous lecture

Course organization. Course introduction ( Week 1)

System Software Experiment 1 Lecture 7

CSE 12 Spring 2018 Week One, Lecture Two

C for Engineers and Scientists: An Interpretive Approach. Chapter 14: File Processing

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

CE Lecture 11

The Design of C: A Rational Reconstruction (cont.)" Jennifer Rexford!

The Design of C: A Rational Reconstruction (cont.)

CSC 2400: Computer Systems. Arrays and Strings in C

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

File Processing. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Input/Output and the Operating Systems

CSC 2400: Computer Systems. Arrays and Strings in C

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

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

COP 3223 Final Review

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Lectures 5-6: Introduction to C

Fundamentals of Programming & Procedural Programming

C Review. SWE2004: Principles in Programming Spring 2014 Euiseong Seo

File Handling in C. EECS 2031 Fall October 27, 2014

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

Chapter 14 - Advanced C Topics

C introduction: part 1

Ch 11. C File Processing (review)

Today s class. Review of more C Operating system overview. Informationsteknologi

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Review Topics. Final Exam Review Slides

Chapter 11 File Processing

Files. Programs and data are stored on disk in structures called files Examples. a.out binary file lab1.c - text file term-paper.

Model Viva Questions for Programming in C lab

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Chapter 12. Files (reference: Deitel s chap 11) chap8

Arrays, Strings, & Pointers

C-LANGUAGE CURRICULAM

Procedural Programming

CSI 402 Lecture 2 Working with Files (Text and Binary)

Today s Learning Objectives

File Handling. 21 July 2009 Programming and Data Structure 1

Programming. Functions and File I/O

Multiple Choice Questions ( 1 mark)

ESC101N: Fundamentals of Computing End-sem st semester

Day14 A. Young W. Lim Tue. Young W. Lim Day14 A Tue 1 / 15

Advanced C Programming Topics

CS 261 Fall Mike Lam, Professor. Structs and I/O

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

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

Arrays and Strings. Antonio Carzaniga. February 23, Faculty of Informatics Università della Svizzera italiana Antonio Carzaniga

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

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays

Day14 A. Young W. Lim Thr. Young W. Lim Day14 A Thr 1 / 14

C for C++ Programmers

BSM540 Basics of C Language

CSE 12 Spring 2016 Week One, Lecture Two

Princeton University Computer Science 217: Introduction to Programming Systems The Design of C

Lecture 9: File Processing. Quazi Rahman

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

CS1003: Intro to CS, Summer 2008

Files and Streams Opening and Closing a File Reading/Writing Text Reading/Writing Raw Data Random Access Files. C File Processing CS 2060

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

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

IO = Input & Output 2

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

Fundamentals of Programming. Lecture 15: C File Processing

Kurt Schmidt. October 30, 2018

Transcription:

C programming basics T3-1 -

Outline 1. Introduction 2. Basic concepts 3. Functions 4. Data types 5. Control structures 6. Arrays and pointers 7. File management T3-2 -

3.1: Introduction T3-3 -

Review of programming Program: steps to follow (recipe) Commands: orders to be carried out Variables: containers of information. Assignment: to give a value to a variable. Expressions: calculations that give a result. Functions: reusable subprograms. Control structures: conditional actions of branching control. T3-4 -

Features of C Very powerful standard libraries. Low level programming: bits, registers, memory. Disadvantages of C: Very flexible: tolerates many unsuitable practices. Memory management is complicated. T3-5 -

3.2: Basic concepts T3-6 -

Basic program: Hello world! #include <stdio.h> /* My first program*/ void main(void) { printf( Hello, world!\n ); //Message } Standard function T3-7 -

int main(void) Variable, assignment, expression { } /* int: Returns an integer*/ int total; // Declaration of variable total=2+2; // assignment return(total); // returns 4 T3-8 -

Operations OPERATION / REPRESENTATION / EXAMPLE Assignment = a=1; a = a+1 ++ a++; ++a; a = a-1 -- a--; --a; a = a+b += a += b; a = a-b -= a -= b; T3-9 -

Example of operations int main(void) { int total1, total2; total1=2+2; total2=7+(total1++); total1=6+(++total2); return(total1+total2); } total1=4 total2=11 total1=5 total2=12 total1=18 Return 30 T3-10 -

3.3: Functions T3-11 -

Functions Prototype: declaration of function. Int addit (int a, int b); Implementation: code. Int addit (int a, int b) { int result; result = a + b; return (result); } Parameters Return value Call: use. printf( Result: %d\n, addit(4,6)); T3-12 -

Printf function Syntax: Printf ("String...", variables); % letter: insert the content of a variable: % d: integer % f: real number % c: character % s: string T3-13 -

Example 1: printf and scanf #include <stdio.h> #define PI 3.14159 void main(void) { float r, area, perimeter; Constant printf ("Enter value of radius: \ n"); scanf ("% f", & r); perimeter = 2 * PI * r; area = PI * r * r; printf ("Area:% f \ n", area); printf ("Perimeter:% f \ n", perimeter); } T3-14 - By reference

Example 2: printf and scanf #include <stdio.h> void main(void) { char name [20]; // string: array of 20 characters } printf ("Enter your name: \ n"); scanf ("% s", name); // name: pointer to 1st character 15

3.4: Data types T3-16 -

Data types int: integers (short, long) float: real numbers (long double) char: character void: null (special type) array: size is initialized pointers: variable size T3-17 -

Conversion between types Implicit (automatic, promotion) printf ("Real:% f", integer); Explicit (made by the programmer, casting) printf ("Real:% f", (float)); T3-18 -

3.5: Control structures T3-19 -

Control structures Conditional: If a condition is true, the next commands are executed. IF (A> B) {C = A;} IF NOT {C = B;} Iterative: while a condition is true, the next commands are executed. WHILE (A> 0) {A = A-1;} T3-20 -

Control structures Typical conditional structure If (expression) {BlockYES} Else {BlockNO} Example: If (a> b) {c = a;} Else {c = b;} T3-21 -

Conditional structure switch switch(c) { case '+': printf("%d\n", a+b); break; case '-': printf("%d\n", a-b); break; case '*': printf("%d\n", a*b); break; case '/': printf("%d\n", a/b); break; default: printf( Error\n"); break; } T3-22 -

Conditional structure switch (2) switch(c) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': } case 'O': case 'U': default: printf ( It is a vowel\n"); break; printf ( It is not a vowel\n"); break; T3-23 -

Iterative structure while Typical iterative structure while (expression) {BlockIteration} Example: int n = 4; while (n> 0) { printf ("Number:% d \ n", n--); } T3-24 -

Iterative structure for It is typically used when the number of iterations is previously known. for (start; condition_to_continue; update) {BlockIteration} Example: for (i = 0; i <100; i ++) {printf ("Number:% d \ n", i);} T3-25 -

3.6: Arrays and pointers T3-26 -

Arrays A group of variables of the same type. Maximum size initialized. int arrayintegers [40]; Example: arrayintegers [4] = 14; printf ("The fifth element is:% d", arrayintegers [4]); T3-27 -

Examples with arrays int days_each_month [12] = {31,29,... 31}; int number; char error [] = Error"; scanf ("%f", & number); // address scanf ("%s", error); // address printf ( %f, number); // value printf ( %s, *error); // value T3-28 -

Pointers Variables that store memory addresses. Very useful when working with data of variable size. Pointer Memory 0xA2003B48 a 56 0 T3-29 -

Examples with pointers float *pointer_to_real; float Real_Normal_variable; Real_Normal_variable = 23.4; pointer_to_real = 23.4;\\Would change the direction it points to *pointer_to_real =23.4; \\Change the value of the address it points to T3-30 -

Pointers with functions Passing the value or by reference: Do I want to change the content? funcion( w); // call NO YES w -> the value void funcion(float r) {... r=45.0; // No effect outside...} w -> by reference void funcion(float *r) {... *r=45.0; //w changed outside...} T3-31 -

Typical pointer: a string String: string of characters. It ends always with the character '\0'. Name 0xA2003B48 Memory... A l u m n o \0... T3-32 -

Examples with string / * Declaration of a string * / char name [5];... scanf ("%s", name); // pointer name [0] = 'A'; // first element of the array printf ("%s \ n", *name); // value T3-33 -

Functions for strings strcpy: copies a string. strcpy (string1, "Hello"); strcmp: compares two strings. if (strcmp (string1, string2))... strcat: concatenates two strings. strcat (string1, string2); strlen: Returns the length of the string. sf (strlen> 10)... T3-34 -

Memory management Overally, the problem of strings and pointers is: How much memory? Static Memory allocation: char help [8]; char *string = "value"; Not practical. We want real variable size. T3-35 -

Memory management Dynamic memory allocation: malloc: Allocates a size in bytes to a variable. Char pointer String = (char *) malloc(n*sizeof(char)); Character number* character size realloc: reassign the memory. free: free/deallocate memory. Important. T3-36 -

main funtion with arguments String array to pass parameters to programs: * argv [] and its counter argc. int main (int argc, char * argv []) {... int count; for (count = 0; count <argc; count ++) printf ("The argument %d is: %s \ n", count, argv [count]);... return 0; } T3-37 -

3.7: File management T3-38 -

File management in C Functions to work with files: fopen / fclose: functions to open / close a file. fgetc / fputc: functions to read / write a character. fgets / fputs: functions to read / write a string. fscanf / fprintf: functions to read / write a block. T3-39 -

File management in C Standard input and output: stdin: standard input, usually related to the keyboard. stdout: standard output, usually related to the screen. stderr: standard error output, usually related to the screen. T3-40 -

Writing examples FILE * file; char c; file = fopen ("path \filename", "r"); if (file == NULL) fprintf (stderr, "Error opening file \n"); else {while ((c = fgetc (file)) = EOF); fputc (c, stdout);} fclose (file); T3-41 -

Reading examples FILE * file; char string [80]; file = fopen ("path \filename", "w"); if (file == NULL) fprintf (stderr, "Error opening file \n"); else {scanf ("%s", string); fprintf (file, "%s \n", * string); } fclose (file); T3-42 -