Program Breakdown, Variables, Types, Control Flow, and Input/Output

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C Programming
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Introduction to C Programming
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
C Programming. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Input & Output: Console
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Week 1 Algorithmization and Programming Languages.
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
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.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Bill Tucker Austin Community College COSC 1315
Numbers in ‘C’ Two general categories: Integers Floats
Basic Data Types & Memory & Representation
Chapter # 2 Part 2 Programs And data
CSCE 206 Structured Programming in C
Chapter 2 Variables.
Chapter Topics The Basics of a C++ Program Data Types
The Machine Model Memory
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
REPETITION CONTROL STRUCTURE
ECE Application Programming
Administrative things
Chapter 2 - Introduction to C Programming
Basic Elements of C++.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Getting Started with C.
Chapter 2 - Introduction to C Programming
Programmazione I a.a. 2017/2018.
Introduction to C Programming
Basic Elements of C++ Chapter 2.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
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.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to CS Your First C Programs
INPUT & OUTPUT scanf & printf.
C Programming Variables.
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Chapter 2 Variables.
Chapter 2 - Introduction to C Programming
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
C Programming Getting started Variables Basic C operators Conditionals
WEEK-2.
C++ Programming Lecture 3 C++ Basics – Part I
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Fundamental Programming
Chapter 2 - Introduction to C Programming
Chapter 2 Variables.
Introduction to C Programming
Variables and Constants
Administrative things
Presentation transcript:

Program Breakdown, Variables, Types, Control Flow, and Input/Output CS 240 – Lecture 2 Program Breakdown, Variables, Types, Control Flow, and Input/Output

Program Breakdown – The Essentials In the usual sense, programs are a sequence of instructions that the processor follows one at a time. C programs indicate the entry-point as the main() function. In general, this function header should be written as int main(int argc, char* argv[]), but it doesn’t matter too much. Execution of the program will begin with the first line of the function named main, regardless of how it’s defined.

Program Breakdown – The Essentials At the top of the file, we generally include what are called “header files” Example: #include <stdio.h> These contain functions and variables for use with your programs. The stdio.h header is stored elsewhere in the system and contains the printf function, among other useful functions for outputting. Header files contained in the working directory should be surrounded with quotes (") instead of angle-brackets (<).

Fundamentals – Comments Comments are NOT code. Comments are parts of the source code whose sole purpose is to be informative to the reader of the code. Often, they’re used at the beginning of files or function to describe their role in a program. Comments are any text content between a /* and a */ /** * Still not code */ /* not code */

Fundamentals – Symbolic Constants Symbolic constants are ALSO not code, in a sense. Symbolic constants describe text to be replaced, and with what. Symbolic constants consist of three parts: #define, a name, and a replacement. The replacement is done in the source code before any code has been compiled, in the pre-compile stage. Conventionally done in header files or at the top of C source files. #define name repl #define ZERO 0;

Fundamentals – Variables Variables are an important part of any program. Without them, a program operates exactly the same every time it is run. There are four basic datatypes that we will start our discussion with. Integer (int) Characters (char) Floating Point (float) Double-precision Floating Point (double) Each of these act like numeric values in memory, but they are to be interpreted differently in practical use. Integral numeric types can be signed or unsigned, whether or not they hold negative values.

Fundamentals – char Type The char type is used to store character information. Examples: 'A', '@', ' ', '\0', '\n', '\r', etc. char variables take 1 byte in memory on 32-bit and 64-bit systems. Reminder: A byte is 8 bits, and bits can be 0 or 1. signed char variables hold values between -128 and 127. Each positive numeric value corresponds to a specific character in the ASCII encoding. unsigned char variables hold values between 0 and 255. Between 0 and 28 char is signed by default.

Fundamentals – ASCII ASCII stands for the American Standard Code for Information Interchange. It is the standard numeric mapping of numbers to characters. It's needed since data in memory is inherently numeric ones and zeros, all of which correspond to binary digits of a number The valid ASCII range is the same as the positive char range. See http://www.asciitable.com/ for specifics on the mapping to see which numbers correspond to which letter.

Fundamentals – int Type The int type is used to store general numeric information. Examples: 1, 10, 42, 57, 9001, 2000000 int variables take 4 bytes on modern 32-bit and 64-bit systems. This varies most on older machines and is heavily architecture dependent. signed int variables can hold values between -2,147,483,648 and 2,147,483,647 231 - 1 = 2,147,483,647 unsigned int variables hold values between 0 and 4,294,967,295 int variables are signed by default

