Compiling and Running a C Program in Unix

Similar documents
CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 0 ] About These Slides

CSCI 171 Chapter Outlines

Subject: Fundamental of Computer Programming 2068

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Kurt Schmidt. October 30, 2018

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

PRINCIPLES OF OPERATING SYSTEMS

2. Numbers In, Numbers Out

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

printf( Please enter another number: ); scanf( %d, &num2);

C: How to Program. Week /Mar/05

CS 61C: Great Ideas in Computer Architecture Introduction to C

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Topic 6: A Quick Intro To C

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Motivation was to facilitate development of systems software, especially OS development.

2. Numbers In, Numbers Out

Lecture 3: C Programm

1. The programming language C is more than 30 years old. True or False? (Circle your choice.)

Introduction to Programming Using Java (98-388)

CS 61c: Great Ideas in Computer Architecture

Lectures 5-6: Introduction to C

EC 413 Computer Organization

Short Notes of CS201

Motivation was to facilitate development of systems software, especially OS development.

ESC101N: Fundamentals of Computing End-sem st semester

Computers Programming Course 5. Iulian Năstac

CS201 - Introduction to Programming Glossary By

CS201 Some Important Definitions

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

The C language. Introductory course #1

OBJECTIVE QUESTIONS: Choose the correct alternative:

Lectures 5-6: Introduction to C

A Fast Review of C Essentials Part I

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

High Performance Programming Programming in C part 1

ET156 Introduction to C Programming

INTRODUCTION 1 AND REVIEW


Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Chapter 2 - Introduction to C Programming

Procedural programming with C

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Fundamental of Programming (C)

This exam is to be taken by yourself with closed books, closed notes, no calculators.

CS349/SE382 A1 C Programming Tutorial

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

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Dynamic memory allocation (malloc)

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

Approximately a Final Exam CPSC 206

JTSK Programming in C II C-Lab II. Lecture 3 & 4

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

CS 31: Intro to Systems C Programming. Kevin Webb Swarthmore College September 13, 2018

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

Programming in C - Part 2

Approximately a Test II CPSC 206

C Program Development and Debugging under Unix SEEM 3460

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

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

211: Computer Architecture Summer 2016

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

Computer Systems Principles. C Pointers

Type Definition. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

C Programming Review CSC 4320/6320

BLM2031 Structured Programming. Zeyneb KURT

High Performance Computing

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

OUTLINE. Review Functions Pointers Function calls Array Pointers Pointer Arrays Data Structures and dynamic Memory Function Pointers quicksort example

Introduction to C Language (M3-R )

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

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

Syntax and Variables

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Code No: R Set No. 1

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

First of all, it is a variable, just like other variables you studied

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

COMP 2355 Introduction to Systems Programming

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

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

Introduction to C. Systems Programming Concepts

