CS代考程序代写 compiler OSU CSE 2421

OSU CSE 2421
Required Reading:
Pointers on C, Section 15.8, 15.8.1,15.10 through 15.10.4 inclusive
J.E.Jones

OSU CSE 2421
 Chalk Talk
◦ Simplified hardware architecture
◦ In your mind’s eye… PVC Pipe
Tennis Balls
J. E. Jones

OSU CSE 2421
 Linux/Unix systems use the ASCII character encoding format
 Most IBM products use the EBCDIC character encoding format
 There is also a character encoding format called Unicode
 For this class we will only work with ASCII
 If you are interested in this type of thing, I suggest https://en.wikipedia.org/wiki/Character_encoding
as a short tutorial/history lesson.
J. E. Jones

OSU CSE 2421
 Reminder: Before a function in a C program is invoked, or called, it must be declared. Otherwise, the compiler will output an error message when you compile. Remember also that a definition of a function includes a declaration.
 When we want to use I/O functions in the C standard library, we will include stdio.h in our source file, which contains declarations of these library functions, so that the compiler can do type-checking of our calls to these functions.
 We will only look at some of the most commonly used of these functions here.
◦ They are all you need for the labs in this course (and all you are allowed to use in your lab programs).
J. E. Jones

OSU CSE 2421
• There is no input or output defined in C itself; functions have been written and incorporated into the standard library for input and output (stdio).
• Two basic types of I/O:
• Character based (no format specifiers) – character by character I/O
{
/* check1.c – shows calls to scanf() and printf() */ int x;
scanf(“%d
”, &x); /* Notice address operator & */ printf(“x=%d
”, x);
}
• int getchar() – input
• int putchar(c) – output
• Formatted – zero or more characters, with a requirement to specify the format of the
input or output (how the sequence of characters read or written is to be interpreted):
• int scanf(parameters go in here) – input – white space is important!!!
• int printf(parameters go in here) – output
• These functions use format strings to specify interpretation (% before string)
• Two functions of a very few in C that can have a differing number of parameters each time they are called.
• Example:
#include
int main()
J. E. Jones

OSU CSE 2421
 These functions can be used to input and output one character at a time.
 Declarations (interfaces):
◦ int getchar(void);
◦ int putchar(int); /* the int value returned can be ignored */
 Notice that both of these functions use the integer (4-byte) values which correspond to a given 1-byte ASCII character. The integer must be cast before being assigned to a char variable or must be printed with a %c format code to be output as a char with printf().
 An integer is returned, rather than a char, because certain text file control values (EOF, or end of file, for example) do not have one-byte ASCII encodings. Check out the ASCII char represented by 0b 1111 1111.
 Now would be a good time to review the ASCII character chart on piazza  Take note of the values for ‘0’-’9’, ‘a’-’z’, ‘A’-’Z’ and their relationship.
J. E. Jones

OSU CSE 2421
/* assume #include */ …
int code;

printf(“Please enter the appropriate single letter code, followed by enter:
”); code = getchar();
if (code != EOF) || ((char) code != ‘
’)
printf(“The code entered was: %c
”, code); /* outputs int as a char */
——————————————————————
/* another way to do it – again, assume #include */

char code;

printf(“Please enter the appropriate single letter code, followed by enter:
”); code = (char) getchar(); /* cast int value read from input to char */
if (code != EOF) || (code != ‘
’)
printf(“The code entered was: %c
”, code); What happens to EOF in the 2nd example???
J. E. Jones

OSU CSE 2421
• Both format I/O
• Both manipulate “standard I/O” location – the keyboard
for input, and the terminal display for output (although we can use something called redirection on the command line to change this – more below)
• printf – output
• Converts values to a character form, according to the format
string, and prints them to “standard out” (stdout)
• scanf – input
• Converts characters from “standard in” (stdin), according to the
format string, and followed by pointer arguments (i.e., addresses), indicating where the resulting values are to be stored
J. E. Jones

OSU CSE 2421
 Input:
◦ scanf(“formatspecificationstring”,&param1,&param2,&param3,…);
◦ Aformatspecificationstringisastringliteraldefiningtheformatfromwhichinput should be read
◦ scanfcanhaveanindeterminatenumberofparameters.The2ndthroughthenth parameters are the addresses of variables into which values are placed (pass by reference)
◦ Thereisa%formatsubstringwithinthefirstparameterthatrepresentstheformattobe used for each of the next n-1 parameters
 Output:
