/* * Comment section */ … Preprocessor directives … … Evil Global Variable Declarations … int main( ) { … Local Variable Declarations … // Comment … Executable Statements … … Local Variable Declarations … // Comment … Executable Statements … return 0; }
// CS156 HW3 // Author: Jack Applin // Purpose: Demo of first program // Input: none // Output: prints Hello World // This is an in-line comment. // /* This is a multi-line comment. */ /* * so is this */ /* and this as well */ // But, of course, // so is this, sort of.
/*
comments */
can’t be nested.
//
comments can be nested.
// This // will // work. // This won’t work: /* int a=1; int b=2; /* hello */ int c=3; */
#include <
whatever.h>
syntax tells the compiler to
include a set of variable and function definitions from another file.
.h
files.
stdio.h
— basic input and output
(e.g., printf
, scanf
)
stdlib.h
— many utility functions
(e.g., exit
, rand
, malloc
, strod
)
math.h
— mathematical functions (e.g., sqrt
, pow
)
Egg
is not egg
).
song_count
is better than s
or sc
.
Standard Data Types | |||
int | char | double & float | void |
-20 | 'x' | -30.4 | |
42 | '3' | 3.0 | |
123456789 | '\n' | 6.022e23 |
Note that a single char
literal is 'x'
with just a single character between the single quotes.
A string literal "xyz abc"
may contain 0 or more characters between the double quotes and is automatically null terminated.
;
double rest_mass;
char grade;
int intyMcIntFace;
float x,y;
int
— an integer type
int zip_code; // a zip code
double
— a floating point type (real number)
double friction; // a friction coefficient
char
— a single character
char grade; // letter grade: ABCDF, but not B+
a + b
: Add
a - b
: Subtract
a * b
: Multiply
a / b
: Divide
a % b
: Modulus (return remainder)
a
and b
can be literals, variables or other expressions.
1.4*2
or var1*3.14
or 2*(var1+2)
a
has the value 12
and b
is 7
, what is a % b
?
Beware, mathematicians: some things are different:
Expression | Result |
---|---|
axb | the variable axb |
a x b | a syntax error |
3d | a syntax error |
a⋅b | a syntax error |
d3 | the variable d3 |
=
expr;
#include <stdio.h> int zip_code; // an evil global variable int main() { double friction; // local variable for amt of friction double number; // local variable for calculation // assign some values zip_code = 44455; friction = 0.04; number = (zip_code * friction) - 3.2; // print number printf("The final number was %f\n", number); return 0; }
The final number was 1775.000000
%d
— print an int variable
%f
— print a double variable
%c
— print a char variable
\n
— the newline character
\t
— the tab character
\"
— double quote
#include <stdio.h> int main() { double friction, number; int numPeople = 3; // assign some values friction = 0.04; number = friction + 3.2 * 2; printf("The final number was %f\n", number); printf("The value of \"friction\" was %f.\n",friction ); printf("There are %d people here.\n", numPeople ); return 0; }
The final number was 6.440000 The value of "friction" was 0.040000. There are 3 people here.
In elementary school, students often learn acronyms such as PEMDAS. These are useful, to a point.
Consider 1 − 2 + 3.
Highest precedence, happens first |
( ) |
* / % |
+ - |
= |
Lowest precedence, happens last |
a+b*c
. This expression is ambiguous
unless we specify an order for the operations to be performed.
printf("1+2*3 = %d\n", 1+2*3); printf("(1+2)*3 = %d\n", (1+2)*3); printf("2*3+4*5 = %d\n", 2*3+4*5); printf("2*(3+4)*5 = %d\n", 2*(3+4)*5);
1+2*3 = 7 (1+2)*3 = 9 2*3+4*5 = 26 2*(3+4)*5 = 70
printf("%d\n", 123.456); printf("%f\n", 789);
c.c: In function 'main': c.c:1: warning: format '%d' expects argument of type 'int', but argument 2 has type 'double' c.c:2: warning: format '%f' expects argument of type 'double', but argument 2 has type 'int' 397568920 123.456000
42 + 0.0
→ 42.0
(floating-point)
3.0 / 1.5
→ 2.0
(floating-point)
3 / 1.5
→ 3.0 / 1.5
→ 2.0
(floating-point)
3 / 2
→ 1
(int)
21 / 8
→ 2
(int)
printf("%f\n", 42 + 0.0); printf("%f\n", 3.0 / 1.5); printf("%f\n", 3 / 1.5); printf("%d\n", 3 / 2); printf("%d\n", 21 / 8);
42.000000 2.000000 2.000000 1 2
How to confuse your readers:
x = 3. + .4;
Was that .4
or 4
? Don’t make me squint to see a leading or
trailing decimal point—help me out with a zero:
x = 3.0 + 0.4;