hacking c code - local machine - binghamtonpmadden/courses/cs120/lectures/cs120-03-2018.pdfbasic...

22
Hacking C Code - Local Machine For CS department machines, use your LDAP password, and log in with ssh to remote.cs.binghamton.edu (unless you're actually sitting at a Unix machine in one of the labs, then just log in). For the University machines, remote log in to bingsuns.binghamton.edu with your PODS passwords

Upload: others

Post on 20-Aug-2020

7 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Hacking C Code - Local Machine - Binghamtonpmadden/courses/cs120/lectures/cs120-03-2018.pdfBasic Editor Commands in Emacs • Run with "emacs yourfile.c" • Up arror carrot "^" means

Hacking C Code - Local Machine

• For CS department machines, use your LDAP password, and log in with ssh to remote.cs.binghamton.edu (unless you're actually sitting at a Unix machine in one of the labs, then just log in).

• For the University machines, remote log in to bingsuns.binghamton.edu with your PODS passwords

Page 2: Hacking C Code - Local Machine - Binghamtonpmadden/courses/cs120/lectures/cs120-03-2018.pdfBasic Editor Commands in Emacs • Run with "emacs yourfile.c" • Up arror carrot "^" means

Basic Editor Commands in Emacs

• Run with "emacs yourfile.c"

• Up arror carrot "^" means to hold the control key

• ^x^s will save the file

• ^x^c will exit the editor

• ^d will delete the letter under the cursor

• ^f is forward. ^b is backwards. ^n and ^p are next and previous lines

• You can Google for emacs tutorials to learn a LOT more

Page 3: Hacking C Code - Local Machine - Binghamtonpmadden/courses/cs120/lectures/cs120-03-2018.pdfBasic Editor Commands in Emacs • Run with "emacs yourfile.c" • Up arror carrot "^" means

Basic Editor Commands in Vi

• run with "vi yourfile.c"

• h, j, k, l will move you around

• x will delete the letter under the cursor

• i puts you into "insert" mode, "esc" takes you out of insert mode

• If not in insert mode, :w will save (write) the file, :q exits the editor

• You can Google for vi tutorials to learn a LOT more

Page 4: Hacking C Code - Local Machine - Binghamtonpmadden/courses/cs120/lectures/cs120-03-2018.pdfBasic Editor Commands in Emacs • Run with "emacs yourfile.c" • Up arror carrot "^" means

Basic Hello World

#include <stdio.h>

int main()

{

// Two slashes makes the line a comment

// and the \n in the printf goes to the

// next line

printf("Hello, world\n");

}

Page 5: Hacking C Code - Local Machine - Binghamtonpmadden/courses/cs120/lectures/cs120-03-2018.pdfBasic Editor Commands in Emacs • Run with "emacs yourfile.c" • Up arror carrot "^" means

Pound Define

#include <stdio.h>

#define MAGICNUMBER 10

int main(){ printf("The magic number is %d\n", MAGICNUMBER);}

Page 6: Hacking C Code - Local Machine - Binghamtonpmadden/courses/cs120/lectures/cs120-03-2018.pdfBasic Editor Commands in Emacs • Run with "emacs yourfile.c" • Up arror carrot "^" means

Printf is your friend

#include <stdio.h>

int main(){ int x;

x = 10; printf("X is %d, and X squared is %d\n", x, x * x);}

Page 7: Hacking C Code - Local Machine - Binghamtonpmadden/courses/cs120/lectures/cs120-03-2018.pdfBasic Editor Commands in Emacs • Run with "emacs yourfile.c" • Up arror carrot "^" means

Simple loops#include <stdio.h>

int main(){ int total, x;

total = 0; // Loop starts at zero, stops when x hits 10 // Note the curly braces to group multiple lines of code // together for (x = 0; x < 10; x = x + 1) { total = total + x; printf("Added %d to the running total, which is now %d\n", x, total); } printf("Final total is %d\n", total); }

Page 8: Hacking C Code - Local Machine - Binghamtonpmadden/courses/cs120/lectures/cs120-03-2018.pdfBasic Editor Commands in Emacs • Run with "emacs yourfile.c" • Up arror carrot "^" means

Multiplication Table#include <stdio.h>int main(){ int i, j;

for (i = 0; i < 6; i = i + 1) { for (j = 0; j < 6; j = j + 1) { printf(" %3d", i * j); } printf("\n"); }}

Page 9: Hacking C Code - Local Machine - Binghamtonpmadden/courses/cs120/lectures/cs120-03-2018.pdfBasic Editor Commands in Emacs • Run with "emacs yourfile.c" • Up arror carrot "^" means

Some Things to Note• The %3d in the prior printf -- this means "print an integer, and have it use three

spaces to hold the number. VERY useful for making numbers line up nicely.

• There are two nested loops; the inner one, where j is counting up, and the outer one, where i is counting up. When we finish things for j the first time through, we go back to the outer loop, and 1 to i, and then start the inner loop again.

• I usually line up the curly braces vertically -- I find it easier to read. The K&R book puts the open curly brace at the end of the prior line. Either way works.

• Spacing does not affect the program; it's just there to make things easier for a human programmer to read.

• In the multiplication table, there's no newline included in the first printf (so each print statement lands on the same line). When we complete the loop for j, then we go to the next line.

Page 10: Hacking C Code - Local Machine - Binghamtonpmadden/courses/cs120/lectures/cs120-03-2018.pdfBasic Editor Commands in Emacs • Run with "emacs yourfile.c" • Up arror carrot "^" means

Other Things In Printf#include <stdio.h>

int main(){ int x = 20; float f = 3.14159; // Why is there a * below? We'll explain later char *s = "This is a string."; printf("Integer %d, floating point number %f, string %s\n", x, f, s);}

Page 11: Hacking C Code - Local Machine - Binghamtonpmadden/courses/cs120/lectures/cs120-03-2018.pdfBasic Editor Commands in Emacs • Run with "emacs yourfile.c" • Up arror carrot "^" means

Printf's friend, Scanf#include <stdio.h>

int main(){ int x, y; printf("Enter two integers: ");

// Why is there an & below? We'll explain later scanf("%d", &x); scanf("%d", &y);

printf("%d times %d is %d\n", x, y, x * y);}

Page 12: Hacking C Code - Local Machine - Binghamtonpmadden/courses/cs120/lectures/cs120-03-2018.pdfBasic Editor Commands in Emacs • Run with "emacs yourfile.c" • Up arror carrot "^" means

Printf's friend, Scanf#include <stdio.h>

int main(){ float x; float y; printf("Enter two numbers: ");

scanf("%f %f", &x, &y);

printf("%f times %f is %f\n", x, y, x * y); // Remember when we did the spacing so that we could // line things up in the multiplication table? Similar // sort of thing with floating point numbers printf("%5.1f times %5.1f is %5.1f\n", x, y, x * y);}

Page 13: Hacking C Code - Local Machine - Binghamtonpmadden/courses/cs120/lectures/cs120-03-2018.pdfBasic Editor Commands in Emacs • Run with "emacs yourfile.c" • Up arror carrot "^" means

Other C stuff -- If statements#include <stdio.h>

int main(){ int x = 4; int y = 5;

if (x > y) { printf("X is bigger.\n"); } else { printf("X is smaller.\n"); }}

Page 14: Hacking C Code - Local Machine - Binghamtonpmadden/courses/cs120/lectures/cs120-03-2018.pdfBasic Editor Commands in Emacs • Run with "emacs yourfile.c" • Up arror carrot "^" means

Other C stuff -- while loops

#include <stdio.h>

int main(){ int x = 0; while (x < 10) { printf("X is %d\n"); x = x + 2; }}

Page 15: Hacking C Code - Local Machine - Binghamtonpmadden/courses/cs120/lectures/cs120-03-2018.pdfBasic Editor Commands in Emacs • Run with "emacs yourfile.c" • Up arror carrot "^" means

Other C Stuff -- subroutines and functions

#include <stdio.h>

int findMax(int a, int b){ if (a > b) return a; return b;}

int main(){ int x = 10; int y = 11; int m = findMax(x, y);

printf("Max of %d and %d is %d\n", x, y, m);}

Page 16: Hacking C Code - Local Machine - Binghamtonpmadden/courses/cs120/lectures/cs120-03-2018.pdfBasic Editor Commands in Emacs • Run with "emacs yourfile.c" • Up arror carrot "^" means

Global and Local Variables

As programs become more complex, you can have many programmers working on the same project. "Local" names for variables prevent one programmer from accidentally using the same name for a variable as someone else. This is part of what the object oriented programming crowd calls "encapsulation."

Page 17: Hacking C Code - Local Machine - Binghamtonpmadden/courses/cs120/lectures/cs120-03-2018.pdfBasic Editor Commands in Emacs • Run with "emacs yourfile.c" • Up arror carrot "^" means

Global and local variables#include <stdio.h>

int z = 4; // Everyone sees zint myFunction(int x){ x = z + 200; // x is local to this function}int main(){ int x = 7; // This X is different int y = 20; myFunction(x); myFunction(y);}

Page 18: Hacking C Code - Local Machine - Binghamtonpmadden/courses/cs120/lectures/cs120-03-2018.pdfBasic Editor Commands in Emacs • Run with "emacs yourfile.c" • Up arror carrot "^" means

Global and local variables

Later on in the course, we'll cover exactly how global and local variables work, how we keep things sorted out, and how they interact.

There should be a moment in a month or two, where you go "oooooooohhhhhh, I get it now."

Page 19: Hacking C Code - Local Machine - Binghamtonpmadden/courses/cs120/lectures/cs120-03-2018.pdfBasic Editor Commands in Emacs • Run with "emacs yourfile.c" • Up arror carrot "^" means

Putting Things Together

Suppose we want a program that prints out a multiplication table of different sizes. We prompt the user for two integers, and then use those to control the loops for the multiplication table.

It might be handy to have the multiplication table printing code as a separate subroutine.

And maybe it would be handy to keep asking for what size table to print out, until the user enters zero for one of the dimensions....

Page 20: Hacking C Code - Local Machine - Binghamtonpmadden/courses/cs120/lectures/cs120-03-2018.pdfBasic Editor Commands in Emacs • Run with "emacs yourfile.c" • Up arror carrot "^" means

Programming Can Be Like Playing Piano

It's pretty easy to push a key down on a piano, and have the piano make a sound. Very good, we're proud of you, you played a note.

But being an actual piano player requires knowing how to play lots of notes, in coordination, and in sequence.

Sort of the same thing with writing code. There's only way to get good at writing code -- writing code. Lots of it. Fingers on the keyboard, banging stuff out. Look at what other people have written, to get an idea of how to do things -- but that's not a substitute for writing code.

If you have not written a lot of code, it will be frustrating at first. Keep hacking away; the light bulb WILL go on, and you'll get the hang of it.

Page 21: Hacking C Code - Local Machine - Binghamtonpmadden/courses/cs120/lectures/cs120-03-2018.pdfBasic Editor Commands in Emacs • Run with "emacs yourfile.c" • Up arror carrot "^" means
Page 22: Hacking C Code - Local Machine - Binghamtonpmadden/courses/cs120/lectures/cs120-03-2018.pdfBasic Editor Commands in Emacs • Run with "emacs yourfile.c" • Up arror carrot "^" means

Why Computer Science is Better

Someone who is smart figures out how to solve the physics problem, and writes the code for it.

#include <stdio.h>#include "physicsproblem.h"

int main(){ float x = 7; float y = 3.2;

float z = physicsproblem(x, y);}