ENGG1003 Quiz: C-Language Notes

34 views5 pages
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
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
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in
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
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers

Related Documents