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

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

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

Fundamentals of Programming Session 4

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 1

Chapter 2, Part I Introduction to C Programming

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Some Computer Preliminaries

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

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

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Full file at C How to Program, 6/e Multiple Choice Test Bank

CC112 Structured Programming

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

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

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Use of scanf. scanf("%d", &number);

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

C: How to Program. Week /Mar/05

Display Input and Output (I/O)

ET156 Introduction to C Programming

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

Chapter 2 - Introduction to C Programming

Programming for Engineers Introduction to C

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Programming I Laboratory - lesson 01

AMCAT Automata Coding Sample Questions And Answers

CS16 Exam #1 7/17/ Minutes 100 Points total

Computer Programming

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

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Full file at

CSE 230 Intermediate Programming in C and C++ Functions

Objectives. In this chapter, you will:

A Fast Review of C Essentials Part I

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #03 The Programming Cycle

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

IT 374 C# and Applications/ IT695 C# Data Structures

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

27-Sep CSCI 2132 Software Development Lecture 10: Formatted Input and Output. Faculty of Computer Science, Dalhousie University. Lecture 10 p.

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Chapter 2. Lexical Elements & Operators

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

EL2310 Scientific Programming

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

C Introduction Lesson Outline

EL2310 Scientific Programming

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

Data types, variables, constants

COMP s1 Lecture 1

CS16 Week 2 Part 2. Kyle Dewey. Thursday, July 5, 12

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

C Syntax Out: 15 September, 1995

Lecture 2: C Programming Basic

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

CSCI 2132 Software Development. Lecture 8: Introduction to C

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

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

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

2. Numbers In, Numbers Out

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Should you know scanf and printf?

C++ Programming: From Problem Analysis to Program Design, Third Edition

1.1 Introduction to C Language. Department of CSE

CSE 303 Lecture 8. Intro to C programming

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

Programming with C++ as a Second Language

Fundamental of Programming (C)

SSOL Language Reference Manual

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

BLM2031 Structured Programming. Zeyneb KURT

Your First C++ Program. September 1, 2010

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Compiling and Running a C Program in Unix

C introduction: part 1

Tutorial No. 2 - Solution (Overview of C)

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

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

Day02 A. Young W. Lim Sat. Young W. Lim Day02 A Sat 1 / 12

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

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

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

EC 413 Computer Organization

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

YOLOP Language Reference Manual

VENTURE. Section 1. Lexical Elements. 1.1 Identifiers. 1.2 Keywords. 1.3 Literals

Course Outline Introduction to C-Programming

2. Numbers In, Numbers Out

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

Lecture Set 2: Starting Java

CPE 101. Overview. Programming vs. Cooking. Key Definitions/Concepts B-1

Chapter 1 & 2 Introduction to C Language

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Typescript on LLVM Language Reference Manual

Transcription:

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

WHY C? C is a programming lingua franca Millions of lines of C code exist Many other languages use C-like syntax C is portable C compilers exist for most platforms C is used for embedded systems, operating systems, and thousands of applications

PROGRAMMING LANGUAGES Programming language: a form of notation used to describe an algorithm to a computer As programmers, we are concerned with: syntax = the rules of the language semantics = the meaning of a program The compiler will only check syntax for you!

TYPES OF ERRORS Syntax Errors: Incorrect program grammar Run-Time Errors: Illegal operations during execution e.g., Division by zero Logic Errors: Incorrect program results

COMPILING A PROGRAM gcc the GNU C Compiler gcc is installed on Sparky Usage: sparky% gcc filename.c sparky% gcc -o name filename.c Other compilers include Xcode and Microsoft Visual Studio

EXECUTING A PROGRAM To execute a compiled program on Sparky, type./filename For example: sparky%./a.out sparky%./myprog

A FIRST PROGRAM /* My first C program */ #include <stdio.h> int main (void) { printf( Hello world!\n ); return 0; }

PROGRAM COMPILATION

PUNCTUATION IN C Statements are terminated with a ; (semicolon) Groups of statements are enclosed by curly braces: { and } Commas separate function arguments Whitespace is ignored (but indentation is recommended as part of good coding style)

