CS 151 - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

CS 151

Description:

printf('Go Beavers'); Text inside double quotes is called a string. ... printf('Go Beavers'); printf means print formatted to the screen using certain ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 33
Provided by: webEngrOr
Category:
Tags: beavers

less

Transcript and Presenter's Notes

Title: CS 151


1
CS 151
  • Lecture 3
  • Programming in C

2
ACM 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

3
Programming 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.

4
Function Structure
int main (void) / C instructions here
/ return 0
5
A Simple Program Printing
  • Make a program that prints text on the screen.
  • Basic outline of a C program
  • Use a library function, printf()

6
C 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.

7
The 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.

8
A 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.

10
Preprocessor 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.
11
Main 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, .
12
Console (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.
13
Formatting 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.

14
Using Formatting characters
  • printf("one\ntwo") prints
  • one
  • two
  • printf("one\ttwo") prints
  • one two
  • printf("one two three") prints
  • one two three

15
Print 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
16
The 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.

17
Some 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 \ ...

18
Using Code Warrior
19
Create 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)

20
Starting Code Warrior
Start the CodeWarrior Integrated Development
Environment
21
Start a new project (program)
22
The New Project Dialog
click
23
The File Dialog
3rd
24
Project Type
2nd
25
Select Project Stationery
26
Project Window (locked)
27
Make It An MDI Child
28
The Project Window
29
The Edit Window
30
Click the RUN Button
31
The Output Window
32
Questions?
  • Read Chapter 2
  • Get started on Programming Assignment 1
  • Take Quiz 1 on BlackBoard starting Monday
Write a Comment
User Comments (0)
About PowerShow.com