lecture 1 cis208 january 14 rd, 2005. compiling %> gcc helloworld.c returns a.out %> gcc –o...

22
Lecture 1 cis208 January 14 rd , 2005

Upload: bryan-holland

Post on 05-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 1 cis208 January 14 rd, 2005. Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld

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

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

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

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

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

#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

#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

#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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.