1 / 31

Arduino club

Arduino club. Session 1: C & An Introduction to Linux. The Big Black Box. cd – "Change Directory“ ls – “List Directory Contents” mkdir – "Make Directory" nano – Terminal Text Editor gedit – GUI Text Editor (boring people only) gcc – For compiling your code. Using GCC.

gilles
Download Presentation

Arduino club

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arduino club Session 1: C & An Introduction to Linux

  2. The Big Black Box cd – "Change Directory“ ls – “List Directory Contents” mkdir – "Make Directory" nano – Terminal Text Editor gedit – GUI Text Editor (boring people only) gcc – For compiling your code

  3. Using GCC GCC is the ‘GNU Compiler Collection’, and is how we compile our code: • gcc <input file> -o <output file> • All C files must end with .c • The output file doesn’t need an extension • You run it with ./<output file>

  4. Basic Program Outline • Pre-processor Commands • Function definitions • Global Variables • Functions

  5. Annoying Syntax As with all language, there are some things you must always remember: • End most lines with a ‘;’ • Start subroutines with a ‘{‘ and end with a ‘}’ • Indent with either tabs or spaces, but not both • All variables must have their type declared

  6. Hello World /* Hello World program */ #include<stdio.h> void main(){ printf("Hello World \n"); } This line is a comment, it doesn't run with the program This tells the program to include the "stdio" library, for input & output functions Starts the "main" function Prints "hello world", followed by a new line, to the terminal

  7. Storing values

  8. Variables & Constants Defining Variables: • Define its type • Name it: • No spaces • Use camelCase or underscores_like_this, just be consistent • Assign its value e.g. "intmyNumber = 4;" e.g. "char my_char = D;" For Constants, simply prefix the statement with "const" e.g. "constintconstantNumber = 42;"

  9. Data Types • INT • Integer, used for storing whole numbers • E.g. 5 • LONG • Like an integer, used for storing bigger numbers • E.g. anything above 32767 or below -32768 • FLOAT • Used for storing decimals • E.g. 3.14159265 • CHAR • Used for storing letters (can also store numbers because fuck you that"s why) • E.g. Y

  10. Arrays Arrays are groups of data of the same data type You define them with a type, name and length: e.g. int scores[7] = {1,1,2,3,5,8,13}; e.g. char myLetters[9] = {“d”,"a","n","s","m","e","l","l","s"}; You can address specific items in the array by calling them as arrayName[position], e.g. - scores[2] would be 2 - myLetters[4] would be "m" n.b. computers count from 0, so -1 from what you think the position is

  11. Strings • Strings are stored as arrays of characters, however we can define them easily with: • char *myString = “I Love Strings!” • We can also create arrays of strings: • char *stringArray[4] = {“I”,”Love”,”Strings”,”So”,”Much”} • This means that the array • char myLetters[9] = {“d”,"a","n","s","m","e","l","l","s"} • Could also be written as: • char *myLetters=“dansmells”

  12. Scope Scope is the name for where a variable is accessible from • A variable with ‘Global’ scope can be accessed from wherever • A variable with ‘Local’ scope can only be accessed from within its function #include<stdio.h> int x = 4; long function(int x){ printf("%d \n",x); } void main(){ function(5); printf("%d \n",x); }

  13. Basic maths

  14. Addition, Subtraction, Multiplication, Division, Modulus & Powers • Addition is done with "+" • This works with characters too, but produces some funky outputs: • e.g. "d + f = Ê" • Subtraction with "-" • Same as with addition, works with every data type • Multiplication with "*" • When using floats, make sure that your result is also a float data type, otherwise it will give an incorrect answer • Division with "/" • Same as multiplication, make sure your datatypes are correct • Modulus with "%" • Finds the remainder when two numbers are divided

  15. Compound Operators We can use these to make mathematical operations on variables easier to read: • x = x + 3 • x += 3 • x = x - 3 • x -= 3 • x = x * 3 • x *= 3 Etc…

  16. Input & output

  17. scanf() & printf() "scanf()" is used for taking input from the terminal: • scanf(“%c”,&myChar); • This will wait until the user inputs a character, and will then store it in "myChar" • Variables must all be declared first "printf()" is used for outputting to the terminal: • printf(“Hello World”); • This will output "Hello World" to the terminal • printf(“"myNumber is %d”,myNumber); • This will output the contents of myNumber to the terminal

  18. Formatting Input & Output When inputting & outputting variables, we have to format them correctly:   %c for characters, %d for integers, %f for floats

  19. Conditionals

  20. "If" Statements These allow us to perform an action, if a condition is met, and otherwise perform another action #include<stdio.h> inti; void main(){ printf("Enter a number! \n"); scanf("%d",&i); if (i == 0){ printf("that number was zero \n"); } else { printf("that number was not zero \n"); } }

  21. Logic Operators • == means equal to • != means not equal to • && means AND • || means OR • > means greater than • < means less than • >= means greater than or equal to • <= means less than or equal to

  22. Loops

  23. "While" Loops These allow us to loop code indefinitely until a condition is met: #include<stdio.h> //You get the point int x = 0; //Declares x variable void main(){ //Starts main while(x<100){ //Starts while loop with condition x++; //Increases x by one printf("%d \n",x); //Prints value of x } }

  24. "For" Loops • Used for performing actions a set amount of times • Also useful for looping through an ARRAY • Started by: "for (initialization, condition, increment) {(functions in here)}" • Initialization is run only once, and is usually decleration of the condition variable • Condition decides when the for loop will stop, once it is false the loop will be exited • The increment decides how the condition variable will be increased/decreased #include<stdio.h> /* includes stdio library, necessary */ void main(){ // starts main function for (int x = 0; x<100; x++){ // sets how the for loop will run printf("%d \n", x); /* prints "x" until it"s equal to 100, then stops */ } }

  25. "For" Loops with Arrays We can also iterate through an array with a for loop, as shown below: #include<stdio.h> //Necessary int myNum[2]={1,3}; //Defines array int arraySize=sizeof(myNum)/sizeof(int); //Works out length of the array int i=0; //Defines i for iterating void main(){ //Starts main for(i=0;i<arraySize;i++){ //Starts for loop printf("%d \n",myNum[i]); //Outputs each field } }

  26. A Note about Iterating • Iterators usually take the form of: • Variable++ • This will increase "Variable" by one every iteration • Variable-- • This will decrease "Variable" by one every iteration • We aren’t limited to this, an iterator can have any mathematic function applied to it, however these are the most common

  27. Functions

  28. Why use functions? • Less repeated code • Easier to fix bad code • Neater

  29. Defining Functions Defining a function is very easy, it goes like so: return_type function_name(arguments,with,datatypes){ /* code goes here*/ } Just remember to define them before you call them!

  30. Calling Functions Calling a function is even easier: function_name(arguments);

  31. Variables can be functions too! If we do x = functionName(arguments) x will then hold the return value of the function ‘functionName’

More Related