34
PROGRAMMAZIONE I A.A. 2018/2019

PROGRAMMAZIONE I - DMIUnipg · PROGRAMMAZIONE I A.A. 2018/2019. WHAT YOU NEED ... SHELL (BASH) Bash is a Unix shell and command language written by Brian Fox for the GNU Project as

Embed Size (px)

Citation preview

PROGRAMMAZIONE I

A.A. 2018/2019

WHAT YOU NEED

The good news for anyone using Linux or Mac OSX is that you are on a system designed for programming in C. The authors of the C language were also instrumental in the creation of the Unix operating system, and both Linux and OSX are based on Unix. In fact, the install will be incredibly easy.You needüThe compiler (try typing “gcc --version” in your shell)üAn editor

LINUX

On most Linux systems you just have to install a few packages. For Debian based systems, like Ubuntu you should just have to install a few things using these commands:

$ sudo apt-get install build-essential

The above is an example of a command line prompt, so to get to where you can run that, find your "Terminal" program and run it first. Then you'll get a shell prompt similar to the $ above and can type that command into it.

Here's how you would install the same setup on an RPM based Linux like Fedora:

$ su -c "yum groupinstall development-tools"

Once you've run that, you should be able to do the first Exercise in this book and it'll work. If not then let me know.

MAC OSX

On Mac OSX the installation is even easier. First, you'll need to either download the latest XCode from Apple, or find your install DVD and install it from there.

WINDOWS

https://www.cygwin.comCygwin is:üa large collection of GNU and Open Source tools which

provide functionality similar to a Linux distribution on Windows.

üa DLL (cygwin1.dll) which provides substantial POSIX API functionality.

SUGGESTIONS AND HOW TO CYGWIN

Install Linux on you laptop/computerUse Linux on one of the PC

If you really want to use CygwinüRemember to select among Devel packages cygwin64-ggc-coreüOnce installed click on cygwin.bat to launch the shellüThen try to digit “gcc”, if it says it exists, OK!üIf it dos not exist, maybe it has a weird name like i686-pc-cygwin-

gcc.exe. In the shell type the following commands that link the name “gcc” to the name of the compiler

• cd /usr/bin/ • ln i686-pc-cygwin-gcc.exe gcc

https://www3.ntu.edu.sg/home/ehchua/programming/howto/Cygwin_HowTo.html

GCC

https://gcc.gnu.orgGNU C Compiler

The GNU Compiler Collection includes front ends for C, C++, Objective-C, Fortran, Java, Ada, and GoGCC 8.2 released [26th July 2018]

Manual https://gcc.gnu.org/onlinedocs/gcc-8.2.0/gcc.pdf

SHELL (BASH)

Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it has been distributed widely as the default login shell for most Linux distributions and Apple's macOS (formerly OS X). Bash is a command processor that typically runs in a text window, where the user types commands that cause actions.

Howto in italian: http://linux.collectiontricks.it/wiki/Guida_ai_comandi_base_della_shell_in_GNU/Linux

Slides on my page

TEXT EDITOR

The choice of text editor for a programmer is a tough one. For beginners I tell them to just use Gedit since it's simple and works for code. However, it doesn't work in certain internationalized situations, and chances are you already have a favorite text editor if you've been programming for a while.üAtom on Windows, Linux, MacOSüBrackets on Windows, Linux, MacOSüGedit on Linux and MacOSüEmacs and Emacs for OSX. Be prepared to do some

learning (in CC mode: mode for editing C, C++, Objective C, Java, Pike, and IDL code.)

üVim and MacVim

LINKS

Atom https://atom.ioBrackets http://brackets.io

Gedit https://wiki.gnome.org/Apps/Gedit

GNU Emacs https://www.gnu.org/software/emacs/üCC mode

https://www.gnu.org/software/emacs/manual/html_mono/ccmode.html

üWhen you open a .c file it automatically goes into CC mode

GEDIT

