{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture03_formattedIO

lecture03_formattedIO - 1 EE312 Lecture 3 Announcements...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1 MW Lecture 3 1 EE312 - Lecture 3 Announcements • Read chapters 5 and 6 for next class • Topics for today will address: – Review – Formatted input and output – Introduction to Control Statements MW Lecture 3 2 Short Term Schedule
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 MW Lecture 3 3 Compute the Volume /* this program computes and outputs the volume of a box */ #include <stdio.h> 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; } MW Lecture 3 4 Converting Fahrenheit to Celsius /* program to convert a given Fahrenheit temperature to Celsius */ #include <stdio.h> #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; } /* program to convert a given Fahrenheit temperature to Celsius */ #include <stdio.h> int main(void) { const float FREEZING_PT = 32.0; const float SCALE_FACTOR = 5.0 / 9.0; 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; }
Background image of page 2
3 Formatted Input and Output • For simplifying the process of inputting values into and out of your program while its running. • The stdio library contains a function for reading values from the keyboard (the standard input device) and a function for writing out values to the screen (the standard output device) according to a given format specification. • The functions are named: – scanf ( ), and printf ( ) • You may use these from now on in your assignments. Input device values Your program running on the computer values Output device MW Lecture 3 6 printf() function (1) printf ( "Format string in quotes", list-of- values) • Defined in a library. Declared in <stdio.h> printf ("Double %d is %d \n", x, 2 * x ); Conversion specifications Values to be printed Format string Escape character
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4 MW Lecture 3 7 Escape Sequences (1) • 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 are 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 MW Lecture 3 8 Escape Sequences (2) • Writing the \a escape causes a beep. • Writing the \n escape causes the cursor to move
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 16

lecture03_formattedIO - 1 EE312 Lecture 3 Announcements...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon bookmark
Ask a homework question - tutors are online