BIL 104E Introduction to Scientific and Engineering Computing Lecture 2
Scientific Notation Floating-point value: Can represent integer and non-integer values such as 2.5, , Scientific notation: A floating-point number is expressed as a mantissa times a power of ten, where mantissa has an absolute value greater than or equal to 1.0 and less than Example: 25.6 = 2.56x10^ = -4.0x10^ = 1.5x10^0 In exponential notation letter e is used to separate the mantissa from the exponent of the power of ten. Example: 25.6 = 2.56e = -4.0e = 1.5e0 Precision: The number of digits allowed by the computer for the decimal portion of the mantissa determines the precision or accuracy. Example: has 4 digits of precision Range: The number of digits allowed for the exponent determines the range Lecture 22
Numeric Data Types In C, numeric values are either integers or floating-point values. There are also non-numeric data types (such as characters) which will be discussed later. Integers: Specified by short, int and long according to the required range. Ranges of values are system dependent. C also allows unsigned qualifier where unsigned integer represents only positive values. Signed and unsigned integers represent same number of values but the ranges are different Lecture 23
Lecture 24 For most systems ranges are: INTEGERSMinMax short int long unsigned short065535
Numeric Data Types Floating point numbers: Specified by float (single-precision), double (double- precision), and long double (extended precision) according to the required precision and range which are also system dependent Lecture 25
Lecture 26 Floating point numbers FLOATING POINT NUMBERS PrecisionMax Exponent Maximum Value float6 digits e+38 double15 digits e+308 long double19 digits e+4932
printf Function The preprocessor directive #include gives the compiler the information that it needs to check referenced to the input/output functions in the Standard C library. printf function allows to print to the screen. Example: printf("Angle = %f radians \n",angle); The first argument which is enclosed in double quotation marks is the control string. The control string can contain text or conversion specifiers or both. – The conversion specifier ( in the example it is %f ) describes the format to use in printing the value of a variable. – The newline indicator (\n) causes a skip to a new line on the screen after the information has been printed. The second argument is the variable which is matched to the conversion specifier in the control string Lecture 27
Specifiers for Output Variable TypeOutput TypeSpecifier for output INTEGER VALUES short, intint%i (integer), %d (decimal) intshort%hi, %hd long %li, %ld intunsigned int%u intunsigned short%hu longunsigned long%lu FLOATING- POINT VALUES float, doubledouble%f (floating-point), %e (exponential form), %E (exponential form), %g (general), %G (general) long double %Lf, %Le, %LE, %Lg, %LG Lecture 28
minimum field width Specifier minimum field width specifier, which may be given between the percent sign (%) and the letter in a format specifier, ensures that the output reaches the minimum width. For example, %10f ensures that the output is at least 10 character spaces wide. If the field width specifies more positions than are needed for the value, the value is printed right-justified, which means that the extra positions are filled with blanks on the left of the value. To left-justify a value, a minus sign is inserted before the field width Lecture 29
minimum field width Specifier SpecifierValue Printed (□ represents blank) %i-145 %4d-145 %3i-145 %6i□□-145 %06i %-6i-145 □□ Lecture 210
precision Specifier SpecifierValue Printed (□ represents blank) %f %6.2f %+8.2f□ %7.5f %e e+02 %.3E1.579E+02 %g Lecture 211
Escape Character, backslash (\) SequenceCharacter Represented \b backspace, moves cursor to the left one character \f formfeed, goes to the top of a new page \n newline \r carriage return, returns to the beginning of the current line \t horizontal tab \v vertical tab \\ backslash \"\" double quote Lecture 212
scanf Function scanf function allows to enter values from the keyboard while the program is being executed. The first argument of the scanf function is a control string that specifies the types of the variables whose values are to be entered from the keyboard. The remaining arguments are the memory locations that correspond to the specifiers in the control string. The memory locations are indicated with the address operator (&). Example: scanf("%i",&year); printf("Enter the distance (m) and velocity (m/s): \n"); scanf("%lf %lf", &distance, &velocity); Lecture 213
Specifiers for Input Variable TypeSpecifier of Input int%i, %d short%hi, %hd long int%li, %ld unsigned int%u unsigned short%hu unsigned long%lu float%f, %e, %E, %g, %G double%lf, %le, %lE, %lg, %lG long double%Lf, %Le, %LE, %Lg, %LG Lecture 214
Character Data Numeric information is represented in a C program as integers or floating-point values. Numeric values are often used in arithmetic computations. Nonnumeric information may consist of alphabetic characters, digits, and special characters. Each character corresponds to a binary code value. The most commonly used binary codes are ASCII (American Standard Code for Information Interchange) and EBCDIC (Extended Binary Coded Decimal Interchange Code). A total of 128 characters can be represented in the ASCII code Lecture 215
char Data Type Note that the binary representation for a character digit is not equal to the binary representation for an integer digit. Nonnumeric information can be represented by constants or by variables. – A character constant is enclosed in single quotes, as in 'A', 'b', and '3'. – A variable that is going to contain a character can be defined as an integer or as a character data type (char). CharacterASCII CodeInteger Equivalent newline, \n % A Lecture 216
Character Initialization – The binary representation for a character can be interpreted as a character or as an integer. – To print a value as an integer, the %i or %d specifier is used; to print a value as a character, the %c specifier is used. – Example: int k=97; char c='a'; printf("value of k: %c; value of c: %c \n",k,c); printf("value of k: %,i; value of c: %i \n",k,c); Output: value of k: a; value of c: a value of k: 97; value of c: Lecture 217
Reading and Printing Characters – A text stream is composed of sequence of characters. – The end of a text stream is indicated with a special value, EOF, which is a symbolic constant defined in stdio.h. – stdin:The standard input for reading (usually keyboard) – stdout:The standard output for writing. (usually monitor) – stderr:The standard error for writing error messages. (always monitor) – Although the printf and scanf functions can be used to read characters using the %c specifier, there are special functions for reading and printing characters: The getc() function The putc() function The getchar() function The putchar() function Lecture 218
getc() and getchar() – The int getc(FILE *stream) function reads the next character from a file stream, and returns the integer value of the character as the function value. – The int getchar(void) function reads a character from the standard input and returns the integer value of the character as the function value. It is equivalent to getc(stdin). – Example: Lecture 219 #include main() { int ch1, ch2; printf("Enter two characters from the keyboard:\n "); ch1=getc(stdin); ch2=getchar(); printf("The first character you entered is: %c\n",ch1); printf("The second character you entered is: %c\n ",ch2); return 0; }
putc() and putchar() – The int putc(int c, FILE *stream) function prints the character that corresponds to the integer argument to the specified file stream. It then returns the same character as the function value. – The int putchar(int) function prints the character that corresponds to the integer argument to the computer screen. It then returns the same character as the function value. – Example: Lecture 220 #include main() { int ch1=65, ch2=98; printf("The character that has numeric value of %d is: ",ch1); putc(ch1,stdout); putc('\n',stdout); printf("The character that has numeric value of %d is: ",ch2); putchar(ch2); putchar('\n'); return 0; }