CPSC 355: Tutorial 2

 

PhD Student

Fall 2017

SSH and More C

Connecting the Arm Server

For Linux/macOS users:

ssh arm.cpsc.uclagary.ca -lyour.username

For Windows users:

Last Day

#include <stdio.h> // Include functions from the standard I/O library

// Define the main function
int main(int argc, char *argv[]) // argc: number of arguments passed
                                 // argv: array to (pointer to) argument text
{
    printf("Hello World!\n");    // prints hello world

    return 0;                    // returns 0 (error code 0 = no error)
}
#include <stdio.h>

int main(int argc, char *argv[])
{
    char name[256];
    int age;

    printf("Please enter your name:\n");
    scanf("%s", name); 

    printf("Please enter your age: \n");
    scanf("%d", &age); // & is the "address of" operator

    printf("Hello %s, you are %d years younger than I am\n", name, 27 - age);
}

Datatypes in C

Name Size (bytes) Range Format String
char 1 [-127 to 128] %c, %h
short 2 [-32,768 to 32,767] %hi
int 4 [-2,147,483,648 to  2,147,483,647] %d
long 8 [very negative, to very postive] %l
float 4 It's complicated... %f
double 8 It's complicated... %f

We'll talk about unsigned types later on...

char is short for character, a string is a list or array of characters

Format strings are powerful but complicated.

Datatypes in C

#include <stdio.h> // Include functions from the standard I/O library

int main(int argc, char *argv[]) 
{
    int v1 = 100;
    char character = 'h'; // the single quotes are an ASCII character literal
    char another_character = 10; // 
    int v2 = 'i';

    // "Literals" are a sort of way of writing values in a different way
    int v3 = 0xFEEDDAD; // Is a hexadecimal literal
    int v4 = 0b10010011; // Is a binary literal (but this is not standard C!)
    int v5 = 022222222; // an octal literal (notice the first 0).

    // Floating point numbers
    float x = 10.1f;
    float d = 10.2d;
}

Try printing some of these...

Arrays

#include <stdio.h> // Include functions from the standard I/O library

int main(int argc, char *argv[]) 
{
    int array[16]; // Make an array of 16 elements
    int example = 123456;

    // we can assign elements of the array
    array[0] = 10;

    printf("Array[0] = %d\n", array[0]);

    // in a loop
    for(int i = 0; i < 16; i++) {
        array[i] = i;
    } 

    for(int i = 0; i < 16; i++) {
        printf("Array[%d] = %d\n", i, array[i]);
    }

    printf("Let's try something different...\n");
    printf("Array[17] = %d?\n", array[17]);
    array[17] = 0;

    printf("example = %d\n", example);
}

Arrays

#include <stdio.h> // Include functions from the standard I/O library

int main(int argc, char *argv[]) 
{
    int *array = calloc(16, sizeof(int)); // Make an array of 16 elements on the /heap/
    int example = 123456;

    // we can assign elements of the array
    array[0] = 10;

    printf("Array[0] = %d\n", array[0]);

    // in a loop
    for(int i = 0; i < 16; i++) {
        array[i] = i;
    } 

    for(int i = 0; i < 16; i++) {
        printf("Array[%d] = %d\n", i, array[i]);
    }

    printf("Let's try something different...\n");
    printf("Array[17] = %d?\n", array[17]);
    array[17] = 0;

    printf("example = %d\n", example);
    free(array); // need to clean up memory we allocated on the heap
}

Strings?

#include <stdio.h> // Include functions from the standard I/O library

int main(int argc, char *argv[]) 
{
    char message[8];

    message[0] = 'H';
    message[1] = 'e';
    message[2] = 'l';
    message[3] = 'l';
    message[4] = 'o';
    message[5] = '!';
    message[6] = '\n';
    message[7] = 0; // 0 means "end of string", it's the NULL character

    puts(message); // Here's a new function puts or put string
}

A string of text is just a list of characters.

Strings

#include <stdio.h> // Include functions from the standard I/O library

int main(int argc, char *argv[]) 
{
    char message[8];

    message[0] = 72;
    message[1] = 101;
    message[2] = 108;
    message[3] = 108;
    message[4] = 110;
    message[5] = 31;
    message[6] = 10;
    message[7] = 0;

    puts(message); 
}

A string of text is just a list of characters.

Strings

#include <stdio.h> // Include functions from the standard I/O library

int main(int argc, char *argv[]) 
{
    char message[256], dest_buffer[256];
    printf("Enter a word\n");
    scanf("%s", message);

    // strlen(str) returns the length of a string
    printf("Your message %s, is %d characters long", message, strlen(message));

    // strcmp compares two strings, returns 0 if they're identical
    // "Hello" == message does not work in C
    if(strcmp("Hello", message) == 0) {
        printf("I see you're just starting comp sci.\n");
    } 

    strcpy(dest_buffer, message);
    dest_buffer[0] = 'X';

    printf("This was your original message:\n%s\n", message);
    printf("This is the copied message:\n%s\n", dest_buffer);
}

C has a bunch of functions to help manipulate strings.

Pointers

#include <stdio.h> // Include functions from the standard I/O library

int main(int argc, char *argv[]) 
{
    int *pointer = NULL; // Set the pointer to point at 0;
    
    int a_value = 100;

    // Doing the following will crash your program
    printf("Value at pointer = %d\n", *pointer); // * means "get value at pointer" here
                                                 // it's also known as dereferencing
                                                 // synonymous to pointer[0]

    pointer = &a_value; // here, & means "get the address value of the variable"
                        // it's known as the reference operator

    printf("Value at pointer = %d\n", *pointer);

    printf("pointer[0] = %d\n", pointer[0]);
}

Pointers are variables, but they point to locations in memory.

getchar, putchar

#include <stdio.h> // Include functions from the standard I/O library

int main(int argc, char *argv[])
{
    char c = 0;
    do {
        c=getchar(); // gets a character from the standard input
        putchar (c); // puts a character on the console
    } while (c != '.');
}

Next time

  • Some simple ARMv8 assembly (if statements, loops)
  • Debugging

CPSC 355: Tutorial 2

By Joshua Horacsek

CPSC 355: Tutorial 2

  • 1,431