Professional Documents
Culture Documents
Full Course
Full Course
h>
int main(void)
{
return 0;
}
For scanf(), we use the & operator before the function to pass the memory address
of the variable to the function. This allows scanf() function to store the user's
input directly in the variable.
When variables are uninitialised, when there's nothing setting them to some initial
value, there's actually no defined behaviour. They are not automatically set to 0.
Whatever is in that place in memory that's been set aside for the variable at the
time the variable is declared, will be the variable's initial value. This can cause
the program to end up with bugs.
float is short for 'floating point value', and allows us to store real numbers
including those with decimal points.
// Example:
printf("x: :';
scanf("%f", &x);
printf("x: %f\n", x);
double is very similar, and also stores real numbers with decimal points, however
it can store numbers with a higher level of precision, essentially with more
decimal points and with a greater range.
// Example:
printf("y: ");
scanf("%lf", &y);
printf("y: %f\n", y);
NOTE: with floating point values and double values, the exact value is not always
stored, it's often a number that is very close to that value. This is because of
the way computers store data as binary, using a system involving fractions that
allow us to approximate real values that have decimal digits and store numbers more
efficiently by doing so however prevents numbers from being stored in a completely
accurate way.
char is short for character which can be a letter, number, punctuation mark etc. We
create characters with single quotes, not double quotes which are for strings.
Characters are fundamentally integer values with character representations for each
one.
We can find which characters are associated with which numbers by bringing up an
ASCII table with the command:
ascii -d
Example:
char c = 'd';
printf("c: ");
scanf("%c", &c);
printf("c: %c\n", c);
You can print out the character value as an integer with the command
printf("c: %d\n", c);
double multiply = x * y;
double add = x + y;
double divide = x / y;
double subtract = x - y;
There are other operations such as modulus, which is relevant for integer values.
Modulus can do all the usual operations with integers, however it performs integer
division. For example, if it takes x and divides it by y, it returns back how many
times y goes into x. The modulus operator actually returns the remainder or
division
Example:
int x = 11, y = 3
int div = x / y
int mod = x % y;
CONTROL STRUCTURES
Example:
int grade = 0;
printf("Grade: ");
scanf("%d", &grade);
We can extend this control structure further by adding another type of if statement
called the if-else-if-else statement that adds in more potential condition.
Example:
int grade = 0;
printf("Grade: ");
scanf("%d", &grade);
return 0;
Example:
int height = 0;
printf("height: ");
scanf("d%", &height);
return 0;
Example:
int height = 0;
printf("height: ");
scanf("%d", &height);
int weight = 0;
printf("weight: ");
scanf("%d", &weight);
return 0;
Another boolean operator is 'not', which takes a 'true' and flips it to 'false' and
vice versa.
Example:
return 0;
Operators have a default precedence, by default the 'not' operator happens first
before the 'less than' operator.
LOOPS
Another type of control structure is the 'loop', of which there are three types.
'If' statements execute one block of statements or another block, loops execute
another block of statements repeatedly.
While Loops
Example:
int i = 0;
//This is called a counter variable
while (i < 5) {
printf("i: %d\n", i);
i = i + 1;
}
return 0;
NOTE: adding 1 to i is such a common thing to do that there are two shortcuts.
i += x;
// This will add x to i, given x is a number
i++
//This will add 1 to i
Example:
int i = 0;
int number = 0;
int total = 0;
int total_numbrers = 0;
printf("How many numbers: ");
scanf("%d", &total_numbers);
return 0;
NOTE: When you're trying to debug errors, throw in as many printf() commands as
possible and print out the values of your different variables.
Indefinite loops are another type of loop, and execute an indefinite number of
times. This loop finds the maximum number that was entered, and exits when -1 is
entered. Until then it keeps asking the user for more numbers, and keeps track of
the maximum number.
Example:
int number = 0;
int max = -1;
return 0;
Example:
int i = 10;
return 0;