Open Gedit and click edit > preferences > view > bracket Matching > highlight matching bracketnext edit > preferences > editor > tab width 4 (u can change it)next edit > preferences > editor > automatic Indentation > enable automatic indentationand lastly, edit > preferences > syntax highlighting > highlight mode > C++From preferencesüDisplay line numbersüTab width = 4üEnable Automatic indentationüAutosave every 5 minutes

BRACKETS

ATOM

EMACS

Emacs and its derivatives are a family of text editors that are characterized by their extensibility. The original EMACS was written in 1976 by Richard Stallman and Guy L. Steele, Jr. as a set of Editor MACroS for the TECO editorThe most popular, and most ported, version of Emacs is GNU Emacs, which was created by Stallman for the GNU Project. (www.gnu.org).Emacs is, along with vi, one of the two main contenders in the traditional editor wars of Unix culture. Both are among the oldest application programs still in use.By June 1987 the project had accumulated and developed free software for an assembler, an almost finished portable optimizing C compiler (GCC), an editor (GNU Emacs), and various Unix utilities (such as ls, grep, awk, make and ld). Then GNU/LINUX (1992) was a complete operating system.

GNU/LINUX

GNU is a Unix-like operating system. That means it is a collection of many programs: applications, libraries, developer tools, even games. The development of GNU, started in January 1984, is known as the GNU Project. Many of the programs in GNU are released under the auspices of the GNU Project.The name “GNU” is a recursive acronym for “GNU's Not Unix.”

The program in a Unix-like system that allocates machine resources and talks to the hardware is called the “kernel”. GNU is typically used with a kernel called Linux.

EMACS BASIC COMMANDS

Basics http://www.ast.cam.ac.uk/~vasily/idl/emacs_commands_list.htmlüC-x C-f "find" file i.e. open/create a file in bufferüC-x C-s save the fileüC-x C-w write the text to an alternate nameüC-x b create/switch buffersüC-x C-b show buffer listüC-x k kill bufferüC-X C-c close down emacsüC-s Search forwardüC-x 2 split window verticallyüM-x compile Run a compiler asynchronously under Emacs,

with error messages going to the *compilation* buffer.

C = ControlM = Meta

SCREENSHOT

SCREENSHOT 2

WARNING

An IDE, or "Integrated Development Environment" will turn you stupid. They are the worst tools if you want to be a good programmer because they hide what's going on from you, and your job is to know what's going on.

They are useful if you're trying to get something done and the platform is designed around a particular IDE, but for learning to code C (and many other languages) they are pointless.

We will enjoy being stupid at the end of the course: after you will be allowed to use an IDE

CODE::BLOCKS HTTP://WWW.CODEBLOCKS.ORG

HELLO WORLD

#include <stdio.h>

int main (){

printf("Hello World\n");return 0;

}

2) Compile it: gcc -o test test.c

3) Then execute it: ./test

1) Write and save it to a test.c file

FROM BASH IN MAC OSX

PLEASE BE SURE TO BE ABLE TO WRITE CODE AND COMPILE ITAS SOON AS POSIBLE

(BEFORE NEXT WEEK)!!!!!!!!!!!

ATTENZIONE

NON correggerò nessun progetto sviluppato tramiteXcode, Code::Blocks, Netbeans, Eclipse

Allora prova di lavoratorio dovete saper compilare solo con gcc

SOLAMENTE GCC

EXAMPLE 1#include <stdio.h>int main(){

int firstNumber, secondNumber, sumOfTwoNumbers;

printf("Enter two integers: ");

// Two integers entered by user is stored using scanf() function

scanf("%d %d", &firstNumber, &secondNumber);

// sum of two numbers in stored in variable sumOfTwoNumberssumOfTwoNumbers = firstNumber + secondNumber;

// Displays sum printf("%d + %d = %d", firstNumber, secondNumber,

sumOfTwoNumbers);

return 0;}

Enter two integers: 121112 + 11 = 23

