Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.

Slides:



Advertisements
Similar presentations
Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Advertisements

Lecture 2 Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Introduction to C Programming
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
Chapter 2 Getting Started in C Programming
A First Book of ANSI C Fourth Edition
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
1 IPC144 Session 11 The C Programming Language. 2 Objectives To format a #define statement correctly To use a #define statement in a C program To construct.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Gator Engineering 1 Chapter 2 C Fundamentals Copyright © 2008 W. W. Norton & Company. All rights reserved.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
Constants in C A Presentation On Department of Computer & Information Technology, M.S.P.V.L. Polytechnic College, Pavoorchatram.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
CSE1301 Computer Programming: Lecture 6 Input/Output.
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Chapter 2: C Fundamentals Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 2 C Fundamentals.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
Formatted Input and Output
Formatted Input/Output
Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA
Wel come.
Input/output.
Chapter 2, Part I Introduction to C Programming
Chapter 3: I/O Management
ICS103 Programming in C Lecture 3: Introduction to C (2)
Formatted Input/Output
Plan of the Day: More on type conversions scanf printf format strings
Programming in C Input / Output.
Input and Output Lecture 4.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Formatted Input/Output
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Formatted Input/Output
A First Book of ANSI C Fourth Edition
Lecture3.
Formatted Input/Output
Formatted Input/Output
Formatted Input/Output
Presentation transcript:

Operating System Discussion Section

The Basics of C Reference: Lecture note 2 and 3 notes.html

The General Form of a Simple Program The simplest C programs have the following form: int main(void) { declarations statements } The word int may be omitted (although it’s good practice to include it). The use of void is also optional. Each declaration and each statement (except for compound statements) must end with a semicolon.

The General Form of a Simple Program C99 does not require that declarations precede statements. However, each variable must be declared before it is used for the first time

Comments Begin with /* and end with */. /* This is a comment */ May extend over more than one line. /* This comment starts on one line, but ends on another. */ Warning: Failing to close a comment will cause the compiler to ignore part of your program. printf("Hello"); /* forgot to close this comment... … printf("Goodbye"); /* so it ends here */ In C99, comments can also be written this way: // This is a comment. This type of comment is terminated by the end of the line.

Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or not to C: "); printf("that is the question.\n"); return 0; }

Example: Computing the Volume /* Illustrates the use of variables, expressions, and assignment statements */ #include int main(void) { int height, length, width, volume; height = 5; length = 20; width = 10; volume = height * length * width; printf("The height is %d\n", height); printf("The length is %d\n", length); printf("The width is %d\n", width); printf("The volume is %d\n", volume); return 0; }

Defining Constants A macro definition has the form #define identifier replacement-list Macros often represent constants: values that won’t change during program execution: #define N 100 Macro definitions are handled by the C preprocessor. Wherever the identifier appears later in the source file, the preprocessor replaces it by the replacement list.

Defining Constants If the replacement list contains operators, it is best to enclose it in parentheses. #define EOF (-1) #define TWO_PI (2* ) By convention, names of C constants contain only upper-case letters. Advantages of defining constants: Programs are easier to read. Programs are easier to modify. Helps reduce errors of inconsistent use, as well as typographical errors.

Example: Converting Fahrenheit to Celsius /* Illustrates macro definition, floating-point numbers, and scanf */ #include #define FREEZING_PT 32.0 #define SCALE_FACTOR (5.0 / 9.0) int main(void) { float fahrenheit, celsius; printf("Enter Fahrenheit temperature: "); scanf("%f", &fahrenheit); celsius = SCALE_FACTOR * (fahrenheit - FREEZING_PT); printf("\nCelsius equivalent is: %.1f\n", celsius); return 0; }

Input and Output Printf Scanf

The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion specifications, which begin with the % symbol. Ordinary characters are printed as they appear in the string; conversion specifications are matched one-by- one with the remaining arguments in the call of printf : int i, j; float x, y; printf("i = %d, j = %d, x = %f, y = %f\n", i, j, x, y);

The printf Function Warning: C compilers don’t check that the number of conversion specifications in a format string matches the number of output items: printf("%d %d\n", i); /* too few items */ printf("%d\n", i, j); /* too many items */ Furthermore, there’s no check that a conversion specification is appropriate for the type of item being printed: printf("%f %d\n", i, x);

Conversion Specifications A conversion specification has the form %-m.pX m (optional) specifies the minimum field width. - (optional) indicates left justification within the field; the default is right justification..p (optional) specifies: –Number of digits after the decimal point (for a floating point number) –Minimum number of digits to print (for an integer)

Conversion Specifications X is one of the following letters: d - decimal signed integer e - exponential floating point f - fixed decimal floating point g - whichever of e or f produces a shorter string This is not a complete description of conversion specifications.

printf Example Sample program: #include int main(void) { int i; float f; i = 40; f = ; printf("|%d|%5d|%-5d|%5.3d|\n", i, i, i, i); printf("|%10.3f|%10.3e|%-10g|\n", f, f, f); return 0; } Output: |40| 40|40 | 040| | | 8.392e+02| |

Escape Sequences String literals sometimes need to contain control characters (such as tab and newline) or characters that have a special meaning in C (such as "). These characters can be represented by escape sequences. Character escapes consist of \ followed by one character: alert (bell) \a backslash \\ backspace \b question mark \? form feed \f single quote \' new-line \n double quote \" carriage return \r horizontal tab \t vertical tab \v

Escape Sequences Writing the \a escape causes a beep. Writing the \n escape causes the cursor to move to the beginning of the next line. Writing the \t escape causes the cursor to move to the next tab stop. The \" escape allows us to print the double quote character: printf("\"Hello!\""); String literals may contain any number of escape sequences: printf("Item\tUnit\tPurchase\n\tPrice\tDate\n");

The scanf Function scanf requires a format string, followed by variables into which input is to be stored. In most cases, each variable must be preceded by the symbol &. Warning: Forgetting to put the & symbol in front of a variable will have unpredictable — and possibly disastrous — results.

The scanf Function scanf format strings are similar to printf format strings, but usually contain only conversion specifications: int i, j; float x, y; scanf("%d%d%f%f", &i, &j, &x, &y); Note: When used with scanf, the e, f, and g conversions are identical: each causes scanf to read a floating-point number. A sign, exponent, and decimal point may or may not be present.

How scanf Works scanf ignores white-space characters (blanks, tabs, and new-line characters) when searching for an input item. If scanf is used in the following way: scanf("%d%d%f%f", &i, &j, &x, &y); the input could be split over several lines: e3 or put on a single line: e3

The scanf Format String A scanf format string may contain ordinary characters in addition to conversion specifications. A non-white-space character in a format string must match the next input character or scanf terminates without reading further. (The non-matching character can be read by a later call of scanf.) The call scanf("%d/%d/%d", &month, &day, &year); will read 5/ 28/ 2002 but not 5 / 28 / 2002

The scanf Format String A white-space character in a format string matches zero or more white-space characters in the input. The call scanf("%d /%d /%d", &month, &day, &year); will read 5 / 28 / 2002 and all similar inputs, regardless of the amount of space before and after each / symbol.

The scanf Format String Warning: Putting a new-line character at the end of a scanf format string is usually a bad idea: scanf("%d\n", &i); After reading an integer, scanf skips white-space characters until it finds a non-white-space character. An interactive program will hang until the user enters a nonblank character.