Lecture 1 Flashcards
C programming basics
\n
newline
include <stdio.h></stdio.h>
int main (int argc, char** argv) {
printf(“Hello World!\n”);
return 0;
}
what does #include <stdio.h> mean?</stdio.h>
it adds the standard input/output functionality to program (header file)
include <stdio.h></stdio.h>
int main (int argc, char** argv) {
printf(“Hello World!\n”);
return 0;
}
what is int argc?
integer argument for main( )
include <stdio.h></stdio.h>
int main (int argc, char** argv) {
printf(“Hello World!\n”);
return 0;
}
what is char** argv?
char pointer (string array) argument for main( )
include <stdio.h></stdio.h>
int main (int argc, char** argv) {
printf(“Hello World!\n”);
return 0;
}
what does \n mean?
new line
include <stdio.h></stdio.h>
int main (int argc, char** argv) {
printf(“Hello World!\n”);
return 0;
}
what does return 0 mean?
returns 0 when main is done executing, 0 here means the program ran and executed normally
what does GCC stand for?
GNU Compiler Collection
What is gcc?
software program/compiler
ex) you use gcc file.c -o file to compile program in terminal
gcc first.c -o first
what does -o first make?
executable object file named “first”
printf(“%s\n”, “Hello World”);
what does the input/output do here with the format specifier and string?
it puts “Hello World” into %s to satisfy the string format specifier, goes to standard out then prints “Hello World”
how to declare a variable?
data type variable name;
how to declare a variable with initialization?
data type variable name = value;
should you initialize or not initialize when declaring a variable?
initialize, the value assigned if you don’t initialize will not be 0 every time –> it will be assigned to whatever is left in memory
what are the name restrictions for variables?
reserved words (for, malloc, etc), cannot exceed 31 characters
cannot start with a digit
what is allowed for the names of variables?
can contain letters, digits, and underscores
ex) _brent
is case significant with naming variables?
yes, the variable Molly and molly are not the same
arithmetic operations
+, -, *, /, %, ++, –
relational operations
==, !=, >, <, >=, <=
logical operations
&&, ||, !
bitwise operations
&, |, ^, ~, «,»_space;
int main (int argc, char** argv) {
int d = 0;
scanf(“%d\n”, d);
printf(“d = %d\n”, d);
return 0;
}
what does scanf( ) do?
scanf( ) reads characters from terminal, its the keyboard (stdin)
- waits for user at that point in the program to type value in the terminal and hit return (same as scanner in java)
**input
int main (int argc, char** argv) {
int d = 0;
scanf(“%d\n”, d);
printf(“d = %d\n”, d);
return 0;
}
what does printf(“d = %d\n”, d) do?
printf( ) displays the output to the screen,
for “d” –> takes whatever int is available and puts into the format specifier (“%d”)
**output
what is a library function that writes things to to the console?
printf()
*output
what is a library function that gets input/reads what the user puts in the terminal?
scanf()
*input
order of precedence
1) parenthesis
2) multiplication, division, modulus
3) addition, subtraction
what if there are multiple operations that have the same precedence?
operate left to right
post fix
a++
use a in the expression then increment
pre fix
++a
increment a then use in expression
basic for loop syntax
for (int i = 0; boolean_expression; increment) {
}
ex) for (int i = 0; i < 3; i++) {
…
}
basic while loop syntax
while (boolean_expression) {
//executes if boolean_expression is
true
}
basic do loop syntax
do {
//execute if boolean_expression is
true
//if false, never executes while loop
} while (boolean_expression) {
//if the boolean_expression^ is
true, loops back through
starting at do
}
is it better to nest or not nest for looping statements?
don’t nest, use compound statements instead
basic branching statement syntax (if and else)
if (boolean_expression) {
//code here executes if
boolean_expression is true
} else {
//code here executes if
boolean_expression is false
}
basic branching statement syntax (if, else if, else)
if (boolean_expression) {
//code here executes if
boolean_expression is true
} else if (boolean_expression2) {
//code here executes if
boolean_expression is false and
boolean_expression2 is true
} else {
//code here executes if none of
the above are true
}
is it better to nest or not nest for branching statements?
don’t nest, better to use compound statements
1 byte =
8 bits
what is malloc equivalent to in java?
new
both allocate space in memory for “new variable”