Introduction
Activity Steps
pico digitsum.c
Notice
the menu at the bottom of the editor window that reminds you how to save your
code with CTRL-O, how to move between pages with CTRL-V and CTRL-Y, how to cut
and uncut a line of text with CTRL-K and CTRL-U, and how to exit with CTRL-X.
Remember to save your code frequently.
Using the editor, write C code for a program that reads in a positive integer
and prints out the sum of all digits. For example, if the user types in the
number 528, the program should output 15. Here is a sample program execution
(user input is in italics):
Please enter a positive integer: 345
The sum of digits is 12
gcc digitsum.c -o digitsum
gcc
(which stands for GNU Compiler Collection) is the name of the compiler. The
first argument digitsum.c
is the name of the C program you wish to compile. The option –o informs the compiler
that the argument to follow (in our case digitsum)
is the name of the executable program to be produced as result of the
compilation. You can give any name you wish to the executable, however keeping
the executable name the same as the program name (without the
.c extension) is a good rule of thumb,
so you can easily associate an executable with the corresponding source code.
Pay careful attention to error and
warning messages, and eliminate them all.
5. To
execute your program, use the command
./digitsum