6.
Create a C source code
getinput.c
that prompts the user
to enter his full name. Save it in an appropriately named
file such as
getFullName.c
. Compile it and execute it.

Note the above program is a new program, so it should
be placed inside a different directory, such
as
~/lab02/getFullName
. In future exercises, each new
program should be placed in a separate directory.
Some of you will realize that the program name
a.out
isn't
always a good choice. If you wants to use a different
name for your executable such as
getFullName
, use the
option -o of gcc, as in
gcc getFullName.c -o getFullName
. The
option "-o" stands for "Output". Recompile your program
to create an executable named
getFullName
.
Note that, by convention, Unix commands or programs
do not use extension names, unlike what is required
under Windows (that is .exe and .com). So do not use
extension names for your Unix executables.
In the above program, we use the standard
function
fgets
to read a line of input from a file, which in
this case is the standard input (stdin).
Both
fgets
and
stdin
are defined in the standard I/O
library, so you must include
<stdio.h>
header file.
stdin
(standard input) is similar to
System.in
in Java,
and
cin
in C++.
stdout
(standard output) is similar
to
System.out
in Java, and
cout
in C++.
stderr
(standard
error) is similar to
System.err
in Java, and
cerr
in C++.
Note also in the above program, we read a line into a
character array of size 128 characters. If you end the
keyboard input with a ENTER key,
fgets
will store the
newline character in the character array. In some
applications, you may need to strip this character away
from the char array. For example the following code can
be used to strip the newline character away from the
char array
line
:
n = strlen(line);
if (line[n-1] == '\n')
line[n-1] = '\0';

In C, the last character in a string must always be the
null character "\0".
7.
Write a C program that reads a line of input from the
standard input and then reverse the line. Finally print
out the reversed line on the standard output.


You've reached the end of your free preview.
Want to read all 6 pages?
- One '14
- standard output, C standard library