Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
1 Lecture 7  Fundamental data types in C  Data type conversion:  Automatic  Casting  Character processing  getchar()  putchar()  Macros on ctype.h.
Types and Variables. Computer Programming 2 C++ in one page!
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Structure of a C program
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
CS150 Introduction to Computer Science 1
1 Chapter 2: Elementary Programming Shahriar Hossain.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Chapter 2 Getting Started in C Programming
Chapter 2: C Fundamentals Dr. Ameer Ali. Overview C Character set Identifiers and Keywords Data Types Constants Variables and Arrays Declarations Expressions.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
1 Workshop Topics - Outline Workshop 1 - Introduction Workshop 2 - module instantiation Workshop 3 - Lexical conventions Workshop 4 - Value Logic System.
Input & Output: Console
C Programming Lecture 4 : Variables , Data Types
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
C Tokens Identifiers Keywords Constants Operators Special symbols.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Week 1 Algorithmization and Programming Languages.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Programming Fundamentals
Variables Symbol representing a place to store information
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Characters and Strings
INTRODUCTION TO ‘C’ PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
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 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Chapter 2: Introduction to C++
ECE Application Programming
ECE Application Programming
Tokens in C Keywords Identifiers Constants
A First C Program (mixing datatypes)
A First C Program (mixing datatypes)
Chapter 2: Introduction to C++
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.
Negative Integer Representation
Lectures on Numerical Methods
A First C Program (mixing datatypes)
Chapter 2: Introduction to C++.
C++ Programming Lecture 3 C++ Basics – Part I
Introduction to C Programming
Presentation transcript:

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI CSCI 230 A First C Program

Dale Roberts Try Your First C Program #include /* I/O header file */ main(){ printf(“Hello world ”); printf(“Welcome to CSCI230\n“); printf(“I am John Smith\n”); printf(“I am John Smith\n”);} A C program contains one or more functions main() is the function name of your main (root) program { }: braces (left & right) to construct a block containing the statements of a function Every statement must end with a ; \ is called an escape character \n is an example of an escape sequence which indicates newline Other escape sequences are: \t \r \a \\ \” Exercise: Use any editor to type and then save your first program as main.c % gcc main.c % a.out and observe its result. header file – contains I/O routines pre-processor directive one statement main must be present in each C program statement terminator Indicates a program building block called function comment

Dale Roberts Identifiers Variable identifiers Begin with a letter or underscore: A-Z, a-z, _ The rest of the name can be letters, underscore, or digits Only the first 8 characters are significant on most compilers (those come after the 8th character will be ignored) while most of C compiler allows 32 significant characters. Example : _abcABCTimetime _a1abcdefgh abcdefghi (may be the same as abcdefgh ) Case sensitive Keywords: reserved names autodoubleifstaticbreakelseintstruct caseentrylongswitchcharexternregister typedeffloatreturnuniondogosizeofcontinue …

Dale Roberts Fundamental Data Type Four Data Types Four Data Types (assume 2’s complement, byte machine) Data TypeAbbreviationSize (byte) Range char ~ 127 unsigned char 10 ~ 255 int 2 or ~ or ~ unsigned intunsigned 2 or 40 ~ or 0 ~ short intshort ~ unsigned short intunsigned short 20 ~ long intlong ~ unsigned long intunsigned long 40 ~ float4 double8 Note:2 7 = 128, 2 15 =32768, 2 31 = Complex and double complex are not available

Dale Roberts Variable declaration type v 1,v 2,v 3, …, v n Example: int i; int j; float k; char c; short int x; long int y; unsigned int z; int a1, a2, a3, a4, a5;

Dale Roberts Numeric, Char, String Constant Constant Numeric constant fixed-point octal O32 (= 24 D ) hexadecimal OxFE or Oxfe (=254 D ) decimal int 32 long (explicit) 32L or 32l an ordinary integer constant that is too long to fit in an int is also to be long floating-point No single precision is used; always use double for constant Example: e E

Dale Roberts Character constant American Standard Code for Information Interchange (ASCII) Printable: single space32 ‘0’ - ‘9’ ‘A’ - ‘Z’ ‘a’ - ‘z’ Nonprintable and special meaning chars ‘\n’ new line10 ‘\t’ tab 9 ‘\\’ back slash 9 ‘\’’ single quote39 ‘\0’ null 0 ‘\b’ back space 8 ‘\f’ formfeed12 ’\r’ carriage return13 ‘\”’ double quote34 ‘\ddd’ arbitrary bit pattern using 1-3 octal digits ‘\Xdd’ for Hexadecimal mode ‘\017’ or ‘\17’ Shift-Ins, ^O ‘\04’ or ‘\4’ or ‘\004’ EOT (^D) ‘\033’ or ‘\X1B’ Numeric, Char, String Constant

