Title: CS 151
1CS 151
- Lecture 3
- Programming in C
2ACM Tour on Saturday
- The Local ACM Chapter is having a tour of
computing facilities on campus on Saturday at
3PM. - See Announcements for Details.
- Good opportunity to meet upperclassfolk,
- women CS students are particularly encouraged to
come and meet
3Programming in C
- A C program consists of functions.
- Functions are starting points of execution.
- Every C program starts with a function called
main - Functions can accept input values, perform tasks,
and possibly return computed values.
4Function Structure
int main (void) / C instructions here
/ return 0
5A Simple Program Printing
- Make a program that prints text on the screen.
- Basic outline of a C program
- Use a library function, printf()
6C Function Libraries
- C comes with a many libraries of "ready-made"
functions. - Two steps to using them.
- Use a include statement
- Call (invoke) the function, i.e. write it's name
in your program.
7The printf() function
- Include the standard i/o (input/output) library
- include ltstdio.hgt
- Invoke the function.
- printf("Go Beavers")
- Text inside double quotes is called a string.
- printf() prints strings on the output screen.
8A simple program printing
/ Name Collaborator(s) Instructor
Date Assignment Description
/ include ltstdio.hgt / preprocessor
directive / int main (void) printf("Go
Beavers") / print some text / return 0
9- Header block
- /
- Name
- Collaborator(s)
- Instructor
- Date
- Assignment
- Description
- /
- C compiler ignores it.
- Required in this course.
10Preprocessor include ltstdio.hgt Include the
library file named stdio.h as part of this
program. . Comments / print some text / This
is a programmers comment an explanation of
the meaning of this part of the program. It is
ignored by the compiler.
11Main Function
int main (void) printf("Go Beavers")
return 0 All programs in the C language
start with the function main. A function is a
named unit of computation in the C language. For
this class, the last statement in main is always
return 0 Execution is line by line starting
after the opening brace, , and ends with the
closing brace, .
12Console (Screen) I/O
I/O means Input/Output printf("Go
Beavers") printf means print formatted to the
screen using certain formatting rules.
Characters inside double quotes are called a
string constant or string literal.
13Formatting Characters in Strings
- '\n' called the newline character. Causes
screen cursor to move down one line and to left. - '\t' called the tab character. Causes cursor to
move to next tab stop on screen. - ' ' called the space character. Shows up as
blank space on screen.
14Using Formatting characters
- printf("one\ntwo") prints
- one
- two
- printf("one\ttwo") prints
- one two
- printf("one two three") prints
- one two three
15Print Specifiers and Parameters
- printf() can have more than one parameter.
-
- printf("s\ns", "one", "two")
- which prints
- one
- two
parameter two
parameter three
parameter one
16The Big Picture
- The source of a C-Program is a text file called
source code. - The compiler reads the source and reports errors.
- Execution starts at beginning of main()
function. - The program executes line by line.
17Some Details
- C is case sensitive
- (printf not same as Printf).
- White Space in the source code (program) is
ignored by the compiler. - Spaces, tabs, newline (Enter key).
- Things have names.
- include, main, printf, return.
- CodeWarrior color codes reserved words
- Punctuation, other symbols have specific meaning.
- , ltgt \ ...
18Using Code Warrior
19Create Folder for Programs
- Create a folder on your z drive (e.g. cs151).
- Always create a new project starting in this
folder. - CodeWarrior will create a new folder under this
folder for each new program using your project
name as the folder name (e.g. pgm1)
20Starting Code Warrior
Start the CodeWarrior Integrated Development
Environment
21Start a new project (program)
22The New Project Dialog
click
23The File Dialog
3rd
24Project Type
2nd
25Select Project Stationery
26Project Window (locked)
27Make It An MDI Child
28The Project Window
29The Edit Window
30Click the RUN Button
31The Output Window
32Questions?
- Read Chapter 2
- Get started on Programming Assignment 1
- Take Quiz 1 on BlackBoard starting Monday