CSCI206 - Computer Organization & Programming

Slides:



Advertisements
Similar presentations
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Advertisements

Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Introduction to Python
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Chapter 2: Introduction to C++.
Introduction to C Programming
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Computer Science 210 Computer Organization Introduction to C.
First Program in C With Output. C Language Keywords.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
1 CSC103: Introduction to Computer and Programming Lecture No 6.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
File IO and command line input CSE 2451 Rong Shi.
Introduction to Programming
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
7. BASIC TYPES. Systems of numeration Numeric Types C’s basic types include integer types and floating types. Integer types can be either signed or unsigned.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
CSCI206 - Computer Organization & Programming
C Formatted Input/Output
Formatted Input and Output
The Machine Model Memory
Strings CSCI 112: Programming in C.
Computer Science 210 Computer Organization
Chapter 2: Introduction to C++
A bit of C programming Lecture 3 Uli Raich.
ECE Application Programming
Characters and Strings
C Programming Tutorial – Part I
Command Line Arguments
TMF1414 Introduction to Programming
Revision Lecture
Introduction to C CSE 2031 Fall /3/ :33 AM.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Programmazione I a.a. 2017/2018.
Java Programming: From Problem Analysis to Program Design, 4e
Input and Output Lecture 4.
Computer Science 210 Computer Organization
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Differences between Java and C
Conversion Check your class notes and given examples at class.
Chapter 2: Introduction to C++.
Introduction to C EECS May 2019.
Programming Languages and Paradigms
Chapter 2 Primitive Data Types and Operations
Characters and Strings
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Presentation transcript:

CSCI206 - Computer Organization & Programming C grammar, variables, and basic IO zyBook: 2.3, 2.6, 2.8, 2.11, 2.16

A quick, simple, complete C program Include file, similar to Python’s import #include <stdio.h> int main(int argc, char * argv[]) { printf(“Hello world!\n”); return 0; } Function parameters, Similar to Python’s C function name, similar to Python’s def name C statements

Coding conventions Names “names should fit” how it is used Based on Linux kernel coding style http://www.eg.bucknell.edu/~cs206/sp18/lab/c-coding-standards/ Names “names should fit” how it is used lowercase with _ separating words, or camel case this is the same as in Python and in Java, though most C porgrammers prefer lowercase with _ length proportional to importance temporary vars can be i, j, k important vars: student_grade, course_name include units if appropriate: timeout_msec, height_ft constants: ALL_CAPS

Coding conventions Indentation and spacing: While indentation in Python is a critical piece of syntax, indentation and spacing in C is more for easier reading and reducing possibilities of errors. The following two pieces of code do the same. int func(int a) { if (a == 0) return 4; else return a * 3 + 4; } int func(int a) {if (a==0) return 4; else return a*3+4;}

C basic types and their ranges int INT_MIN and INT_MAX insigned int 0 and UINT_MAX float - FLT_MIN to + FLT_MAX double - DBL_MIN to + DBL_MAX char Depending on the coding scheme

Implicit type conversion If any value in a numeric expression is floating point, both operands are converted to floating point. else, integer operations are used. even if the result would be floating point. e.g., int x = 4 / 5; // what is x? e.g., What does it result in Python? x = 4 / 5 #? x = 4 // 5 #?

Explicit type conversion Use (type) to force a type conversion. x = (int) 3.5; // results in x == 3 Note, float->int truncates (doesn’t round). (int)3.9999; //? What if we want to round up?

String There is no native string data type in C There is a character data type (char) A string is a null-terminated sequence of chars char test[] = “test”; “test” occupies 5 bytes in memory (include the null terminator!) Note: unlike Python, strings in C are mutable! t e s \0

Manipulating Strings Looping through arrays of char is tedious work... #include <string.h> Gives access to a number of useful functions (length, concatenate, copy, etc) Go read the manual man 3 string

#include <stdio.h> man 3 printf printf introduction "Printf" by I, Surachit. Licensed under CC BY-SA 3.0 via Wikimedia Commons - http://commons.wikimedia.org/wiki/File:Printf.svg#mediaviewer/File:Printf.svg printf uses a format string the % character is a special character whose value will be provided by a variable in the variable list.

%[flags][width][.precision]type d,i = integer u = unsigned integer f,F,e,E = double x = hexadecimal unsigned int s = null terminated string c = character p = pointer % = print the % character

Result of executing cbasics

%[flags][width][.precision]type + = always include +/- for a numeric value - = left align output (default is right aligned) 0 = use 0 instead of space to pad to a given width

%[flags][width][.precision]type a number the minimum number of characters to output defaults to padding on the right with spaces

%[flags][width][.precision]type a number for floats the number of digits to the right of the decimal for strings the maximum number of characters to output (truncate)

printf - example formats printf( “%5.2f”, 3.14159265359 ); // _3.14 5-position output (including the decimal), 2 to the right of the decimal, padded on the left with spaces printf( “%05.2f”, 3.14159265359 ); // 03.14 same as above but padded on the left with 0’s printf( “%-5.2f”, 3.14159265359 ); // 3.14_ same as first, but padded on the right with spaces (cannot right pad with 0’s!)

Result of executing cbasics-pos

scanf - read input using format strings int i; scanf (“%d”, i);

scanf - read input using format strings int i; scanf (“%d”, i); Parameters are passed by value, this can’t work!

scanf - read input using format strings int i; i = scanf (“%d”);

scanf - read input using format strings int i; i = scanf (“%d”); There is a return code, it is the number of inputs read (zero means failure)

scanf - read input using format strings int i; scanf (“%d”, &i); & is the address-of operator. This gives the address of the local variable i to scanf scanf reads an integer (%d) and stores it in the given memory address

Loops

Decisions

Avoid common bugs by ensuring each clause is in parens! Decisions Avoid common bugs by ensuring each clause is in parens! ( ) ( )

Decisions warning: case blocks do not start a new scope block and fall through unless you use the break keyword

Conversion How many MiB is 2048 KiB? 1.0 2.0 2.048 1024 2048 Go to socrative.com Log in as student Pick “MENG7820” as the classroom How many MiB is 2048 KiB? 1.0 2.0 2.048 1024 2048

Conversion What is 5 KiB in KB? 1.024 4.8828125 5.0 5.120 5.256 Go to socrative.com Log in as student Pick “MENG7820” as the classroom What is 5 KiB in KB? 1.024 4.8828125 5.0 5.120 5.256