Docsity

C Programming: A Comparison with Java and Basics Tutorial - Prof. Richard Carver, Study notes of Computer Science

Partial preview of the text

Download C Programming: A Comparison with Java and Basics Tutorial - Prof. Richard Carver and more Study notes Computer Science in PDF only on Docsity!

Introduction to C

Acknowledgement: These slides are adapted from lecture slides made available by Prof. Dave O’ Halloran (Carnegie Mellon University)

Outline

• Overview comparison of C and Java

• Good evening

• Preprocessor

• Command line arguments

• Arrays and structures

• Pointers and dynamic memory

What we will cover

• A crash course in the basics of C

• You should read the K&R C book for

lots more details

Java programmer gotchas (1)

int i

for(i = 0; i < 10; i++)

NOT

for(int i = 0; i < 10; i++)

Java programmer gotchas (2)

  • Uninitialized variables
    • catch with –Wall compiler option #include <stdio.h> int main(int argc, char argv[]) {* int i; factorial(i); return 0; }

Java programmer gotchas (3)

• Error handling

  • No exceptions
  • Must look at return values

ā€œGood eveningā€

#include <stdio.h> int main(int argc, char argv[]) { / print a greeting / printf("Good evening!\n"); return 0; } $ ./goodevening Good evening! $*

C Preprocessor

#define CS367
ā€œComputer Systems & Programming\n" int main(int argc, char
argv[]) { printf(CS367); return 0; }
*

After the preprocessor (gcc –E)

int main(int argc, char argv) { printf(ā€Computer Systems & Programming\n"); return 0; }*

Conditional Compilation

#define CS int main(int argc, char argv) { #ifdef CS printf("Computer Systems & Programming\n"); #else printf("Some other class\n"); #endif return 0; }*

After the preprocessor (gcc –E)

int main(int argc, char argv) { printf(ā€œComputer Systems & Programming\n"); return 0; }*

Command Line Arguments (3)

$ ./cmdline Computer Systems and Programming 5 arguments 0: ./cmdline 1: Computer 2: Systems 3: and 4: Programming $

Arrays

• char foo[80];

  • An array of 80 characters
  • sizeof(foo)

= 80 Ɨ sizeof(char)

= 80 Ɨ 1 = 80 bytes

• int bar[40];

  • An array of 40 integers
  • sizeof(bar)

= 40 Ɨ sizeof(int)

= 40 Ɨ 4 = 160 bytes

Structures

  • Aggregate data #include <stdio.h> struct name { char name;* int age; }; / <== DO NOT FORGET the semicolon / int main(int argc, char argv[]) {* struct name bovik; bovik.name = "Harry Bovik"; bovik.age = 25; printf("%s is %d years old\n", bovik.name, bovik.age); return 0; }

Pointers

• Pointers are variables that hold an

address in memory.

• That address contains another

variable.

Pointers made easy (2)

**f_addr = 3.2; / indirection operator / float g = f_addr;/ indirection: g is now 3.2 / f = 1.3; / but g is still 3.2 / f (^) f_addr 4300 4304

f (^) f_addr 4300 4304

Function Parameters

• Function arguments are passed ā€œby

valueā€.

• What is ā€œpass by valueā€?

  • The called function is given a copy of the

arguments.

• What does this imply?

  • The called function can’t alter a variable

in the caller function, but its private copy.

• Three examples

Example 1: swap_

void swap_1(int a, int b) { int temp; temp = a; a = b; b = temp; } Q: Let x=3, y=4, after swap_1(x,y); x =? y=? A1: x=4; y=3; A2: x=3; y=4;

Example 2: swap_

**void swap_2(int *a, int *b) { int temp; temp = *a; *a = b; b = temp; } Q: Let x=3, y=4, after swap_2(&x,&y); x =? y=? A1: x=3; y=4; A2: x=4; y=3;

Not like Java

• No new

• No garbage collection

• You ask for n bytes

  • Not a high-level request such as

ā€œI’d like an instance of class String ā€

malloc

• Allocates memory in the heap

  • Lives between function invocations

• Example

  • Allocate an integer
    • int iptr =* (int) malloc(sizeof(int));*
  • Allocate a structure
    • struct name nameptr = (struct name)** malloc(sizeof(struct name));

free

• Deallocates memory in heap.

• Pass in a pointer that was returned by

malloc.

• Example

  • int iptr =*

(int*) malloc(sizeof(int));

free(iptr);

• Caveat: don’t free the same memory

block twice!