lecture 1 cis208 january 14 rd, 2005. compiling %> gcc helloworld.c returns a.out %> gcc –o...
TRANSCRIPT
![Page 1: Lecture 1 cis208 January 14 rd, 2005. Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f345503460f94c520f2/html5/thumbnails/1.jpg)
Lecture 1
cis208January 14rd, 2005
![Page 2: Lecture 1 cis208 January 14 rd, 2005. Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f345503460f94c520f2/html5/thumbnails/2.jpg)
Compiling
%> gcc helloworld.creturns a.out
%> gcc –o helloworld helloworld.creturns helloworld
![Page 3: Lecture 1 cis208 January 14 rd, 2005. Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f345503460f94c520f2/html5/thumbnails/3.jpg)
Executing
%> a.out%> ./a.out
%> helloworld%> ./helloworld
![Page 4: Lecture 1 cis208 January 14 rd, 2005. Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f345503460f94c520f2/html5/thumbnails/4.jpg)
The C Language
Derived from B & BCPL
Redefined as ANSI CAmerican National Standards Institute
![Page 5: Lecture 1 cis208 January 14 rd, 2005. Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f345503460f94c520f2/html5/thumbnails/5.jpg)
C / Java Differences
Not Object OrientatedProcedural implementation and designNo PolymorphismCall-by-Value onlyNo Virtual MachineDirect Memory Access and allocationVery FastEasier to Optimize.
![Page 6: Lecture 1 cis208 January 14 rd, 2005. Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f345503460f94c520f2/html5/thumbnails/6.jpg)
#include <stdio.h>int main(void) {printf(“%s %s\n”, “Hello”,”World”);return 0;}
![Page 7: Lecture 1 cis208 January 14 rd, 2005. Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f345503460f94c520f2/html5/thumbnails/7.jpg)
#include <stdio.h>
Adds other source files
Similar to java’s import statements
Implicit nesting - 8 Levels
![Page 8: Lecture 1 cis208 January 14 rd, 2005. Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f345503460f94c520f2/html5/thumbnails/8.jpg)
#include continued
Preprocessor command
#include <stdio.h><> : standard libraries.
#include “foo.h” “ “ : local h files. Give path.
![Page 9: Lecture 1 cis208 January 14 rd, 2005. Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f345503460f94c520f2/html5/thumbnails/9.jpg)
Common Librariesstdio.h : standard I/O
printf, scanf, gets, puts, getch, etc
stdlib.h : various functions rand, abs, besearch, exit, etc
string.h : string functions strlen, memcpy, strcat, strchr, etc
math.h : math functions must compile with –lm option trig functions,log, pow, sqrt
![Page 10: Lecture 1 cis208 January 14 rd, 2005. Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f345503460f94c520f2/html5/thumbnails/10.jpg)
int main(void)
int is return type.
must have main function
in gcc, arguments & returns not necessary if void.
![Page 11: Lecture 1 cis208 January 14 rd, 2005. Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f345503460f94c520f2/html5/thumbnails/11.jpg)
Variable Declaration
Must be at beginning
No initialization == junk values.
= is assignment operator variable = value; Correctvalue = variable;
Wrong
![Page 12: Lecture 1 cis208 January 14 rd, 2005. Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f345503460f94c520f2/html5/thumbnails/12.jpg)
declaration cont.
int main(void) {char a, b=‘b’;int j = 5,i,k=8;int p; ..}
![Page 13: Lecture 1 cis208 January 14 rd, 2005. Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f345503460f94c520f2/html5/thumbnails/13.jpg)
basic types Page 110
char : characters. ‘a’, ‘b’, ‘1’, ‘2’, etc
int : integers. 1, 3 1000, -11, etc
float : floating point decimals 1.11, 0.002, 1.231, etc
double : same as float, but larger range
![Page 14: Lecture 1 cis208 January 14 rd, 2005. Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f345503460f94c520f2/html5/thumbnails/14.jpg)
printf
part of <stdio.h>
Formatted output to standard out usually console can be changed advanced topic
![Page 15: Lecture 1 cis208 January 14 rd, 2005. Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f345503460f94c520f2/html5/thumbnails/15.jpg)
printf
int printf(const char *control_string, … , );
returns number of characters written to stdout
![Page 16: Lecture 1 cis208 January 14 rd, 2005. Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f345503460f94c520f2/html5/thumbnails/16.jpg)
control_string (printf cont)
Represents what will be printed.
printf(“I like C \n”);
\n is newline character
printf(“I\nlike\nC\n”);
I
like
C
![Page 17: Lecture 1 cis208 January 14 rd, 2005. Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f345503460f94c520f2/html5/thumbnails/17.jpg)
printf cont…
\ == escape character
\n : newline\t : horizontal tab
page 113
![Page 18: Lecture 1 cis208 January 14 rd, 2005. Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f345503460f94c520f2/html5/thumbnails/18.jpg)
format specifiers %
variable place holder in control string values plugged in during runtime
printf(“%d\n%c %s\n”, 1,’a’,”nice”);
1-to-1 correspondence
1
a nice
![Page 19: Lecture 1 cis208 January 14 rd, 2005. Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f345503460f94c520f2/html5/thumbnails/19.jpg)
format specifiers
different types of input %c – character %d – decimal value %i - same as %d %f – float value %s – string
Page 494
![Page 20: Lecture 1 cis208 January 14 rd, 2005. Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f345503460f94c520f2/html5/thumbnails/20.jpg)
int main(void){int i = 5, j = 2,k = 3;char a = ‘7’;printf(“%d + %d equals %c\n”,j,i,a);i = printf(“%d”,i+j+k);return 0;}
![Page 21: Lecture 1 cis208 January 14 rd, 2005. Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f345503460f94c520f2/html5/thumbnails/21.jpg)
Minimum Field width
numerical paddingnumerical values between % and specifieradds white spaces or zerosprintf(“%05d%5d”,5,6);
Mostly used to line up columns in console output
00005 6
![Page 22: Lecture 1 cis208 January 14 rd, 2005. Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld](https://reader035.vdocuments.net/reader035/viewer/2022062517/56649f345503460f94c520f2/html5/thumbnails/22.jpg)
Precision Specifier
sets number of significant digits
float j = 5.0;printf(“%2.5f\n”,j);
two white spaces and 7 digits after decimal point.