◦ printf(“formatspecificationstring”,param1,param2,param3,…);
◦ Aformatspecificationstringisastringliteraldefiningtheformatinwhichoutputshould be formatted
◦ printfcanhaveanindeterminatenumberofparameters.The2ndthroughthenth parameters are values to be printed (pass by value)
◦ Thereisa%formatsubstringwithinthefirstparameterthatrepresentstheformattobe used for each of the next n-1 parameters
J. E. Jones

OSU CSE 2421
When programming in C, you use format strings — the percentage sign and a conversion code, for the most part (although some other characters between % and the conversion character are optional – see below) — as placeholders for variables you want to read from input or display. The above table shows the format strings and what they display…
Conversion Character
Displays Argument (Variable’s Contents) As
%c %d, %i %e
%f
Single character
%g
Signed value in %e or %f format, whichever is shorter
%o %s %u
Unsigned octal (base 8) integer (int) String of text (NULL terminated) Unsigned decimal integer (int)
%x %%
Unsigned hexadecimal (base 16) integer (int) (percent character)
Signed decimal integer (int)
Signed floating-point value in E notation
Signed floating-point value (float)
J. E. Jones

J. E. Jones
OSU CSE 2421

OSU CSE 2421
/* NOTE: The codes for formatted I/O from above are used */
printf(“%d %d
”, fahr, celsius);
Causes the values of the two integers fahr and celsius to be printed, with a tab
( ) between them, and followed by a newline(
). printf(“%3d %6d
”, fahr, celsius);
To print the first number to three digits wide, and the second to six digits wide
NOTE: Each % construction in the first argument of printf is paired with the corresponding second argument, third argument, etc.; they must match up properly by number and type, or you will get wrong answers or a compile time error.
printf(“
a=%f
b=%f
c=%f
d=%f”,a,b,c,d);
What does this statement print?
J. E. Jones

OSU CSE 2421
 printf(“
a=%f
b=%f
c=%f
d=%f
”,a,b,c,d); ◦ If a=6.2, b=5.455, c=3.1415, d=6.0, then output is:
a=6.200000 b=5.455000 c=3.141500 d=6.000000
 printf(“
a=%4.2f
b=%6.4f
c=%5f
d=%3f
”,a,b,c,d); ◦ If a=6.2, b=5.455, c=3.1415, d=6.0, then output is:
a=6.20 b=5.4550 c=3.141500 d=6.000000
J. E. Jones

OSU CSE 2421
float x = 3.14159; printf(“output:”); printf(“%4.3f
”, x); printf(“%4f
”, x); printf(“%.3f
”, x);
output: 3.142 3.141590 3.142
 minimum field width is 4! Notice it rounds up.
 minimum field width is 4, but no precision specified –
6 digits is default
 Explicitly says 3 numerals after decimal point
J. E. Jones

OSU CSE 2421
int y = 25; printf(“%d
”, y); printf(“%i
”, y); printf(“%4d
”, y); printf(“%1d
”, y); printf(“%05d
”, y);
output: 25
25
25  two spaces before two-digit value
25 00025
 minimum field width specified is too small to print the value of the data, so printf ignores it!  leading zeroes used to fill minimum field width
J. E. Jones

OSU CSE 2421
int day, month, year;
scanf(“%d/%d/%d”, &month, &day, &year);
int anInt; scanf(“%i%%”, &anInt);
Input:
01/29/13
month has the value 1
day has the value 29
year has the value 13
The / characters are not saved. Input:
111/222/2018
month has the value 111
day has the value 222
year has the value 2018
The / characters are not saved.
Input:
23%
anInt has the value 23 The % character is not
saved. Input:
152%
anInt has the value 152 The % character is not
saved.
J. E. Jones

OSU CSE 2421
int int_1; long long_1;
scanf(“%d %ld”, &int_1, &long_1); Input:
-23 200
int_1 has the value -23
long_1 has the value 200
/*stored as 4 bytes */ /*on stdlinux*/ /*stored as 8 bytes */ /*on stdlinux*/
double d; scanf(“%lf”, &d); Input:
3.14
d has the value 3.14
/*stored as 8 bytes */ /*on stdlinux*/
J. E. Jones

