E 102 F 23 Lecture 003 A
E 102 F 23 Lecture 003 A
Electromechanical Systems
Engineering Programming in C
Lecture 03
Fall 2023
1
Basic I/O
There is no input or output defined in C itself
Character based (no format specifiers)
getchar() - input
putchar(c) - output
Formatted
scanf(stuff goes in here) - input *** white space is important!!!
printf(stuff goes in here) - output
Format Specifiers (% before specifier) – see next slide
%c Single character
%d Signed decimal integer (int)
%e Signed floating-point value in E notation
%f Signed floating-point value (float)
%g Signed value in %e or %f format, whichever is shorter
%i Signed decimal integer (int)
%o Unsigned octal (base8) integer (int)
%s String of text
%u Unsigned decimal integer (int)
%x Unsigned hexadecimal (base16) intege r(int)
%% (percentcharacter)
Inputs from the Keyboard
Inputs from the keyboard are read using the function scanf. The function
scanf requires a format string: that is, a format of the item (string) to be
read. A typical use of scanf for reading a string from a keyboard is:
The % sign is a format descriptor which determines the type of the next
input item and how to store that value.
Display to Monitor: printf
int main () {
/* We will use one floating-point and one integer variable. */
double x = .00000123456789;
int n = 12345;
/* Display an integer. */
printf("Here is n: %d\n\n", n);
/* Display a double three different ways. */
printf("Here is x: %g\n", x);
printf("Here is x: %f\n", x);
printf("Here is x: %e\n\n", x);
/* Read in an integer. */
printf("Please enter an integer: ");
scanf("%d", &n);
printf("The integer was %d\n\n", n);
/* Read in a double. */
printf("Please enter a double: ");
scanf("%lf", &x);
printf("The double was %g\n\n", x);