BSM540 Basics of C Language

Similar documents
BSM540 Basics of C Language

BSM540 Basics of C Language

C Programming Language

C Programming Language

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

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

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

DECLARAING AND INITIALIZING POINTERS

CC112 Structured Programming

Lecture 2: C Programming Basic

CSC 270 Survey of Programming Languages

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

C programming basics T3-1 -

Intermediate Programming, Spring 2017*

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?

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

Number Systems, Scalar Types, and Input and Output

Advanced C Programming Topics

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

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

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

C Programming Language

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

3. Types of Algorithmic and Program Instructions

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Language comparison. C has pointers. Java has references. C++ has pointers and references

Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print

Should you know scanf and printf?

C Programming Language

1/31/2018. Overview. The C Programming Language Part 2. Basic I/O. Basic I/O. Basic I/O. Conversion Characters. Input/Output Structures and Arrays

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Lecture 3. More About C

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

Programming and Data Structures

Input/Output Week 5:Lesson 16.1

C Programming Language

CSCI 2132 Software Development. Lecture 8: Introduction to C

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

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

Function I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects

A Fast Review of C Essentials Part I

Chapter 8: Character & String. In this chapter, you ll learn about;

Function I/O. Last Updated 10/30/18

Programming Language B

ET156 Introduction to C Programming

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

EL2310 Scientific Programming

Lesson 3 Introduction to Programming in C

Array Initialization

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

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

Lesson 7. Reading and Writing a.k.a. Input and Output

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

Introduction to Computing Lecture 03: Basic input / output operations

The C language. Introductory course #1

INTRODUCTION TO C++ C FORMATTED INPUT/OUTPUT. Dept. of Electronic Engineering, NCHU. Original slides are from

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

Computers Programming Course 5. Iulian Năstac

Use a calculator and c = 2 π r to calculate the circumference of a circle with a radius of 1.0.

Introduction To C. Programming. Presented by: Jim Polzin. otto:

CSC231 C Tutorial Fall 2018 Introduction to C

At the end of this module, the student should be able to:

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Basic Types and Formatted I/O

Computer Programming 3 th Week Variables, constant, and expressions

C Overview Fall 2014 Jinkyu Jeong

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

sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

C PROGRAMMING QUESTIONS AND

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

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

SEQUENTIAL STRUCTURE. Erkut ERDEM Hacettepe University October 2010

Storage class and Scope:

ET156 Introduction to C Programming

Contents. A Review of C language. Visual C Visual C++ 6.0

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -VARIABLE AND DATA TYPES- SPRING 2015, SEON-JU AHN, CNU EE

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

C: How to Program. Week /Mar/05

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS )

Structured programming

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

Chapter 3 Basic Data Types. Lecture 3 1

BLM2031 Structured Programming. Zeyneb KURT

UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS

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

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

SU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator

Chapter 7. Basic Types

Lecture 02 C FUNDAMENTALS

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

Programming. Structures, enums and unions

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

High Performance Programming Programming in C part 1

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

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

Work relative to other classes

Unit 4. Input/Output Functions

Fundamentals of Programming

Transcription:

BSM540 Basics of C Language Chapter 4: Character strings & formatted I/O Prof. Manar Mohaisen Department of EEC Engineering

Review of the Precedent Lecture To explain the input/output functions printf() and scanf() To explain how to define and treat variables of several data types Integers, floating-point, and characters To introduce nonprinting sequences To give several programming examples To give several C keywords 2

Explain how to handle strings Lecture Objectives Explain how to define constants in C scanf() again! 3

String Character Occupies one byte in memory char toll; toll = A ; String It is a sequence (i.e., array) of characters Computer terminates the string in memory by a NULL character \0 char comment[30] = Please no homework ; P l e a s e n o h o m e w o r k \0 each cell is one byte null character 4

Handling Strings A short example # include <stdio.h> # include <string.h> // for strlen() prototype void main() { float weight; int size, letters; char name[40]; // name is an array of 40 chars } printf("hi! What's your first name?\n"); scanf("%s", name); printf("%s, what's your weight in kg?\n", name); scanf("%f", &weight); size = sizeof name; letters = strlen(name); printf("hello %s,\n", name); printf("your name has %d letters,\n", letters); printf("and your weight is %.2f kg.\n", weight); 5

Another example Handling Strings contd. # include <stdio.h> # include <string.h> /* provides strlen() prototype */ # define PRAISE "What a super marvelous name!" void main(void) { char name[40]; } printf("what's your name?\n"); scanf("%s", name); printf("hello, %s. %s\n", name, PRAISE); printf("your name of %d letters occupies %d memory cells.\n", strlen(name), sizeof name); Output What s your name? Manar Hello Manar. What a super marvelous name! Your name of 5 letters occupies 40 memory cells 6

Constants We define a constant using the # define directive # define name value Examples # define PI 3.14159 // no semicolon The compiler substitutes 3.14159 whenever PI is used in your code # define CH A The compiler substitutes A whenever CH is used in your code # define name Huey Freeman The compiler substitutes Huey Freeman whenever name is used in your code # define age 19 The compiler substitutes 19 whenever age is used in your code # define PRINT(x) printf( x = %d\n, x) The compiler substitutes printf( x = %d\n, x) whenever PRINT(x) is used in your code 7

Again! An example Constants contd. # include <stdio.h> # define PI 3.14159 // constant # define PRINT(x, y) printf("circumference = %1.2f, area = %1.2f\n", x, y) void main() { float area, circum, radius; // variable declaration } Output printf("what is the radius of your pizza?\n"); scanf("%f", &radius); area = PI * radius * radius; // calculate area of the pizza circum = 2.0 * PI *radius; // calculate circum of the pizza printf("your basic pizza parameters are as follows:\n"); PRINT(circum, area); What s is the radius of your pizza? 0.5 Your basic pizza parameters are as follows: circumference = 3.14, area = 0.79 8

Constants contd. Conversion specification modifiers for printf() Conversion specifiers Conversion specification Output %d Decimal integer %c Single character %f Floating-point number %p A pointer (memory address) %s Character string %u Unsigned decimal integer %o Octal integer %x Hexadecimal integer 9

A Comment on scanf() When the argument is STRING (array of characters) There will be no & preceding the variable name This is because the array s name includes the address! (we will study it later)... char name[40]; printf( Please enter your first name.\n ); scanf( %s, name);... When the argument is a single variable (not an array!) There must be the & before the variable name This is because variable s name does not include the address... int age; printf( Please enter your age.\n ); scanf( %d, &age);... 10

It s find the error time! 12 errors Let s check what is wrong! define B Huey define X 10 main(int) { int age; char name; printf( My name is %c, please enter your first name., B); scanf( %s, name); printf( All right, %c, what s your age?\n, name); scanf( %f, age); xp = age + X; // your age after 10 years printf( after %d years, you will be %d years old.\n, xp); } return 0; 11

Lecture Keywords Keywords String scanf() # define 12

Explain how to handle strings Lecture Summary Explain how to define constants in C scanf() again! 13