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

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

Data Types and Variables in C language

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

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

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

C: How to Program. Week /Mar/05

CC112 Structured Programming

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

Fundamental of Programming (C)

Chapter 2 - Introduction to C Programming

ET156 Introduction to C Programming

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

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

DEPARTMENT OF MATHS, MJ COLLEGE

Programming and Data Structures

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

6.096 Introduction to C++ January (IAP) 2009

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Lecture 2 Tao Wang 1

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

CSc Introduction to Computing

Fundamentals of Programming Session 4

ANSI C Programming Simple Programs

Basic Elements of C. Staff Incharge: S.Sasirekha

Fundamentals of C. Structure of a C Program

BSM540 Basics of C Language

Programming in C++ 4. The lexical basis of C++

C Language, Token, Keywords, Constant, variable

LECTURE 02 INTRODUCTION TO C++

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

VARIABLES AND CONSTANTS

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

Introduction to Computing Lecture 03: Basic input / output operations

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

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

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Fundamentals of Programming

Chapter 1 & 2 Introduction to C Language

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

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

INTRODUCTION 1 AND REVIEW

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

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

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

Unit 4. Input/Output Functions

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

ET156 Introduction to C Programming

Introduction to Computing Lecture 01: Introduction to C

Lecture 02 C FUNDAMENTALS

Basic Types and Formatted I/O

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

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

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

Presented By : Gaurav Juneja

Syntax and Variables

Computer Programming : C++

Introduction to the C Programming Language

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Number Systems, Scalar Types, and Input and Output

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Reserved Words and Identifiers

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

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

Computers Programming Course 5. Iulian Năstac

A Fast Review of C Essentials Part I

Fundamental of Programming (C)

Introduction to C# Applications

2 nd Week Lecture Notes

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

Data Type Fall 2014 Jinkyu Jeong

C Overview Fall 2014 Jinkyu Jeong

Data types, variables, constants

UNIT-2 Introduction to C++

Getting started with Java

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

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

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

Fundamentals of Programming

Introduction to C. Systems Programming Concepts

BASIC ELEMENTS OF A COMPUTER PROGRAM

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

1.1 Introduction to C Language. Department of CSE

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

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++

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

ME 172. Sourav Saha. Md. Mahamudul Hossain Kazi Fazle Rabbi Saddam Hossain Joy Kamruzzaman Lecturer,Dept. of ME,BUET

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

C - Basic Introduction

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

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

BITG 1233: Introduction to C++

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

PROGRAMMAZIONE I A.A. 2018/2019

Basics of Programming

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

Transcription:

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar.. A Simple Program. simple.c: Basics of C /* CPE 101 Fall 2008 */ /* Alex Dekhtyar */ /* A simple program */ /* This is a comment! It ends here -> */ #include <stdio.h> /* preprocessor directive */ #define KMPERMILE 1.6 /* preprocessor directive */ int main() { /* main() function: beginning */ int miles; /* integer variable declaration */ float km; /* float variable declaration */ printf("\n\nmiles2kilometers R US!\n"); /* output */ printf("\n"); printf("how many miles? "); scanf("%d", &miles); /* input */ km = miles * KMPERMILE; /* assignment statement */ printf("%d miles is %f kilometers\n", miles, km); /* formatted output */ return 0; /* return statement */ } /* end of main() function */ 1

2 Anatomy C Program. A C program consists of a sequence of comments, preprocessor directives, declarations and definitions. simple.c program contains comments, preprocessor directives and a function definition (but no declarations. More on declarations - later in the course). Comments. Comments are ignored by the C compiler. Comments must be enclosed in /* and */. A single comment can span multiple lines. /* This is a comment in C */ /* This is a comment too */ Preprocessor directives. C comes with a preprocessor, a program that runs prior to submitting the C program to the compiler. C preprocessor plays a number of functions, but the most important ones are: Inclusion of multiple (library) files into the C program being compiled. Declaration of constants. Preprocessor directives are instructions in the C source file that are read (parsed) and executed by the C preprocessor. We concentrate on two preprocessor directives now, will learn more about others later in the course: Inclusion of external C library files is achieved using the #include directive. Example. The #include preprocessor directive from simple.c brings in the external library of input-output functions. Both printf and scanf (see below) are functions from the stdio.h library. Constants are declared using #define preprocessor directive. #include <stdio.h> #define KMPERMILES 1.6 main() function definition. In order to compile into an executable file, a C program must contain the definition of the main() function. In any C program, the first statement to be executed is the first statement of its main() function. (i.e, execution starts at the beginning of the main() function). In ANSI C (and we are studying the ANSI C standard) the definition of main() is as follows: int main() { <declarations> <statements> return 0; } Here, <declarations> is a sequence of variable declarations and <statements> is the sequence of steps (instructions) to be executed.

