CIS 190: C/C++ Programming: Introduction and Getting Started
CIS 190: C/C++ Programming: Introduction and Getting Started
CIS 190: C/C++ Programming: Introduction and Getting Started
Lecture 1
Introduction and Getting Started
Outline
Syllabus
Quiz
Intro to C Programming
Basics
Sieve of Eratosthenes example
More Basics
Homework
Lecture/Recitation
the lecture portion is shared by all CIS 19X
this is the recitation its the actual class
homeworks, projects, and your grade
Grades
grades are based on:
homework (70%)
final project (30%)
no exams
Homeworks
due roughly weekly
coding environment is left to you, but
homeworks must run correctly on the
eniac.seas.upenn.edu machines
Late Days
you have 3 late days for use on homeworks
can use up to 2 days for a single homework
cant use late days for project deliverables
Final Project
timeline of about a month
design and implement your own choice of
project in C++
must work in pairs
past projects include implementations of
Solitaire and Clue, text-based adventures, and
helper apps like to-do lists
9
10
11
Policies Attendance
attendance is not mandatory
there are significant advantages though:
ask questions in real time
input on what topics we cover
hints and tips for homeworks
13
Policies Contact
use the Piazza page for any questions about
the course or assignments
use private questions if you need to post code
dont email the TAs/Instructor
Outline
Syllabus
Quiz
Intro to C Programming
Basics
Sieve of Eratosthenes example
More Basics
Homework
15
Quiz
16
Outline
Syllabus
Quiz
Intro to C Programming
Basics
Sieve of Eratosthenes example
More Basics
Homework
17
Word of warning
C is a language that values speed and
programmer control over guaranteed
safety. You are expected to develop habits
that will keep you safe rather than doing
random things and hoping to be caught.
Kate Gregory
18
Basics
editor
xemacs, emacs, pico, vim
setup guide for Windows machines on website
compiler
run in terminal; gcc
hello_world.c
#include <stdio.h>
int main()
{
printf(Hello world!\n);
return 0;
}
LIVECODING
20
21
Anatomy of hello_world.c
/* includes functions from the
standard library, like printf */
#include <stdio.h>
/* main entry point to our program */
int main()
{
/* prints Hello World! and a newline to screen*/
printf(Hello world!\n);
/* returns the result code for the
program 0 means there was no error */
return 0;
}
LIVECODING
22
Variable types in C
cannot assume variables will be initialized
variables available in C:
int, char, float , double, etc.
long/short, signed/unsigned, etc.
not available in C:
boolean, String
23
printf
printf(format string, <arguments>);
%d
%c
%s
%f
:
:
:
:
int
char
string
float
(e.g., 7)
(e.g., a)
(e.g., hello)
(e.g., 6.5)
25
printf example
unlike System.out.println, you
need to insert line breaks manually: \n
26
28
scanf
reads information from the console (user)
need to know details about input (formatting)
scanf example
int x;
printf(Enter value for x: );
scanf(%d, &x);
printf(x is %d\n, x);
> ./a.out
> Enter value for x:
31
scanf example
int x;
printf(Enter value for x: );
scanf(%d, &x);
printf(x is %d\n, x);
> ./a.out
> Enter value for x: 18
32
scanf example
int x;
printf(Enter value for x: );
scanf(%d, &x);
printf(x is %d\n, x);
> ./a.out
> Enter value for x: 18
> x is 18
33
Arrays in C
Declaration:
<type> <name> [size];
float xArray [10];
Indexing starts at 0:
xArray[9]; /* end of the array */
34
Limitations of Arrays
no bounds checking
no built-in knowledge of array size
cant return an array from a function
arrays are static
once created, their size is fixed
35
Declaring arrays in C
size needs to be pre-determined at compile
time
for example, this means you cannot make it a
value that is read in from the user
unless you allocate the memory manually
(which well cover in later lectures)
36
Outline
Syllabus
Quiz
Intro to C Programming
Basics
Sieve of Eratosthenes example
More Basics
Homework
38
Sieve of Eratosthenes
algorithm to quickly identify prime numbers
before starting, choose the highest number
youd like to check for being prime
assume all numbers >= 2 are prime numbers
for each prime number you encounter, mark
all of its multiples as composite (not prime)
39
40
Outline
Syllabus
Quiz
Intro to C Programming
Basics
Sieve of Eratosthenes example
More Basics
Homework
41
Functions in C
42
Function Parts
prototype:
function must be declared before it can be used
int SquareNumber (int n);
definition:
function must be defined
int SquareNumber (int n) {
return (n * n); }
call:
int answer = SquareNumber(3);
43
Functions in hello_world.c
#include <stdio.h>
void PrintHelloWorld();
int main()
{
PrintHelloWorld();
return 0;
}
/* function prototype */
/* function call */
void PrintHelloWorld()
/* function definition */
{
printf(Hello world!\n);
}
LIVECODING
44
C-style strings
there is no native String type in C
instead, strings in C are represented
as an array of characters
terminated by the NULL character, \0
(backslash zero)
45
char
char str2 [] = cat;
element
char
char str3 [5];
element
char
47
\0
\0
char
48
\0
\0
char
49
\0
\0
\0
\0
54
Outline
Syllabus
Quiz
Intro to C Programming
Basics
Sieve of Eratosthenes example
More Basics
Homework
55
Homework 1
five things to do:
complete hello_world.c
complete answers.txt
turn the above two files in using turnin
read the style guide online
fill out availability for OH on when2meet
use your upenn username
56