ANSI C Programming Simple Programs

Size: px
Start display at page:

Download "ANSI C Programming Simple Programs"

Transcription

1 ANSI C Programming Simple Programs /* This program computes the distance between two points */ #include <stdio.h> #include <stdlib.h> #include <math.h> main() { /* Declare and initialize variables */ double x1=1, y1=5, x2=4, y2=7, side_1, side_2, distance; /* Compute sides of a right triangle */ side_1=x2-x1; side_2=y2-y1; distance=sqrt(side_1*side_1 + side_2*side_2); /* Print distance */ printf ( the distance between the two points is %5.2f \n, distance); /* Exit program */ return EXIT_SUCCESS; } /* */ Comments Preprocessor directives: #include <stdio.h> information related to the output statement #include <stdlib.h> contains a constant we will use in exiting the program #include <math.h> contains information related to the function used to compute the square root of a value h extension specifies header files 1

2 < and > around the file names indicate that the files are included with the Standard C Library. This library is contained in the files that accompany ANSI C compiler. main contains two types of commands, declarations and statements. The declarations define the memory locations that will be used by the statements. The declarations may or may not give initial values to be stored in the memory locations. The following is the declarations part along with the comment preceding. /* Declare and initialize variables */ double x1=1, y1=5, x2=4, y2=7, side_1, side_2, distance; double indicates that the variable is double-precision floating-point The statements specify the operations to be performed in this program. /* Compute sides of a right triangle */ side_1=x2-x1; side_2=y2-y1; distance=sqrt(side_1*side_1 + side_2*side_2); /* Print distance */ printf ( the distance between the two points is %5.2f \n, distance); ATTENTION : DECLARATIONS AND STATEMENTS MUST END WITH A SEMICOLON!!! (Note: The details of the syntax in the statements will be discussed in a little bit!) To exit the program we use a return statement. The general form of a C program is: preprocessing functions main() { declarations; statements; } 2

