119
The C Programming Language • Currently one of the most commonly-used programming languages • “High-level assembly” • Small but powerful • Very portable:compiler exists for virtually every processor

The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Embed Size (px)

Citation preview

Page 1: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

The C Programming Language

• Currently one of the most commonly-used programming languages

• “High-level assembly”

• Small but powerful

• Very portable:compiler exists for virtually every processor

Page 2: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Hello World in C

#include <stdio.h>

int main() {printf(“Hello, world!\n”);return 0;

}

Include a header file that lets you use I/O related C functions

Similar to Java’s import

Page 3: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Why is C Dangerous

• C’s small feature set is an advantage and disadvantage

• The price of C’s flexibility

• C does not, in general, try to protect a programmer from his/her mistakes

Page 4: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Commonly used header files• stdio.h

Standard C library for I/O• stdlib.h

General purpose standard C library • string.h• math.h

sqrt, pow, sin, cos• ctype.h

a bunch of very useful character functions: isdigit, isalpha, isalnum, isspace, islower, isupper, tolower/toupper

• time.h

Page 5: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Hello World in C

#include <stdio.h>

int main() {printf(“Hello, world!\n”);return 0;

}

Program mostly a collection of functions

“main” function special: the entry point

“int” qualifier indicates function returns an integer

I/O performed by a library function

Page 6: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

C is Function-oriented

• C came before OO concept

• C program resemble java programs with a single giant class

• C is procedural Program organization and modularization is

achieved through function design Carefully plan your function return type and

parameter list Write small functions!

Page 7: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Functions

• Function: Unit of operation A series of statements grouped together

• Must have the main function

• C functions are stand-alone

• Most programs contain multiple function definitions Must be declared/defined before being used

Page 8: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Functionsint menuChoice() { int choice;printf("1. Yes\n""0. No\n""Enter the number corresponding to your choice: ");

scanf("%d", &choice);return choice;

}

