Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

#include <stdio.

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

These are common operators seen in C programming, using double as an example:

// Let x = 5.4, y = 2.5

double x = 5.4, y = 2.5

double multiply = x * y;
double add = x + y;
double divide = x / y;
double subtract = x - y;

printf("multiply: %f\n", multiply);


printf("add: %f\n", add);
printf("divide: %f\n", divide);
printf("subtract: %f\n", subtract);

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;

printf("div: %d\n", div);


printf("mod: %d\n", mod);

CONTROL STRUCTURES

Example:

int grade = 0;

printf("Grade: ");
scanf("%d", &grade);

\\ The code below is called an if-else statement or an if-else control structure.

if (grade >= 50) {


printf("Pass\n");
printf("Congrats!\n");
} else {
printf("Fail\n");
printf("Good luck next time!\n");
}

\\ The space enclosed within the curly brackets is called a block.


printf("If done\n");

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

if (grade >= 90) printf("A\n");


else if (grade >= 80) printf("B\n");
else if (grade >= 70) printf("C\n");
else if (grade >= 60)
{
printf("D\n");
}
else printf("Fail\n");

return 0;

Conditions typically depend on relational operators.

Example:

int height = 0;
printf("height: ");
scanf("d%", &height);

if (height <= 155)


printf("Not tall enough!\n");
else
printf("Tall enough!\n");

return 0;

Another type of operator is the logical operators, or th e boolean operators. These


allow us to combine booleans into new boolean values, such as for checking that
some condition and another condition is true. They allow us to make compound,
complex conditionals.

Example:

int height = 0;
printf("height: ");
scanf("%d", &height);

int weight = 0;
printf("weight: ");
scanf("%d", &weight);

if (height > 150 && weight > 50)


printf("Good to ride!\n");
else
printf("Not good to ride!\n");

return 0;

Another boolean operator is 'not', which takes a 'true' and flips it to 'false' and
vice versa.

Example:

// Brackets can be used to influence which operation will happen first

if (! (height > 150) )


printf("Good to ride!\n");
else
printf("Not good to ride!\n");

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

while (i < total_numbers) {


printf("Enter Number %d: ", i+1);
scanf("%d", &number);
total = total + number
i++;
}
printf("total: %d\n", total);
printf("average; %d\n", total / 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;

while (number != -1) {


printf("Enter a number: ");
scanf("%d", &number);
if (number > max) max = number;
printf("max: %d\n", max);
}

// An 'if' statement inside a loop is called a nested structure

printf("max: %d\n", max);

return 0;

Infinite loops are loops that don't stop.

Example:

int i = 10;

while (i < 20) {


printf("i: %d\n", i)
i = i - 1;
// i-- will take 1 away from i, as i++ adds 1 to i
}

return 0;

You might also like