Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to C Programming Chapter 2 : Data Input, Processing and Output."— Presentation transcript:

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

2 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.

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

4 The printf Executable Statement Sends output to the screen which is the standard out device. General form printf(format descriptor, var1, var2, …); 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 %

5 Printf() examples Easiest to start with some examples printf(“%s\n”, “hello world”); Translated: “print hello world as a string followed by a newline character” printf(“%d\t%d\n”, 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 : %f : %f\n”, x, y, z); English: “print the value of the floating point variable x, followed by a space, then a colon, then a space, etc.

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

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

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

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

10 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; ??

11 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;

12 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 More Arithmetic Operators

13 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

14 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?

15 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.

16 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

17 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?

18 18 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)

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

20 20 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.

21 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.

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


Download ppt "Introduction to C Programming Chapter 2 : Data Input, Processing and Output."

Similar presentations


Ads by Google