3 Constants and Variables Constants are specific values, 2, 3.14, -15 Variables are memory locations that are assigned a name or identifier Identifiers are used to reference the value stored in the memory location Rules for selecting Identifiers 1. An identifier must begin with an alphabetic character or the underscore character 2. Alphabetic characters in an identifier can be lowercase or uppercase 3. An identifier can contain digits, but not as the first character 4. An identifier can be of any length but the first 31 characters`of the identifier must be unique 5. An identifier cannot be any of the keywords ( = words with special meaning to the C compiler) Attention: C is case sensitive KEYWORDS auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continuous for signed void default goto sizeof volatile do if static while Scientific Notation A floating-point value represents both integer and non-integer values ( 2.5, ) In scientific notation it is written as a mantissa times a power of 10. The mantissa has an absolute value greater than or equal to 1.0 and less than 10.0 ( e.g = 2.56 x 10 1 ) In exponential notation the letter e is used to separate the mantissa from the exponent of the power of ten. ( e.g = 2.56e1, 1.5 = 1.5e0) The number of digits for the decimal portion of the mantissa determines the precision (accuracy) and the number of digits allowed for the exponent determines the range. Numeric Data Types Thet are either integers or floating-point values. Non-numeric data types will be discussed later. 3

4 Numeric Data Type Limits Integers short maximum = 32,767 int maximum = 32,767 long maximum = 2,147,483,647 Floating Point float 6 digits of precision maximum exponent = 38 double Symbolic Constants PI maximum value = e digits of precision maximum exponent = 308 maximum value = e long double 19 digits of precision maximum exponent = 4932 maximum value = e Consider the following preprocessing directive to assign the value of to the variable PI. #define PI So, area = PI*radius*radius; Assignment Statements An assignment statement is used to assign value to an identifier. The general form of the assignment statement is identifier = expression; an expression can be a constant, another variable or the result of an operation. The following two sets of statements declare and give values to the variables sum and x1. double sum = 10.5; double sum; int x1=3; int x1;. sum = 10.5; x1 = 3; The set of statements on the left define and initialize the variables at the same time the statements on the right could be used at any point in the program and thus may be used to change (as opposed to initialize) the values in variables. Multiple statements are also allowed in C as it is shown: x=y=z=0; 4

5 We can also assign a value from one variable to another with an assignment statement: rate = state_tax; In this case the value for the rate depends on the value of state_tax Numeric conversion : If a value is moved to a data type that is higher in order, no information will be lost; if it is moved to a value that is lower in order, information may be lost. The order is as follows: high: long double double float long integer integer low: short integer Arithmetic operators: area_square = side * side * is used for multiplication + is used for addition - is used for subtraction / is used for division Consider this x = x + 1; In algebra this statement is not valid. In C it meas that x is assigned the value of x plus 1. For example if the value of x is 5 before the execution of the statement, after the execution the value of x will be 6. C also uses a modulus operator, %, which is used to compute the remainder in a division between two integers. Examples 5%2 = 1, 7%2=1 6%3=0 a=9, b=4 then a/b = 2 and a%b = 1. Of course you are not allowed to divide by zero! Binary operators ( *, +, -, /, %): they operate on two values. The result of the binary oprators with values of the same type is also a value of the same type. Unary operators (+, -): they operate on one value, e.g. -x Keep in mind: an integer division can sometimes produce unexpected results because any decimal portion of the integer is dropped; the result is a truncated result not a rounded result. Mixed operation: an operation between values of different types. Before the noperation is performed, the value with the lower type is converted or promoted to the higher type and the operation is performed with values of the same type. 5

6 Int sum, count; float average; average = sum/count; If sum = 18 and count = 5 the average = 3 not 3.6!!!!!! To compute the sum correctly we use a cast operator a unary operator that allows us to specify a type change in the value before the next computation. So, average = (float) sum/count; Priority of operations Overflow and Underflow Increment and decrement operators ++count; count++; y--; is equivalent to y=y-1; w = ++x-y is equivalent to x=x+1; w=x-y; Precedence of Arithmetic Operators Precedence Operator Associativity 1 parentheses ( ) innermost first 2 unary operators right to left + - type 3 binary operators left to right * / % 4 binary operators left to right + - w = x++ -y; is equivalent to w=x-y; x=x+1; Abbreviated assignment operators Any statrement of the form identifier = identifier operator expression; can be written in this form: identifier operator = expression; What do we mean by the following? a=b+=c+d; To evaluate this property we use the following table 6

7 Precedence of Arithmetic and Assignment Operators Precedence Operator Associativity 1 parentheses innermost first 2 unary operators right to left binary operators left to right * / % 4 binary operators left to right assignment operators right to left =, +=, - =, /=, %= And the statement a=b+=c+d; is equivalent to the following: a=(b+= (c+d)); And if we replace the abbreviated forms with the longer forms of the operations we have a=(b=b+(c+d)); or b=b+(c+d); a=b; NOTE: USING ABBREVIATED ASSIGNMENT STATEMENTS IN A MULTIPLE ASSIGNMENT STATEMENT IS NOT RECOMMENDED!!!!! Standard Input and Output #include <stdio.h> This directive gives the compiler the information that it needs to check references to the input/output functions in the Standard C library printf FUNCTION The printf function allows us to print values and explanatory text to the screen. printf( Angle = %f radians \n, angle); The printf function contains two arguments - a control string and an identifier to specify the value to be printed. A control string is enclosed in double quotation marks and it can contain text or conversion specifiers or both. A conversion specifier describes the format to use in printing the value of a variable Let us analyze in full detail the above example. The control string specifies that the character angle will be printed on the screen. The next group of characters (%f) represents a conversion specifier that indicates that a value is to be printed next, which then will be followed by the characters radians. The next combination of characters (\n) represents new line indicator. The second argument in the printf statement is the variable angle, it is matched to the conversion specifier in the control string. Note: If the value of the angle is 2.84 the output generated by this particular example is: Angle = radians. 7

8 Now let us look closer to the conversion specifiers Numeric Conversion Specifiers for Output Statements Variable Type Output Type Specifier Integer Values short, int int %i, %d int short %hi, %hd long long %li, %ld int unsigned in %u int unsigned short %hu long unsigned long %lu Floating-Point Values float, double double %f, %e, %E, %g, %G long double Long double %Lf, %Le, %LE, %Lg, %LG For example, to print a short or an int use an %i (integer) or %d (decimal) specifier (either specifier gives the same results); and to print a long, use an %li or %ld specifier. To print a float or a double, use an %f ( floating-point form), %e (exponential form as in 2.3e+02), or %E (exponential form as in 2.3E+02). The %g (general) specifier prints the value using an %f or %e specifier, depending on the size of the value; the %G specifier is the same as the %g, except that it prints the value using an %f or %E specifier. Field width After selecting the correct specifier additional information can be added. A minimum field width can be specified along with an optional precision that controls the number of characters printed. The field width and the precision can be used together or sepaerately. If the precision is omitted, a default of 6 is used for the %f specifier. The decimal portion of a value is rounded to the specified precision; thus, the value will be printed as 14.52, if a %2f specification is used. The specification %5i indicates that a short or an int is to be printed with a minimum field width of 5. The field width will be increased if necessary to print the corresponding value. If the field width specifies more positions than are needed for the value the value is right-justified, which means that the extra positions are filled with blanks on the left of the value. To left-justified a value a minus sign is inserted before the field width, as in %-8i. If a plus sign is inserted before the field width as in %+6f, a sign will always be printed with a value. Example: double Specifier Value Printed %f %6.2f %+8.2f b %7.5f %e e+02 %E 1.579E+02 %G

9 Escape character The backlash, \, is an escape character when it is used in the control string. The other escape sequences recognized by C are listed below: Sequence \ a \ b \ f \ n newline \ r \ t \ v \\ \? Character Represented alert (bell) character backspace formfeed carriage return horizontal tab vertical tab backlash question mark \ single quote \ double quote scanf FUNCTION This function allows us to enter values from the keyboard when a program is executed. Suppose that a program computes the number of acres of new forest growth after a specified period of time elapses. If the time elapsed is a constant in the program, we have to change the value of the constant and then to recompile and reexecute the program to obtain the output for a different time period. Alternatively if we use the scanf function to read the time period, we do not need to recompile the program, we only need to reexecute it and enter the desired time period from the keyboard. The first argument in the scanf function is a control string that specifies the types of the variables whose values are to entered from the keyboard. The type of specifiers are shown in the following table. Numeric Conversion Specifiers for Input Statements Variable Type Specifier Integer Values int %i, %d short %hi, %hd long int %li, %ld unsigned in %u unsigned short %hu unsigned long %lu Floating-Point Values float, double %f, %e, %E, %g, %G %lf, %le, %le, %llg, %lg long double %Lf, %Le, %LE, %Lg, %LG 9

10 Thus for example, the specifiers for an integer variable are %i or %d; The specifiers for a float variable are %f, %e, %g; and the specifiers for a double variable are %lf, %le, %lg. It is very important to use a correct specifier. For examples errors will occur if you use an %f specifier to read the value for a double variable. The remaining arguments in the scanf function are memory locations that corrwspond to the specifiers in the control string. These memory locations are indicated with the adreess operator, &. This operator is unary operator that determines the memory address of the identifier with which it is associated. Thus, if the value to be entered through the keyboard is an integer that is to be stored in the variable year, we could use this statement to read the value: scanf( %i, &year); The precedence level of the address operator is the same as the other unary operators. If there are several unary operators in the same statement, there are associated from right to left. A common error in the scanf statement is to omit the address operator for the identifiers. If we wish to read more than one value from the keyboard we can use statements such as the following: scanf( %lf %lf, &distance, &velocity); When this statement is executed the program will read two values from the keyboard and convert them into two double values. Mathematical functions #include <math.h> fabs(x), sqrt(x), pow(x,y), ceil(x), floor(x), exp(x), log(x), log10(x), sin(x), cos(x), tan(x) Debugging Notes 1. Declarations and C statements must end with a semicolon. 2. Preprocessor directives do not enter with a semicolon. 3. If possible avoid assignments that could potentially cause information to be lost. 4. Use paretheses in a long expession to be sure it is evaluated as desired. 5. Use double precision or extrended precision to avoid problems with exponent overflow or underflow. 6. Be sure that the specifiers matches the variable type in a scanf statement. 7. Errors can occur if user input values cannot be converted correctly to the specifier variable type in a scanf statement. 8. Do not forget the address operator with identifiers in the scanf statement. 9. Remember that symbolic constant definitions do not end withn a semicolon. 10. In nested function references, each set of arguments must be in its own set of parentheses. 11. Remember that the logarithmic functions cannot be used with negative or zero values for arguments. 12. Be sure to use angles in radians with the trigonometric functions. 10

11 13. Remember that many of the inverse trigonometric functions and hyperbolic functions have restrictions on the ranges of allowable input values Preprocessor directives #include <stdio.h> #include <stdlib.h> #include <math.h> C Statements Summary Preprocessor directive to define a symbolic constant #define PI Declarations for integers short sum=0; int year_1, year_2; long k; Declarations for floating-point values float height_1, height_2; double length=10, side1, side2; long double distance, velocity; Argument statement area = 0.5 * base * (height_1+height_2); Keyboard input statement scanf( %i, &year); Screen output statement printf( The area is %f square feet \n, area); Program exit statement Return EXIT_SUCCESS; Happy Programming! 11

C: How to Program. Week /Mar/05

C: How to Program. Week /Mar/05 1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers

More information

Chapter 2. Outline. Simple C++ Programs

Chapter 2. Outline. Simple C++ Programs Chapter 2 Simple C++ Programs Outline Objectives 1. Building C++ Solutions with IDEs: Dev-cpp, Xcode 2. C++ Program Structure 3. Constant and Variables 4. C++ Operators 5. Standard Input and Output 6.

More information

Chapter 2 - Introduction to C Programming

Chapter 2 - Introduction to C Programming Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic

More information

Chapter 4: Basic C Operators

Chapter 4: Basic C Operators Chapter 4: Basic C Operators In this chapter, you will learn about: Arithmetic operators Unary operators Binary operators Assignment operators Equalities and relational operators Logical operators Conditional

More information

Data types, variables, constants

Data types, variables, constants Data types, variables, constants Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic in C 2.6 Decision

More information

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

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath UNIT - I Introduction to C Programming Introduction to C C was originally developed in the year 1970s by Dennis Ritchie at Bell Laboratories, Inc. C is a general-purpose programming language. It has been

More information

Engineering Problem Solving with C++, Etter/Ingber

Engineering Problem Solving with C++, Etter/Ingber Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs C++, Second Edition, J. Ingber 1 Simple C++ Programs Program Structure Constants and Variables C++ Operators Standard Input

More information

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

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants Data types, variables, constants Outline.1 Introduction. Text.3 Memory Concepts.4 Naming Convention of Variables.5 Arithmetic in C.6 Type Conversion Definition: Computer Program A Computer program is a

More information

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

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Introduction to C Programming Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline Printing texts Adding 2 integers Comparing 2 integers C.E.,

More information

Data Types and Variables in C language

Data Types and Variables in C language Data Types and Variables in C language Basic structure of C programming To write a C program, we first create functions and then put them together. A C program may contain one or more sections. They are

More information

DEPARTMENT OF MATHS, MJ COLLEGE

DEPARTMENT OF MATHS, MJ COLLEGE T. Y. B.Sc. Mathematics MTH- 356 (A) : Programming in C Unit 1 : Basic Concepts Syllabus : Introduction, Character set, C token, Keywords, Constants, Variables, Data types, Symbolic constants, Over flow,

More information

Programming and Data Structures

Programming and Data Structures Programming and Data Structures Teacher: Sudeshna Sarkar sudeshna@cse.iitkgp.ernet.in Department of Computer Science and Engineering Indian Institute of Technology Kharagpur #include int main()

More information

Chapter 1 & 2 Introduction to C Language

Chapter 1 & 2 Introduction to C Language 1 Chapter 1 & 2 Introduction to C Language Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 1 & 2 - Introduction to C Language 2 Outline 1.1 The History

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering

More information

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

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University Fundamental Data Types CSE 130: Introduction to Programming in C Stony Brook University Program Organization in C The C System C consists of several parts: The C language The preprocessor The compiler

More information

Programming for Engineers Introduction to C

Programming for Engineers Introduction to C Programming for Engineers Introduction to C ICEN 200 Spring 2018 Prof. Dola Saha 1 Simple Program 2 Comments // Fig. 2.1: fig02_01.c // A first program in C begin with //, indicating that these two lines

More information

C Programming a Q & A Approach

C Programming a Q & A Approach C Programming a Q & A Approach by H.H. Tan, T.B. D Orazio, S.H. Or & Marian M.Y. Choy Chapter 2 Variables, Arithmetic Expressions and Input/Output 2.1 Variables: Naming, Declaring, Assigning and Printing

More information

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

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are: LESSON 1 FUNDAMENTALS OF C The purpose of this lesson is to explain the fundamental elements of the C programming language. C like other languages has all alphabet and rules for putting together words

More information

Programming. C++ Basics

Programming. C++ Basics Programming C++ Basics Introduction to C++ C is a programming language developed in the 1970s with the UNIX operating system C programs are efficient and portable across different hardware platforms C++

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Lecture 3 - Constants, Variables, Data Types, And Operations Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad Outline C Program Data types Variables

More information

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University Lesson #3 Variables, Operators, and Expressions Variables We already know the three main types of variables in C: int, char, and double. There is also the float type which is similar to double with only

More information

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

Full file at   C How to Program, 6/e Multiple Choice Test Bank 2.1 Introduction 2.2 A Simple Program: Printing a Line of Text 2.1 Lines beginning with let the computer know that the rest of the line is a comment. (a) /* (b) ** (c) REM (d)

More information

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

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed C Overview C OVERVIEW Goals speed portability allow access to features of the architecture speed C fast executables allows high-level structure without losing access to machine features many popular languages

More information

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

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA. DECLARATIONS Character Set, Keywords, Identifiers, Constants, Variables Character Set C uses the uppercase letters A to Z. C uses the lowercase letters a to z. C uses digits 0 to 9. C uses certain Special

More information

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

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure C Overview Basic C Program Structure C OVERVIEW BASIC C PROGRAM STRUCTURE Goals The function main( )is found in every C program and is where every C program begins speed execution portability C uses braces

More information

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

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language 1 History C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC

More information

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

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach C Fundamentals & Formatted Input/Output adopted from KNK C Programming : A Modern Approach C Fundamentals 2 Program: Printing a Pun The file name doesn t matter, but the.c extension is often required.

More information

Basics of Programming

Basics of Programming Unit 2 Basics of Programming Problem Analysis When we are going to develop any solution to the problem, we must fully understand the nature of the problem and what we want the program to do. Without the

More information

A Fast Review of C Essentials Part I

A Fast Review of C Essentials Part I A Fast Review of C Essentials Part I Structural Programming by Z. Cihan TAYSI Outline Program development C Essentials Functions Variables & constants Names Formatting Comments Preprocessor Data types

More information

Computer System and programming in C

Computer System and programming in C 1 Basic Data Types Integral Types Integers are stored in various sizes. They can be signed or unsigned. Example Suppose an integer is represented by a byte (8 bits). Leftmost bit is sign bit. If the sign

More information

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

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance. 2.1 Introduction (No questions.) 2.2 A Simple Program: Printing a Line of Text 2.1 Which of the following must every C program have? (a) main (b) #include (c) /* (d) 2.2 Every statement in C

More information

6.096 Introduction to C++ January (IAP) 2009

6.096 Introduction to C++ January (IAP) 2009 MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Welcome to 6.096 Lecture

More information

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

These are reserved words of the C language. For example int, float, if, else, for, while etc. 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 a sequence of letters and digits, but must start with a letter.

More information

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park Variables in C CMSC 104, Fall 2012 John Y. Park 1 Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement 2 What Are Variables in C? Variables in C have the

More information

Fundamentals of Programming Session 4

Fundamentals of Programming Session 4 Fundamentals of Programming Session 4 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2011 These slides are created using Deitel s slides, ( 1992-2010 by Pearson Education, Inc).

More information

Structured Programming. Dr. Mohamed Khedr Lecture 4

Structured Programming. Dr. Mohamed Khedr Lecture 4 Structured Programming Dr. Mohamed Khedr http://webmail.aast.edu/~khedr 1 Scientific Notation for floats 2.7E4 means 2.7 x 10 4 = 2.7000 = 27000.0 2.7E-4 means 2.7 x 10-4 = 0002.7 = 0.00027 2 Output Formatting

More information

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

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah Lecturer Department of Computer Science & IT University of Balochistan 1 Outline p Introduction p Program development p C language and beginning with

More information

Fundamentals of C. Structure of a C Program

Fundamentals of C. Structure of a C Program Fundamentals of C Structure of a C Program 1 Our First Simple Program Comments - Different Modes 2 Comments - Rules Preprocessor Directives Preprocessor directives start with # e.g. #include copies a file

More information

Lecture 02 C FUNDAMENTALS

Lecture 02 C FUNDAMENTALS Lecture 02 C FUNDAMENTALS 1 Keywords C Fundamentals auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void

More information

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

Chapter 2: Overview of C. Problem Solving & Program Design in C Chapter 2: Overview of C Problem Solving & Program Design in C Addison Wesley is an imprint of Why Learn C? Compact, fast, and powerful High-level Language Standard for program development (wide acceptance)

More information

ET156 Introduction to C Programming

ET156 Introduction to C Programming ET156 Introduction to C Programming g Unit 22 C Language Elements, Input/output functions, ARITHMETIC EXPRESSIONS AND LIBRARY FUNCTIONS Instructor : Stan Kong Email : skong@itt tech.edutech.edu General

More information

CS1010E Lecture 3 Simple C Programs Part 2

CS1010E Lecture 3 Simple C Programs Part 2 CS1010E Lecture 3 Simple C Programs Part 2 Joxan Jaffar Block COM1, Room 3-11, +65 6516 7346 www.comp.nus.edu.sg/ joxan cs1010e@comp.nus.edu.sg Semester II, 2015/2016 Lecture Outline Standard Input and

More information

Basic Elements of C. Staff Incharge: S.Sasirekha

Basic Elements of C. Staff Incharge: S.Sasirekha Basic Elements of C Staff Incharge: S.Sasirekha Basic Elements of C Character Set Identifiers & Keywords Constants Variables Data Types Declaration Expressions & Statements C Character Set Letters Uppercase

More information

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP Operators and Expression Dr Muhamad Zaini Yunos JKBR, FKMP Arithmetic operators Unary operators Relational operators Logical operators Assignment operators Conditional operators Comma operators Operators

More information

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

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 1 BIL 104E Introduction to Scientific and Engineering Computing Lecture 1 Introduction As engineers and scientists why do we need computers? We use computers to solve a variety of problems ranging from evaluation

More information

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

6-1 (Function). (Function) !*+!#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x (Function) -1.1 Math Library Function!"#! $%&!'(#) preprocessor directive #include !*+!"#!, Function Description Example sqrt(x) square root of x sqrt(900.0) is 30.0 sqrt(9.0) is 3.0 exp(x) log(x)

More information

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

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14 C introduction Variables Variables 1 / 14 Contents Variables Data types Variable I/O Variables 2 / 14 Usage Declaration: t y p e i d e n t i f i e r ; Assignment: i d e n t i f i e r = v a l u e ; Definition

More information

CS102: Variables and Expressions

CS102: Variables and Expressions CS102: Variables and Expressions The topic of variables is one of the most important in C or any other high-level programming language. We will start with a simple example: int x; printf("the value of

More information

C - Basic Introduction

C - Basic Introduction C - Basic Introduction C is a general-purpose high level language that was originally developed by Dennis Ritchie for the UNIX operating system. It was first implemented on the Digital Equipment Corporation

More information

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

More information

2. Numbers In, Numbers Out

2. Numbers In, Numbers Out REGZ9280: Global Education Short Course - Engineering 2. Numbers In, Numbers Out Reading: Moffat, Chapter 2. REGZ9280 14s2 2. Numbers In, Numbers Out 1 The Art of Programming Think about the problem Write

More information

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

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9 Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Uppercase Alphabets Lowercase Alphabets Character Set A, B, C, Y, Z a, b, c, y, z Digits

More information

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018 CSE 1001 Fundamentals of Software Development 1 Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018 Identifiers, Variables and Data Types Reserved Words Identifiers in C Variables and Values

More information

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

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double ME 172 Lecture 2 variables scanf() printf() 07/03/2011 ME 172 1 Data Types and Modifier Basic data types are char int float double Modifiers signed unsigned short Long 07/03/2011 ME 172 2 1 Data Types

More information

Presented By : Gaurav Juneja

Presented By : Gaurav Juneja Presented By : Gaurav Juneja Introduction C is a general purpose language which is very closely associated with UNIX for which it was developed in Bell Laboratories. Most of the programs of UNIX are written

More information

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

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar.. .. 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!

More information

Data Type Fall 2014 Jinkyu Jeong

Data Type Fall 2014 Jinkyu Jeong Data Type Fall 2014 Jinkyu Jeong (jinkyu@skku.edu) 1 Syntax Rules Recap. keywords break double if sizeof void case else int static... Identifiers not#me scanf 123th printf _id so_am_i gedd007 Constants

More information

C Programming

C Programming 204216 -- C Programming Chapter 3 Processing and Interactive Input Adapted/Assembled for 204216 by Areerat Trongratsameethong A First Book of ANSI C, Fourth Edition Objectives Assignment Mathematical Library

More information

Introduction to C++ Introduction and History. Characteristics of C++

Introduction to C++ Introduction and History. Characteristics of C++ Introduction and History Introduction to C++ Until 1980, C programming was widely popular, and slowly people started realizing the drawbacks of this language and at the same time, the engineers had come

More information

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

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York CSc 10200! Introduction to Computing Lecture 2-3 Edgardo Molina Fall 2013 City College of New York 1 C++ for Engineers and Scientists Third Edition Chapter 2 Problem Solving Using C++ 2 Objectives In this

More information

Unit 4. Input/Output Functions

Unit 4. Input/Output Functions Unit 4 Input/Output Functions Introduction to Input/Output Input refers to accepting data while output refers to presenting data. Normally the data is accepted from keyboard and is outputted onto the screen.

More information

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types and

More information

Programming in C++ 5. Integral data types

Programming in C++ 5. Integral data types Programming in C++ 5. Integral data types! Introduction! Type int! Integer multiplication & division! Increment & decrement operators! Associativity & precedence of operators! Some common operators! Long

More information

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types Introduction to Computer Programming in Python Dr William C Bulko Data Types 2017 What is a data type? A data type is the kind of value represented by a constant or stored by a variable So far, you have

More information

INTRODUCTION 1 AND REVIEW

INTRODUCTION 1 AND REVIEW INTRODUTION 1 AND REVIEW hapter SYS-ED/ OMPUTER EDUATION TEHNIQUES, IN. Programming: Advanced Objectives You will learn: Program structure. Program statements. Datatypes. Pointers. Arrays. Structures.

More information

Department of Computer Applications

Department of Computer Applications Sheikh Ul Alam Memorial Degree College Mr. Ovass Shafi. (Assistant Professor) C Language An Overview (History of C) C programming languages is the only programming language which falls under the category

More information

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

Programming in C++ 4. The lexical basis of C++ Programming in C++ 4. The lexical basis of C++! Characters and tokens! Permissible characters! Comments & white spaces! Identifiers! Keywords! Constants! Operators! Summary 1 Characters and tokens A C++

More information

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered )   FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING ( Word to PDF Converter - Unregistered ) http://www.word-to-pdf-converter.net FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING INTRODUCTION TO C UNIT IV Overview of C Constants, Variables and Data Types

More information

C Language, Token, Keywords, Constant, variable

C Language, Token, Keywords, Constant, variable C Language, Token, Keywords, Constant, variable A language written by Brian Kernighan and Dennis Ritchie. This was to be the language that UNIX was written in to become the first "portable" language. C

More information

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura CS 2060 Week 4 1 Modularizing Programs Modularizing programs in C Writing custom functions Header files 2 Function Call Stack The function call stack Stack frames 3 Pass-by-value Pass-by-value and pass-by-reference

More information

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers Computer Science & Engineering 150A Problem Solving Using Computers Lecture 03 - Stephen Scott (Adapted from Christopher M. Bourke) 1 / 41 Fall 2009 Chapter 3 3.1 Building Programs from Existing Information

More information

2. Numbers In, Numbers Out

2. Numbers In, Numbers Out COMP1917: Computing 1 2. Numbers In, Numbers Out Reading: Moffat, Chapter 2. COMP1917 15s2 2. Numbers In, Numbers Out 1 The Art of Programming Think about the problem Write down a proposed solution Break

More information

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

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,

More information

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O Overview of C Basic Data Types Constants Variables Identifiers Keywords Basic I/O NOTE: There are six classes of tokens: identifiers, keywords, constants, string literals, operators, and other separators.

More information

BITG 1233: Introduction to C++

BITG 1233: Introduction to C++ BITG 1233: Introduction to C++ 1 Learning Outcomes At the end of this lecture, you should be able to: Identify basic structure of C++ program (pg 3) Describe the concepts of : Character set. (pg 11) Token

More information

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions Computer Science & Engineering 150A Problem Solving Using Computers Lecture 03 - Functions Stephen Scott (Adapted from Christopher M. Bourke) Fall 2009 1 / 1 cbourke@cse.unl.edu Chapter 3 3.1 Building

More information

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

Intro to Computer Programming (ICP) Rab Nawaz Jadoon Intro to Computer Programming (ICP) Rab Nawaz Jadoon DCS COMSATS Institute of Information Technology Assistant Professor COMSATS IIT, Abbottabad Pakistan Introduction to Computer Programming (ICP) What

More information

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

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University C Programming Notes Dr. Karne Towson University Reference for C http://www.cplusplus.com/reference/ Main Program #include main() printf( Hello ); Comments: /* comment */ //comment 1 Data Types

More information

Lecture 14. Daily Puzzle. Math in C. Rearrange the letters of eleven plus two to make this mathematical statement true. Eleven plus two =?

Lecture 14. Daily Puzzle. Math in C. Rearrange the letters of eleven plus two to make this mathematical statement true. Eleven plus two =? Lecture 14 Math in C Daily Puzzle Rearrange the letters of eleven plus two to make this mathematical statement true. Eleven plus two =? Daily Puzzle SOLUTION Eleven plus two = twelve plus one Announcements

More information

EEE145 Computer Programming

EEE145 Computer Programming EEE145 Computer Programming Content of Topic 2 Extracted from cpp.gantep.edu.tr Topic 2 Dr. Ahmet BİNGÜL Department of Engineering Physics University of Gaziantep Modifications by Dr. Andrew BEDDALL Department

More information

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

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. C How to Program, 6/e 1992-2010 by Pearson Education, Inc. An important part of the solution to any problem is the presentation of the results. In this chapter, we discuss in depth the formatting features

More information

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

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science The component base of C language Nguyễn Dũng Faculty of IT Hue College of Science Content A brief history of C Standard of C Characteristics of C The C compilation model Character set and keyword Data

More information

Introduction to C Language

Introduction to C Language Introduction to C Language Instructor: Professor I. Charles Ume ME 6405 Introduction to Mechatronics Fall 2006 Instructor: Professor Charles Ume Introduction to C Language History of C Language In 1972,

More information

C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ Programming: From Problem Analysis to Program Design, Third Edition C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine

More information

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University Overview of C, Part 2 CSE 130: Introduction to Programming in C Stony Brook University Integer Arithmetic in C Addition, subtraction, and multiplication work as you would expect Division (/) returns the

More information

C Program Structures

C Program Structures Review-1 Structure of C program Variable types and Naming Input and output Arithmetic operation 85-132 Introduction to C-Programming 9-1 C Program Structures #include void main (void) { } declaration

More information

Lecture 3 Tao Wang 1

Lecture 3 Tao Wang 1 Lecture 3 Tao Wang 1 Objectives In this chapter, you will learn about: Arithmetic operations Variables and declaration statements Program input using the cin object Common programming errors C++ for Engineers

More information

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a = Morteza Noferesti Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a = 5 x = a + b valid identifier in C

More information

C Functions. 5.2 Program Modules in C

C Functions. 5.2 Program Modules in C 1 5 C Functions 5.2 Program Modules in C 2 Functions Modules in C Programs combine user-defined functions with library functions - C standard library has a wide variety of functions Function calls Invoking

More information

BLM2031 Structured Programming. Zeyneb KURT

BLM2031 Structured Programming. Zeyneb KURT BLM2031 Structured Programming Zeyneb KURT 1 Contact Contact info office : D-219 e-mail zeynebkurt@gmail.com, zeyneb@ce.yildiz.edu.tr When to contact e-mail first, take an appointment What to expect help

More information

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

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are

More information

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14 Variables in C CMSC 104, Spring 2014 Christopher S. Marron (thanks to John Park for slides) 1 Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement 2 What

More information

Work relative to other classes

Work relative to other classes Work relative to other classes 1 Hours/week on projects 2 C BOOTCAMP DAY 1 CS3600, Northeastern University Slides adapted from Anandha Gopalan s CS132 course at Univ. of Pittsburgh Overview C: A language

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information. Chapter 3 Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus Lecture 03 - Introduction To Functions Christopher M. Bourke cbourke@cse.unl.edu 3.1 Building Programs from Existing

More information

Operators and Expressions:

Operators and Expressions: Operators and Expressions: Operators and expression using numeric and relational operators, mixed operands, type conversion, logical operators, bit operations, assignment operator, operator precedence

More information

Number Systems, Scalar Types, and Input and Output

Number Systems, Scalar Types, and Input and Output Number Systems, Scalar Types, and Input and Output Outline: Binary, Octal, Hexadecimal, and Decimal Numbers Character Set Comments Declaration Data Types and Constants Integral Data Types Floating-Point

More information

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Fundamentals of Programming. Lecture 3: Introduction to C Programming Fundamentals of Programming Lecture 3: Introduction to C Programming Instructor: Fatemeh Zamani f_zamani@ce.sharif.edu Sharif University of Technology Computer Engineering Department Outline A Simple C

More information

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators Expressions 1 Expressions n Variables and constants linked with operators Arithmetic expressions n Uses arithmetic operators n Can evaluate to any value Logical expressions n Uses relational and logical

More information

CMSC 104 -Lecture 5 John Y. Park, adapted by C Grasso

CMSC 104 -Lecture 5 John Y. Park, adapted by C Grasso CMSC 104 -Lecture 5 John Y. Park, adapted by C Grasso 1 Topics Naming Variables Declaring Variables Using Variables The Assignment Statement 2 a + b Variables are notthe same thing as variables in algebra.

More information