Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Julian Gough
October 1, 2013
Introduction to C
Outline
1 Introduction to C
Revision from this morning
Second Simple Program
Good Programming Hygiene
Coursework
Introduction to C Revision from this morning
Homework
Understanding Plagiarism
Terminology
Terminology
Terminology
Terminology
Terminology
Terminology
Terminology
Terminology
Terminology
Terminology
Terminology
Terminology
Terminology
int main(void)
{
printf("Hello, world!\n");
return 0;
}
Introduction to C Revision from this morning
int main(void)
{
printf("Hello, world!\n");
return 0;
}
int main(void)
{
printf("Hello, world!\n");
return 0;
}
#include <stdio.h>
Lines that begin with a # are called preprocessing directives.
Visual Studio displays reserved words in blue.
<stdio.h> is a standard library needed for the printf function.
Introduction to C Revision from this morning
int main(void)
{
printf("Hello, world!\n");
return 0;
}
int main(void)
Every C program needs exactly one function called main()
The program will start its execution at the top of main.
Introduction to C Revision from this morning
int main(void)
{
printf("Hello, world!\n");
return 0;
}
int main(void)
{
printf("Hello, world!\n");
return 0;
}
printf()
One of the pre-defined library functions being called (invoked) using as
single argument the string: "Hello, world!\n" (strings in red).
The \n means print the single character newline.
The function printf relies on including <stdio.h>
Introduction to C Revision from this morning
int main(void)
{
printf("Hello, world!\n");
return 0;
}
return 0
Instruct the OS that the function main() has completed successfully.
Notice all declarations and statements are terminated with a semi-colon.
Introduction to C Revision from this morning
The Preprocessor
I #include <stdio.h>
Variables
int main(void){
I int length, width, depth, volume;
printf("What are the dimensions of the box? ");
type name;
Variables are used to hold changing values of various types.
Some standard types are int, float, and void.
Variables need to have a name, aka identifier.
Finding good names drastically improves readability of code!
Variables must be declared.
Once declared, they can be assigned a value.
Later we will learn about scope and how to create our own types.
Introduction to C Second Simple Program
Variables
int main(void){
int length, width, depth, volume;
printf("What are the dimensions of the box? ");
Declare-then-initialize
int volume;
volume = 0;
Declare-and-initialize
int volume = 0;
Introduction to C Second Simple Program
Variables
int main(void){
int length, width, depth, volume;
printf("What are the dimensions of the box? ");
Basic Arithmetic
+, -, *, /, %
Add, Subtract, Multiply, Divide, Modulus.
Integer division / discards remainder i.e.
1/2 is 0 , 7/2 is 3.
Modular remainder % is only available for integer arithmetic
7%4 is 3, 12%6 is 0.
Introduction to C Second Simple Program
Functions
int boxvolume(int x, int y, int z);
...
volume = boxvolume(length, width, depth);
...
int boxvolume(int x, int y, int z){
return x * y * z;
}
Functions
I int boxvolume(int x, int y, int z);
...
volume = boxvolume(length, width, depth);
...
int boxvolume(int x, int y, int z){
return x * y * z;
}
Functions
int boxvolume(int x, int y, int z);
...
I volume = boxvolume(length, width, depth);
...
int boxvolume(int x, int y, int z){
return x * y * z;
}
Functions
int boxvolume(int x, int y, int z);
...
volume = boxvolume(length, width, depth);
...
I int boxvolume(int x, int y, int z){
I return x * y * z;
I }
Functions
int boxvolume(int x, int y, int z);
...
volume = boxvolume(length, width, depth);
...
int boxvolume(int x, int y, int z){
return x * y * z;
}
Structured Programming
Programming Language Pragmatics IIIpage 242
Using printf()
Using printf()
Using scanf()
Preprocessor directives
Function prototypes
int main(void){
Declarations
Statements
}
Function definitions
Introduction to C Good Programming Hygiene
Preprocessor directives
Function prototypes
int main(void){
Declarations
Statements
}
Function definitions
Structured Programming
Programming Language Pragmatics IIIpage 242
Literate Programming
I believe that the time is ripe for significantly better documentation of
programs, and that we can best achieve this by considering programs
to be works of literature. Hence, my title: Literate Programming.
Let us change our traditional attitude to the construction of programs:
Instead of imagining that our main task is to instruct a computer what
to do, let us concentrate rather on explaining to human beings what we
want a computer to do.
The practitioner of literate programming can be regarded as an
essayist, whose main concern is with exposition and excellence of style.
Such an author, with thesaurus in hand, chooses the names of variables
carefully and explains what each variable means. He or she strives for a
program that is comprehensible because its concepts have been
introduced in an order that is best for human understanding, using a
mixture of formal and informal methods that reinforce each other.
Donald E. Knuth
Introduction to C Good Programming Hygiene
Style
Layout examples
#include <stdio.h>
int main(void)
{
printf("Hello, world!\n");
return 0;
}
Style
Layout examples
#include <stdio.h>
int main(void)
{
printf("Hello, world!\n");
return 0;
}
Style
Layout examples
#include <stdio.h>
int main(void)
{
printf("Hello, world!\n");
return 0;
}
Style
Layout examples
#include <stdio.h>
int main(void){
printf("Hello, world!\n");
return 0;
}
Style
Layout examples
#include <stdio.h>
int main(void)
{
printf("Hello, world!\n");
return 0;
}
The program with more elaborate comments
Introduction to C Good Programming Hygiene
Style
Layout examples
#include <stdio.h>
int main(void)
{
printf("Hello, world!\n");
return 0;
}
Style
Layout examples
Style
Layout examples
Style
Layout examples
A deliberate mess
Introduction to C Good Programming Hygiene
Style
Good style keeps code readable
Makes it easier to maintain
Reduces the number of bugs
Usually faster to write
Style
Good style keeps code readable
Makes it easier to maintain
Reduces the number of bugs
Usually faster to write
Layout aspect
How to format comments
How to position braces
How to indent
Introduction to C Good Programming Hygiene
Style
Good style keeps code readable
Makes it easier to maintain
Reduces the number of bugs
Usually faster to write
Naming aspect
Using descriptive names for variables
but also for functions and constants
Avoid similar names
Introduction to C Good Programming Hygiene
Style
Good style keeps code readable
Makes it easier to maintain
Reduces the number of bugs
Usually faster to write
Commenting
To state what a piece of code does
To explain how a piece of code does something
To remark why a piece of code is there
Introduction to C Good Programming Hygiene
Bugs
Or problems with your program...
COMSM11500 Assessment
Courseworks with increasing weight
what weight announced work on deadline
CW1 5 oct 1 lab 1 oct 7
CW2 10 oct 29 lab 5 nov 1
CW3 15 nov 2 lab 4/6 nov 11
CW4 20 nov 12 lab 7/8 dec 2
Final 50 dec 3 lab 10/11/12 dec 20
Ethics
COMS11500 Guidelines
Stick to C99 and standard libraries (no C++).
If in doubt, ask!
We will develop style guidelines jointly.
Introduction to C Coursework
Help
Labs
There will be a team of teaching assistants in the labs. They are not
allowed to write pieces of code for you! What they can do:
Help you with any Visual Studio issues.
Explain the coursework problem.
Explain general C syntax etc.
Point out simple bugs if prompted.
Suggest debugging routines.
Forum
To avoid duplication (answering the same question over and over), we will
make extensive use of a bulletin board/forum.
Any problems with the computers see the central service desk:
http://www.bristol.ac.uk/it-services/contacts/
Introduction to C Coursework
Coursework
Typical Workflow
Read
problem
descrip-
tion
Create
Think about program
Oops
Works? Fix
OK
Oops
Check style
OK
Submit!
Introduction to C Coursework
Terminology
Variable
#include
Identifier
<stdio.h>
Type
main
Declaration
return
Statement
int
Function
float
Prototype
void
Definition
printf
Body
scanf
Parameter
"%d"
Argument
"\n"
Return value