C Introduction Lesson Outline

Similar documents
Software Lesson 1 Outline

What is Software? Software, for our purposes, is just a word meaning programs. CS1313: Software Lesson Fall

Numeric Data Types Outline

Chapter 2, Part I Introduction to C Programming

CS : Programming for Non-majors, Fall 2018 Programming Project #2: Census Due by 10:20am Wednesday September

CS : Programming for Non-majors, Summer 2007 Programming Project #2: Census Due by 12:00pm (noon) Wednesday June

Fundamentals of Programming Session 4

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

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Programming for Engineers Introduction to C

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

Software Lesson 2 Outline

Arithmetic Expressions Lesson #1 Outline

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

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

Programming I Laboratory - lesson 01

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

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

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

Physics 306 Computing Lab 1: Hello, World!

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

Characters Lesson Outline

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Tutorial No. 2 - Solution (Overview of C)

Welcome! COMP s1. Programming Fundamentals

Lab 1 Introduction to UNIX and C

C: How to Program. Week /Mar/05

Fundamental of Programming (C)

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

1.1 Introduction to C Language. Department of CSE

Outline of Fortran 90 Topics

My First Command-Line Program

Data Types and Variables in C language

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Chapter 2 - Introduction to C Programming

An overview of Java, Data types and variables

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

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

Getting started with C++ (Part 2)

IS12 - Introduction to Programming. Lecture 7: Introduction to C

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

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

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

Should you know scanf and printf?

Week 1, continued. This is CS50. Harvard University. Fall Cheng Gong

Chapter 2: Basic Elements of C++

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

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

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

COMP 110 Project 1 Programming Project Warm-Up Exercise

Welcome to... CS113: Introduction to C

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

CS 177 Recitation. Week 1 Intro to Java

Lab # 02. Basic Elements of C++ _ Part1

COMP s1 Lecture 1

EL2310 Scientific Programming

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

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

Introduction to Java. Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

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

Lesson 1A - First Java Program HELLO WORLD With DEBUGGING examples. By John B. Owen All rights reserved 2011, revised 2015

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

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

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

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

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

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

Introduction to Using OSCER s Linux Cluster Supercomputer This exercise will help you learn to use Sooner, the

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

Course Outline Introduction to C-Programming

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. Programming Problems

Absolute Value. if Lesson 1 CS1313 Spring

Reversing. Time to get with the program

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

CS102: Variables and Expressions

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

بسم اهلل الرمحن الرحيم

Program Fundamentals

Your first C and C++ programs

Introduction to C Programming

CSCI 2132 Software Development. Lecture 8: Introduction to C

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 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Objectives. In this chapter, you will:

GCC C++: Comment Text

Some Computer Preliminaries

Unit. Programming Fundamentals. School of Science and Technology INTRODUCTION

Computer Programming : C++

CS : Programming for Non-Majors, Fall 2018 Programming Project #5: Big Statistics Due by 10:20am Wednesday November

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

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

Installing and Using Dev-C++

CSC105, Introduction to Computer Science I. Introduction. Perl Directions NOTE : It is also a good idea to

More Data Types. The Char Data Type. Variable Declaration. CS200: Computer Science I. Module 14 More Data Types

Fundamental of Programming (C)

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Review of Fundamentals

Lesson 1: Writing Your First JavaScript

Transcription:

Outline 1. Outline 2. hello_world.c 3. C Character Set 4. C is Case Sensitive 5. Character String Literal Constant 6. String Literal Cannot Use Multiple Lines 7. Multi-line String Literal Example 8. Newline 9. Newline Example 10. Statements 11. Statement Terminator 12. Standard Input & Standard Output 13. Block Delimiters 14. What Is a Comment? #1 15. What Is a Comment? #2 16. Are Comments Necessary? 17. hello_world.c with Comments 18. hello_world.c without Comments 19. Flowchart for hello_world.c CS1313 Fall 2015 1

hello_world.c /* ************************************************* *** Program: hello_world *** *** Author: Henry Neeman (hneeman@ou.edu) *** *** Course: CS 1313 010 Fall 2015 *** *** Lab: Sec 011 Fridays 11:30am *** *** Description: Prints the sentence *** *** "Hello, world!" to standard output. *** ************************************************* */ #include <stdio.h> int main () { /* main */ /* ******************************** *** Execution Section (body) *** ******************************** * * Print the sentence to standard output * (i.e., to the terminal screen). */ printf("hello, world!\n"); } /* main */ CS1313 Fall 2015 2

C Character Set These are the characters that C recognizes. Letters (upper case and lower case) A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z Digits 0 1 2 3 4 5 6 7 8 9 Special Characters (punctuation etc) space (also known as blank) " ( ) * + - / : =! & $ ; < > %?,. ˆ # @ { } [ ] \ CS1313 Fall 2015 3

C is Case Sensitive C is case sensitive: it distinguishes between UPPER case (CAPITAL) and lower case (small) letters. Keywords in C for example, the keyword int MUST be in lower case. For example: #include <stdio.h> int main () { /* main */ int height_in_cm; height_in_cm = 160; printf("my height is %d cm.\n", height_in_cm); } /* main */ CS1313 Fall 2015 4

Character String Literal Constant A character string literal constant is a sequence of characters delimited by a double quote at the beginning and a double quote at the end. A character string literal constant is also known as a character string literal or a string literal for short. For example, in this printf statement: printf("this is a printf.\n"); the following is a string literal: "This is a printf.\n" The output of this printf statement is: This is a printf. followed by a newline, also known as a carriage return. CS1313 Fall 2015 5

