C Overview Fall 2015 Jinkyu Jeong

Similar documents
C Overview Fall 2014 Jinkyu Jeong

The detail of sea.c [1] #include <stdio.h> The preprocessor inserts the header file stdio.h at the point.

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

CC112 Structured Programming

Data Type Fall 2014 Jinkyu Jeong

C programming basics T3-1 -

ET156 Introduction to C Programming

Input/Output and the Operating Systems

CSCI 2132 Software Development. Lecture 8: Introduction to C

BSM540 Basics of C Language

ET156 Introduction to C Programming

Chapter 11 Introduction to Programming in C

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

COMP s1 Lecture 1

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

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

C Program Structures

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

Chapter 11 Introduction to Programming in C

A Crash Course in C. Steven Reeves

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

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

Fundamental of Programming (C)

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Chapter 11 Introduction to Programming in C

1. The keyword main in C language is used for

Structured programming

Chapter 11 Introduction to Programming in C

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

CSI 402 Lecture 2 Working with Files (Text and Binary)

Introduction to Computing Lecture 03: Basic input / output operations

Fundamental of Programming (C)

Program Organization and Comments

Informatica e Sistemi in Tempo Reale

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

Fundamentals of Programming

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

Introduction to Computing Lecture 01: Introduction to C

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

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

File Input/Output. Similarly, for reading from files and writing to files, we'll use the functions

Array Initialization

Dynamic memory allocation

System Software Experiment 1 Lecture 7

Approximately a Final Exam CPSC 206

Programming and Data Structures

File IO and command line input CSE 2451

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

today cs3157-fall2002-sklar-lect05 1

C introduction: part 1

CSC 270 Survey of Programming Languages

PRINCIPLES OF OPERATING SYSTEMS

BİL200 TUTORIAL-EXERCISES Objective:

C Language: Review. INFO High Performance Scientific Computing. 26 septembre 2017

Programming Language A

Work relative to other classes

York University Faculty Science and Engineering Fall 2008

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

CSE 303 Lecture 8. Intro to C programming

Lecture 3. More About C

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

C programming for beginners

Fundamentals of Programming

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

ME 172. Sourav Saha. Md. Mahamudul Hossain Kazi Fazle Rabbi Saddam Hossain Joy Kamruzzaman Lecturer,Dept. of ME,BUET

Topic 6: A Quick Intro To C

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Chapter 2: Overview of C. Problem Solving & Program Design in C

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Arrays, Pointers, and Strings

S8352: Java From the Very Beginning Part I - Exercises

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

Programming in C. Pointers and Arrays

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

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

Procedures, Parameters, Values and Variables. Steven R. Bagley

CMPE-013/L. Introduction to C Programming

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

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

A Fast Review of C Essentials Part I

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

ANSI C Programming Simple Programs

Government Polytechnic Muzaffarpur.

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

H192 Midterm 1 Review. Tom Zajdel

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

Programming Language B

Programming and Data Structure

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

Basic Elements of C. Staff Incharge: S.Sasirekha

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

The C language. Introductory course #1

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

CSE 230 Intermediate Programming in C and C++ Input/Output and Operating System

Transcription:

C Overview Fall 2015 Jinkyu Jeong (jinkyu@skku.edu) 1

# for preprocessor Indicates where to look for printf() function.h file is a header file #include <stdio.h> int main(void) printf("hello, world!\n"); return 0; Entry point Called on program start Only one main( ) in any program Belongs to stdio.h Hello... is a parameter to printf() 2

Marathon Distance Program Convert the distance to kilometers 1 mile = 1.609 km = 1760 yards We know that the marathon length is 26 miles and 385 yards, then what is it in kilometers? the answer is 42.185968 3

Variables, Expression and Assignment /* The distance of a marathon in kilometers. */ #include <stdio.h> int main(void) int miles, yards; float kilometers; Declaration of variables miles = 26; yards = 385; Assignment expression kilometers = 1.609 * (miles + yards / 1760.0); printf("\na marathon is %f kilometers.\n\n", kilometers); return 0; 4

Preprocessor Performs before compilation # indicates that this line is a directive #define for symbolic constants #define PI 3.141592 #define YARDS_PER_MILE 1760 #include <file- name> imports a header file from some where #include file- name from your directory 5