Fundamentals – float and double Type The float and double types represent non-integer numeric values. Examples: 0.5f, 19.99f, 3.1415f, 2.7182, 1.6180 float variables take 4 bytes on 32-bit and 64-bit machines double variables take twice that many at 8 bytes. float and double do not have unsigned counterparts. float has a positive min of 1.1754 x 10-38 and max of 3.4028 x 1038 double has a positive min of 1.1 x 10-4932 and max of 3.4 x 104932 There are actually numbers missing in between! How floating-point numbers exactly work may be discussed later.

Code – Declaration Statements Code is made up of a sequence of statements, each followed by a semicolon (;). A block statement is a sequence of statements surrounded by braces ({) For code to make use of variables, it must contain what are called declaration statements. Declaration statements consist of at least two pieces: a type and a name. The above example declares that sum is an int type and the program should, after this point in the code, have memory reserved for it. type name; int sum;

Code – Assignment Statement For variables to do anything, they must be given values. Assignment statements consist of at least three parts: a variable, an equal sign (=), and a value. The above example ensures that after this point on the program, the variable sum contains the value 0. The value in the assignment statement should be of the same kind of type. name = value; sum = 0;

Code – Function Calls name(arg1, arg2); printf("hi\n"); Function calls represent repeatable work that doesn’t need to be written again. A rule of thumb is that if you write the same code three or more times in a program, it should be rewritten as a function. We will discuss function definition in later classes. Function calls consist of a function name and arguments. The above example runs the code in the printf() function with "hi\n" as the first argument’s value. Function calls can be used as values if they are typed. name(arg1, arg2); printf("hi\n");

Code – Expressions Expressions are a sequence of operators and operands which evaluate to a value. Examples: 1 + 2, 3 * 5, 8 < 13, x = 21, y != 34, z == 55 Expressions that consist of a single non-variable value are called literals. Example: 1, 2.0f, 'c', 4.00 When an expression contains parenthesis, the inner-most expression is evaluated first. sum = sum + 1; letter = 'L'; cost = price * (tax + 1);

Code – Boolean Operators In C, there is no basic Boolean type. In its place, we use numeric types and establish a scheme on numbers which can be used. Numeric 0 is equivalent to FALSE Everything else is equivalent to TRUE As a result, Boolean comparison operators give values 0 or 1. 2 > 1 is equal to 1 5 != 5 is equal to 0 The usual Boolean inequality operators: >, >=, ==, =<, < Not equal-to uses the exclamation point notation: !=

Control Flow – if statements if statements are a control flow compound statement which determine whether or not statement or block-statement is executed. if statements necessarily have the following format: The then-condition is a numeric-valued expression. If then-condition evaluates to 0, the else-statement is executed. If then-condition evaluates to anything else, the then-statement is executed. The else clause is optional. if (then-condition) then-statement else else-statement if (x != 0) y = 1 / x; else printf("Error\n");

Control Flow – while statements while statements are a looping control flow compound statement used for repeated execution of statements. while statements have a condition and a statement body Like the if statement, the while statement only executes the statement- body if the condition is true. Afterwards, if the condition is still true, the while loop repeats again, and again until the condition isn’t. (check, exec, check, exec, …) Often called a while loop. while (condition) statement-body while (1) printf("I never stop!");

Output – printf for printing values printf as a function can be used to print to the screen values of the basic types. Integers: printf("%d", 2018); Characters: printf("%c", 'L'); Floats and Doubles: printf("%f", 1.5); The first argument to printf is always the "Format String" The format string consists of raw text as well as %-prefixed placeholders. printf will print the format string replacing the placeholders with values from the remaining argument list in order. printf("Hell%c! It's the year %d!\n", 'o', 2018);

Input – The Importance of Input Reading input from another source is very important for most programs. Simply having variables in a program's source code isn't enough to ensure that every run of a program can be different. All the variables would have the same values every run if they weren't changed by some outside force. Providing input to a program gives the program a different "state" to work with when executing its statements. One prime source for input is from the command-line interface.

Input – Single Character Input Reading in one character is a fundamental building block of all text input systems. In stdio.h, the getchar() function returns one byte of input. Defined as int getchar(), we'll worry about these specifics later. Now, it's important to note that while the char type is expected to be used for characters, getchar returns an int type. The reason for this is to encode more than just char values! getchar returns a -1 int value if there is no input to receive, so input should be stored in an int variable before being stored in a char.

Input – Single Character Input getchar() Proper use of getchar is as follows: Question you might have: "Can't char variables hold -1, too?" Answer: Yes, we're being sneaky here. We'll have a better answer for why the int -1 is different from the char -1 late on. For now, be aware that getchar should always be stored in an int variable first. int c = getchar(); if (c == -1) printf("Reached end of input\n"); else { char letter = c; printf("Received: %c\n", letter); }