3 Variables. Variable is a named collection (sequence) of memory cells, which, at different times can contain different content (values). Variables have three key properties: Name. Variable name allows us to refer to the variable in the program (rather than referring to a specific memory address or addresses). It provides a level of abstraction between the code in a high-level langauge (like C) and the code in machine language. Type. The type of a variable is information about the class of values that can be stored in the variable. Type of a variable affects two things: the number of memory cells reserved for the variable, the interpretation of the content of the variable. Content. The actual value(s) stored in the memory. Variable Types. C has a wide range of types: from very simple to very complex. A table of some simple variable types is shown below. C type Explanation Range number of bytes int Integer numbers -32767... 32767 2 float Floating point decimals 3.4 38...3.4 38 6 (?) double Double precision floating point decimals 1.7 308... 1.7 308 10 (?) char a single character -128... 127 1 bool boolean value (true or false) 0, 1 1 void no type none 0 Variable declarations. A variable declaration has the syntax: <type> <VariableName1>, <VariableName2>,...<VariableNameN>; Here, <type> is one of C types (or one of user-created types more about it at the end of the course), and <VariableName1>,...,<VariableNameN> are the names of the variable. A variable declaration can include one or more variable names. It must end with a semicolon ( ; ). Variable Names Reserved Words and Identifiers. Variable names, dubbed user-defined identifiers are formed as follows: An identifier must consist of only letters ( a... z, A,... Z ), digits ( 0,..., 9 ) and underscores ( ). An identifier cannot begin with a digit. An identifier cannot be a C reserved word, nor can it be a C standard identifier. Reserved Words. Some words (identifiers) are used in C language and have special meaning. They cannot be used as variable names, and their occurrence in C programs is interpreted according to special rules of C syntax. Some of the reserved words in C are: type reserved words Type names int, float, double, bool, char, void C control structures if, while, for, return, case, switch, break, continue, goto, do misc. sizeof, typedef, union, struct

4 Standard Identifiers. Standard identifiers are the names of C functions from the standard C library. The complete list of these identifiers is found in Appendix B of your textbook. Some standard identifiers you have already seen are printf and scanf. I/O functions. C does not have statements for input and output. But it does have two standard library functions. printf(). printf() is the output function. printf("string"); outputs the string. Strings must be enclosed in double quotes. printf("formatted string", <variable1>,...,<variablen>); formatted output. Format placeholders ( %d for integer, %f for floating point, %c for character) are inserted into the the "formatted string". When output is generated, they are replaced with the values of variables in the variable list, in the order of appearance. Example. printf{"%d miles is %f kilometers\n", miles, km); Let s assume that miles has value 1 and km has value 1.6. What the line above will produce as output is: 1 miles is 1.600000 kilometers scanf(). scanf() is the input function. scanf("format", &<variable1>,..., & <variablen>) reads from the standard input stream the values of variables whose names are <variable1>,... <variablen>. The format string consists of the format placeholders: %d, %lf (for floating point numbers), %c. Example scanf("%d", \&miles); reads an integer value from the standard input (keyboard, by default) into the variable miles. Note the use of the ampersand (&) in front of the file name. While the real meaning of this symbol will be discussed at the end of the course, for now, it suffices to say that the presence of the & in front of the variable name, gives the scanf function the permission/ability to change the value of the variable. Note: Both printf and scanf functions are part of the standard C library of functions. Standard C library consists of a number of header files, each containing the code for a set of like-minded functions. All standard input/output functions reside in the stdio.h header file. This is why, any C program that contains input or output must contain the #include <stdio.h> preprocessor directive. Constants Constants are values used throughout the program. In C, each constant must come with a type, and each type has its own syntax for constant values.

5 Via the preprocessor #define directive, we can name some of the constants and use their names, rather than values in computations. Integer constants. Integer constants can be either signed or unsigned. Examples of signed integer constants are: -1 +123-643054 +23456 Examples of unsigned integer constants are: 0 1 999 34235 785 Note: Integer constants in C may not contain commas separating the orders. That is, 12,390 is NOT a correct integer constant. Floating point constants. Floating point constants come in two different forms: the decimal point notation and the exponent notation and can be signed or unsigned. The decimal point constants have the form IntegerPart.DecimalPart. Examples of such constants are: or 1.0 0.001-12.12 3.1415926 +100000.00001.434 The constants in the exponent notation have the form <Integer>.<Decimal>e<Exponent> <Integer>.<Decimal>e-<Exponent> The value of such a constant is Integer.Decimal 10 Exponent (or Integer.Decimal 10 Exponent ). The following are valid floating point constants: 2e-2 1.2e3 4.5e-5 6e+2 Their values are respectively: 2 10 2 = 0.02; 1.2 10 3 = 1200; 4.5 10 5 = 0.000045; 6 10 2 = 600. The following are invalid floating point constants 20 /* no decimal point - it s an integer constant */ 45e0.45 /* the exponent must be an integer */ 3.342e /* no exponent */ 344,453e200 /* comma is not allowed */ Character constants. Character constants are individual (ASCII) characters in single quotes ( ). Examples of character constants are a b C D T 3 & " _ ~ The following are NOT valid character constants: "a" /* this is a one-character string, but not a character */ ab /* only one character allowed inside single quotes */

6 String constants. A string constant is any text inside double quotation marks ( ). Examples are: "a" "Hello, world!" "Train station" "LOL" "1234" "20,234" "/.?>$342?5^%%" "\n" "\t" String constants can include special characters (escape sequences). Symbol meaning \n new line \r carriage return \t tab \b backspace \\ \ (backslash) %% % (percent) \ (single quote) \" (double quote)