EXAMPLE 2#include <stdio.h>int main(){

int dividend, divisor, quotient, remainder;

printf("Enter dividend: ");scanf("%d", &dividend);

printf("Enter divisor: ");scanf("%d", &divisor);

// Computes quotientquotient = dividend/divisor;

// Computes remainderremainder = dividend%divisor;

printf("Quotient = %d\n",quotient);printf("Remainder = %d",remainder);

return 0;}

Enter dividend: 25Enter divisor: 4Quotient = 6Remainder = 1

EXAMPLE 3

#include <stdio.h>int main(){

int n1, n2;

printf("Enter two positive integers: ");scanf("%d %d",&n1,&n2);

while(n2!=0){

if(n1 > n2)n1 -= n2;

elsen2 -= n1;

}printf("GCD = %d\n",n1);

return 0;}

Enter two positive integers: 81153GCD = 9

SOFTWARE LICENSES

THE CHURCH OF EMACS

The Church of Emacs, formed by Richard Stallman, is a parody religion created for Emacs users.

While it refers to vi as the editor of the beast (vi-vi-vi being 6-6-6 in Roman numerals), it does not oppose the use of vi; rather, it calls proprietary software anathema. The Church of Emacs has its own newsgroup, alt.religion.emacs

Richard Stallman as St IGNUcius, a saint in the Church of Emacs

GNU PUBLIC GENERAL LICENSE

GPLv3 http://www.gnu.org/licenses/gpl-3.0.en.htmlThe Foundations of the GPLüNobody should be restricted by the software they use. There

are four freedoms that every user should have:üthe freedom to use the software for any purpose,üthe freedom to change the software to suit your needs,üthe freedom to share the software with your friends and

neighbors, andüthe freedom to share the changes you make.When a program offers users all of these freedoms, we call it free software.

SOME FAQ

Does the GPL allow me to sell copies of the program for money? üYes, the GPL allows everyone to do this. The right to sell

copies is part of the definition of free software. Except in one special situation, there is no limit on what price you can charge.

Does the GPL allow me to charge a fee for downloading the program from my distribution site?üYes. You can charge any fee you wish for distributing a copy

of the program. If you distribute binaries by download, you must provide “equivalent access” to download the source—therefore, the fee to download source may not be greater than the fee to download the binary.

SOME FAQ 2

Does the GPL allow me to distribute a modified or beta version under a nondisclosure agreement? üNo. The GPL says that your modified versions must carry all

the freedoms stated in the GPL. Thus, anyone who receives a copy of your version from you has the right to redistribute copies (modified or not) of that version. You may not distribute any version of the work on a more restrictive basis.

I want to get credit for my work. I want people to know what I wrote. Can I still get credit if I use the GPL?üYou can certainly get credit for the work. Part of releasing a

program under the GPL is writing a copyright notice in your own name (assuming you are the copyright holder). The GPL requires all copies to carry an appropriate copyright notice.

HOW TO USE IT FOR YOUR SW.

the process involves adding two elements to each source file of your program: üa copyright notice (such as “Copyright 1999 Terry Jones”),üa statement of copying permission, saying that the program

is distributed under the terms of the GNU General Public License (or the Lesser GPL).

You should also include a copy of the license itself somewhere in the distribution of your program. In GNU programs the license is usually in a file called COPYING.

It is very important for practical reasons to include contact information for how to reach you, perhaps in the README file, but this has nothing to do with the legal issues of applying the license.

EXAMPLE OF A PREAMBLEThe copying permission statement should come right after the copyright notices. For a one-file program, the statement (for the GPL) should look like this

/** test.c* Copyright (C) 2015 Francesco Santini** This program is free software: you can redistribute it and/or modify* it under the terms of the GNU General Public License as published by* the Free Software Foundation, either version 3 of the License, or* (at your option) any later version.

* This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License* along with this program. If not, see <http://www.gnu.org/licenses/>.*/