Introduction: The Unix shell and C programming

Similar documents
Warm-up sheet: Programming in C

COMP s1 Lecture 1

FALL 2017 CSCI 304 LAB1 (Due on Sep-19, 11:59:59pm)

Intermediate Programming, Spring Misha Kazhdan

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

Exercise 1: Basic Tools

Lecture 1. A. Sahu and S. V. Rao. Indian Institute of Technology Guwahati

1. The Mac Environment in SIE 1222

Lab 1 Introduction to UNIX and C

1. The Mac Environment in Sierra Hall 1242

3/13/2012. ESc101: Introduction to Computers and Programming Languages

Functions. Arash Rafiey. September 26, 2017

CMPT 300. Operating Systems. Brief Intro to UNIX and C

Computers and Computation. The Modern Computer. The Operating System. The Operating System

SU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar)

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

CMPUT 201: Practical Programming Methodology. Guohui Lin Department of Computing Science University of Alberta September 2018

Check the entries in the home directory again with an ls command and then change to the java directory:

Lab 1 Introduction to UNIX and C

Programming and Data Structure Laboratory (CS13002)

CpSc 1111 Lab 1 Introduction to Unix Systems, Editors, and C

Basic Unix Commands. CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

PDS Lab Section 16 Autumn Tutorial 1. Unix Commands pwd The pwd command displays the full pathname of the current directory.

CSC111 Computer Science II

Intro to Linux & Command Line

Reviewing gcc, make, gdb, and Linux Editors 1

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection

Saint Louis University. Intro to Linux and C. CSCI 2400/ ECE 3217: Computer Architecture. Instructors: David Ferry

Introduction to Supercomputing

Using the Unix system. UNIX Introduction

Programming Tools. Venkatanatha Sarma Y. Lecture delivered by: Assistant Professor MSRSAS-Bangalore

Scientific Computing 1 Tutorial 1a 10/18/2012 Solution

Introduction to Linux

Practical Session 0 Introduction to Linux

Programming Studio #1 ECE 190

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

EL2310 Scientific Programming

Introduction to C An overview of the programming language C, syntax, data types and input/output

Helpful Tips for Labs. CS140, Spring 2015

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Intermediate Programming, Spring 2017*

Midterm Exam. CSCI 2132: Software Development. March 4, Marks. Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5)

ENCM 339 Fall 2017: Editing and Running Programs in the Lab

15213 Recitation Section C

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

C Programming. The C Preprocessor and Some Advanced Topics. Learn More about #define. Define a macro name Create function-like macros.

Beyond this course. Machine code. Readings: CP:AMA 2.1, 15.4

The Compilation Process

Getting started with UNIX/Linux for G51PRG and G51CSA

Getting started with Hugs on Linux

CpSc 1111 Lab 9 2-D Arrays

CSE 303 Lecture 8. Intro to C programming

Lecture 01 - Working with Linux Servers and Git

Laboratory 1 Semester 1 11/12

CSE 351. Introduction & Course Tools

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

The C language. Introductory course #1

Introduction to Linux. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

CS 261 Recitation 1 Compiling C on UNIX

Programming Studio #1 ECE 190

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

Refresher workshop in programming for polytechnic graduates General Java Program Compilation Guide

You should see something like this, called the prompt :

Technical Questions. Q 1) What are the key features in C programming language?

TNM093 Practical Data Visualization and Virtual Reality Laboratory Platform

CSCE 212H, Spring 2008, Matthews Lab Assignment 1: Representation of Integers Assigned: January 17 Due: January 22

Computer Programming: Skills & Concepts (CP) Variables and ints

We first learn one useful option of gcc. Copy the following C source file to your

AMCAT Automata Coding Sample Questions And Answers

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Physics 306 Computing Lab 1: Hello, World!

CS19001/CS19002 Programming and Data Structures Lab Autumn/Spring Semester. Introduction. Abhijit Das. January 4, 2015

Programming for Electrical and Computer Engineers. Loops

Getting started with Hugs on Linux

Tutorial 1: Unix Basics

Systems Programming and Computer Architecture ( ) Exercise Session 01 Data Lab

Introduction to Linux Environment. Yun-Wen Chen

CS101 Linux Shell Handout

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

Chapter 11 Introduction to Programming in C

Unix Tools / Command Line

Linux File System and Basic Commands

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