int main() {int choice;

printf("=== Expert System ===\n");printf("Question1: ...\n");choice = menuChoice();

if (choice == 1) { /* yes */printf("Question 2: ...\n");

choice = menuChoice();/* skipped */

Page 9: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Function parametersvoid km_mile_conv(int choice) { int input; printf("Enter a %s value: ", choice==1?"mile":"km"); scanf("%lf", &input); if (choice == 1) printf("%f mile(s) = %f km(s)\n", input, input*1.6); else printf("%f km(s) = %f mile(s)\n", input, input/1.6);}int main() { int choice; scanf("%d", &choice); switch (choice) { case 1: km_mile_conv(choice); break; caea 2: km_mile_conv(choice); break; /* more cases */ }}

Page 10: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Function Return and Parameters

• The syntax for C functions is the same as Java methods

• void keyword can be omitted

void km_to_mile(void) { }

mile_to_km() { }

int main() { int choice;}

Page 11: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Function Prototype• A prototype is a

function declaration which includes the return type and a list of parameters

• A way to move function definitions after main

• Need not name formal parameters

/* function prototypes */double km2mile(double);double mile2km(double);int main() {}/* actual function definitions */double km2mile(double k) { }double mile2km(double m) { }

Page 12: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Function name overloading

• Does not exist in C

• Exists in C++

Page 13: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Local/Global Variables• Variables declared inside a function are local• Function arguments are local to the function

passed to• A global variable is a variable declared

outside of any function.• In a name conflict, the local

variable takes precedence• When local variable shadows

function parameter?

int x = 0;int f(int x) { int x = 1; return x;}

int main() { int x; x = f(2);}

Page 14: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Scope of Global Variables• The scope of a global variable starts at the

point of its definition.

• Globals should be used with caution

If using globals at all, declare

them at the top.

int x;int f() { }

int y;int g(){}

int main() {

}

Page 15: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Keep main Uncluttered

• Your main function should consist mainly of function calls

• One main input loop or conditional is okay

• Write your main and choose your function name in such a way so that the main algorithm and program structure is

clearly represented the reader can get an idea how your program

works simply by glancing at your main

Page 16: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Program Organization

• #include and #define first

• Globals if any

• Function prototypes, unless included with header file already

• int main()– putting your main before all other functions makes it easier to read

• The rest of your function definitions

Page 17: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Data Types• Integer types:

different number of bits-different ranges char: ASCII character, typically 8 bits=1 byte

range –127 to 128

short: typically 16 bits int: 32 bits

range ±2 billion

long: typically 32 bits long long: (some compilers support) 64-bits

Integer types can be preceded by unsigned which disables representing negative numbers

e.g. unsigned char range 0-255

Page 18: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Data TypesFloating-point types

float 4 bytes double 8 bytes long double 12 bytes

Floating point constants default to double unless suffixed by f or l

Examples: 0.67f, 123.45f, 1.2E-6f, 0.67, 123.45

Page 19: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Boolean

• C does not have type boolean—use int

• False is represented by 0

• Any expression evaluates to non-zero is considered true

• True is typically represented by 1 however

Page 20: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Constants

• Integer const int year = 2002;

• Floating point number const double pi = 3.14159265;

• Constants are variables whose initial value can not be changed.

• Comparable to static final

Page 21: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

#define

• Often used to define constants #define TRUE 1 #define FALSE 0 #define PI 3.14 #define SIZE 20

• Anywhere PI occurs compiler replaces with 3.14

• Offers easy one-touch change of scale/size

• #define vs const The preprocessor directive uses no memory

Page 22: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Types and Casting

• Choose types carefully

• An arithmetic operation requires that the two values are of the same type

• For an expression that involves two different types, the compiler will cast the smaller type to the larger type

• Example: 4 * 1.5 = 6.0

Page 23: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Mixing Data Types

• int values only int 4 / 2 2 3 / 2 1 int x = 3, y = 2;

x / y 1

• Involving a double value double 3.0 / 2 1.5

Page 24: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

• sizeof(type) The sizeof operator returns the number of bytes

required to store the given type

sizeof and Type Conversions

Implicit conversions arithmetic assignment function parameters function return type

Explicit conversions casting

int x;

x = (int) 4.0;

Page 25: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Use of char (character)• Basic operations

Declaration: char c; Assignment: c = 'a'; Reference: c = c + 1;

• Constants Single-quoted character (only one) Some special characters: '\n‘ (newline), '\t' (tab), '\"' (double quote), '\'' (single quote), '\\' (backslash)

Page 26: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

• A char type represents an integer value

• A single quoted character is called a “character constant”.

• C characters use ASCII representation:• 'A' = 65 … 'Z' = 'A' + 25 = 90• 'a' = 97 … 'z' = 'a' + 25 = 122• '0'!= 0 (48), '9' - '0' = 9

• Never make assumptions of char values Always write 'A' instead of 65

Characters are Integers

Page 27: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

ASCII Table

American Standard Code for Information InterchangeA standard way of representing the alphabet, numbers, and symbols(in computers)

Page 28: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Output Functions

• Output charactersprintf("Text message\n");

• Output an integerint x = 100;

printf("Value = %d\n", x);

Output: Value = 100

\n for new line

Page 29: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Variations• Output a floating-point number

double y = 1.23;

printf("Value = %f\n", y);

• Output multiple numbersint x = 100;

double y = 1.23;

printf("x = %d, y = %f\n", x, y);

Output: x = 100, y = 1.230000

15 digits below decimal(excluding trailing 0’s)

Page 30: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

printf Summary

printf(" ", );

• Text containing special symbols %d for an integer %f for a floating-point number \n for a newline

• List of variables (or expressions) In the order correspoding to the % sequence

Page 31: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

% Specification• (most commonly used ones)

• %c convert an int to an unsigned char and print as a character

• %i int, char (printas a signed decimal number)

• %d same as above (d for decimal)

• %f float, double (floating-point)

• %e float, double (exponential, e.g., 1.5e3)

• %s string pointed by a char*

Page 32: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Formatting

• Precision %.#f

• Width %#f, %#d Note: Entire width

• Various combinations of the above and other options available

Replace #with digit(s)

Page 33: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Formatting Example

%f with 1.23456789 >1.234568<%.10f with 1.23456789 >1.2345678900<%.2f with 1.23456789 >1.23<

%d with 12345 >12345<%10d with 12345 > 12345<%2d with 12345 >12345<

%f with 1.23456789 >1.234568<%8.2f with 1.23456789 > 1.23<

Page 34: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

char Input/Output

• Input char getchar() receives/returns a character Built-in function

• Output printf with %c specification putchar()

int main() {char c;c = getchar();printf("Character >%c< has the value %d.\n", c,

c);return 0;

}

Page 35: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

scanf Function

scanf(" ", );

• Format string containing special symbols %d for int %f for float %lf for double %c for char \n for a newline

• List of variables (or expressions) In the order correspoding to the % sequence

Page 36: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

scanf Function

• The function scanf is the input analog of printf

• Each variable in the list MUST be prefixed with an &.

• Ignores white spaces unless format string contains %c

Page 37: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

scanf Function

int main() { int x;

printf("Enter a value:\n"); scanf("%d", &x); printf("The value is %d.\n", x);

return 0;}

Page 38: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

scanf with multiple variables

int main() { int x; char c; printf("Enter an int and a char:"); scanf("%d %c", &x, &c); printf("The values are %d, %c.\n", x, c);

return 0;}

Page 39: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Control Structures

• Conditionals if-else switch

• Loops while for do-while

• SAME syntax as in Java

Page 40: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Assignment as Expression

• Assignment Assignments are expressions Evaluates to value being assigned

• Exampleint x = 1, y = 2, z = 3;

x = (y = z);

3 3 3

evaluates to 3

if (x = 3) { ...}

evaluates to 3 (true)

Page 41: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Complex Data Types

• Pointers

• Arrays

• Strings

• Structures

Page 42: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Variable and Address• Variable = Storage in computer

memory Contains some value Must reside at a specific location

called address Basic unit – byte Imagine memory as a one-

dimensional array with addresses as byte indices

A variable consists of one or more bytes, depending on its type (size)

Memory703146301104695

20112

0123456789

3031

address value

char

int

Page 43: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Pointer – Reference

• A pointer (pointer variable) is a variable that stores an address (like Java reference)

• Recall in Java, when one declares variables of a class type, these are automatically references.

• In C, pointers have special syntax and much greater flexibility.

Page 44: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Address Operations in C

• Declaration of pointer variables The pointer declarator ‘*’

• Use of pointers The address of operator ‘&’ The indirection operator ‘*’ – also known as

de-referencing a pointer

Page 45: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Pointer Declaration

• Syntax destinationType * varName;

• Must be declared with its associated type.

• Examples int *ptr1;

A pointer to an int variable char *ptr2;

A pointer to a char variable

ptr1

ptr2

will contain addresses

Page 46: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Pointers are NOT integers

• Although memory addresses are essentially very large integers, pointers and integers are not interchangeable.

• Pointers are not of the same type

• A pointer’s type depends on what it points to int *p1; char *p2;

Page 47: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Address of operator

0012FF88 8

ip i (@0012FF88)

int i = 8;int *ip;

ip = &i;

Page 48: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Pointer Assignment

• A pointer p points to x if x’s address is stored in p

• Example int x = 1;int *p, *q;

p = &x;

q = p;

Interpreted as:

p 567

x 1

address = 567

p x 1

q 567

q

Page 49: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Pointer Assignment

• Example int x=1, y=2, *p, *q;p = &x; q = &y;

q = p;

p 567

y 2

address = 988

q 988

x 1

address = 567

567

Page 50: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Indirection Operator

• Syntax * pointerVar Allows access to value of memory being pointed to Also called dereferencing

• Example int x = 1, *p;p = &x;printf("%d\n", *p);*p refers to x; thus prints 1

p x 1

Note: ‘*’ in a declaration and ‘*’ in an expression are different.int *p; int * p; int* p;

Page 51: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Assignment Using Indirection Operator

• Allows access to a variable indirectly through a pointer pointed to it.

• Example int x = 1, *p;p = &x;*p = 2;printf("%d\n", x);

*p is equivalent to x

p x 1

p x 2

Page 52: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

The NULL Pointer

• A pointer that contains the address zero known as the NULL pointer It points to nothing Many of the standard header files define NULL

• NULL means false in conditionsint *ip;

if (ip) {

/* ip is not NULL */

}

Page 53: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Pointer initialization

• DO NOT derefence an undefined/unitialized pointer! It could be pointing anywhere.

Page 54: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Pass by Value

void f(int x) { x = x * x; printf("%d", x);}

int main() { int x = 3; f(x); printf("%d", x); return 0;}

The variable x in f gets a copy of the value of the variable x in main.

Does not change the value of x in main.

• Same as Java, modification to function arguments during function execution has no effect outside of function

Page 55: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Pass by Value and Pointers

• All functions are pass-by-value in C

• Pass-by-value sill holds even if the parameter is a pointer A copy of the pointer’s value is made – the

address stored in the pointer variable The copy is then a pointer pointing to the same

object as the original parameter Thus modifications via de-referencing the copy

STAYS.

Page 56: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Function Arguments

• x and y are copies of the original, and thus a and b can not be altered.

void swap(int x, int y) { int tmp; tmp = x; x = y; y = tmp;}

int main() { int a = 1, b = 2;

swap(a, b); return 0;}

Wrong!

Page 57: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Pointers and Function Arguments

• Passing pointers – a and b are passed by reference (the pointers themselves px and py are still passed by value)

void swap(int *px, int *py) { int tmp; tmp = *px; *px = *py; *py = tmp;}

int main() { int a = 1, b = 2; swap(&a, &b); return 0;}

px

a 1

py

b 2

Page 58: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

pointers to pointers

• int i;

• int *ip = &i;

• int **iip = &ip; /* iip points to ip */

• ….

Page 59: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Generic pointers

• Sometimes necessary to store or pass pointers without knowing what type they refer to—use the generic pointer type: void *

int x;

int *xp = &x;

void *vp = xp; /*okay, types are compatible*/

float *fp = ip; /*error */

• BUT, a generic pointer cannot be derefenced: x = *vp; /*error */

Page 60: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Arrays

element

0 1 2 k2 k1 index

• Declaration – int a[5];• Allocates a static array

• Assignment – a[0] = 1;

• Reference – y = a[0];

a ? ? ? ? ?

a0 4

? ? ? ?1

Page 61: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Pointers and Arrays• Arrays are contiguous

allocations of memory of the size: sizeof(elementType) * numberOfElements

• Given the address of the first byte, using the type (size) of the elements one can calculate addresses to access other elements

Memory703146301104631

4512

0123456789

3031

address value

array

1

pointer

Page 62: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Name of an Array

• The variable name of an array is also a pointer to its first element.

• a == &a[0]• a[0] == *a

a:a[0] a[1] a[8]

a a+1 a+8

Page 63: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

• One can add/subtract an integer to/from a pointer

• The pointer advances/retreats by that number of elements (of the type being pointed to) a+i == &a[i]

a[i] == *(a+i)

• Subtracting two pointers yields the number of elements between them

Pointer Arithmetic

Page 64: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Array name is a constant pointer

int arr[5];

int x;

int *s = &x;

arr = s; /*illegal */

Page 65: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

#define SIZE 10

void init(int a[]) {int i;

for(i = 0;i<SIZE;i++){a[i] = 0;

}}

int main() {int a[SIZE];

init(a);return 0;

}

Arrays as Arguments

/* equivalent pointer alternative */void init(int *a) { int i;

for(i = 0;i<SIZE;i++){ *(a+i) = 0; }}

• Arrays are passed by reference

• Modifications stay

Page 66: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Character and String

• String is not a special type An array of characters Terminated with a special, null character

• Null character Its integer value is 0. Its C representation is '\0'.

• E.g., "abc" is internally'a' 'b' 'c' '\0'

'\0' '0' (zero)

'\0' '\n'

Page 67: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Example

Big-O

Little-o Zero

Null character

'O' 'o' '0' '\0'

Values: 79 111 48 0

Page 68: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Declaration/Initialization

• Declaration: char s[5];

• Initialization: char t[] = "abc";

char *s = t;

Note: Strings cannot be assigned using '=' (except initialization).

Page 69: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

String Output

• Use printf with the %s specificationPrints character elements until \0 is reached

char s[] = "abc";

printf("%s", s); /* prints abc */

printf("string: >%s<", s); /* prints string: >abc< */

Page 70: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

printf and Strings

int main() { char s[] = "01234"; char *p; p = s;

printf("%s\n", s); printf("%s\n", p+1);}

• %s: Displays characters from the specified address until '\0'

Page 71: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

String Input with scanf

• Use the scanf function with %s

• Matches the input string up to the first white space or '\n' and stores it into buf Given input "CSE 123\n" scanf %s will have stored "CSE" Input buffer after scanf call: "123\n"

• No need for & in front of buf

scanf("%s", buf);

Page 72: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

String Input with gets

• The gets function

#define BUFLEN 200

int main() {

char buf[BUFLEN];

gets(buf); printf("string: >%s<", buf);}

allocate a large buffer(e.g., more than 2 lines)

Page 73: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Notes• gets deletes \n from input.

• If the user presses ENTER without any other characters, the first position will be the null character (called ‘empty string’).

• In case the user enters a string longer than the buffer, gets may cause a serious run-time error.

Page 74: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

String Output with puts

• The puts function

#define BUFLEN 200

int main() { char buf[BUFLEN];

gets(buf); puts(buf); return 0;}

puts adds '\n' to output,equivalent to printf("%s\n", buf);

Page 75: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Library String Functions• #include <string.h>

• Return the Length of a stringsize_t strlen(const char *str)

• Copy a string (including the '\0') src to destchar *strcpy(char *dest, const char *src)

• Concatenate two strings: append src to dest char *strcat(char *dest, const char *src)

• Compare two stringsint strcmp(const char *s1, const char *s2)

Return: 0 if identical; ASCII difference between the first mismatch otherwise>0 for, e.g., "abc" vs. "abb", <0 for "abc" vs. "abd"

Page 76: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

strcpy example

#include <string.h>

char words[100];

strcpy(words, “Hello!”);

Page 77: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Structures

• To group multiple (heterogeneous) variables

• Similar to Java classes, but not as powerful A structure has only data members All members are public

Page 78: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Structure Type Declaration

• Pattern struct StructType { /* members */

}; Typically global

• Members Analogous to data

declaration

struct Aircraft{ char id[10]; int x; int y; int z; int prevZ; int heading; int verticalSpeed; int speed;};

int main() { /* skipped */}

Page 79: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Struct Instance

• Aircraft identifies a structure type, also known as a structure tag.

• a is an instance of the structure type Aircraft

• Keyword struct may not be dropped

struct Aircraft { /*members*/

};

struct Aircraft a;

structure tag

Page 80: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

typedef• A way to define a synonym for existing

(complicated) types. typedef int Bool; typedef int*** Intptr3;

Page 81: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

typedef and Structures

• This is a case of programmer laziness!

• Instead ofstruct Aircraft boeing747;

usetypedef struct Aircraft Arcrft;

thenArcrft boeing747;

• Arcrft is a new user-defined type.

Page 82: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Structure Variable Declaration

typedef struct Ac{ char id[10]; int x; int y; int z; int prevZ; int heading; int verticalSpeed; int speed;} Aircraft;

int main() { Aircraft a, b, c; /* skipped */}

Page 83: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Member Assignment/Reference

• Assignment pattern structVar.memberName = exp;

• Reference pattern structVar.memberName

typedef struct { char id[10]; int x; int y; int z; int prevZ; int heading; int verticalSpeed; int speed;} Aircraft;

int main() { Aircraft a; a.z = 0; a.prevZ = a.z; /* skipped */}

Page 84: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Structure Initialization

• Like array initializations, this only works at the time of declaration.

• Afterwards you must assign/initialize each member one by one.

typedef struct { char id[10]; int x; int y; int z; int prevZ; int heading; int verticalSpeed; int speed;} Aircraft;

int main() { Aircraft a = {"N3NK", 0, 0, 0, 0, 270, 0, 0}; /* skipped */}

Page 85: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Structure Assignment

• Pattern structVar1 = structVar2;

• Each member’s value will be copied

typedef struct { char id[10]; int x; int y; int z; int prevZ; int heading; int verticalSpeed; int speed;} Aircraft;

int main() { Aircraft a = {"N3NK", 0, 0, 0, 0, 270, 0, 0}; Aircraft b; b = a; /* skipped */}

Page 86: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Additional Examples

typedef struct {

int ssn;

float debt;

} Person;

typedef struct {

int type;

int value;

int address;

char name[32];

} Variable;

ssn

debt

type

value

address

name

Page 87: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Complex Structures

• Various structure members Basic types: int, double, char, etc.

Arrays Pointers Structures

• Arbitrary combination possible

typedef struct {int x;int y;int z;

} Position;

typedef struct {char id[10];Position pos;int prevZ;int heading;int verticalSpeed;int speed;

} Aircraft;

Page 88: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Array of Structures

typedef struct {char id[10];Position pos;int prevZ;int heading;int verticalSpeed;int speed;

} Aircraft;

int main() { Aircraft aircrafts[2] =

{ { init list for elem 0 }, { init list for elem 1 } };

aircrafts[0].pos.x = 0;}

Page 89: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Structure with Array of Structures

typedef struct {char id[10];Position pos;int prevZ;int heading;int verticalSpeed;int speed;

} Aircraft;

typedef struct { int numOfAircrafts; Aircraft aircrafts[100];} Radar;

int main() { Radar r; r.aircrafts[0].pos.x = 0;}

Page 90: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Structure Arguments

• The argument variable b is a copy of the original variable a.

• Analogous to basic variables, different from arrays

• Cannot change the original variable a

void updateStatus(Aircraft b) { b.heading += 90;}

int main() {Aircraft a = initialization;updateStatus(a);return 0;

}

Page 91: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Structure Return

• The local variable b is modified and returned.

• The returned b can be assigned (copied) to the original a.

Aircraft updateStatus(Aircraft b) { b.heading += 90; return b;}

int main() { Aircraft a = initialization; a = updateStatus(a);}

Page 92: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Pointer to Structure

• To modify the original value, pass the pointer to a structure

void updateStatus(Aircraft *b) { (*b).heading += 90;}

int main() { Aircraft a = initialization; updateStatus(&a); return 0;}

Page 93: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Shorthand• To deal with pointers to structure, the

shorthand form is more commonly used.

• Pattern StructPtrVarmember-of-structure;void updateStatus(Aircraft *b) { b->heading += 90; /* same as (*b).heading */}

int main() { Aircraft a = initialization; updateStatus(&a); return 0;}

Page 94: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Summary: pointers to structures

Aircraft a;

Aircraft *a2 = &a;

(*a2).heading = 90;

Is equivalent to

a2->heading = 90

Page 95: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

File I/O

• Accessing a stream in C is done through file pointers: A variable pointing to a file FILE *fp;

The type FILE is defined in stdio.h Certain streams have predefined pointers with

standard names – stdin, stdout and stderr

fp

Page 96: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Opening/Closing a File

fp = fopen("file.dat", "r")

• Full path name as well as the filename may be included btw the quotes

• Always test against NULL• Closing a file when done: fclose(fp);

"r" – reading"w" – writing (overwriting)!"a" – appending

filename

Returns the null pointer NULL (zero) on error, i.e. trying to read a file that doesn’t exist.

Page 97: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Reading• Reading a char– returns char read or EOF

int fgetc(FILE *fp)

int getc(FILE *fp) // macro

int getchar() <==> int fgetc(stdin)

• Reading a string

char *fgets(char *s, int size, FILE *fp) Reads in at most (size-1) characters and stores them in the buffer pointed by s

Reading stops after a newline of EOF ‘\0’ is stored after the last character

char *gets(char *s, int size) // from stdin

Page 98: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Writing

• Writing a char– returns char written int fputc(int c, FILE *fp)

int putc(int c, FILE *fp) // macro

int putchar(int c) <==> int fputc(…, stdin)

int ungetc(int c, FILE *fp)

//pushes c back to stream where it is

//available for subsequent read operations

• Writing a string int fputs(const char *s, FILE *fp)

Writes the string and a training newline into fp

int puts(const char *s) // to stdout

Page 99: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Example: File Copy by Lineint main() { char buf[BUFLEN], inFile[BUFLEN], outFile[BUFLEN]; FILE *in, *out;

printf("Enter source filename: "); fgets(inFile,BUFLEN-1,stdin); trim_newline(inFile); // get outFile as well from user in = fopen(inFile, "r"); out = fopen(outFile, "w");

if ((in == NULL) || (out == NULL)) { printf("*** File open error\n"); return; }

/* NULL returned at EOF */ while (fgets(buf, BUFLEN-1, in) != NULL) { fputs(buf, out); }

fclose(in); fclose(out); return 0;}

filecopy2.c

Page 100: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Formated I/O

• Reading – returns number of matches or EOF

int fscanf(FILE *fp, "...", variableList);

• Writing – returns number of chars written

int fprintf(FILE *fp, "...", variableList);

• scanf is equivalent to fscanf with stdin

• printf to fprintf with stdout

Page 101: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Buffering and fflush(…)

• A buffer is an area of memory that acts as a temporary holding area for I/O

• Characters/strings are not usually written to a file as son as they are written to a stream They are accumulated in buffer and written to

file as a block

• To force the buffer to be written to file, use: fflush(FILE *fp) Otherwise, buffer is flushed when full, or when

stream is closed, or at exit.

Page 102: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Dynamic Memory Allocation

• The most important usage of pointers.

• C’s data structures are normally fixed in size, i.e. static. Static data structures must have their sizes

decided at time of compilation Arrays are good examples

• Through pointers, C supports the ability to allocate storage during program execution.

Page 103: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

The Heap

• The pool of memory from which dynamic memory is allocated is separate, and is known as the heap.

• There are library routines to allocate and free memory from the heap.

• Heap memory is only accessible through pointers.

• Mixing statically and dynamically allocated memory is not allowed.

Page 104: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

malloc() and free()• Library routines for managing the heap• Dynamically allocate and free arbitrary-sized chunks

of memory in any order void *malloc (size_t size);

Allocates a block of size bytes from the heap Returns a pointer to the block allocated (casting to correct type

required, value pointed by void * cannot be accessed directly.) size_t is an unsigned integer type used for very large integers. On failure, malloc returns a null pointer. Make sure to test for

error

void free (void *ptr); frees memory space pointed by ptr.

• #include<stdlib.h>

Page 105: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Example: Allocating an int Array

int *a;a = (int *) malloc(sizeof(int)*6); a[5] = 3;free(a);

• Never attempt to free memory that has not been previously allocated via malloc!

• Memory allocated through malloc is not cleared or initialized in anyway. Initialize yourself!

Page 106: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Example: String Allocations

• By default void* will be casted to char*, so in fact no casting is necessary here.

char* newStr(char *str) {char *s;s = (char *) malloc(strlen(str) + 1);return strcpy(s, str);

}

char* newStr2(char *str, char *str2){char *s;s = (char *) malloc(strlen(s) + strlen(s2) + 1);strcpy(s, str); return strcat(s, str2);

}

Page 107: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Dynamic Memory Layout

0xffffffff

0Text

Data

BSS

Heap

Stack

char *p1 = malloc(3);char *p2 = malloc(4);char *p3 = malloc(1);free(p2);char *p4 = malloc(6);free(p3);char *p5 = malloc(2);free(p1);free(p4);free(p5);

Page 108: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Dynamic Memory Layout

0xffffffff

0Text

Data

BSS

Heap

Stack

char *p1 = malloc(3);char *p2 = malloc(4);char *p3 = malloc(1);free(p2);char *p4 = malloc(6);free(p3);char *p5 = malloc(2);free(p1);free(p4);free(p5);

p1

Page 109: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Dynamic Memory Layout

char *p1 = malloc(3);char *p2 = malloc(4);char *p3 = malloc(1);free(p2);char *p4 = malloc(6);free(p3);char *p5 = malloc(2);free(p1);free(p4);free(p5);

0xffffffff

0Text

Data

BSS

Heap

Stack

p1

p2

Page 110: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Dynamic Memory Layout

char *p1 = malloc(3);char *p2 = malloc(4);char *p3 = malloc(1);free(p2);char *p4 = malloc(6);free(p3);char *p5 = malloc(2);free(p1);free(p4);free(p5);

0xffffffff

0Text

Data

BSS

Heap

Stack

p1

p2

p3

Page 111: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Dynamic Memory Layout

char *p1 = malloc(3);char *p2 = malloc(4);char *p3 = malloc(1);free(p2);char *p4 = malloc(6);free(p3);char *p5 = malloc(2);free(p1);free(p4);free(p5);

0xffffffff

0Text

Data

BSS

Heap

Stack

p1

p3

p2

Page 112: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Dynamic Memory Layout

char *p1 = malloc(3);char *p2 = malloc(4);char *p3 = malloc(1);free(p2);char *p4 = malloc(6);free(p3);char *p5 = malloc(2);free(p1);free(p4);free(p5);

0xffffffff

0Text

Data

BSS

Heap

Stack

p1

p2

p3p4

Page 113: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Dynamic Memory Layout

char *p1 = malloc(3);char *p2 = malloc(4);char *p3 = malloc(1);free(p2);char *p4 = malloc(6);free(p3);char *p5 = malloc(2);free(p1);free(p4);free(p5);

0xffffffff

0Text

Data

BSS

Heap

Stack

p1

p2

p3p4

Page 114: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Dynamic Memory Layout

char *p1 = malloc(3);char *p2 = malloc(4);char *p3 = malloc(1);free(p2);char *p4 = malloc(6);free(p3);char *p5 = malloc(2);free(p1);free(p4);free(p5);

0xffffffff

0Text

Data

BSS

Heap

Stack

p1

p2,p5

p3p4

Page 115: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Dynamic Memory Layout

char *p1 = malloc(3);char *p2 = malloc(4);char *p3 = malloc(1);free(p2);char *p4 = malloc(6);free(p3);char *p5 = malloc(2);free(p1);free(p4);free(p5);

0xffffffff

0Text

Data

BSS

Heap

Stack

p1

p2,p5

p3p4

Page 116: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Dynamic Memory Layout

char *p1 = malloc(3);char *p2 = malloc(4);char *p3 = malloc(1);free(p2);char *p4 = malloc(6);free(p3);char *p5 = malloc(2);free(p1);free(p4);free(p5);

0xffffffff

0Text

Data

BSS

Heap

Stack

p1

p2,p5

p3p4

Page 117: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Dynamic Memory Layout

char *p1 = malloc(3);char *p2 = malloc(4);char *p3 = malloc(1);free(p2);char *p4 = malloc(6);free(p3);char *p5 = malloc(2);free(p1);free(p4);free(p5);

0xffffffff

0Text

Data

BSS

Heap

Stack

p1

p2,p5

p3p4

Page 118: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

The Love-hate Relationship with malloc

• Most experience C-programmers have such a dilemma. malloc is fast, efficient and flexible The dreaded memory leak – neglecting to free

memory Reaching beyond malloced bounds Heap fragmentation – this is not really a

programming error, and is therefore even harder to fix

Page 119: The C Programming Language Currently one of the most commonly-used programming languages “High-level assembly” Small but powerful Very portable:compiler

Summary

• Learn how to handle memory management in C• malloc and related functions are essential to C

programming• Watch for the following common errors

Not checking to see if malloc returned NULL No freeing memory after use: memory leak Dangling pointes:

trying to access memory after freeing Set pointer to NULL after freeing it, otherwise it wil still hold the adress

of the memory space that you freed