Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
-
Upload
cleopatra-lamb -
Category
Documents
-
view
220 -
download
0
description
Transcript of Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Announcements
• Assignment 1 due Wednesday at 11:59PM• Quiz 1 on Thursday
1
CSc 352: Basic C Programming
What’s new relative to Java?
• Some syntactic differences– a lot of the basic syntax is similar
• assignment, conditionals, loops, etc.
• The biggest differences are conceptual– procedural rather than object-oriented
• no notion of classes and inheritance hierarchies– much closer to the machine
• debugging sometimes requires thorough understanding of what’s going on at the machine level
– explicit dynamic memory management (malloc, free)– pointers
3
What’s new relative to Java?
• No Garbage Collection• No array boundary protection• You have much more control
– This means you can write faster programs– This also means code can be hard to debug and security
vulnerabilities are much more likely• C is generally compiled to “machine code” (java
programs require java “machine” to be installed)
4
The program development process
5
source code
gcc
executable code
• C program• created with an editor• ASCII text • human-readable
• machine code • created using compiler• binary file • not human-readable
Simple programs (single source file)
compiler
The program development process
6
file1.c
source files
executable code
More complex programs (many source files)
file2.c
filen.c…
object files
file1.o
file2.o
gcc -c
compiler
gcc -c
gcc -c
linker
filen.o
A simple program
7
A simple program
8
a simple C programthat prints out “hello”
Points to note:• execution begins at main(): every program must have
one• printf() is a standard C library routine
A simple program…
9
invoking the C compiler
compilerwarning
executable file produced by compiler
executing the program
Gcc options: -Wall
10
gcc –Wall generates warnings on questionable constructs
• if no return type is specified for a function, it defaults to int
• need to supply prototype for “printf”
Fixing the compiler warnings
11
specifies prototype for printf()
specifies return type explicitly
Summary
• execution starts at main()– every program should have one
• the return type for a function defaults to int– should specify explicitly: good style
• need to supply prototypes for functions imported from elsewhere (e.g., standard libraries)– specified using “#include …”
12
A simple program revisited
13
Gcc options: -Wall
14
gcc –Wall generates warnings on questionable constructs
• if no return type is specified for a function, it defaults to int
• need to supply prototype for “printf”
Fixing the compiler warnings
15
How do we know what file to include?
• The man command displays documentation for commands (and more). Here is an abridged example—the "man page" for cat:
• % man cat• CAT(1) User Commands CAT(1)
• NAME• cat - concatenate files and print on the standard output
• SYNOPSIS• cat [OPTION]... [FILE]...
• DESCRIPTION• Concatenate FILE(s), or standard input, to standard output.
• -A, --show-all• equivalent to –vET• ...
• With no FILE, or when FILE is -, read standard input.
• man uses less to display pages. Type space to go forwards, b to go backwards. Type /STRING<ENTER> to search for a string, then n to search for the next occurrence. h (for help) shows lots more less commands
CSC 352 Fall 2015, Unix Slide 16
The man command
The UNIX "manual" is divided into these sections: (from man man) 1 User commands 2 System calls (functions provided by the kernel) 3 Library calls (functions within program libraries) 4 Special files (usually found in /dev) 5 File formats and conventions eg /etc/passwd 6 Games 7 Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7) 8 System administration commands (usually only for root) 9 Kernel routines [Non standard]
Recall that man cat showed CAT(1). That "(1)" tells us that cat is a user command.
man malloc shows MALLOC(3). That "(3)" tells us that malloc is a library function.
Manual sections
18
man scanf
Header file
19
man printf
Where is the header file?
20
man printf bottom
There is both a user command and library call called printf
21
man 3 printf
22
gcc -o Option
• We’ve seen the command to compile a c program is gcc– To compile the program hello_1.c type gcc hello_1.c– The compiler creates an executable file (if there are no errors)– By default that file is named a.out– This is an executable file, meaning one can type it as a command in the shell
23
gcc -o Option
• If we compile another program, say gcc test.c then a new executable called a.out is created. If there is an existing file, it is overwritten.
• What if we want to keep both executables?– use the -o option
24
gcc -o option
• The -o option allows you to specify a name for the output (executable) file– gcc -o <output file> <c file>
• Why did I call the output testing instead of test? (Let’s open a terminal)
25
Reminder of things done in class
Simple declarations and statements
26
two uninitialized global variables, x and y, of type int
a variable, z, of type int, that is local to the function main; initialized to 12
simple arithmetic expressions and assignment statementsprint format specifier:
%d = “print a decimal value”
Simple conditionals, while loops
27
if statement
while statement
error message:sent to stderr
return value communicates normal/abnormal execution
For loops
28
Function calls, recursion
29
recursion
function call
Formatted Output: printf()
• takes a variable no. of arguments:– printf(“…fmtStr…”, arg1, arg2, …, argn)
• “… fmtStr…” is a string that can contain conversion specifiers• the no. of conversion specifiers should be equal to n• “regular” (non-%) characters in fmtStr written out unchanged
– each conversion specifier is introduced by ‘%’• this is followed by additional (optional) characters specifying how
wide the output is to be, precision, padding, etc.• the conversion specifier indicates how the specified value is to be
interpreted:– d = decimal integer, e.g.: printf(“value = %d\n”, n);– x = hex integer, e.g.: printf(“hex value of %d is %x\n”, x, x);– f = floating point number, e.g.: printf(“area = %f\n”, A);
30
text: Ch. 3Sec. 3.1
Function calls (cont’d)
31
What happens when this printf() is called?
• the arguments are evaluated (as in all C function calls)• factorial(6) evaluated
• when factorial() returns, the printf is executed:• printf(“ … ”, 720)
• This causes the stringfactorial(6) = 720
to be printed out
Formatted Input: scanf()
• takes a variable no. of arguments:– scanf(“…fmtStr…”, arg1, arg2, …, argn)
• “… fmtStr…” is a string that can contain conversion specifiers• the no. of conversion specifiers should be equal to n• argi are locations where values that are read in should be placed• each conversion specifier is introduced by ‘%’
– similar to conversions for printf
• execution behavior:– uses format string to read in as many input values from
stdin as it can– return value indicates the no. of values it was able to read
• return value of EOF (-1) indicates no further input (“end of file”).
32
text: Ch. 3Sec. 3.2
scanf()
• Specifying where to put an input value:– in general we need to provide a pointer to the location
where the value should be placed• for a scalar variable X, this is typically written as &X
– Examples:• scanf(“%d”, &n) : read a decimal value into a variable n• scanf(“%d %d %d”, &x, &y, &z) : read three decimal values and
put them into variables x, y, z respectively– suppose the input contains the values 12, 3, 71, 95, 101. Then:
» x 12; y 3; z 71; return value = 3– suppose the input contains the values 19, 23. Then:
» x 19; y 23; z is unassigned; return value = 2
33