Mills HPC Tutorial Series. Linux Basics I

Introduction to Algorithms and Programming I Lab. Exercises #1 Solution

The C standard library

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

Computational Modelling 102 (Scientific Programming) Tutorials

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

Creating, Compiling and Executing

Introduction to Linux

Programming Studio #9 ECE 190

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1 Lecture 7. COMP s1. Before we begin. Strings. Programming Fundamentals. Overview. Andrew Bennett

Transcription:

Introduction: The Unix shell and C programming 1DT048: Programming for Beginners Uppsala University June 11, 2014 You ll be working with the assignments in the Unix labs. If you are new to Unix or working from a terminal, the following table can be used as a quick reference. There are also many great resources online if you want to learn more. man <cmd> Shows a manual for the supplied command ls List files and directories in current directory pwd Show the working directory cd <dir> Go to the given directory cd.. Go back one directory mv <file1> <file2> Move file1 to file2 rm <file> Remove a file rm -r <dir> Remove a directory and all files within it. Be careful, there is no undo button! cp <file1> <file2> Copy file1 to file2 mkdir <dir> Create a directory Ctrl+C Key combination to abort most commands <command> & Run a command in the background gcc file.c -o output Compile file.c into executable file output emacs file.c & Open the editor emacs and load file.c./program Run the executable file program in the current dir We will be using the GNU C Compiler (gcc). The manual for the compiler is quite long (15000+ lines), so don t worry about learning all of its features. To get you started, we go over the creation, compilation and execution of an example program. First, we create a new directory to work in and step into that directory: ~]$ mkdir example ~]$ cd example ~/example]$ Then we create a C program file called example.c 1

~/example]$ emacs example.c The file is edited to look as shown below: / Author : Jonas Flodin / p r i n t f ( This t e x t i s p r i n t e d to the s c r e e n \n ) ; The file is saved by pressing Ctrl+X followed by Ctrl+S. Then we exit emacs by pressing Ctrl+X followed by Ctrl+C. (If you accidentally press some other command, you can abort it by pressing Ctrl+G). It should be noted that there are many editors other than emacs which you may prefer; vim, pico, gedit and nedit to name a few. You are of course welcome to use any editor of your choice. We compile the program into an executable using gcc: ~/example]$ gcc -Wall example.c -o executable The flag -Wall tells gcc to warn us about possible errors or design flaws that it can discover. It is a good habit to use this flag. You may also consider the -std=c99 flag, which enables some newer additions to the C language, e.g., declaration of variables in the for loop header. Finally we list the files to see that some output has been produced and then we run the executable file. ~/example]$ ls example.c executable ~/example]$./executable This text is printed to the screen ~/example]$ Now we ve created and executed a program. Exercises Now that we know how to create and run a program, we move on to the exercises. The solutions to the exercises can be found at the end of this document, but we encourage you to try to solve them without looking at the solution first. Please refer to the lecture slides for more information on C programming. Exercise 1 Output 2

In the introductory program we include the library stdio.h, which contains the function printf. This function is used to produce output in the form of characters that are printed in the terminal. In its simplest form, the function is called with a string as argument: printf("this text is printed to the screen\n"); That is great, but we want our programs to output more than just the fixed strings that the programmer writes in the program. To print the contents of variables, we add format specifiers to the string and add the variables we want to print as arguments: int number; char letter; printf("%d is an integer and %c is a character\n", number, letter); Different types of variables have different specifiers, all starting with a percentage sign. Common specifiers are %d for integers, %f for floats, %c for characters and %s for strings. To output a percentage sign, we use %%. Write a function that outputs: a) The string: One half is 50% b) two integers and their difference. c) two floats and the result of dividing one with the other Write a main function that calls your other functions. Example output: [.../intro]$./e1 One half is 50% The difference between 10 and 3 is 7 1.000000 / 3.000000 is 0.333333 [.../intro]$ Exercise 2 Input For input we use the function scanf, also from the library stdio.h. The scanf function takes a format string followed by references to where the input should be stored. Example that reads an integer to a variable: int number; scanf("%d", &number); Notice that the & character in front of the varable name. It means that the variable is passed as reference to scanf. It allows scanf to update the value of the variable. If & is not there, the program would likely crash at that point. When reading a string, the & sign can be omitted: 3