String Literal Cannot Use Multiple Lines A character string literal constant can only use one line; that is, both of its delimiters MUST be on the same line of source code text. So, this is CORRECT: printf("this string literal takes one line"); printf(" and so does this string literal.\n"); And this is WRONG WRONG WRONG: printf("this string literal takes more than one line so it s WRONG!\n"); Some compilers will accept this but won t be happy; other compilers will simply reject it. Regardless, if this appears in a program in CS1313, YOU WILL BE SEVERELY PENALIZED! CS1313 Fall 2015 6

Multi-line String Literal Example % cat bad_string_literal.c #include <stdio.h> int main () { /* main */ printf("this string literal takes more than one line so it's WRONG!\n"); } /* main */ % gcc -o bad_string_literal bad_string_literal.c gcc bad_string_literal.c bad_string_literal.c: In function main : bad_string_literal.c:5: error: missing terminating " character bad_string_literal.c:6: error: more undeclared (first use in this function) bad_string_literal.c:6: error: (Each undeclared identifier is reported only once bad_string_literal.c:6: error: for each function it appears in.) bad_string_literal.c:6: error: expected ) before than bad_string_literal.c:6: error: missing terminating ' character bad_string_literal.c:7: error: expected ; before } token CS1313 Fall 2015 7

Newline In C, you can place a newline, also known as a carriage return, inside a string literal using: \n If a newline appears inside a string literal in the source code, then when the string literal is output, the newline causes the output to move to a new line. CS1313 Fall 2015 8

% cat newline.c #include <stdio.h> Newline Example int main () { /* main */ printf("howdy do!\n"); printf("this string literal contains a newline in the\nmiddle "); printf("but this string literal contains a newline at the end.\n"); printf("so there!\n"); } /* main */ % gcc -o newline newline.c % newline Howdy do! This string literal contains a newline in the middle but this string literal contains a newline at the end. So there! Note: In general, it s better programming practice to put newlines only at the end of your string literals, not in the middle, because in the middle they can be difficult for programmers (for example, graders) to see. CS1313 Fall 2015 9

Statements A statement in a program is like a sentence in a natural language: it s the smallest possible collection of words and punctuation that can stand by itself and have meaning. For example: printf("hello, world.\n"); This statement tells the compiler to output to the terminal screen the string literal Hello, world. followed by a newline. CS1313 Fall 2015 10

Statement Terminator In C, every statement ends with a semicolon, which is known as the statement terminator. For example: int height_in_cm; height_in_cm = 160; printf("my height is %d cm.\n", height_in_cm); Notice: a statement CAN take more than one line (but recall that a string literal CAN T take more than one line). The way you find the end of a statement is by finding its statement terminator. CS1313 Fall 2015 11

Standard Input & Standard Output Standard input is when a user types at the keyboard. It is sometimes shortened to stdin, pronounced standard in. Standard output is when the computer outputs to the terminal screen. It is sometimes shortened to stdout, pronounced standard out. In C: a scanf statement always inputs from stdin, and a printf statement always outputs to stdout. More on this later. CS1313 Fall 2015 12

Block Delimiters The open curly brace, also known as the left brace, { acts as the start of a block and is known as the block open. The close curly brace, also known as the right brace, } acts as the end of a block and is known as the block close. The block open and block close are said to delimit the block: they indicate where the block begins and where the block ends. Delimit: Indicate where something begins and ends. CS1313 Fall 2015 13

What Is a Comment? #1 A comment is a piece of text in a source file that: tells human beings (for example, programmers) something useful about the program, BUT is ignored by the compiler, so it has absolutely no affect on how the program runs. In C, the start of a comment is indicated by /* and the end of a comment is indicated by */ All text appearing between these comment delimiters is part of the comment, and therefore is ignored by the compiler. Delimit: Indicate where something begins and ends. CS1313 Fall 2015 14

What Is a Comment? #2 A comment is a piece of text in a source file that: tells human beings (for example, programmers) something useful about the program, BUT is ignored by the compiler, so it has absolutely no affect on how the program runs. In C, the start of a comment is indicated by /* and the end of a comment is indicated by */ A comment can use multiple lines of text. The delimiters DON T have to be on the same line. CS1313 Fall 2015 15

Are Comments Necessary? Comments are ignored by the compiler, so strictly speaking they aren t needed to compile and run. But, if you don t put them into one of your CS1313 programming projects, YOU MAY LOSE A FULL LETTER GRADE OR MORE on that project. Why? Comments tell human beings useful things about your program. They help programmers including you, a month later when you ve forgotten everything about your program to understand your program. They also tell graders that you know what the heck you re doing. CS1313 Fall 2015 16

hello_world.c with Comments /* ************************************************* *** Program: hello_world *** *** Author: Henry Neeman (hneeman@ou.edu) *** *** Course: CS 1313 010 Fall 2015 *** *** Lab: Sec 011 Fridays 11:30am *** *** Description: Prints the sentence *** *** "Hello, world!" to standard output. *** ************************************************* */ #include <stdio.h> int main () { /* main */ /* ******************************** *** Execution Section (body) *** ******************************** * * Print the sentence to standard output * (i.e., to the terminal screen). */ printf("hello, world!\n"); } /* main */ CS1313 Fall 2015 17

hello_world.c without Comments #include <stdio.h> int main () { printf("hello, world!\n"); } CS1313 Fall 2015 18

Flowchart for hello_world.c int main () { printf("hello, world!\n"); } Start An oval denotes either the start or the end of the program, or a halt operation within the program (which we ll learn about later). Output Hello, world! End References: http://www.edrawsoft.com/flowchart-symbols.php A parallelogram denotes either an input operation or an output operation. An arrow denotes the flow of the program. CS1313 Fall 2015 19