Simple C Programs

CS 2073
Computer Programming w/ Eng.
Applications
Hello World! in C
using
DOS Window Command Prompt
Turgay Korkmaz
Office: SB 4.01.13
Phone: (210) 458-7346
Fax: (210) 458-4437
e-mail: [email protected]
web: www.cs.utsa.edu/~korkmaz
Lecture = 1;
1
Hello World! Program
in Windows




Install Microsoft Visual C++ Express 2008 (free)
http://www.microsoft.com/express/vc/
Write a Hello World! Program
Compile and Execute it in Windows
In class, we will do this in a simple way using
DOS Window Command Prompt
If you are interested in using IDE or Linux,
yes you can! I’ll give you instructions to start.
2
Hello World!
in DOS Window
This black window is called
DOS window
command prompt
Start  All Programs 
Microsoft Visual C++ Express 2008 
Visual Studio Tools 
Visual Studio 2008 Command Prompt
3
Hello World!
in DOS Window
In the DOS window (black one), type
C:\Program Files…> Z: At home, type
Z:\> mkdir myprog
Z:\> cd myprog
Z:\myprog> notepad hello.c


hello – Notepad window will
appear and ask if you want to
create the file. Click yes!
cd C:\
4
Hello World!
in DOS Window
In hello – Notepad
window, type your
program and save it
 Then,
 Go back to the DOS window and type
Z:\myprog> cl hello.c
Z:\myprog> hello
Hello World!

5
Exercise: Develop a program that
prints your name on the screen
Start  All Programs  Microsoft Visual C++ Express 2008  Visual
Studio Tools  Visual Studio 2008 Command Prompt
C:\Program Files…> Z:
At home, type cd C:\
Z:\> cd myprog
Z:\> mkdir ex1
Z:\> cd ex1
Z:\myprog\ex1> notepad myname.c


Type your program in myname - Notepad
Compile and execute it!
Z:\myprog\ex1> cl myname.c
Z:\myprog\ex1> myname
Turgay Korkmaz
6
Save/Print results from the
DOS window (black one):

Inside DOS window, type
Z:\myprog\ex1> notepad output.txt







Right-click mouse inside DOS window
Select the Mark option
Drag and highlight the results you want to print
Hit Enter
Move the cursor with the mouse to output.txt, which you
have previously opened with Notepad
Paste the text you have marked into output.txt
Save it or Use Print of Notepad
7
See your files in Windows and
submit your program

We ask you to
submit C source
file (hello.c) and
output.txt using
WebCT (BB)

Open Windows file manager and
type Z:\myprog or C:\myprog
into Address field
On C source file,
Click Right
Button and
select Open With
 Notepad
8
Test HW




Modify your last program such that, after your
name, it prints the names of your three
favorite courses! Make sure the first one is
CS 2073 Computer Programming with
Engineering Applications 
Save it As test1.c, compile and execute it
Copy/paste the output into output1.txt
Submit test1.c and output1.txt via
9
WebCT(BB)
/* Another Exercise: Implement this program and execute it.
What does it do? */
#include <stdio.h>
#include <stdlib.h>
int main( )
{
double tC; /* temperature in Centigrade */
double tF; /* temperature in Fahrenheit */
/*
* Prompt for centigrade temperature
*/
printf("What is the temperature in Centigrade? ");
scanf("%lf", &tC);
/*
* Convert it to Fahrenheit
*/
tF = 9.0 * tC / 5.0 + 32.0;
printf("%.2f in Centigrade is %.2f in Fahrenheit\n“, tC, tF);
}
return 0;
/* signal normal end of program */
10
To compile a C program under Windows, you can also use Microsoft Visual Studio or
Microsoft Visual C++ 2008 Express IDE (integrated development environment), a
software application that provides comprehensive facilities to computer
programmers. An IDE consists of at least a source code editor, a compiler and a
debugger etc. !! In class we will go with the simple DOS window approach to write
and compile our programs !! But if you are interested in using IDE, then here are
the directions. If you are not interested in IDE, SKIP this slide!

1. Select Start -> All Programs -> Microsoft Visual C++ 2008 Express Edition
2. Select File -> New -> Project from IDE menu
3. A dialog box will appear: Select Win32, Win32 Console Application, and name your
project (e.g., HW1), click OK
4. Win32 Application Wizard will appear. Click Next, mark/select Empty Project, click Finish
5. Select Project -> Add new item from IDE menu
6. A dialog box will appear: Select C++ File, name your file (e.g., HW1.c), click Add
7. Type/edit your C program in HW1.c window and save it

8. Now you need to compile your program as follows.













9. Select Build -> Built Solution from IDE menu
10. The compiler will show the results in the output window in IDE,
11. If there is any error, go to Step 7 to edit/correct your program and compile it.
12. If there is no error, then you can run the program as follows.
13. Select Debug -> Start without debugging from IDE menu
14. A DOS window (black one) will appear and show the output and/or get input
15. To print the program, Select File -> Print from IDE menu
11
Hello World! in Linux
Similar to DOS Window. If you plan to use Linux, I can give you more instructions.
Just ASK. But, if you are not interested in Linux, SKIP this slide!

Login to a linux machine
SSH Secure Shell (e.g., main212.cs.utsa.edu)
main212:> mkdir myprog
main212:> cd myprog
main212:> pico hello.c



Type your program … and save it (ctrl-o)
Compile and execute your program
main212:> gcc hello.c –o hello
main212:> hello
12