char my_variable[100]; scanf("%s", &my_variable); Write functions that: a) asks for two integers and outputs them and their sum. b) asks for two floats and outputs their product. c) asks for a word and prints it twice on the same row. Write a main function that calls your other functions. Example output: [.../intro]$./e2 Give two integers: 12 5 You entered 12 and 5, their sum is: 17 Give two floats: 3.14 2 You entered 3.140000 and 2.000000, their product is: 6.280000 Give a word: Yey! Yey! Yey! [.../intro]$ Exercise 3 Conditionals If-else statements are used to make a program behave differently depending on the program state or user input. As an example, one can use if-statements to make sure that input is sane before performing an operation int a; int b;... if(b == 0){ printf("error: Divide by zero!\n"); // Code for error handling.... else{ printf("division evaluates to: %d\n", a/b); Write functions that: a) ask for an integer and output whether the entered number is zero or not. b) ask for two floats and outputs the largest of the inputs 4

c) ask for an integer and, if the number is divisible by two, divides it by two, otherwise multiplies it by three and output the result. Here, the modulo operator % is useful. d) ask for three integers and output whether any of them are equal. Use only one if-else-statement Write a main function that calls your other functions. Example output: [.../intro]$./ex3 Give an integer: 12 The number you entered does not equal zero Give two floats: 13.4 20 20.000000 is the largest Give an integer: 14 Result is: 7 Give three integers: 1 13 1 Some numbers are equal [.../intro]$./ex3 Give an integer: 0 The number you entered equals zero Give two floats: 13.2-150 13.200000 is the largest Give an integer: 7 Result is: 21 Give three integers: 2 5 13 All are unique [.../intro]$ Exercise 4 Loops Loops are used to execute a statement or a block of code multiple times. A loop will continue to execute as long as the loop condition is satisfied. These two example loops will print the numbers 1 to 10 on one line and then 11 to 20 on the next line: int i,j; i = 1; while(i < 11){ printf("%d ", i); i=i+1; printf("\n"); for(j=11;j<=20;j++){ printf("%d ", j); 5

printf("\n"); Write functions that: a) print all even numbers between 0 and 40. b) print all the numbers between 1 and 100, with 10 numbers on each line. Use two for loops. All columns should be aligned. c) ask for a number than prints the number squared. This repeats until the 0 is entered. Write a main function that calls your other functions. Example output: [.../intro]$./ex4 Even numbers between 0 and 40: 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 Numbers 1 to 100: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 Give a number: 2 The square of 2 is 4 Give a number: 5 The square of 5 is 25 Give a number: 9 The square of 9 is 81 Give a number: 0 You entered zero. [.../intro]$ Exercise 5 Functions Functions are a great way to make code reusable, improve the structure of the code and isolate errors. Write functions that: a) take two floats as argument and returns the minimum of those. 6

