Вы находитесь на странице: 1из 4

C Programming Unit 1 Intro

Our first C Program


Note -- I do not want you to do these exercises on your personal machine, I want
you to be able to do it at the command prompt on UISACAD.
Every C program must have a function called main() to be a valid C program.
In fact, a C program that has ONLY a main() function with nothing in it is a valid
program.
Connect to the VPN and UISACAD, then we'll use a text editor like Joe, or Nano, or
Vi.
Personally I like Vi because it comes with every Linux/Unix flavor out there.
Navigate to your home directory by typing cd ~,
then type vi empty.c
Type i to go to Insert Mode. The bottom of the screen will change to -- INSERT -Type the following:
main()
{
}
This creates an empty C program with a valid main() funnction.
Press the <Esc> key to exit insert mode. The -- INSERT -- indication will disappear
Then type :(colon) to go into command mode, the cursor will jump to the colon at
the bottom of the screen.
Then at the colon type wq and <Enter> to write the file to disk and quit Vi.
If you want to exit Vi without saving the file, use the colon to go to command mode,
then type q! <Enter>.
Other Vi commands can be found at http://www.cs.colostate.edu/helpdocs/vi.html
Now lets compile the program.
At the Linux command prompt type
gcc -o empty.out empty.c
GCC is the GNU C Compiler. (GNU, for the unfamilar is a recursive acronym stands
for GNU's Not Unix) GNU is a free Unix-like operating system written by the rather
eccentric but principled Richard M Stallman, who also wrote GCC. and whom I met
last spring in my office in Urbana, and had a brief conversation with about his Free
Software ideas. But I digress. Maybe we can talk about it later in the semester.
gcc calls the compiler, then the -o switch names the output file, and finally the
source code file is named.
Assuming you typed the program correctly, iand there are no compiler errors, it
should simply return to the UISACAD Linux prompt.
Now let's run the program - type empty.out and press <Enter> to run the program
you just wrote.
Note--if you get a "file not found" error, it usually means your home directory is not
in your path. You may need to tell Linux the relative path to the file. If you are in
the directory where empty.out is stored, you can simply prepend a ./ (dot slash) in
front of the file name (./empty.out) which tells the operating system to run
empty.out from the current directory.
If all goes well, It should simply return to the UISACAD Linux prompt, and there you
have it, you have written your very first C program. It does absolutely nothing, but
it is a legal, valid, C program.
Comments

Here is how C code can be commented to add notes that you do not want the
compiler to see, but that will make the code easier to understand on later review.
//Note that I can create comments in my code by typing two forward
slashes and
//everything from the slashes to the end of the line will be considered
comments
/* I can also use slash-star to begin a comment and everything after that
becomes a single continuous comment
until I end the comment with a star-slash */
Our Second C Program
When a line begins with a # (hash mark or pound sign) that is called a
preprocessing directive. The compiler scans the code before compiling and follows
the instructions given by preprocessing directives.
In this case, it is an include directive. An include finds the C code in the file name
given and inserts it as the position of the INCLUDE as if it were typed in along with
your code. This allows the C programmer to take advantage of functions that have
already been written without re-inventing the wheel.
The include directive here compiles in the header file containing functions for
Standard I/O in C
We also now have content within the main() function that actually does something.
One line commands within a function must end with a semi-colon (;).
The printf command tells the program to print something to the screen, in the this
case, the string "Hello Word!\n"
Note the backslash-n inside the print string. The \n is a command for New Line. If
this command were not in the string, when the program stopped running, the
command prompt would be on the same line as the program output, which could be
confusing. The New Line command provides a fresh new line for the command
prompt to appear on after the program runs.
==========================
Open your text editor again to write a program called helloworld.c
vi helloworld.c
Press i to go into -- INSERT -- mode
Type the following:
#include <stdio.h>
main ()
{
printf("Hello world!\n");
}
Type <Esc> then :wq to save and exit.
Now compile.
gcc -o helloworld.out helloworld.c
Now run.
./helloworld.out

Did you get the output you expected?


Third Practice Program in C The For/Next Loop
The For/Next Loop is one of the basic programming structures available. It is simply
a method to perform the same action or actions repeatedly until a specified
condition is met. The code below starts with a counter value equal to 0, prints it to
the screen, then increments it, and prints the next value, and so forth. When the
counter gets to 10, the loop stops and the program ends.
The line in the code that guides all this is
for (i = 0; i < 10; i++)
which means start with i=0, run the loop as long as i is less than 10, and at the end
of each loop, increment i by 1 (the ++ operator).
Open a text editor like Joe, Nano, or Vi and type the following
/* This is a program that demonstrates a
simple C program */
/* prog1.c [TODAY's DATE] by [YOUR
NAME] */
#include <stdio.h>
main ()
{
int i;
for (i = 0; i < 10; i++)
{
printf("The value of i is %d\n", i);
}
}
Next, save it by typing CTRL-K, then X.
Next, compile it by entering
gcc prog1.c -o prog1.out
Finally, run it by entering
prog1.out
If you are getting too many errors when you compile a C program that they fill up
several pages, you can send the errors to a file and look at them with joe or another
editor.
Use this command to compile and send the errors to a file called error.out
gcc prog1.c -o prog1.out >> error.out 2>&1
To view the error file, enter the command
less error.out
Use the cursor keys (up/down arrows) to view the file. To exit simply type q

Вам также может понравиться