So here is a simpler example. In file...
Transcript of So here is a simpler example. In file...
![Page 1: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/1.jpg)
C
So here is a simpler example. In file hello.c
#include <stdio.h>
/* This is a
block comment */
int main(void)
{
// do something interesting
printf("hello world\n");
return 0;
}
1 / 109
![Page 2: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/2.jpg)
C
Points of note
• General syntax — comments, curly brackets, semicolons,quotes, etc. — is pretty familiar
• Filename can be anything that ends .c, no relationshipconnecting filenames to classes goes on: C does not haveclasses
• The #include line we shall describe later: for now justthink of it as something to put at the start of every C file
• The function main is the entry point of the program. i.e.,when the program is run, it starts executing from here
• It doesn’t have any fancy type: it just returns an integer
2 / 109
![Page 3: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/3.jpg)
C
Points of note
• General syntax — comments, curly brackets, semicolons,quotes, etc. — is pretty familiar
• Filename can be anything that ends .c, no relationshipconnecting filenames to classes goes on: C does not haveclasses
• The #include line we shall describe later: for now justthink of it as something to put at the start of every C file
• The function main is the entry point of the program. i.e.,when the program is run, it starts executing from here
• It doesn’t have any fancy type: it just returns an integer
3 / 109
![Page 4: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/4.jpg)
C
Points of note
• General syntax — comments, curly brackets, semicolons,quotes, etc. — is pretty familiar
• Filename can be anything that ends .c, no relationshipconnecting filenames to classes goes on: C does not haveclasses
• The #include line we shall describe later: for now justthink of it as something to put at the start of every C file
• The function main is the entry point of the program. i.e.,when the program is run, it starts executing from here
• It doesn’t have any fancy type: it just returns an integer
4 / 109
![Page 5: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/5.jpg)
C
Points of note
• General syntax — comments, curly brackets, semicolons,quotes, etc. — is pretty familiar
• Filename can be anything that ends .c, no relationshipconnecting filenames to classes goes on: C does not haveclasses
• The #include line we shall describe later: for now justthink of it as something to put at the start of every C file
• The function main is the entry point of the program. i.e.,when the program is run, it starts executing from here
• It doesn’t have any fancy type: it just returns an integer
5 / 109
![Page 6: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/6.jpg)
C
Points of note
• General syntax — comments, curly brackets, semicolons,quotes, etc. — is pretty familiar
• Filename can be anything that ends .c, no relationshipconnecting filenames to classes goes on: C does not haveclasses
• The #include line we shall describe later: for now justthink of it as something to put at the start of every C file
• The function main is the entry point of the program. i.e.,when the program is run, it starts executing from here
• It doesn’t have any fancy type: it just returns an integer
6 / 109
![Page 7: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/7.jpg)
C
Points of note
• General syntax — comments, curly brackets, semicolons,quotes, etc. — is pretty familiar
• Filename can be anything that ends .c, no relationshipconnecting filenames to classes goes on: C does not haveclasses
• The #include line we shall describe later: for now justthink of it as something to put at the start of every C file
• The function main is the entry point of the program. i.e.,when the program is run, it starts executing from here
• It doesn’t have any fancy type: it just returns an integer
7 / 109
![Page 8: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/8.jpg)
C
• In this example main has an empty (void) argument list: Clikes you to indicate explicitly there are no arguments
• The function printf (“print formatted”) prints stuff, here astring with a newline at the end (the \n)
• The program exits when you return from main
• main returns a value back to the operating system whenthe program finishes. The OS can use this in various ways,if it wishes. The convention is 0 means “finishedsuccessfully”, while non-zero values can signify variouskinds of error
8 / 109
![Page 9: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/9.jpg)
C
• In this example main has an empty (void) argument list: Clikes you to indicate explicitly there are no arguments
• The function printf (“print formatted”) prints stuff, here astring with a newline at the end (the \n)
• The program exits when you return from main
• main returns a value back to the operating system whenthe program finishes. The OS can use this in various ways,if it wishes. The convention is 0 means “finishedsuccessfully”, while non-zero values can signify variouskinds of error
9 / 109
![Page 10: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/10.jpg)
C
• In this example main has an empty (void) argument list: Clikes you to indicate explicitly there are no arguments
• The function printf (“print formatted”) prints stuff, here astring with a newline at the end (the \n)
• The program exits when you return from main
• main returns a value back to the operating system whenthe program finishes. The OS can use this in various ways,if it wishes. The convention is 0 means “finishedsuccessfully”, while non-zero values can signify variouskinds of error
10 / 109
![Page 11: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/11.jpg)
C
• In this example main has an empty (void) argument list: Clikes you to indicate explicitly there are no arguments
• The function printf (“print formatted”) prints stuff, here astring with a newline at the end (the \n)
• The program exits when you return from main
• main returns a value back to the operating system whenthe program finishes. The OS can use this in various ways,if it wishes. The convention is 0 means “finishedsuccessfully”, while non-zero values can signify variouskinds of error
11 / 109
![Page 12: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/12.jpg)
C
We can compile this file
% gcc -Wall -o hello hello.c
12 / 109
![Page 13: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/13.jpg)
C
• The % is a command line prompt
• -Wall is a option to the compiler that tells it to report allwarnings. A warning is something in your code that mightnot be technically wrong, but is sufficiently dodgy to beworth looking at. Always use this option. You should aim towrite code with no warnings (and no errors!)
• -Wextra gives even more warnings• -Werror makes warnings into errors: the compiler will
refuse to produce any output if there are any warnings• -o hello says put the compiled program in the file namedhello. This filename can be anything you like, notnecessarily related to the source code file name
13 / 109
![Page 14: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/14.jpg)
C
• The % is a command line prompt• -Wall is a option to the compiler that tells it to report all
warnings. A warning is something in your code that mightnot be technically wrong, but is sufficiently dodgy to beworth looking at. Always use this option. You should aim towrite code with no warnings (and no errors!)
• -Wextra gives even more warnings• -Werror makes warnings into errors: the compiler will
refuse to produce any output if there are any warnings• -o hello says put the compiled program in the file namedhello. This filename can be anything you like, notnecessarily related to the source code file name
14 / 109
![Page 15: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/15.jpg)
C
• The % is a command line prompt• -Wall is a option to the compiler that tells it to report all
warnings. A warning is something in your code that mightnot be technically wrong, but is sufficiently dodgy to beworth looking at. Always use this option. You should aim towrite code with no warnings (and no errors!)
• -Wextra gives even more warnings
• -Werror makes warnings into errors: the compiler willrefuse to produce any output if there are any warnings
• -o hello says put the compiled program in the file namedhello. This filename can be anything you like, notnecessarily related to the source code file name
15 / 109
![Page 16: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/16.jpg)
C
• The % is a command line prompt• -Wall is a option to the compiler that tells it to report all
warnings. A warning is something in your code that mightnot be technically wrong, but is sufficiently dodgy to beworth looking at. Always use this option. You should aim towrite code with no warnings (and no errors!)
• -Wextra gives even more warnings• -Werror makes warnings into errors: the compiler will
refuse to produce any output if there are any warnings
• -o hello says put the compiled program in the file namedhello. This filename can be anything you like, notnecessarily related to the source code file name
16 / 109
![Page 17: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/17.jpg)
C
• The % is a command line prompt• -Wall is a option to the compiler that tells it to report all
warnings. A warning is something in your code that mightnot be technically wrong, but is sufficiently dodgy to beworth looking at. Always use this option. You should aim towrite code with no warnings (and no errors!)
• -Wextra gives even more warnings• -Werror makes warnings into errors: the compiler will
refuse to produce any output if there are any warnings• -o hello says put the compiled program in the file namedhello. This filename can be anything you like, notnecessarily related to the source code file name
17 / 109
![Page 18: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/18.jpg)
Aside
Note: this is an example of compiling a C program using acommand-line compiler (The GNU C compiler gcc in this case)
Other compilers will likely take different arguments, e.g.,another compiler might not recognise -Wall and could havesomething else equivalent (e.g., -v)
Many IDEs exist that are supposed to make the managementand compilation of large programs easier (e.g., Eclipse, VisualStudio, Xcode)
Exercise. Investigate these to find something that suits yourpersonal taste
18 / 109
![Page 19: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/19.jpg)
Aside
Note: this is an example of compiling a C program using acommand-line compiler (The GNU C compiler gcc in this case)
Other compilers will likely take different arguments, e.g.,another compiler might not recognise -Wall and could havesomething else equivalent (e.g., -v)
Many IDEs exist that are supposed to make the managementand compilation of large programs easier (e.g., Eclipse, VisualStudio, Xcode)
Exercise. Investigate these to find something that suits yourpersonal taste
19 / 109
![Page 20: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/20.jpg)
Aside
Note: this is an example of compiling a C program using acommand-line compiler (The GNU C compiler gcc in this case)
Other compilers will likely take different arguments, e.g.,another compiler might not recognise -Wall and could havesomething else equivalent (e.g., -v)
Many IDEs exist that are supposed to make the managementand compilation of large programs easier (e.g., Eclipse, VisualStudio, Xcode)
Exercise. Investigate these to find something that suits yourpersonal taste
20 / 109
![Page 21: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/21.jpg)
Aside
Note: this is an example of compiling a C program using acommand-line compiler (The GNU C compiler gcc in this case)
Other compilers will likely take different arguments, e.g.,another compiler might not recognise -Wall and could havesomething else equivalent (e.g., -v)
Many IDEs exist that are supposed to make the managementand compilation of large programs easier (e.g., Eclipse, VisualStudio, Xcode)
Exercise. Investigate these to find something that suits yourpersonal taste
21 / 109
![Page 22: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/22.jpg)
Aside
NB: there is a difference between an IDE and a compiler
Compiler: converts a text source program into executable code
IDE: a tool to help the programmer write better programs
An IDE will generally contain a compiler that it calls when youwant to produce executable code, but they are very differentthings
Keep them separate in your mind
22 / 109
![Page 23: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/23.jpg)
Aside
NB: there is a difference between an IDE and a compiler
Compiler: converts a text source program into executable code
IDE: a tool to help the programmer write better programs
An IDE will generally contain a compiler that it calls when youwant to produce executable code, but they are very differentthings
Keep them separate in your mind
23 / 109
![Page 24: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/24.jpg)
Aside
NB: there is a difference between an IDE and a compiler
Compiler: converts a text source program into executable code
IDE: a tool to help the programmer write better programs
An IDE will generally contain a compiler that it calls when youwant to produce executable code, but they are very differentthings
Keep them separate in your mind
24 / 109
![Page 25: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/25.jpg)
Aside
NB: there is a difference between an IDE and a compiler
Compiler: converts a text source program into executable code
IDE: a tool to help the programmer write better programs
An IDE will generally contain a compiler that it calls when youwant to produce executable code, but they are very differentthings
Keep them separate in your mind
25 / 109
![Page 26: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/26.jpg)
Aside
NB: there is a difference between an IDE and a compiler
Compiler: converts a text source program into executable code
IDE: a tool to help the programmer write better programs
An IDE will generally contain a compiler that it calls when youwant to produce executable code, but they are very differentthings
Keep them separate in your mind
26 / 109
![Page 27: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/27.jpg)
More Aside
Many C compilers exist, both paid-for and free. Differentcompilers may produce more or less efficient compiled codefrom the same source.
If all is well, a given standard-compliant (e.g., ANSI C11) Cprogram should compile with a standard-compliant C compilerand should run and produce equivalent results independent ofthe compiler
Exercise. Think about why I said “equivalent results”, not“identical results”
27 / 109
![Page 28: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/28.jpg)
More Aside
Many C compilers exist, both paid-for and free. Differentcompilers may produce more or less efficient compiled codefrom the same source.
If all is well, a given standard-compliant (e.g., ANSI C11) Cprogram should compile with a standard-compliant C compilerand should run and produce equivalent results independent ofthe compiler
Exercise. Think about why I said “equivalent results”, not“identical results”
28 / 109
![Page 29: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/29.jpg)
More Aside
Many C compilers exist, both paid-for and free. Differentcompilers may produce more or less efficient compiled codefrom the same source.
If all is well, a given standard-compliant (e.g., ANSI C11) Cprogram should compile with a standard-compliant C compilerand should run and produce equivalent results independent ofthe compiler
Exercise. Think about why I said “equivalent results”, not“identical results”
29 / 109
![Page 30: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/30.jpg)
More Aside
Not all (any?) C compilers are fully standard-compliant
Not many C programs are fully standard-compliant
30 / 109
![Page 31: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/31.jpg)
More Aside
Not all (any?) C compilers are fully standard-compliant
Not many C programs are fully standard-compliant
31 / 109
![Page 32: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/32.jpg)
More Aside
Some compiler writers deliberately put support for non-standardthings in their compilers: for reasons both good and bad
Good: to add extensions that are genuinely useful to theprogrammer
Bad: to add extensions that the programmer will become relianton, not realising they are non-standard, so locking them in tousing this particular compiler
A program that excludes extensions and follows the standardwill be much more portable
32 / 109
![Page 33: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/33.jpg)
More Aside
Some compiler writers deliberately put support for non-standardthings in their compilers: for reasons both good and bad
Good: to add extensions that are genuinely useful to theprogrammer
Bad: to add extensions that the programmer will become relianton, not realising they are non-standard, so locking them in tousing this particular compiler
A program that excludes extensions and follows the standardwill be much more portable
33 / 109
![Page 34: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/34.jpg)
More Aside
Some compiler writers deliberately put support for non-standardthings in their compilers: for reasons both good and bad
Good: to add extensions that are genuinely useful to theprogrammer
Bad: to add extensions that the programmer will become relianton, not realising they are non-standard, so locking them in tousing this particular compiler
A program that excludes extensions and follows the standardwill be much more portable
34 / 109
![Page 35: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/35.jpg)
More Aside
Some compiler writers deliberately put support for non-standardthings in their compilers: for reasons both good and bad
Good: to add extensions that are genuinely useful to theprogrammer
Bad: to add extensions that the programmer will become relianton, not realising they are non-standard, so locking them in tousing this particular compiler
A program that excludes extensions and follows the standardwill be much more portable
35 / 109
![Page 36: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/36.jpg)
More Aside
Gcc is a widely available and widely used free compiler on alarge number of architectures that produces reasonable (butnot the best) code
It’s not the case of paying more to get a better compiler. . .
Many other C compilers exist: Intel; Clang; Microsoft; Norcroft;etc.
36 / 109
![Page 37: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/37.jpg)
More Aside
Gcc is a widely available and widely used free compiler on alarge number of architectures that produces reasonable (butnot the best) code
It’s not the case of paying more to get a better compiler. . .
Many other C compilers exist: Intel; Clang; Microsoft; Norcroft;etc.
37 / 109
![Page 38: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/38.jpg)
More Aside
Gcc is a widely available and widely used free compiler on alarge number of architectures that produces reasonable (butnot the best) code
It’s not the case of paying more to get a better compiler. . .
Many other C compilers exist: Intel; Clang; Microsoft; Norcroft;etc.
38 / 109
![Page 39: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/39.jpg)
More Aside
Also, use a text editor (or an IDE) to write programs, not aword processor
You are not that stupid are you?
And always use a fixed width font when printing out orviewing code. Layout is important in all languages, particularlyin C
39 / 109
![Page 40: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/40.jpg)
More Aside
Also, use a text editor (or an IDE) to write programs, not aword processor
You are not that stupid are you?
And always use a fixed width font when printing out orviewing code. Layout is important in all languages, particularlyin C
40 / 109
![Page 41: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/41.jpg)
More Aside
Also, use a text editor (or an IDE) to write programs, not aword processor
You are not that stupid are you?
And always use a fixed width font when printing out orviewing code. Layout is important in all languages, particularlyin C
41 / 109
![Page 42: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/42.jpg)
C
Running the program
% ./hello
hello world
I usually include the ./ to ensure I run the program namedhello that lives in the current directory, not some program ofthe same name from somewhere else in the system
42 / 109
![Page 43: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/43.jpg)
C
Running the program
% ./hello
hello world
I usually include the ./ to ensure I run the program namedhello that lives in the current directory, not some program ofthe same name from somewhere else in the system
43 / 109
![Page 44: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/44.jpg)
C
The program is a stand-alone binary (machine instructions)which you can simply run
It is compiled for a specific OS and hardware architecture,generally the machine you used the compiler on
So that binary probably won’t run on a different OS or differenthardware architecture: the program will need recompiling forthem
There is no analogue to the java runtime you need to run aJava program
44 / 109
![Page 45: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/45.jpg)
C
The program is a stand-alone binary (machine instructions)which you can simply run
It is compiled for a specific OS and hardware architecture,generally the machine you used the compiler on
So that binary probably won’t run on a different OS or differenthardware architecture: the program will need recompiling forthem
There is no analogue to the java runtime you need to run aJava program
45 / 109
![Page 46: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/46.jpg)
C
The program is a stand-alone binary (machine instructions)which you can simply run
It is compiled for a specific OS and hardware architecture,generally the machine you used the compiler on
So that binary probably won’t run on a different OS or differenthardware architecture: the program will need recompiling forthem
There is no analogue to the java runtime you need to run aJava program
46 / 109
![Page 47: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/47.jpg)
C
The program is a stand-alone binary (machine instructions)which you can simply run
It is compiled for a specific OS and hardware architecture,generally the machine you used the compiler on
So that binary probably won’t run on a different OS or differenthardware architecture: the program will need recompiling forthem
There is no analogue to the java runtime you need to run aJava program
47 / 109
![Page 48: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/48.jpg)
C
Java: “write once, compile once, run everywhere”
C: “write once, compile everywhere, run everywhere”
Another trade-off
Compiling Java produces machine-independent (byte)code thatwill run anywhere — anywhere there is a Java runtime toexecute that code
Compile C produces machine-specific code that only runs onone OS/architecture, but is optimised for that architecture
48 / 109
![Page 49: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/49.jpg)
C
Java: “write once, compile once, run everywhere”
C: “write once, compile everywhere, run everywhere”
Another trade-off
Compiling Java produces machine-independent (byte)code thatwill run anywhere — anywhere there is a Java runtime toexecute that code
Compile C produces machine-specific code that only runs onone OS/architecture, but is optimised for that architecture
49 / 109
![Page 50: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/50.jpg)
C
Java: “write once, compile once, run everywhere”
C: “write once, compile everywhere, run everywhere”
Another trade-off
Compiling Java produces machine-independent (byte)code thatwill run anywhere — anywhere there is a Java runtime toexecute that code
Compile C produces machine-specific code that only runs onone OS/architecture, but is optimised for that architecture
50 / 109
![Page 51: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/51.jpg)
C
Java: “write once, compile once, run everywhere”
C: “write once, compile everywhere, run everywhere”
Another trade-off
Compiling Java produces machine-independent (byte)code thatwill run anywhere — anywhere there is a Java runtime toexecute that code
Compile C produces machine-specific code that only runs onone OS/architecture, but is optimised for that architecture
51 / 109
![Page 52: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/52.jpg)
CCompiler Warnings
A bad program. hello2.c
#include <stdio.h>
int main(void)
{
int n;
n = n + 1;
printf("hello world\n");
return 0;
}
52 / 109
![Page 53: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/53.jpg)
CCompiler Warnings
% cc -Wall -o hello2 hello2.c
hello2.c: In function ’main’:
hello2.c:7:5: warning: ’n’ is used uninitialized
in this function
53 / 109
![Page 54: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/54.jpg)
CCompiler Warnings
A simple example of a warning message
It is very important you get used to reading warning and errormessages
You will see loads!
Get used to them, and get used to fixing the things they refer to:don’t ignore warnings
The quality of error messages varies with the compiler. Gccproduces generally reasonable messages
54 / 109
![Page 55: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/55.jpg)
CCompiler Warnings
A simple example of a warning message
It is very important you get used to reading warning and errormessages
You will see loads!
Get used to them, and get used to fixing the things they refer to:don’t ignore warnings
The quality of error messages varies with the compiler. Gccproduces generally reasonable messages
55 / 109
![Page 56: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/56.jpg)
CCompiler Warnings
A simple example of a warning message
It is very important you get used to reading warning and errormessages
You will see loads!
Get used to them, and get used to fixing the things they refer to:don’t ignore warnings
The quality of error messages varies with the compiler. Gccproduces generally reasonable messages
56 / 109
![Page 57: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/57.jpg)
CCompiler Warnings
A simple example of a warning message
It is very important you get used to reading warning and errormessages
You will see loads!
Get used to them, and get used to fixing the things they refer to:don’t ignore warnings
The quality of error messages varies with the compiler. Gccproduces generally reasonable messages
57 / 109
![Page 58: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/58.jpg)
CCompiler Warnings
A simple example of a warning message
It is very important you get used to reading warning and errormessages
You will see loads!
Get used to them, and get used to fixing the things they refer to:don’t ignore warnings
The quality of error messages varies with the compiler. Gccproduces generally reasonable messages
58 / 109
![Page 59: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/59.jpg)
CCompiler Warnings
In this case, the compiler happens to generate a runnableexecutable; for more serious errors it wouldn’t
What happens when you run it is difficult to say. . .
Note that very exceptionally this kind of thing (running withundefined results) it what you want, but only if you are aprogrammer who is either (a) very clever, or (b) very stupid
59 / 109
![Page 60: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/60.jpg)
CCompiler Warnings
In this case, the compiler happens to generate a runnableexecutable; for more serious errors it wouldn’t
What happens when you run it is difficult to say. . .
Note that very exceptionally this kind of thing (running withundefined results) it what you want, but only if you are aprogrammer who is either (a) very clever, or (b) very stupid
60 / 109
![Page 61: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/61.jpg)
CCompiler Warnings
In this case, the compiler happens to generate a runnableexecutable; for more serious errors it wouldn’t
What happens when you run it is difficult to say. . .
Note that very exceptionally this kind of thing (running withundefined results) it what you want, but only if you are aprogrammer who is either (a) very clever, or (b) very stupid
61 / 109
![Page 62: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/62.jpg)
CCompiler Warnings
The C compiler Clang is reasonably widely available
One of its design aims is to give detailed and accurate errorand warning messages
And to replace Gcc
It is still under heavy development
It also uses -Wall to show warnings
62 / 109
![Page 63: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/63.jpg)
CCompiler Warnings
The C compiler Clang is reasonably widely available
One of its design aims is to give detailed and accurate errorand warning messages
And to replace Gcc
It is still under heavy development
It also uses -Wall to show warnings
63 / 109
![Page 64: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/64.jpg)
CCompiler Warnings
The C compiler Clang is reasonably widely available
One of its design aims is to give detailed and accurate errorand warning messages
And to replace Gcc
It is still under heavy development
It also uses -Wall to show warnings
64 / 109
![Page 65: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/65.jpg)
CCompiler Warnings
The C compiler Clang is reasonably widely available
One of its design aims is to give detailed and accurate errorand warning messages
And to replace Gcc
It is still under heavy development
It also uses -Wall to show warnings
65 / 109
![Page 66: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/66.jpg)
CCompiler Warnings
The C compiler Clang is reasonably widely available
One of its design aims is to give detailed and accurate errorand warning messages
And to replace Gcc
It is still under heavy development
It also uses -Wall to show warnings
66 / 109
![Page 67: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/67.jpg)
CCompiler Warnings
% clang -Wall -o hello2 hello2.c
hello2.c:7:7: warning: variable ’n’ is uninitialized when
used here
[-Wuninitialized]
n = n + 1;
^
hello2.c:5:8: note: initialize the variable ’n’ to silence
this warning
int n;
^
= 0
Here Clang even gives a suggestion on how to fix the warning
67 / 109
![Page 68: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/68.jpg)
CCompiler Warnings
% clang -Wall -o hello2 hello2.c
hello2.c:7:7: warning: variable ’n’ is uninitialized when
used here
[-Wuninitialized]
n = n + 1;
^
hello2.c:5:8: note: initialize the variable ’n’ to silence
this warning
int n;
^
= 0
Here Clang even gives a suggestion on how to fix the warning
68 / 109
![Page 69: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/69.jpg)
CFunction Definition
#include <stdio.h>
int factorial(int n)
{
if (n < 2) return 1;
return n*factorial(n-1);
}
int main(void)
{
printf("factorial of %d is %d\n", 10, factorial(10));
return 0;
}
69 / 109
![Page 70: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/70.jpg)
C
Produces output
factorial of 10 is 3628800
70 / 109
![Page 71: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/71.jpg)
C
printf("factorial of %d is %d\n", 10, factorial(10));
The first argument to printf is a template for the output
Everything apart from % and \n are copied directly
The backslash introduces special characters; in particular \nmeans “put a newline here”
The % says “read the next argument and put its value here”
71 / 109
![Page 72: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/72.jpg)
C
printf("factorial of %d is %d\n", 10, factorial(10));
The first argument to printf is a template for the output
Everything apart from % and \n are copied directly
The backslash introduces special characters; in particular \nmeans “put a newline here”
The % says “read the next argument and put its value here”
72 / 109
![Page 73: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/73.jpg)
C
printf("factorial of %d is %d\n", 10, factorial(10));
The first argument to printf is a template for the output
Everything apart from % and \n are copied directly
The backslash introduces special characters; in particular \nmeans “put a newline here”
The % says “read the next argument and put its value here”
73 / 109
![Page 74: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/74.jpg)
C
printf("factorial of %d is %d\n", 10, factorial(10));
The first argument to printf is a template for the output
Everything apart from % and \n are copied directly
The backslash introduces special characters; in particular \nmeans “put a newline here”
The % says “read the next argument and put its value here”
74 / 109
![Page 75: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/75.jpg)
C
The character after the % indicates how the argument should betreated
%d means an integer
%f means a floating point number
%s means a string
Generally it is up to the programmer to get arguments of theright types in the right order
75 / 109
![Page 76: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/76.jpg)
C
The character after the % indicates how the argument should betreated
%d means an integer
%f means a floating point number
%s means a string
Generally it is up to the programmer to get arguments of theright types in the right order
76 / 109
![Page 77: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/77.jpg)
C
The character after the % indicates how the argument should betreated
%d means an integer
%f means a floating point number
%s means a string
Generally it is up to the programmer to get arguments of theright types in the right order
77 / 109
![Page 78: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/78.jpg)
C
The character after the % indicates how the argument should betreated
%d means an integer
%f means a floating point number
%s means a string
Generally it is up to the programmer to get arguments of theright types in the right order
78 / 109
![Page 79: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/79.jpg)
C
The character after the % indicates how the argument should betreated
%d means an integer
%f means a floating point number
%s means a string
Generally it is up to the programmer to get arguments of theright types in the right order
79 / 109
![Page 80: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/80.jpg)
C
printf("integer %d\nfloating point %f\nstring %s\n",
23 + 42, 99.0, "hello world");
produces
integer 65
floating point 99.000000
string hello world
when run
80 / 109
![Page 81: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/81.jpg)
C
Incorrect code:
printf("integer %d\nfloating point %f\nstring %s\n",
99.0, 23 + 42, "hello world");
produces
printf1.c: In function ’main’:
printf1.c:9:3: warning: format ’%d’ expects type ’int’,
but argument 2 has type ’double’
printf1.c:9:3: warning: format ’%f’ expects type ’double’,
but argument 3 has type ’int’
when you try to compile it
81 / 109
![Page 82: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/82.jpg)
C
Other compilers might not be so helpful and simply do what youask
Giving a floating point number to %d the compiler might simplyinterpret the (bit pattern that represents the) floating pointnumber as (a bit pattern that represents) an integer, and print it
This is a part of the “you asked for it, you got it” approach of C
82 / 109
![Page 83: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/83.jpg)
C
Other compilers might not be so helpful and simply do what youask
Giving a floating point number to %d the compiler might simplyinterpret the (bit pattern that represents the) floating pointnumber as (a bit pattern that represents) an integer, and print it
This is a part of the “you asked for it, you got it” approach of C
83 / 109
![Page 84: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/84.jpg)
C
Other compilers might not be so helpful and simply do what youask
Giving a floating point number to %d the compiler might simplyinterpret the (bit pattern that represents the) floating pointnumber as (a bit pattern that represents) an integer, and print it
This is a part of the “you asked for it, you got it” approach of C
84 / 109
![Page 85: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/85.jpg)
C
printf is a lot more powerful than this: it does all kinds offormatted output (thus the “f” in the name)
Look at the documentation for printf for the gory details
man printf on Linux/Unix systems
In fact, there is masses of documentation online, e.g., man cosfor the cosine function
These manual pages contain a great amount of detailedinformation: make sure you read them closely to get the mostbenefit
85 / 109
![Page 86: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/86.jpg)
C
printf is a lot more powerful than this: it does all kinds offormatted output (thus the “f” in the name)
Look at the documentation for printf for the gory details
man printf on Linux/Unix systems
In fact, there is masses of documentation online, e.g., man cosfor the cosine function
These manual pages contain a great amount of detailedinformation: make sure you read them closely to get the mostbenefit
86 / 109
![Page 87: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/87.jpg)
C
printf is a lot more powerful than this: it does all kinds offormatted output (thus the “f” in the name)
Look at the documentation for printf for the gory details
man printf on Linux/Unix systems
In fact, there is masses of documentation online, e.g., man cosfor the cosine function
These manual pages contain a great amount of detailedinformation: make sure you read them closely to get the mostbenefit
87 / 109
![Page 88: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/88.jpg)
C
printf is a lot more powerful than this: it does all kinds offormatted output (thus the “f” in the name)
Look at the documentation for printf for the gory details
man printf on Linux/Unix systems
In fact, there is masses of documentation online, e.g., man cosfor the cosine function
These manual pages contain a great amount of detailedinformation: make sure you read them closely to get the mostbenefit
88 / 109
![Page 89: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/89.jpg)
C
printf is a lot more powerful than this: it does all kinds offormatted output (thus the “f” in the name)
Look at the documentation for printf for the gory details
man printf on Linux/Unix systems
In fact, there is masses of documentation online, e.g., man cosfor the cosine function
These manual pages contain a great amount of detailedinformation: make sure you read them closely to get the mostbenefit
89 / 109
![Page 90: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/90.jpg)
C
Exercise. Compile and run hello.c on your own machine
Exercise. Modify hello2.c to print the value of n. Try on avariety of different OSs and compilers and compare the results
Exercise. Read up on printf. How do you print a percent (%),a double quote (") and a backslash (\)? What is the differencebetween %e, %f and %g?
90 / 109
![Page 91: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/91.jpg)
C
Exercise. Modify factorial to print
1 1
2 2
3 6
4 24
5 120
7 5040
8 40320
9 362880
10 3628800
91 / 109
![Page 92: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/92.jpg)
Types
C has relatively few built-in types—remember it’s lowlevel!—actually just versions of types supported natively byhardware
Integers of various kinds and sizes:
• char
• short int (or simply short)• int
• long int (or simply long)• long long int (or simply long long)
92 / 109
![Page 93: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/93.jpg)
Types
C has relatively few built-in types—remember it’s lowlevel!—actually just versions of types supported natively byhardware
Integers of various kinds and sizes:
• char
• short int (or simply short)• int
• long int (or simply long)• long long int (or simply long long)
93 / 109
![Page 94: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/94.jpg)
Types
C has relatively few built-in types—remember it’s lowlevel!—actually just versions of types supported natively byhardware
Integers of various kinds and sizes:
• char
• short int (or simply short)• int
• long int (or simply long)• long long int (or simply long long)
94 / 109
![Page 95: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/95.jpg)
TypesIntegers
Not every compiler supports all these types, particularly longlong
As a language, C is adaptable to many kinds of hardware, fromtiny embedded systems to huge mainframes
These are all ranges of integers that have proved to be useful inreal programs
Interestingly, the C standard does not specify how big each ofthese types are
An int is often 32 bits (4 bytes), but it doesn’t have to be
95 / 109
![Page 96: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/96.jpg)
TypesIntegers
Not every compiler supports all these types, particularly longlong
As a language, C is adaptable to many kinds of hardware, fromtiny embedded systems to huge mainframes
These are all ranges of integers that have proved to be useful inreal programs
Interestingly, the C standard does not specify how big each ofthese types are
An int is often 32 bits (4 bytes), but it doesn’t have to be
96 / 109
![Page 97: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/97.jpg)
TypesIntegers
Not every compiler supports all these types, particularly longlong
As a language, C is adaptable to many kinds of hardware, fromtiny embedded systems to huge mainframes
These are all ranges of integers that have proved to be useful inreal programs
Interestingly, the C standard does not specify how big each ofthese types are
An int is often 32 bits (4 bytes), but it doesn’t have to be
97 / 109
![Page 98: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/98.jpg)
TypesIntegers
Not every compiler supports all these types, particularly longlong
As a language, C is adaptable to many kinds of hardware, fromtiny embedded systems to huge mainframes
These are all ranges of integers that have proved to be useful inreal programs
Interestingly, the C standard does not specify how big each ofthese types are
An int is often 32 bits (4 bytes), but it doesn’t have to be
98 / 109
![Page 99: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/99.jpg)
TypesIntegers
Not every compiler supports all these types, particularly longlong
As a language, C is adaptable to many kinds of hardware, fromtiny embedded systems to huge mainframes
These are all ranges of integers that have proved to be useful inreal programs
Interestingly, the C standard does not specify how big each ofthese types are
An int is often 32 bits (4 bytes), but it doesn’t have to be
99 / 109
![Page 100: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/100.jpg)
TypesIntegers
This helps the adaptability of C to many kinds of hardware
But it also introduces a certain amount of extra work in portinga program from one kind of hardware to another
But this is probably a good thing: you don’t want to blindly runyour program assuming ints are 32 bit on some hardwarewhere they are not
E.g., the processor in an embedded system might not support32 bit integers, but only 16 bit, perhaps
100 / 109
![Page 101: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/101.jpg)
TypesIntegers
This helps the adaptability of C to many kinds of hardware
But it also introduces a certain amount of extra work in portinga program from one kind of hardware to another
But this is probably a good thing: you don’t want to blindly runyour program assuming ints are 32 bit on some hardwarewhere they are not
E.g., the processor in an embedded system might not support32 bit integers, but only 16 bit, perhaps
101 / 109
![Page 102: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/102.jpg)
TypesIntegers
This helps the adaptability of C to many kinds of hardware
But it also introduces a certain amount of extra work in portinga program from one kind of hardware to another
But this is probably a good thing: you don’t want to blindly runyour program assuming ints are 32 bit on some hardwarewhere they are not
E.g., the processor in an embedded system might not support32 bit integers, but only 16 bit, perhaps
102 / 109
![Page 103: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/103.jpg)
TypesIntegers
This helps the adaptability of C to many kinds of hardware
But it also introduces a certain amount of extra work in portinga program from one kind of hardware to another
But this is probably a good thing: you don’t want to blindly runyour program assuming ints are 32 bit on some hardwarewhere they are not
E.g., the processor in an embedded system might not support32 bit integers, but only 16 bit, perhaps
103 / 109
![Page 104: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/104.jpg)
TypesIntegers
All the C standard says is that
1 = sizeof(char)≤ sizeof(short int)≤ sizeof(int)≤ sizeof(long int)≤ sizeof(long long int)
While sizeof(char) = 1 this does not mean a char is alwaysone byte
CPUs with 4 byte chars exist
They have 32-bit ints with sizeof(int) = 1
104 / 109
![Page 105: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/105.jpg)
TypesIntegers
All the C standard says is that
1 = sizeof(char)≤ sizeof(short int)≤ sizeof(int)≤ sizeof(long int)≤ sizeof(long long int)
While sizeof(char) = 1 this does not mean a char is alwaysone byte
CPUs with 4 byte chars exist
They have 32-bit ints with sizeof(int) = 1
105 / 109
![Page 106: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/106.jpg)
TypesIntegers
All the C standard says is that
1 = sizeof(char)≤ sizeof(short int)≤ sizeof(int)≤ sizeof(long int)≤ sizeof(long long int)
While sizeof(char) = 1 this does not mean a char is alwaysone byte
CPUs with 4 byte chars exist
They have 32-bit ints with sizeof(int) = 1
106 / 109
![Page 107: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/107.jpg)
TypesIntegers
All the C standard says is that
1 = sizeof(char)≤ sizeof(short int)≤ sizeof(int)≤ sizeof(long int)≤ sizeof(long long int)
While sizeof(char) = 1 this does not mean a char is alwaysone byte
CPUs with 4 byte chars exist
They have 32-bit ints with sizeof(int) = 1
107 / 109
![Page 108: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/108.jpg)
Types
Typically, on modern 64 bit PCs we have
Type byteschar 1short int 2int 4long int 8long long int 8
But you should not rely on this in a portable program
Sizes were indeed a problem when people started moving theirC programs from 32 bit processors to 64 bit processors
108 / 109
![Page 109: So here is a simpler example. In file hellopeople.bath.ac.uk/masrjb/CourseNotes/Notes.bpo/CM20214/slides0… · printf("hello world\n"); ... General syntax — comments, curly brackets,](https://reader034.fdocuments.net/reader034/viewer/2022042420/5f3771158920e350fe0214c6/html5/thumbnails/109.jpg)
Types
Typically, on modern 64 bit PCs we have
Type byteschar 1short int 2int 4long int 8long long int 8
But you should not rely on this in a portable program
Sizes were indeed a problem when people started moving theirC programs from 32 bit processors to 64 bit processors
109 / 109