b) take four floats as argument and returns the minimum. Make use of the function defined in a). c) are the same as in a) and b), but returns the maximum. d) take four floats as argument and returns their sum. Write a main function that asks the user for four floats and then outputs the minimum, maximum, their sum and mean value. Use the functions from a) - d) to implement this. Example output: [.../intro]$./ex5 Give four floats: 10.0-2.3 13.2 20.4 min: -2.300000 max: 20.400000 sum: 41.299999 mean: 10.325000 [.../intro]$ Solutions Exercise 1: void f u n c a ( ) { p r i n t f ( One h a l f i s 50%%\n ) ; void f u n c b ( ) { int a =10; int b=3; p r i n t f ( The d i f f e r e n c e between %d and %d i s %d\n, a, b, a b ) ; void f u n c c ( ) { f l o a t a =1.0; f l o a t b =3.0; p r i n t f ( %f / %f i s %f \n, a, b, a/b ) ; f u n c a ( ) ; f u n c b ( ) ; f u n c c ( ) ; Exercise 2: void f u n c a ( ) { 7

int a, b ; p r i n t f ( Give two i n t e g e r s : ) ; s c a n f ( %d %d,&a,&b ) ; p r i n t f ( You e n t e r e d %d and %d, t h e i r sum i s : %d\n, a, b, a+b ) ; void f u n c b ( ) { f l o a t a, b ; p r i n t f ( Give two f l o a t s : ) ; s c a n f ( %f %f,&a,&b ) ; p r i n t f ( You e n t e r e d %f and %f, t h e i r product i s : %f \n, a, b, a b ) ; void f u n c c ( ) { char word [ 1 0 0 ] ; p r i n t f ( Give a word : ) ; s c a n f ( %s, word ) ; p r i n t f ( %s %s \n, word, word ) ; f u n c a ( ) ; f u n c b ( ) ; f u n c c ( ) ; Exercise 3: void f u n c a ( ) { int number ; p r i n t f ( Give an i n t e g e r : ) ; s c a n f ( %d, &number ) ; i f ( number == 0){ p r i n t f ( The number you e n t e r e d e q u a l s z e r o \n ) ; else { p r i n t f ( The number you e n t e r e d does not equal z e r o \n ) ; void f u n c b ( ) { f l o a t a, b ; p r i n t f ( Give two f l o a t s : ) ; s c a n f ( %f %f, &a, &b ) ; i f ( a>b ){ p r i n t f ( %f i s the l a r g e s t \n, a ) ; else { p r i n t f ( %f i s the l a r g e s t \n, b ) ; void f u n c b a l t e r n a t e ( ) { f l o a t a, b ; p r i n t f ( Give two f l o a t s : ) ; s c a n f ( %f %f, &a, &b ) ; p r i n t f ( %f i s the l a r g e s t \n, a>b?a : b ) ; void f u n c c ( ) { 8

int number ; int r e s u l t ; p r i n t f ( Give an i n t e g e r : ) ; s c a n f ( %d, &number ) ; i f ( number%2 == 0){ r e s u l t = number / 2 ; else { r e s u l t = number 3 ; p r i n t f ( Result i s : %d\n, r e s u l t ) ; void f u n c d ( ) { int a, b, c ; p r i n t f ( Give t h r e e i n t e g e r s : ) ; s c a n f ( %d %d %d,&a,&b,& c ) ; i f ( a==b a==c b==c ){ p r i n t f ( Some numbers are equal \n ) ; else { p r i n t f ( A l l are unique \n ) ; f u n c a ( ) ; f u n c b ( ) ; f u n c c ( ) ; f u n c d ( ) ; Exercise 4: void f u n c a ( ) { int i ; p r i n t f ( Even numbers between 0 and 4 0 : \ n ) ; for ( i =0; i <=40; i ++){ i f ( i %2==0){ p r i n t f ( %d, i ) ; p r i n t f ( \n ) ; void f u n c b ( ) { int i ; int j ; p r i n t f ( Numbers 1 to 1 0 0 : \ n ) ; for ( i =0; i <10; i ++){ for ( j =1; j <11; j ++){ p r i n t f ( %2d, i 10 + j ) ; p r i n t f ( \n ) ; void f u n c c ( ) { int number ; p r i n t f ( Give a number : ) ; 9

s c a n f ( %d, &number ) ; while ( number!= 0){ p r i n t f ( The square o f %d i s %d\n, number, number number ) ; p r i n t f ( Give a number : ) ; s c a n f ( %d, &number ) ; p r i n t f ( You e n t e r e d z e r o. \ n ) ; f u n c a ( ) ; f u n c b ( ) ; f u n c c ( ) ; Exercise 5: f l o a t min2 ( f l o a t a, f l o a t b ){ i f ( a<b ) return a ; return b ; f l o a t min4 ( f l o a t a, f l o a t b, f l o a t c, f l o a t d ){ return min2 ( min2 ( a, b ), min2 ( c, d ) ) ; f l o a t max2( f l o a t a, f l o a t b ){ i f ( a>b ) return a ; return b ; f l o a t max4( f l o a t a, f l o a t b, f l o a t c, f l o a t d ){ return max2(max2( a, b ), max2( c, d ) ) ; f l o a t sum4 ( f l o a t a, f l o a t b, f l o a t c, f l o a t d ){ return a+b+c+d ; f l o a t a, b, c, d ; p r i n t f ( Give f o u r f l o a t s : ) ; s c a n f ( %f%f%f%f,&a,&b,&c,&d ) ; p r i n t f ( min : %f \n, min4 ( a, b, c, d ) ) ; p r i n t f ( max : %f \n, max4( a, b, c, d ) ) ; p r i n t f ( sum : %f \n, sum4 ( a, b, c, d ) ) ; p r i n t f ( mean : %f \n, sum4 ( a, b, c, d ) / 4 ) ; 10