CSI 333 Lecture 2 Introduction to C: Part I 2 1 / 16

Similar documents
The C Programming Language course syllabus associate level

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Sources: On the Web: Slides will be available on:

Informatica e Sistemi in Tempo Reale

The programming language C. sws1 1

Computer Programming Tutorial

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

CP Lab 2: Writing programs for simple arithmetic problems

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

Tutorial on C Language Programming

So far we have considered only numeric processing, i.e. processing of numeric data represented

Pemrograman Dasar. Basic Elements Of Java

Introduction to Python

The C Programming Language

J a v a Quiz (Unit 3, Test 0 Practice)

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

Java Basics: Data Types, Variables, and Loops

How To Write Portable Programs In C

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

C++ Programming Language

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

Module 816. File Management in C. M. Campbell 1993 Deakin University

Introduction to Java

PROG0101 Fundamentals of Programming PROG0101 FUNDAMENTALS OF PROGRAMMING. Chapter 3 Algorithms

Object Oriented Software Design

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Programming and Software Development CTAG Alignments

Time Limit: X Flags: -std=gnu99 -w -O2 -fomitframe-pointer. Time Limit: X. Flags: -std=c++0x -w -O2 -fomit-frame-pointer - lm

Object Oriented Software Design

5 Arrays and Pointers

VB.NET Programming Fundamentals

C Programming Tutorial

Fundamentals of Programming

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

C PROGRAMMING FOR MATHEMATICAL COMPUTING

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

An Incomplete C++ Primer. University of Wyoming MA 5310

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Moving from CS 61A Scheme to CS 61B Java

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Statements and Control Flow

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Lab Experience 17. Programming Language Translation

Introduction to Data Structures

Management Information Systems 260 Web Programming Fall 2006 (CRN: 42459)

C++ INTERVIEW QUESTIONS

6.s096. Introduction to C and C++

Computational Mathematics with Python

COS 217: Introduction to Programming Systems

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

#820 Computer Programming 1A

arrays C Programming Language - Arrays

Introduction to Java. CS 3: Computer Programming in Java

CSI 333 Lecture 1 Number Systems

CSI 402 Lecture 13 (Unix Process Related System Calls) 13 1 / 17

CS 253: Intro to Systems Programming

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Java (12 Weeks) Introduction to Java Programming Language

Programming Languages CIS 443

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 6 Program Control

Computer Programming I & II*

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Computer Programming I

Computational Mathematics with Python

El Dorado Union High School District Educational Services

System Calls and Standard I/O

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Basic Programming and PC Skills: Basic Programming and PC Skills:

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Simple C Programs. Goals for this Lecture. Help you learn about:

How To Use The C Preprocessor

ELEC3730 Embedded Systems Lecture 1: Introduction and C Essentials

PHP Debugging. Draft: March 19, Christopher Vickery

C++ Outline. cout << "Enter two integers: "; int x, y; cin >> x >> y; cout << "The sum is: " << x + y << \n ;

C Examples! Jennifer Rexford!

Semantic Analysis: Types and Type Checking

COSC282 BIG DATA ANALYTICS FALL 2015 LECTURE 2 - SEP 9

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

1 Description of The Simpletron

1 The Java Virtual Machine

VHDL Test Bench Tutorial

Project 2: Bejeweled

RARITAN VALLEY COMMUNITY COLLEGE ACADEMIC COURSE OUTLINE. CISY 105 Foundations of Computer Science

public static void main(string[] args) { System.out.println("hello, world"); } }

Programming in C an introduction

Top 72 Perl Interview Questions and Answers

Lecture 7: Programming for the Arduino

Transcription:

CSI 333 Lecture 2 Introduction to C: Part I 2 1 / 16

Basics of C Remark: Skim Chapters 1 through 6 of Deitel & Deitel. You will notice the following: C is (more or less) a subset of Java. (So, you are not really learning a new language.) The differences between C and Java are not difficult to master. Why learn C now? C is usually preferred over Java in implementing systems software because: C compilers generating good quality machine code are readily available. Size of executable created from a C source is typically smaller. 2 2 / 16

Basics of C (continued) Parts common to C and Java: Primitive data types (int, char, float and double). Arrays. Arithmetic and logical operators and expressions. Pre and post increment/decrement operators. Assignment statement. Control statements (if, switch, etc.) Loops (for, while, etc.). Functions and return statement. Note: The above list is not exhaustive. Main Difference C is not an object-oriented language. 2 3 / 16

