Output, Arithmetic, and Input
COP-3223H
Table of Contents
Practice, practice, practice
Practice makes gooder
- Take the opportunity to practice coding as much as possible.
- Hand-type simple programs, e.g., Arup's practice programs
Practice may not may you perfect in this class, but not practicing will hurt you.
Homeworks
- Practice as much as possible, using homeworks as an opportunity.
Actually type by hand the homework (not doing so violates academic integrity because it does not complete the homework as assigned).
This will give you practice using the programming environment and typing out C programs.
On the exams (and in coding interviews), you will need to write code from scratch with no access to a compiler, search engines, etc.
Attendance
- Attend class to keep up with material
Even if you don't understand everything during class, it will give you the initial exposure to material, let you see live coding, and ask questions.
Live coding in class
- Participate, either as a viewer or code along with me
AI Policy
No AI use permitted whatsoever
Submitting code not created by (other than templates given in class) and hand-typed by you, including AI generated code, is unauthorized assistance and violates academic integrity.
Moreover, using AI or other code not made by you will make exams much more difficult for you, since these do not permit accces to assistants.
Hello, world!
#include <stdio.h>
int main(int argc, char **argv) {
printf("hello, world!\n");
}
Project: hello
The first project will be to write a hello world program in emacs and successfully commit and push it to your repo.
Performing arithmetic
int main(int argc, char **argv) {
1 + 2;
}
Syntax
- End statements with a semi-colon.
Printing
#include <stdio.h>
int main(int argc, char **argv) {
printf("%d\n", /* put your number/expression here */);
}
printf("STRING TO PRINT", expression, expression, expression, ...) is a function call, i.e., a named piece of code. We don't need to know what the code is to use it. The comma-delimited list within parentheses are the inputs to the function. Think of a function as like a separate program, with its own inputs and outputs, that we can run within our program.
The "quoted" content is a string, i.e., a sequence of characters of text.
%d tells printf that we want to print an integer value.
\n means move the cursor to the next line in the shell.
Gotcha: a missing \n will produce a slightly different output in the shell.
Printing a number
#include <stdio.h>
int main(int argc, char **argv) {
printf("%d\n", 4);
}
Printing an expression
#include <stdio.h>
int main(int argc, char **argv) {
printf("%d\n", 1 + 2);
}
Expressions in C
Integer Arithmetic
| Operator | Description |
|---|---|
1 + 2 |
Integer addition |
2 - 3 |
Integer subtraction |
3 * 4 |
Integer multiplication |
8 / 3 |
Integer division |
8 % 3 |
Remainder |
-2 |
Negation |
https://www.gnu.org/software/c-intro-and-ref/manual/c-intro-and-ref.html#Basic-Arithmetic
Notice negation and subtraction use the same symbol, as in written math
Operator precedence and parentheses
Highest to lowest
- parentheses
- negation
- multiplication and division (same precedence)
- addition and subtraction (same precedence)
Other operators
Leave aside the conditional operators for conditions
Bit twiddling (optional)
Example expressions
- 1+2;
- printf(ā%d\nā, 1+2);
- 1 + 2 * 3; // (order of operations)
- 6 / 2 * (1 + 2);
- 2 * 2 * 2; // (no exponent operator)
// is a comment; it and everything after it is ignored.
Memory
Here we will discuss automatic memory, i.e., the compiler and C runtime will automatically manage the memory allocation and deallocation for you. You only need to declare and name the memory space. Just like in our desktop calculator, this memory is rewritable.
Variable declaration
Create memory slots and give them names:
int x; int y;
The names must start with a letter or an underscore, but subsequently can be any combination of letters, numbers, or an underscore. Names have a maximum length that is defined by the compiler (and C language standard).
Data types
int(for integer) is the data type.- It defines what values a variable holds (and what the arithmetic operations mean).
int
- 32-bit signed integer (on 64-bit architectures)
- Integer arithmetic
Remember the desktop calculator: what happens with division?
Gotcha: integer arithmetic
Write a C program that computes the sum: 1/3 + 1/3 + 1/3
Integer literals, i.e., constant numbers without a decimal point, are treated as integers. Since the type determines the operation, this is computed using integer arithmetic, resulting in 0 + 0 + 0 = 0.
int is finite
- Has minimum and maximum sizes (a finite set)
Write a program to print INT_MAX and INT_MIN (limits.h).
Precision and float
#include <stdio.h>
int main() {
float f1;
float f2;
float fresult;
float sum;
f1 = 1;
f2 = 3;
fresult = (f1 / f2);
sum = fresult * 3 * 10000000000000000;
printf("%f\n", sum);
}
Variable assignment
Storing to memory slots
x = 3; y = 2;
Variable use
Recalling from memory slots
y; x;
Printing variables (expressions)
Write a program that prints a variable with printf.
Using variables in expressions
Write a program that uses variables in other variable assignments and in an expression in a printf call.
Gotcha: reusing variables
x = 2; x = x + 1;
What is the value of x?
The C = is not like the mathematical =, which states equality. The above would appear to be a contradictory otherwise. C's = really means assignment, i.e., store a value in memory. It's subtlely different and we'll see more when we get to conditional branching. Additionally, = is an operator in an expression, just like + is, albeit with a very different meaning (memory storage). For instance, we write x = y = z; and this means use the value z assign it to y, then assign that resulting value to z.
Assignment expressions are complex in C, with many ways to define the l-value, i.e., the memory location to store to.
Examples
- Square
- Cube
- 5!
Diagram the memory usage. Looks just like the desktop calculator model.
int x; int square; int cube; x = 4; square = x * x; cube = x * x * x;
int x; int result; x = 4; // what if x is given by the user? result = 1; result = x * result; x = x - 1; result = x * result; x = x - 1; result = x * result; x = x - 1;
Game: the magic of six
Multiply by three, add six, divide by 3, then subtract the original number.
((x * 3) + 6) / 3 - x 3/3x + 6/3 - x = x + 2 - x
int x = 8
int result;
result = ((x * 3) + 6) / 3 - x;
// result = x * 3;
// result = result + 6;
// result = result / 3;
// result = result - x;
printf("%d\n", result);
Game: the magic of nine
Multiply by nine and sum the digits.
- How do we sum the digits?
- How do we get an individual digit? Hint: integer division
#include <stdio.h>
int main(int argc, char **argv) {
int num;
// read an integer from the user
scanf("%d", &num);
// print the number from the user
printf("%d\n", num);
int x;
// multiply by nine
x = num * 9;
printf("%d\n", x);
// get the ones, tens, and hundreds digits
int ones_digit = x % 10;
int tens_digit = (x / 10) % 10;
int hundreds_digit = (x / 10 / 10) % 10;
// sum the first three digits
int sum = ones_digit + tens_digit + hundreds_digit;
// print the sum
printf("%d\n", sum);
}
Input
#include <stdio.h>
int main() {
int num;
scanf("%d", &num);
}
- Notice the use of
%dparalleling printf. - Notice the
&prefixingnum. This allows thescanffunction to store tonum(call by reference), which would not be possible without it (call by value).
Examples
- Square
- Cube
- Factorial?
Can we take a value from the user and compute factorial?
Game: shadow
Want to play shadow?
Repeat the users input
#include <stdio.h>
int main() {
int num;
printf("enter a number: ");
scanf("%d", &num);
printf("%d\n", num);
}
Game: the magic of nine
Multiply by nine and sum the digits.
- How do we sum the digits?
- How do we get an individual digit? Hint: integer division
- This is only for two digit numbers
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int x;
int sum;
printf("enter a two-digit number: ");
scanf("%d", &x);
printf("x: %d\n", x);
x = x * 9;
printf("x: %d\n", x);
sum = 0;
// get ones place digit
sum = sum + x % 10;
printf("sum: %d\n", sum);
// shift digits right
x = x / 10;
// get ones place digit
sum = sum + x % 10;
printf("sum: %d\n", sum);
}