Introduction to C Programming Chapter 2 Data Input

  • Slides: 22
Download presentation
Introduction to C Programming Chapter 2 : Data Input, Processing and Output

Introduction to C Programming Chapter 2 : Data Input, Processing and Output

Reading keyboard input • To be useful, program must be able to read data

Reading keyboard input • To be useful, program must be able to read data from external source, e. g. • • • User input from keyboard Database File Socket scanf library function reads user-typed input from the keyboard.

Scanf function • Basic syntax • scanf( format-specifier, &var 1, &var 2, etc. );

Scanf function • Basic syntax • scanf( format-specifier, &var 1, &var 2, etc. ); • Examples • int a; scanf(“%d”, &a); • double x; scanf(“%f”, &x); • Blocks program until user enters input!

The printf Executable Statement • • Sends output to the screen which is the

The printf Executable Statement • • Sends output to the screen which is the standard out device. General form • printf(format descriptor, var 1, var 2, …); • format descriptor is composed of • Ordinary characters • copied directly to output • Conversion specification • Causes conversion and printing of next argument to printf • Each conversion specification begins with %

Printf() examples • Easiest to start with some examples • printf(“%sn”, “hello world”); •

Printf() examples • Easiest to start with some examples • printf(“%sn”, “hello world”); • Translated: “print hello world as a string followed by a newline character” • printf(“%dt%dn”, j, k); • Translated: “print the value of the variable j as an integer followed by a tab followed by the value of the variable k as an integer followed by a new line. ” • printf(“%f : %fn”, x, y, z); • English: “print the value of the floating point variable x, followed by a space, then a colon, then a space, etc.

More on format statements • The format specifier in its simplest form is one

More on format statements • The format specifier in its simplest form is one of: • %s • sequence of characters known as a String • Not a fundamental datatype in C (really an array of char) • %d • Decimal integer (ie base ten) • %f • Floating point • Note that there are many other options. These are the most common, though, and are more than enough to get started.

List of format specifiers 7 • Both printf () and scanf () use the

List of format specifiers 7 • Both printf () and scanf () use the format specifiers • example: printf(“%c”, ’a’); scanf(“%d”, &a); • More format specifiers %c %d %i %f %o %s %u %x %% The character format specifier. The integer format specifier (same as %d). The floating-point format specifier. The unsigned octal format specifier. The string format specifier. The unsigned integer format specifier. The unsigned hexadecimal format specifier. Outputs a percent sign.

Invisible characters • Some special characters are not visible directly in the output stream.

Invisible characters • Some special characters are not visible directly in the output stream. These all begin with an escape character (ie ); • • n t a v newline horizontal tab alert bell vertical tab

Example on scanf and printf #include <stdio. h> // program reads and prints the

Example on scanf and printf #include <stdio. h> // program reads and prints the same thing int main() { int number ; printf (“ Enter a Number: ”); scanf (“%d”, &number); printf (“Number is %dn”, number); return 0; } Output : Enter a number: 4 Number is 4

Arithmetic Operations • Five simple binary arithmetic operators + “plus”, example, c = a

Arithmetic Operations • Five simple binary arithmetic operators + “plus”, example, c = a + b - “minus” , example, c = a - b * “times” , example, c = a * b / “divided by” , example, c = a/b % “modulus” , example, c = a % b • What are the values of c in each case above if int a = 10, b = 2; float a = 10, b = 2; int a = 10; float b = 2; ? ?

More Arithmetic Operators 11 • Prefix Increment : ++a • example: • int a=5;

More Arithmetic Operators 11 • Prefix Increment : ++a • example: • int a=5; • b=++a; // value of b=6; a=6; • Postfix Increment: a++ • example • int a=5; • b=a++; //value of b=5; a=6;

More Arithmetic Operators 12 • Modulus (remainder): % • example: • 12%5 = 2;

More Arithmetic Operators 12 • Modulus (remainder): % • example: • 12%5 = 2; • Assignment by addition: += • example: • int a=4; • a+=1; //(means a=a+1) value of a becomes 5 • Can use -, /, *, % also

Relational Operators • • Four basic operators for comparison of values in C. These

Relational Operators • • Four basic operators for comparison of values in C. These are typically called relational operators: > “greater than” < “less than” >= “greater than or equal to” <= “less than or equal to” • For the declaration int a=1, b=2, c; what is the value of the following expressions? a > b; a<b; a>=b; a<=b

Relational Operators, cont. • Typically used with conditional expressions, e. g. if (a <

Relational Operators, cont. • Typically used with conditional expressions, e. g. if (a < 1) then … • However, also completely valid expressions which evaluate to a result – either 1 (true) or 0 (false). int c, a=2, b=1; c = (a > b) What is the value of c?

Equality Operators • • • C distinguished between relational and equality operators. This is

Equality Operators • • • C distinguished between relational and equality operators. This is mainly to clarify rules of order of precedence. Two equality operators == “is equal to” != “is not equal to” • These follow all of the same rules for relational operators described on the previous slide.

Logical Operators • • Logical Operators are used to create compound expressions There are

Logical Operators • • Logical Operators are used to create compound expressions There are two logical operators in C • || “logical or” • A compound expression formed with || evaluates to 1 (true) if any one of its components is true • && “logical and” • A compound expression formed with && evaluates to true if all of its components are true

Logical Operators, cont. • Logical operators, like relational operators, are typically used in conditional

Logical Operators, cont. • Logical operators, like relational operators, are typically used in conditional expressions • if ( (a == 1) && (b < 3) || (c == 1) ) etc. • However, these can also be used in regular expressions • int a = 1, b = 2, c = 3, d; • d = ( a > b ) || ( c == (b – 1) ); • What is the value of d here?

Type Conversions • The operands of a binary operator must have a the same

Type Conversions • The operands of a binary operator must have a the same type and the result is also of the same type. • Integer division: c = (9 / 5)*(f - 32) • The operands of the division are both int and hence the result also would be int. For correct results, one may write c = (9. 0 / 5. 0)*(f - 32) • In case the two operands of a binary operator are different, but compatible, then they are converted to the same type by the compiler. The mechanism (set of rules) is called Automatic Type Casting. • c = (9. 0 / 5)*(f - 32) • It is possible to force a conversion of an operand. This is called Explicit Type casting. • c = ((float) 9 / 5)*(f - 32) 18

Automatic Type Casting • char and short operands are converted to int • Lower

Automatic Type Casting • char and short operands are converted to int • Lower data types are converted to the higher data types and result is of higher type. • The conversions between unsigned and signed types may not yield intuitive results. • Example float f; double d; long l; int i; short s; d + f f will be converted to double i / s s will be converted to int l / i i is converted to long; long result 19

Explicit Type Casting • The general form of a type casting operator is (type-name)

Explicit Type Casting • The general form of a type casting operator is (type-name) expression • It is generally a good practice to use explicit casts than to rely on automatic type conversions. • Example C = (float)9 / 5 * ( f – 32 ) • float to int conversion causes truncation of fractional part • double to float conversion causes rounding of digits • long int to int causes dropping of the higher order bits. 20

Operator Precedence 21 • Meaning of a + b * c ? • is

Operator Precedence 21 • Meaning of a + b * c ? • is it a+(b*c) or (a+b)*c ? • • All operators have precedence over each other *, / have more precedence over +, -. • If both *, / are used, associativity comes into picture. (more on this later) • example : • 5+4*3 = 5+12= 17.

Precedence Table 22 Highest on top ++ -- (Postfix) ++ -- (Prefix) * /

Precedence Table 22 Highest on top ++ -- (Postfix) ++ -- (Prefix) * / % + - << >> < > & | && ||