A Simple C Program (Example 1) #include <stdio.h> int main(void) { printf("hello World!\n"); return 0; } /* End of main. */ Notes about the program: Every C program must have a function named main. In the above example, The main function returns a value of type int. The function does not have any parameters. 2 4 / 16

Notes on Example 1 (continued) <stdio.h>: printf). Header file needed to use I/O functions (such as Each C program can use three standard I/O devices: stdin, stdout and stderr. The function printf writes to stdout. The symbol \n denotes the newline character. The return statement is followed by a value. (In general, the value may be an expression.) Comments are enclosed within "/*" and "*/". (Most C compilers will also accept "//".) 2 5 / 16

Compiling and Executing C programs 1 Suppose file hello.c contains the program of Example 1. 2 Compile the program using the following Unix command: gcc -c hello.c 3 If the compilation produces syntax errors, edit the file to correct the errors and compile again. (Repeat steps 2 and 3 until all syntax errors have been fixed.) 4 Create the executable version of the program using the following Unix command: gcc hello.c The executable version is created in a file called a.out. 5 Execute the program using the following Unix command: a.out 2 6 / 16

Additional Notes about Compiling C programs Compilation Tip To compile a C program, it is better to use the following Unix command: gcc -c -g -Wall hello.c This identifies syntax errors and also produces warnings that are useful in identifying potential problems. The -g part is useful for debugging (to be seen later). The command gcc hello.c -o prog.out produces the executable version in prog.out instead of a.out. Program Example 2: See Handout 2.1. 2 7 / 16

Notes on Example 2 Function scanf (from <stdio.h>) is used to read values from stdin. The format "%d" indicates that the value to be read is a decimal integer. Very Important: The scanf function needs addresses of variables (i.e., pointers) to read values. (The addresses of variables x and y are denoted by &x and &y respectively.) Bus Error Leaving out the & in a call to scanf often causes a bus error when the program is executed. The function printf also uses formats. In general, values to be printed may be expressions. 2 8 / 16

Additional notes about C The primitive data types provided in C are: char, int, float, double. The int data type can also use the modifiers short, long or unsigned. Example: long int val; unsigned long p; short x; The number of bytes needed for the above data types is machine dependent. C does not have the byte or boolean types of Java. In C, the value 0 represents false and any nonzero value represents true. 2 9 / 16

Additional notes about C (continued) Example: int x, y;.. if (x-y) { /* Here, the value of x-y is nonzero. */.. } else { /* Here, the value of x-y is zero. */.. } 2 10 / 16

Additional notes about C (continued) Caution Comments using /* and */ can t be nested. For example, the following causes a syntax error. (Why?) /* -- A line of comment /* -- Invalid nesting -- */ */ Commenting out sections of C code must be done with care. The safest way to comment out a section of C code is: #if 0 --- code to be commented out -- #endif Note: The #if and #endif are part of the macro facility supported by C. (More on macros later.) 2 11 / 16

Additional notes about C (continued) Symbolic constants can be introduced through the #define statement. For example, #define SIZE 100 #define MAX_LEN 20 int values[size]; char str[max_len]; Note: The #define statement is also part of the macro facility supported by C. Program Example 3: See Handout 2.2. 2 12 / 16

Notes on Program Example 3 Note the definition of symbolic constants using #define. Formats "%d" and "%f" are used in the call to function printf: Format "%4d" means that the value to be printed is a decimal integer and the printing width must be at least 4 characters. Format "%6.1f" means that the value to be printed is of type float and the printing width must be at least 6 characters with one character after the decimal point. 2 13 / 16

Notes on printf Prints output to stdout. The first parameter to printf is a string specifying format for printing values. (e.g. %d for int, %f for float, etc.) Values to be printed appear after the format string separated by commas. Value to be printed may be an expression. (The expression will be evaluated and its value will be printed.) 2 14 / 16

Notes on scanf Reads input from stdin. The first parameter to scanf is a string specifying format for reading values. Addresses of (i.e., pointers to) variables whose values are to be read follow the format string. Recall: Leaving out the & in a call to scanf often causes bus error at run time. Remark: For a complete list of format specifications (to be used with printf or scanf), see Chapter 9 of Deitel & Deitel. 2 15 / 16

Suggested Exercises Study the handouts discussed in this lecture. Make sure that you understand how to use the standard I/O functions scanf and printf. Go through the program examples in Chapters 1 through 5 of Deitel & Deitel. (This will allow you to become familiar with the syntax of C. You will also notice the similarities between C and Java.) Try writing your favorite short pieces of code (e.g. sequential search, simple sorting algorithms) in C. Compile and execute some C programs in Unix. 2 16 / 16