OSU CSE 2421
 IMPORTANT: scanf ignores leading (but not following) white space characters when it reads numeric values from input:
int i;
printf(“Enter an integer:
”); scanf(“%d”, &i);
/*scanf will ignore any leading white space characters */
J. E. Jones

OSU CSE 2421
char string1[10]; /* IMPORTANT: string must hold 9 chars + null */
scanf(“%9s”, string1); /*array identifier is */ /*address of 1st element*/
Input:
VeryLongString
string1==“VeryLongS”
int int1;
scanf(“%*s %i”, &int1);
/*read arbitrary length*/
Input:
Age: 29
int1 has the value 29 “Age: “ is not saved
/*string of non-numeric*/ /*chars before int1 */
NOTE: pressing the enter key causes characters that are being held in the input buffer to be transferred to standard in (stdin)
J. E. Jones

OSU CSE 2421
int int1, int2; scanf(“%2i”, &int1); scanf(“%2i”, &int2); Input:
2345
Then:
int1 has the value 23 int2 has the value 45
NOTE: pressing the enter key causes characters being held in the input buffer to be transferred to standard in (stdin)
J. E. Jones

OSU CSE 2421
Letter %% d
i
Type of Matching Argument
Auto-skip;
Leading Example White-Space
Sample Matching Input
o
u
x
a, e, f, g c
yes unsigned int aUInt; scanf(“%o”, &aUInt);
s n [
no char s[30]; scanf(“%29s”, s);
% (a literal, matched but not converted or assigned)
no int anInt; scanf(“%i%%”, &anInt);
23%
-23 200
0x23
023
23
1A
1.2 3.4
Q
hello
X: 123 (cnt==6)
int
int
unsigned int unsigned int unsigned int float or double char
array of char int
array of char
yes int anInt; long l;
scanf(“%d %ld”, &anInt, &l);
yes int anInt; scanf(“%i”, &anInt);
yes unsigned int aUInt; scanf(“%u”, &aUInt);
yes unsigned int aUInt; scanf(“%x”, &aUInt);
yes float f; double d; scanf(“%f %lf”, &f, &d);
no char ch;
scanf(” %c”, &ch);
no int x, cnt;
scanf(“X: %d%n”, &x, &cnt);
no char s1[64], s2[64]; scanf(” %[^
]”, s1);
Hello World field1field2
scanf(“%[^ ] %[^ ]”, s1, s2);
J. E. Jones

OSU CSE 2421
Letter %% d
i
Type of Matching Argument
Auto-skip;
Leading Example White-Space
Sample Matching Input
o
u
x
a, e, f, g c
unsigned int
yes unsigned int aUInt; scanf(“%o”, &aUInt);
s n [
array of char
no char s[30];
% (a literal, matched but not converted or assigned)
no int anInt; scanf(“%i%%”, &anInt);
23%
-23 200
0x23
023
23
1A
1.2 3.4
Q
hello
X: 123 (cnt==6)
int
yes int anInt; long l;
scanf(“%d %ld”, &anInt, &l);
int
yes int anInt; scanf(“%i”, &anInt);
unsigned int
yes unsigned int aUInt; scanf(“%u”, &aUInt);
unsigned int
yes unsigned int aUInt; scanf(“%x”, &aUInt);
float or double
yes float f; double d; scanf(“%f %lf”, &f, &d);
char
no char ch;
scanf(” %c”, &ch);
Note space between “ and %
scanf(“%29s”, s);
int no array of char no
int x, cnt;
scanf(“X: %d%n”, &x, &cnt);
char s1[64], s2[64];
scanf(” %[^
]”, s1); scanf(“%[^ ] %[^ ]”, s1, s2);
Hello World field1field2
J. E. Jones

OSU CSE 2421
 What is the difference between %i and %d? The table says they both print signed decimal integers.
◦ https://www.geeksforgeeks.org/difference-d-format-specifier-c- language/
 I really can’t explain it any better than this link does.
◦ printf: There is no difference between the %i and %d format
specifiers
◦ scanf: %d and %i behave differently
 %d assume base 10 while %i auto detects the base.
 For example, 012 would be 1210 with %d but 1010 (128)with %i.
J. E. Jones

Leave a Reply

Your email address will not be published. Required fields are marked *