COMMENTS /* My first C program */ #include <stdio.h> int main (void) { printf( Hello world!\n ); return 0; }

COMMENTS ON COMMENTS Comments are: used to document code ignored by the compiler delimited by /* and */ required in this class Comments add value to your code They explain how and why you are doing something

PREPROCESSING DIRECTIVES /* My first C program */ #include <stdio.h> int main (void) { printf( Hello world!\n ); return 0; }

#INCLUDE STATEMENTS Our sample program uses a function (piece of code) named printf() printf() is defined in a file named stdio.h The #include statement tells the compiler that it can find the definition of printf() elsewhere (in stdio.h) Analogy: the bibliography of a term paper

STANDARD LIBRARIES IN C Standard libraries contain frequently-used functions for C programs Ex. input/output, math functions stdio.h is the C standard library for input and output functions You can also create your own libraries of common code for your programs

THE C PREPROCESSOR Files are passed to the preprocessor before they move on to the compiler The preprocessor: strips out comments makes substitutions for named constants inserts the contents of #include-d files Directives to the preprocessor begin with #

THE MAIN ( ) FUNCTION /* My first C program */ #include <stdio.h> int main (void) { printf( Hello world!\n ); return 0; }

MORE ON MAIN ( ) Program execution begins and ends with the main() function Program statements are executed sequentially When all of the statements in main() have been executed, the program terminates

THE PRINT STATEMENT /* My first C program */ #include <stdio.h> int main (void) { printf( Hello world!\n ); return 0; }

THE printf() STATEMENT The printf() function: sends program output to the display is part of the standard I/O library Output is specified in a quote-enclosed control string \n is a special newline character

THE RETURN STATEMENT /* My first C program */ #include <stdio.h> int main (void) { printf( Hello world!\n ); return 0; }

RETURN VALUES Many functions return values e.g., a mathematical function main() returns a value (exit status code) to the operating system to indicate program status Here, 0 means everything completed OK

PROGRAM STRUCTURE 1.Preprocessor Directives a. #include-d files b. Other definitions/declarations 2.Supporting Functions 3.The main() function

EXAMPLE PROGRAM 2 #include <stdio.h> int main (void) { printf( Programming is fun.\n ); printf( Doing it in C is even more fun.\n ); return 0; }

EXAMPLE PROGRAM 3 #include <stdio.h> int main (void) { printf( Testing...\n..1\n...2\n...3\n ); return 0; }

VARIABLES Programs use variables to store data Variables are named blocks of memory Variables must be declared before use Different kinds of variables store different kinds of data integers, floating-point numbers, characters

DECLARING VARIABLES Variables may be declared with or without an initial value: int x; int y = 5; int z = x; Variables must be assigned a value before use

VARIABLES AND MEMORY int a; /* a can hold an int value */ int b = 3; /* b holds the value 3 */

ASSIGNMENTS Assignments store values in variables General form: <target variable> = <expression>; = means is assigned the value, not is equal to!!! Example: area = length * width;

PROGRAM 4 #include <stdio.h> int main(void) { int feet = 6; int inches = feet * 12; printf( %d feet = %d inches,feet,inches); return 0; }

ANALYSIS OF PROGRAM 4 feet and inches are integer variables %d is a placeholder (format specifier) for an int variable Program output: 6 feet = 72 inches

USER INPUT The printf() function is used to display output on the screen The scanf() function is used to read input from the keyboard scanf() is also defined in stdio.h

USING scanf() scanf() reads in data and stores it in one or more variables int userage; scanf( %d, &userage);

scanf() USAGE The first argument (the control string) contains a series of placeholders These are like the ones that printf() uses: %d = int, %f = float, %c = char, etc. Spaces are used to separate placeholders and absorb whitespace %d absorbs leading spaces and reads an integer value

scanf() USAGE, PT. 2 The remaining arguments to scanf() are a comma-separated list of variable names Input is stored in these variables Each variable name is preceded by & &i means the memory address associated with variable i We ll talk more about this later on

scanf() EXAMPLES int a, b, c; char d; scanf( %d %d, &a, &b); scanf( %c %d, &d, &c);