C-LANGUAGE CURRICULAM

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Transcription:

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 95 ] Compiling and Running a C Program in Unix Simple scenario in which your program is in a single file: Suppose you want to name your program test. 1. edit your source code in a file called test.c 2. compile your source code: gcc -o test test.c The -o test part of the command creates an executable file called test. (The o stands for output.) If you left off the -o test, the executable would be in a file called a.out. 3. if compile time errors are discovered, thengotostep1 4. run yourprogramsimplybytypingtest (or a.out) 5. if run time errors are discovered, thengotostep1

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 96 ] Structure of a C Program A C program is a list of function definitions. A function is the C equivalent of a method in Java. Every C program must contain one, and only one, function called main, where execution begins. Functions are free-standing (not contained inside any other entity). #include <stdio.h> int main() { /* this is a comment */ printf("hello, world.\n"); } æ The #include tells the compiler to include the header file stdio.h, which contains various declarations relating to standard input-output. æ For now, ignore the return value of main. æ printf is the function that prints to the screen. æ The \n is the newline character. æ Comments are delimited with /*... */. Remember to end your comment!

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 97 ] A Useful Library See the Reek book (especially Chapter 16) for a description of what you can do with built-in libraries. In addition to stdio.h, æ stdlib.h lets you use functions for, e.g., arithmetic random numbers ways to terminate execution sorting and searching æ math.h provides more advanced math functions (e.g., trigonometry) æ string.h has string manipulation functions

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 98 ] Printf The function printf is used to print the standard output (screen): æ It can take a variable number of arguments. æ The first argument must be a string. æ The first argument might have embedded in it some holes that indicate they are to be filled with data. æ A hole is indicated by a percent sign (%) followed by a code, which indicates what type of data is to be written. æ Following the first argument is a series of additional arguments that supply the data for the holes. Example: printf("name = %s, age = %i, fraction = %f", "Fred", 30,.333 ); Output is: Name = Fred, age = 30, fraction = 0.333000

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 99 ] Variables and Arithmetic Expressions The main numeric data types that we will use are: æ char æ int æ double Variables are declared and manipulated in arithmetic expressions pretty much as in Java. For instance, #include <stdio.h> int main() { int a = 3; int b = 4; int c = a*b - b/3; printf("answer is %i\n", c); } However, in C, all variable declarations of a block must precede all statements.

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 100] Reading from the Keyboard The function scanf reads in data from the keyboard. int num1, num2; scanf("%i %i", &num1, &num2); æ scanf takes a variable number of arguments æ The first argument is a string that consists of a series of holes. æ Each hole is indicated by a percent sign (%) followed by a code indicating type of data to be read. æ After the first argument is a series of arguments, corresponding to the holes in the first argument. æ The subsequent arguments must each be preceded by an ampersand! (Related to pointers.) æ The code for an int is %i; the code for a double is %lf (not %d,asforprintf). When you run this program, it will wait for you to enter two integers, and then continue. The integers can be on the same line separated by a space, or on two lines.

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 101] Functions Functions in C are pretty much like methods in Java (dealing only with primitive types). Example: #include < stdio.h > double times2 (double x) { x = 2*x; return x; } main () { double y = 301.4; printf("original value is %f; final value is %f.\n", y, times2(y)); } æ Functions must be declared before they are invoked. Requires careful ordering or use of prototypes. æ As in Java, parameters are passed by value. In the example, the value of y, 301.4, is copied into x. The change to x does not affect y. æ As in Java, if the function does not return any value, use void as the return type. æ Parameters and local variables of functions behave like those in Java.

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 102] Recursive Functions Recursion is essentially the same as in Java. The only difference is if you have mutually recursive functions, also called indirect recursion: for instance, if function A calls function B, while B calls A. Then you have a problem with the requirement that functions be defined before they are used. You can get around this problem with function prototypes, which just give the signature of the function (return type, function name, and list of arguments, without the code body). You first list the prototype(s), then you can give the actual function definitions.

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 103] Global Variables and Constants C also provides global variables. æ A global variable is defined outside all functions, including main. æ A global variable can be used inside any function in the same file. Generally, global variables that can be changed are frowned upon, as contributing to errors. However, global variables are very appropriate for constants. Constants are defined using macros: #include <stdio.h> #define INTEREST_RATE.06 main () { double principal = 10000.; printf("amount of interest is %f.\n", INTEREST_RATE * principal); } The #define defines a macro, meaning that the text INTEREST RATE is to be substituted with the text.06 at compile time.

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 104] Boolean Expressions æ The operators to compare two values are the same as in Java: ==,!=, <,etc. æ However, instead of returning a boolean value, they return either 0 (means false) or 1 (means true). æ Actually, C interprets any non-zero integer as true. Thus the analog in C of a boolean expression in Java is any expression that produces an integer. It is interpreted as producing a truth value, by letting 0 mean false and a non-zero value mean true. As in Java, boolean expressions can be operated on with && (conditional and), (conditional or), and! (negation). Some examples: æ (10 == 3) evaluates to 0 (false), since 10 does not equal 3 æ!(10 == 3) evaluates to 1 (true), since 10 6=3 æ!( (x < 4) (y == 5) ) :ifxis 10 and y is 5, then this evaluates to 0 (false), since y is 5

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 105] If Statements and Loops Given the preceding interpretation of boolean expression, the following statements are the same in C as in Java: æ if æ if-else æ while æ for Since Boolean expressions are essentially integers, you can have a for statement like this in C: for (int count = 99; count; count--) {... } æ count is initialized to 99; æ the loop is executed as long as the expression count is non-zero (remember that non-zero means true, in the context of a boolean expression); æ count is decremented by 1 at each iteration. æ This loop is executed 99 times.

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 106] Switch C has a switch statement that is like that in Java: switch ( <integer-expression> ) { case <integer-constant-1> : <statements-for-case-1> break; case <integer-constant-2> : <statements-for-case-2> break;... default : <default-statements> } Don t forget the break statements! The integer expression must produce a value belonging to any of the integral data types (various size integers and characters).

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 107] Enumerations This is something neat that Java does not have. An enumeration is a way to give mnemonic names to a group of related integer constants. For instance, suppose you need to have some codes in your program to indicate whether a library book is checked in, checked out, or lost. Intead of #define CHECKED_IN 0 #define CHECKED_OUT 1 #define LOST 2 you can use an enumeration declaration: enum {CHECKED_IN, CHECKED_OUT, LOST}; The names in the list are matched up with 0, 1, 2,... If you want to give specific values, you can do that too: enum { Jan = 1, Feb, Mar, Apr };

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 108] Using an Enumeration in a Switch Statement int status; /* some code to give status a value */ switch (status) { case CHECKED_IN : /* handle a checked in book */ break; case CHECKED_OUT : /* handle a checked out book */ break; case LOST : /* handle a lost book */ break; }

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 109] Enumeration Data Type You can give a name to an enumeration and thus create an enumeration data type. The syntax is: enum <name-of-enum-type> <actual enumeration> For example: enum book_status { CHECKED_IN, CHECKED_OUT, LOST }; Why bother to do this? Because you can then create variables of the enumeration type: enum book_status status; enum book status is the type, analogous to int, and status is the variable name. And why bother to do this? To get compiler support to help make sure these variables only take on prescribed values. For instance, the following will be allowed: status = LOST; but the following will not: status = 5; In fact, some compilers will not even allow status = 0;

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 110] Type Synonyms The enumeration type is our first example of a user defined type in C. It s rather unpleasant to have to carry around the word enum all the time for this type. Instead, you can give a name to this type you have created, and subsequently just use that type without having to keep repeating enum. For example: enum book_status { CHECKED_IN, CHECKED_OUT, LOST }; typedef enum book_status BookStatus;... BookStatus status; The typedef statement causes BookStatus to be a synonym for enum book status.

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 111] Structures C also gives you a way to create more general types of your own, as structures These are essentially like objects in Java, if you just consider the instance variables. A structure groups together related data items that can be of different types. The syntax to define a structure is: struct <structure-name> { <first-variable-declaration>; <second-variable-declaration>;... }; /* don t forget this semi-colon! */ For instance: struct student { int age; double grade_point; }; Then you can declare a variable whose type is the student structure: struct student stu;

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 112] Storage on the Stack The statement struct student stu; causes the entire stu structure to be stored stack: stack on the stu (holds a float) (holds an int) grade_point age

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 113] Using typedef with Structures When using the structure type, you have to carry along the word struct. To avoid this, you can use a typedef to declare a user-defined type, i.e., to provide a name that is a synonym for struct student. For instance, struct student { int age; double grade_point; }; typedef struct student Student; A more concise way to do this is: typedef struct { int age; double grade_point; } Student; Now you can create a Student variable: Student stu;

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 114] Using a Structure You can access the pieces of a structure using dot notation (analogous to accessing instance variables of an object in Java) : stu.age = 20; stu.grade_point = 3.0; sum_ages = sum_ages + stu.age; You can also have the entire struct on either the left or the right side of the assignment operator: Student stu1, stu2; stu2.age = 21; stu2.grade_point = 3.5; stu1 = stu2; The result is like assigning primitive types in Java! The entire contents of stu2 are copied to stu1.

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 115] Figure for Copying a Structure stu2 3.5 21 grade_point age stu2 3.5 21 grade_point age stu1 garbage garbage grade_point age stu1 3.5 21 grade_point age

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 151] String Manipulation Functions Example typedef struct { char* name; int age; double grade_point; } Student; Student* constructstudent(int age, double gpa) { char inputbuffer[100]; /* read name into this */ Student* sptr; sptr = (Student*) malloc(sizeof(student)); sptr->age = age; sptr->grade_point = gpa; /* here s the new part: */ printf("enter student s name: "); scanf("%s", inputbuffer); /* allocate just enough space for the name */ sptr->name = (char*) malloc ( (strlen (inputbuffer) + 1)*sizeof(char) ); /* copy name into new space */ strcpy (sptr->name, inputbuffer); return sptr; } When constructor returns, inputbuffer goes away. Space allocated for Student object is an int,a double and just enough space for the actual name.