Hacking C Code - Local Machine

Similar documents
1. The Mac Environment in Sierra Hall 1242

1. The Mac Environment in SIE 1222

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

CSCI 2132 Software Development. Lecture 8: Introduction to C

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Computer Programming: Skills & Concepts (CP) Variables and ints

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays

Physics 306 Computing Lab 1: Hello, World!

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

C: How to Program. Week /Mar/05

CSC 270 Survey of Programming Languages

COMP s1 Lecture 1

Engineering 12 - Spring, 1998

CSC111 Computer Science II

Structured programming

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

University of Maryland College Park Dept of Computer Science CMSC106 Fall 2013 Midterm I Key

How to approach a computational problem

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

The Basics of C Programming

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Lecture 6. Statements

Chapter 1 & 2 Introduction to C Language

EL2310 Scientific Programming

notice the '' you must have those around character values, they are not needed for integers or decimals.

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Dept. of CSE, IIT KGP

Lecture 3. More About C

Chapter 2 - Introduction to C Programming

Introduction to Programming

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Helpful Tips for Labs. CS140, Spring 2015

1/31/2018. Overview. The C Programming Language Part 2. Basic I/O. Basic I/O. Basic I/O. Conversion Characters. Input/Output Structures and Arrays

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

REU C Programming Tutorial: Part I FIRST PROGRAM

Programming for Electrical and Computer Engineers. Loops

Formatted Input/Output

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

PDS Lab Section 16 Autumn Tutorial 1. Unix Commands pwd The pwd command displays the full pathname of the current directory.

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Chapter 3: Arrays and More C Functionality

Program Organization and Comments

The Compilation Process

Programming in C. Session 2. Seema Sirpal Delhi University Computer Centre

Arithmetic Expressions Lesson #1 Outline

CpSc 1111 Lab 4 Formatting and Flow Control

Question 2. [2 points] Which of the following is a correct statement to obtain user input? (Assume that fleems is an int variable.

Chapter 2. Editing And Compiling

Introduction: The Unix shell and C programming

Statements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14

Compiling and Running a C Program in Unix

T H E I N T E R A C T I V E S H E L L

CpSc 1111 Lab 5 Formatting and Flow Control

EL2310 Scientific Programming

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

The C language. Introductory course #1

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

printf("%c\n", character); printf("%s\n", "This is a string"); printf("%s\n", string); printf("%s\n",stringptr); return 0;

Saint Louis University. Intro to Linux and C. CSCI 2400/ ECE 3217: Computer Architecture. Instructors: David Ferry

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

C-1. Overview. CSE 142 Computer Programming I. Review: Computer Organization. Review: Memory. Declaring Variables. Memory example

Lecture 10: Recursive Functions. Computer System and programming in C 1

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.

CSE 303 Lecture 8. Intro to C programming

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Array Lesson 1 Outline

Introduction to Computing Lecture 03: Basic input / output operations

1. The programming language C is more than 30 years old. True or False? (Circle your choice.)

War Industries Presents: An Introduction to Programming for Hackers Part III - Advanced Variables & Flow Control.

EECE.2160: ECE Application Programming Spring 2018

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

Structured programming

Programming & Data Structure: CS Section - 1/A DO NOT POWER ON THE MACHINE

Fundamentals of Programming

Lab 1 Introduction to UNIX and C

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen

CS4023 Week06 Lab Exercise

Fundamental of Programming (C)

In further discussion, the books make other kinds of distinction between high level languages:

Standard I/O in C and C++

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

Structured programming. Exercises 3

printf( Please enter another number: ); scanf( %d, &num2);

Control Flow, Functions and Basic Linkage

Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II)

Programming Style Guide v1.1

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

Chapter 13 Control Structures

2. Numbers In, Numbers Out

Transcription:

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

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

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

Basic Hello World // Two slashes makes the line a comment // and the \n in the printf goes to the // next line printf("hello, world\n");

Pound Define #define MAGICNUMBER 10 printf("the magic number is %d\n", MAGICNUMBER);

Printf is your friend int x; x = 10; printf("x is %d, and X squared is %d\n", x, x * x);

Simple loops 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);

Multiplication Table 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");

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.

Other Things In Printf 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);

Printf's friend, Scanf 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);

Printf's friend, Scanf 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);

Other C stuff -- If statements int x = 4; int y = 5; if (x > y) printf("x is bigger.\n"); else printf("x is smaller.\n");

Other C stuff -- while loops int x = 0; while (x < 10) printf("x is %d\n"); x = x + 2;

Other C Stuff -- subroutines and functions int findmax(int a, int b) if (a > b) return a; return b; int x = 10; int y = 11; int m = findmax(x, y); printf("max of %d and %d is %d\n", x, y, m);

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."

Global and local variables int z = 4; // Everyone sees z int myfunction(int x) x = z + 200; // x is local to this function int x = 7; // This X is different int y = 20; myfunction(x); myfunction(y);

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."

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...

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.

Why Computer Science is Better Someone who is smart figures out how to solve the physics problem, and writes the code for it. #include "physicsproblem.h" float x = 7; float y = 3.2; float z = physicsproblem(x, y);