Dale Roberts String Constant will be covered in Array section String is a array of chars but ended by ‘\0’ String constant is allocated in a continuous memory space of Data Segment, so it can not be rewritten Example: “ ABCD ”... A B C D ‘\0’ Ans: 13+1 = 14 bytes Question: “I am a string” takes ? Bytes 4 chars but takes 5 byte spaces in memory Numeric, Char, String Constant

Dale Roberts Character constants & ASCII codes: char x; x=‘a’;/* x = 97*/ Notes: –‘a’ and “a” are different; why? ‘a’ is the constant 97 “a” is an array of character constants, { ‘a’, ‘\0’} or {97, 0} –“a” + “b” +”c” is invalid but ‘a’+’b’+’c’ = ? (hint: ‘a’ = 97 in ASCII) –if the code used is not ASCII code, one should check out each value of character Numeric, Char, String Constant 1 38 ‘a’ + ‘b’ + ‘c’ = = 294 = in the memory

Dale Roberts Initialization If a variable is not initialized, the value of variable may be either 0 or garbage depending on the storage class of the variable. int i=5; float x=1.23; char c=‘A’; int i=1, j,k=5; char c1 = ‘A’, c2 = 97; float x=1.23, y=0.1;

Dale Roberts Memory Concepts Each variable has a name, type, and value 1) 1) int x; 2) 2) scanf(“%d”, &x); 3) 3) user inputs 10 4) 4) x = 200; After the execution of (1)x After the execution of (2)x After the execution of (3)x After the execution of (4)x Previous value of x was overwritten

Dale Roberts Sample Problem Write a program to take two numbers as input data and print their sum, their difference, their product and their quotient. Problem Inputs float x, y;/* two items */ problem Output float sum;/* sum of x and y */ float difference;/* difference of x and y */ float product;/* product of x and y */ float quotient;/* quotient of x divided by y */ Pseudo Code: Declare variables of x and y; Prompt user to input the value of x and y; Print the sum of x and y; Print the difference of x and y; Print the product of x and y; If y not equal to zero, print the quotient of x divided by y

Dale Roberts Example Program #include int main(void) { float x,y; float sum; printf(“Enter the value of x:”); scanf(“%f”, &x); printf(“\nEnter the value of y:”); scanf(“%f”, &y); sum = x + y; printf(“\nthe sum of x and y is:%f”,sum); printf(“\nthe sum of x and y is:%f”,x+y); printf(“\nthe difference of x and y is:%f”,x-y); printf(“\nthe product of x and y is:%f”,x*y); if (y != 0) printf(“\nthe quotient of x divided by y is:%f”,x/y); else printf(“\nquotient of x divided by y does not exist!\n”); return(0); } function name list of argument along with their types return value and its type Body inequality operator

Dale Roberts Data Type Conversion Rule #1 char, short  int float  double Rule #2 (double > long > unsigned > int) If either operand is double, the other is converted to double, and the result is double Otherwise, if either operand is long, the other is converted to long, and the result is long Otherwise, if either operand is unsigned, the other is converted to unsigned, and the result is unsigned Otherwise, the operand must be int

Dale Roberts Examples Example: here c: char, u: unsigned,i: int, s: short,l: long, Expression Final Data TypeExplanation c – s / i int short  int, int/int, char  int, int-int u * 3 – i unsigned int(3)  unsigned, unsigned*unsigned=unsigned, int  unsigned, unsigned-unsigned=unsigned u * 3.0 – i double unsigned  double, double*double, int  double, double-double=double c + i int char  int c double char  int (rule 1), int  double(rule 2) 3 * s * 1 long

Dale Roberts Data Type Conversion (cont.) Note: Conversion of char to int may be signed extension (Most machine like IBM PC, VAX, 68000, 68020) or zero filled Conversion of int to long is signed extension, so does short unsigned to long is zero filled Var = expr f = d; /* round off */ i = f; /* truncates fractions part, if the number is too big to fit, the result is undetermined */ i = l; s = i; and c = i /* eliminates high order bits */

Dale Roberts If a specific type is required, the following syntax may be used, called cast operator. (type) expr Example : float f=2.5; x = (int)f + 1; /* the result is 3, Q: will f value be changed? */ Unsigned int to int: there is not actual conversion between int and unsigned int. Example : (Assuming 2’s complement machine and int is 2 bytes long) unsigned i = 65535; int j = i; /* j will be –1 */ int j = -2; unsigned i = 1 + j; /* i= */