www.cs.wustl.edu/~schmidt/ece011/chapter2.doc
rs, Expressions, and the
If Statement
A
Gentle Introduction
Reference: Brooks, Chapter 2
Continuing from our last session, we now create a
new project
This time, we shall include it in the same workspace
as our existing project
Do a File|New and fill in the dialog as we did before
This time, however, I checked the Add to current workspace button
The default would be to make it a subfolder of hello
Now our workspace has two projects in it. One
is the project from the previous cha</span><span class="Normal--Char" style=" font-family: 'Times New Roman', 'Arial';
font-size: 14pt;">pter, hello. The other is our new project,
circle.
Notice the hello.c file is still open
Select the File|New menu option and fill in the dialog
Notice that you can choose any name you wish for
the file, but it must end with a .c
Visual Studio determi</span><span class="Normal--Char" style=" font-family: 'Times New Roman', 'Arial';
font-size: 14pt;">nes which compiler to invoke according to the file's
suffix
A .c file is a C source file. Visual Studio will
invoke the C compiler on it.
A .cpp file is a C++ source file. Visual Studio will
invoke the C++ compiler on it.
As a result of this, you sho</span><span class="Normal--Char" style=" font-family: 'Times New Roman', 'Arial';
font-size: 14pt;">uld see something like what we show below.
An empty window for main.c will appear superimposed
on top of the existing windows
Also, the circle project now has a + to the left
of it which we can click
Here we have a program to compute the area of a circl</span><span
class="Normal--Char" style=" font-family: 'Times New Roman', 'Arial';
font-size: 14pt;">e
It asks the user to give the radius, performs the
computation, the prints out a result
Here again is the program
[Instructor: discuss the components
of this program to introduce next section]
/*
* File : main.c
* Author: Robert C. Carden IV
*/
#include <stdio.h>
int main (void)
{
const double pi = 3.14159;
double radius, area;
printf ("Enter the radius of the circle: ");
scanf ("%lf", &radius);
/* area = PI times R^2 */
area = pi * radius * radius;
printf ("Using PI=%g to compute the area...\n", pi);
printf ("The area of a circle of radius %f is
%-10.4f\n",
radius, area);
return 0;
}
Here is an example run of this program:
Dissection
of the Circle Program - Comments
/*
* File : main.c
* Author: Robert C. Carden IV
*/
Comments start with a /* character sequence and end the a */
Comments are remarks and are ignored by the compiler
Programmers include comments to clarify their program, document their work, and in general, make their programs more understandabl</span><span
class="Normal--Char" style=" font-family: 'Times New Roman', 'Arial';
font-size: 14pt; font-style: italic;">e to other people and themselves
Good programmers write comments as they write the
code
Comments may span multiple lines (as in the example
above) or may be restricted to one line:
/* area = PI times R^2 */
Comments may also be embedded within a stateme</span><span
class="Normal--Char" style=" font-family: 'Times New Roman', 'Arial';
font-size: 14pt;">nt:
printf ( /* format */ "Hello world\n");
Many compilers support C++ style comments
In C++ a comment starts with a // and extends to the end of a line
// This is a typical C++ comment
printf ("Hello world\n); // say hello to the world
// printf ("Goodbye cruel world\n");
Dissection
of the Circle Program - Preprocessor Statements
#include <stdio.h>
Lines starting with a # character are preprocessor statements
Preprocessor statements begin with the # character and extend to the end of that logical line