announcements assignment 1 due wednesday at 11:59pm quiz 1 on thursday 1
DESCRIPTION
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 3TRANSCRIPT
![Page 1: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/1.jpg)
Announcements
• Assignment 1 due Wednesday at 11:59PM• Quiz 1 on Thursday
1
![Page 2: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/2.jpg)
CSc 352: Basic C Programming
![Page 3: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/3.jpg)
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
![Page 4: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/4.jpg)
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
![Page 5: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/5.jpg)
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
![Page 6: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/6.jpg)
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
![Page 7: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/7.jpg)
A simple program
7
![Page 8: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/8.jpg)
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
![Page 9: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/9.jpg)
A simple program…
9
invoking the C compiler
compilerwarning
executable file produced by compiler
executing the program
![Page 10: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/10.jpg)
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”
![Page 11: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/11.jpg)
Fixing the compiler warnings
11
specifies prototype for printf()
specifies return type explicitly
![Page 12: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/12.jpg)
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
![Page 13: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/13.jpg)
A simple program revisited
13
![Page 14: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/14.jpg)
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”
![Page 15: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/15.jpg)
Fixing the compiler warnings
15
How do we know what file to include?
![Page 16: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/16.jpg)
• 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
![Page 17: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/17.jpg)
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
![Page 18: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/18.jpg)
18
man scanf
Header file
![Page 19: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/19.jpg)
19
man printf
Where is the header file?
![Page 20: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/20.jpg)
20
man printf bottom
There is both a user command and library call called printf
![Page 21: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/21.jpg)
21
man 3 printf
![Page 22: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/22.jpg)
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
![Page 23: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/23.jpg)
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
![Page 24: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/24.jpg)
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)
![Page 25: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/25.jpg)
25
Reminder of things done in class
![Page 26: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/26.jpg)
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”
![Page 27: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/27.jpg)
Simple conditionals, while loops
27
if statement
while statement
error message:sent to stderr
return value communicates normal/abnormal execution
![Page 28: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/28.jpg)
For loops
28
![Page 29: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/29.jpg)
Function calls, recursion
29
recursion
function call
![Page 30: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/30.jpg)
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
![Page 31: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/31.jpg)
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
![Page 32: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/32.jpg)
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
![Page 33: Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1](https://reader036.vdocument.in/reader036/viewer/2022081512/5a4d1b7a7f8b9ab0599b893a/html5/thumbnails/33.jpg)
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