ENGG1003 Quiz: C-Language Notes
Variables
Declaring Variables
Declared variables must be declared before being used.
Int a,b=1,c; //Creates variable a, b and c. b equalling 1.
You cannot change variable type in c, once it’s been declared
Types
Datatype
Code
Format
Specifier
Bit
Restrictions
Integer
char
%c
8bits
• Between -128 to 127
int
%d
16bits
• Can’t store fractions or decimals
long
%li
64bit
• Much greater range approx. -2.1B to
+2.1B
Floating point
integers
float
double
%f
Floating Point Numbers
Datatype
Code
Bit
Precision
float
32bit
•
double
•
Don’t use floating point numbers for while, if and do statements.
Format Specifiers
Convert ACII (often keyboard input) into a datatype (such as an integer)
%.2f produces, rounds to nearest 2 decimal places for a float %.df, where d is the number of decimals
Literals and Constants
Literals can be changed, constants stay the same throughout the code
x = 5; //is a literal integer
y = 2.0; //is a literal double
Types
• Decimals: 123 // Cannot have a zero in front of?? FIX
• Hexadecimals: 0xA34 // Zero-x
• Octal:0125 // not commonly used
Memory use
Using the smallest number type possible, conserves memory space
• Important when the target platform has limited memory resources
• Float instead of double most of the time
const char x = 1; (or x=1u; //makes it unsigned)
// stores data value in one byte, might be good for pin assignment
Arduino has an 8bit processor
Format Specifier
scanf(“%d”, &a) // &a where to store the data (var a)
Variable Assignment
a = a + 5 (increase a by 5)
Naming variables
• Case sensitive
• Cannot be a c language keyword (int, float, while e.c.t)
• Cannot use special characters
• Must begin with a letter
o But can contain numbers and underscores
Naming Conventions
Camel Case: exampleVariable
Changing datatypes
If a mix of characters is used in an operation, the output will come out as the biggest variable type
used.
Short/char -> int -> long -> float -> double
-- FIX: If converting a data types, does the resulting variable have to be declared, therefore why does
the above matter?
Libraries
Libraries are added using the header within a document code
#include <libname.h>
Standard library <stdlib.h>
Mathematics Library <math.h>
Maximum Values <limits.h>
Time <>
Mathematics Functions
Power operatioms
Random
Int dieRoll
dieRoll = (rand() % 6) + 1 //FIX check