Preprocessor Example #include <stdio.h> #define AREA 2337 #define SQ_MILES_PER_SQ_KILOMETER 0.3861021585424458 #define SQ_FEET_PER_SQ_MILE (5280 * 5280) #define SQ_INCHES_PER_SQ_FOOT 144 #define ACRES_PER_SQ_MILE 640 pacific_sea.h /* Measuring the Pacific Sea. */ #include "pacific_sea.h" int main(void) const int pacific_sea = AREA; /* in sq kilometers */ double acres, sq_miles, sq_feet, sq_inches; pacific_sea.c printf("\nthe Pacific Sea covers an area"); printf(" of %d square kilometers.\n", pacific_sea); sq_miles = SQ_MILES_PER_SQ_KILOMETER * pacific_sea; 6

I/O Using stdio.h printf( any string or characters %d %f, a, b); An output function indicates a format to be displayed % is followed by a single character for a format c (char), d (decimal), e (exponential), f(floating), s (string) Escape with \ \n, \t, \, \\ scanf( %d, &age); An input function Takes something from the standard input, and interpret as a decimal 7

I/O Example #include <stdio.h> int main(void) char c1, c2, c3; int i; float x; double y; printf("\n %s \n %s", "Input three characters,", "an int, a float, and a double: "); scanf("%c %c %c %d %f %lf", &c1, &c2, &c3, &i, &x, &y); printf("\nhere is the data that you typed in:\n"); printf("%3c%3c%3c%5d%17e%17e\n\n", c1, c2, c3, i, x, y); return 0; 8

Control Flow Each statement is executed one by one sequentially statement; statement; Special statements change the flow if (expr) a single statement OR statements while (expr) a single statement OR statements for (expr1; expr2; expr3) a single statement OR statements expr1; while (expr2) statements expr3; 9

Control Flow Example #include <stdio.h> int main(void) int i = 1, sum = 0; while (i <= 5) sum += i; ++i; printf("sum = %d\n", sum); return 0; 10

Arrays Deal with multiple same type data int xxx[3]; Declares 3 integers; xxx[0], xxx[1], xxx[2] int i; i = 2; xxx[i] = xxx[0] + 79; A string abc a b c \0 11

Pointer Address is a location in the imaginary space int age[100]; 100 integers Pointer is a special variable storing an address (location) Declaration of a variable with * char *p; int *a = age; address a 12

Functions Can you write a program of 10,000 lines in a single file? Divide your whole code into many small chunks Some chunks may look similar Make them into a single one; how? This is a function In math y=f(x) z=f(x, y) main() is a special function called by... 13

#include <stdio.h> float float void maximum(float x, float y); minimum(float x, float y); prn_info(void); int main(void) int i, n; float max, min, x; prn_info(); printf("input n: "); scanf("%d", &n); printf("\ninput %d numbers:", n); scanf("%f", &x); max = min = x; for (i = 2; i <= n; ++i) scanf("%f", &x); max = maximum(max, x); min = minimum(min, x); return 0; float maximum(float x, float y) if (x > y) return x; else return y; float minimum(float x, float y) if (x < y) return x; else return y; void prn_info(void) printf("\n%s\n%s\n\n", "This program reads an integer value for n, and then", "processes n real numbers to f ind max and min values."); 14

Files You need files, believe me. xfp = fopen( file- name, r ); Checks if the file is available Prepares a pointer, xfp, to a location inside a file Now read from (write to) the file using the pointer c = getc(xfp); n = fscanf, %d %d %f %s, i, j, x, name); 15

/* Count uppercase letters in a file. */ #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) int c, i, letter[26]; FILE *ifp, *ofp; ifp = fopen(argv[1], "r"); ofp = fopen(argv[2], "w"); for (i = 0; i < 26; ++i) /* initialize array to zero */ letter[i] = 0; while ((c = getc(ifp))!= EOF) if (c >= 'A' && c <= 'Z') /* find uppercase letters */ ++letter[c - 'A']; for (i = 0; i < 26; ++i) /* print results */ if (i % 6 == 0) putc('\n', ofp); fprintf(ofp, "%c:%5d putc('\n', ofp); return 0; ", 'A' + i, letter[i]); 16