PROGRAMMAZIONE I A.A. 2017/2018
HINTS
PRESENTATION IS POWERFUL int ok = thiscouldgowrong(); if (!ok) fprintf(stderr, "Error: exiting...\n"); return(0); Good presentation does not draw attention or distract; it serves only to reveal the code s intent. This helps programmers work with the code effectively. You can see what the author intended here: return(0) was only to be called when the test failed. But the presentation has hidden the real behaviour: the code will always exit.
LAYOUT Code layout concerns include indentation, use of whitespace around operators, capitalisation, brace placement (be it K&R style, Allman, Whitesmith, or the like), and the age-old tabs versus spaces indent debate Allman while (x == y) { something(); somethingelse(); } Whitesmiths while (x == y) { something(); somethingelse(); } KR while (x == y) { something(); somethingelse(); }
KR The K&R style is commonly used in C (Ritchie designed C). int main(int argc, char *argv[]) { /*...*/ while (x == y) { something(); somethingelse(); } if (some_error) do_correct(); else continue_as_usual(); } finalthing(); /*...*/
NAMES A name conveys the identity of an object; it describes the thing, indicates its behaviour and intended use. A misnamed variable can be very confusing. Do not create unnecessarily long variable name Do not use underscore as first character to avoid confusion between system variable and user defined variables because many system variables starts with underscore Variable names are case-sensitive. üsum,sum,sum, sum these are different variable names. Reserve words with one/more Capital letters allowed eg. Int, Float, char are allowed but try to skip them.
NAMING CONVENTION See the difference üa = b * c; üweekly_pay = hours_worked * pay_rate; Struct Struct Members Enum Enum Members Trivial variables Local variables Pointers Global variables TitleCase lower_case or lowercase ETitleCase ALL_CAPS i,x,n,f etc... lower_case or lowercase p_lowercase or p_lower_case g_lowercase or g_lower_case
WRITE LESS CODE More code means there is more to read and more to understand it makes our programs harder to comprehend. The more code there is, the more work is required to make modifications the program is harder to modify. Code harbours bugs. The more code you have, the more places there are for bugs to hide.
EXAMPLE A simple and common class of pointless code is the unnecessary use of conditional statements and tautological logic constructs. if (expression) return 1; else return 0; return expression; if (something!= 0) { //... } if (something) { //... }
COMMENTS Add comments as more as you can For instance, before each function declaration to let understand other programmers what they do with the parameters they take as input. But there are also stupid comments: ++i; // increment i // loop over all items, and add them up int total = 0; for (int n = 0; n < MAX; n++) { total += items[n]; }
SCANF
PROBLEMS WITH SCANF Example scanf.c Input buffer #include <stdio.h> int main() { char a, b; int c= 0; printf("enter a character: "); scanf("%c", &a); d \n printf("enter a character: "); scanf("%c", &b); printf("enter an int: "); scanf("%d", &c); } return 0; MacBook-Francesco:ProgrammI francescosantini$./scanf Enter a character: d Enter a character: Enter an int: 5
SOLUTION #include <stdio.h> int main() { char a, b; int c= 0; printf("enter a character: "); scanf("%c", &a); printf("enter a character: "); scanf( %c", &b); printf("enter an int: "); scanf("%d", &c); } return 0;
FGETS #include<stdio.h> int main(void) { char a[10]; if (fgets (a, 10, stdin) == NULL) printf("error\n"); } printf("%s", a); MacBook-Francesco:ProgrammI francescosantini$./fgets abcdefghilmnopq abcdefghi
WHY C (C++)
POPULARITY (2016) https://blog.newrelic.com/2016/08/18/popul ar-programming-languages-2016-go/
ONE MORE CHART http://pypl.github.io/pypl.html
QUOTATIONS So what do all these data points add up to? Java remains incredibly popular, and job seekers can t go wrong learning it. JavaScript skills may not land you a job as quickly, but you ll be in good company with legions of other programmers. There s still interest in the various flavors of C as well, and PHP plays a big role in the mid-market. But perhaps the real takeaway is that we seem to be moving into a polyglot world where organizations increasingly employ multiple languages. The idea is to take advantage of each language s special strengths as well as the varied expertise of the programming team.
WEB http://spectrum.ieee.org/static/interactivethe-top-programming-languages-2016
ENTERPRISE
EMBEDDED
C PERFORMANCE
WHY C IS FASTER Newer languages which have support for garbage collection, dynamic typing and other facilities which make it easier for the programmer to write programs. The catch is, there is additional processing overhead which will degrade the performance of the application. C doesn't have any of that, which means that there is no overhead. üthat means that the programmer needs to be able to allocate memory and free them to prevent memory leaks, and must deal with static typing of variables.
BITCORE https://bitcore.io
JOBS
JOBS
JOBS
ARDUINO/GENUINO
INTRO https://www.arduino.cc Arduino is an open-source electronics platform based on easy-to-use hardware and software. Arduino boards are able to read inputs - light on a sensor, a finger on a button, or a Twitter message - and turn it into an output - activating a motor, turning on an LED, publishing something online. You can tell your board what to do by sending a set of instructions to the microcontroller on the board. Arduino was born at the Ivrea Interaction Design Institute as an easy tool for fast prototyping, aimed at students without a background in electronics and programming.
ARDUINO A worldwide community of makers - students, hobbyists, artists, programmers, and professionals It runs on Mac, Windows, and Linux Arduino Programming Language ühttps://www.arduino.cc/en/reference/homepage Examples ühttps://youtu.be/eid1lnpm_24 (http://tinyurl.com/huwmec8) ühttps://youtu.be/f4wua8h0cpo