Bài giảng Lập trình C - Session 3: Operators, Expressions and InputOutput in C

 Operators and Expressions:

Expression definition

Classify arithmetic, relational, logical and binary logical operators

Operator precedence

Assignment operator and Casting

Input/Output in C

Formatted I/O functions : scanf()/printf()

Character I/O functions :getchar()/putchar(char c)

 

ppt40 trang | Chuyên mục: C/C++ | Chia sẻ: dkS00TYs | Lượt xem: 1816 | Lượt tải: 1download
Tóm tắt nội dung Bài giảng Lập trình C - Session 3: Operators, Expressions and InputOutput in C, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
Operators and Expression * Operators,Expressionsand Input/Output in C Session 3 Elementary Programming with C/Session 3/ * of 41 Objectives Operators and Expressions: Expression definition Classify arithmetic, relational, logical and binary logical operators Operator precedence Assignment operator and Casting Input/Output in C Formatted I/O functions : scanf()/printf() Character I/O functions :getchar()/putchar(char c) Operators and Expression * Operators, Expressions Elementary Programming with C/Session 3/ * of 41 Expressions Combination of Operators and Operands Example 2 * y + 5 Operands Operators Elementary Programming with C/Session 3/ * of 41 Operators 4 Types Arithmetic Relational Logical Bitwise Elementary Programming with C/Session 3/ * of 41 Arithmetic Expressions Mathematical expressions can be expressed in C using arithmetic operators Examples a * (b + c/d) - 22 ++i % 7 5 + (c = 3 + 8) Elementary Programming with C/Session 3/ * of 41 Relational & Logical Operators-1 Used to test the relationship between two variables, or between a variable and a constant Relational Operators Elementary Programming with C/Session 3/ * of 41 Logical operators are symbols that are used to combine or negate expressions containing relational operators Relational & Logical Operators-2 Expressions that use logical operators return zero for false, and 1 for true Example: if (a>10) && (a 3 AND 3 3 AND 3 3 AND 33 AND 33] AND [3 10 AND (2+2^4-8/4 > 6 OR (211)) Elementary Programming with C/Session 3/ * of 41 The solution : 1) 5+9*3^2-4 > 10 AND (2+2^4-8/4 > 6 OR (True AND False)) The inner parenthesis takes precedence over all other operators and the evaluation within this is as per the regular conventions 5+9*3^2-4 > 10 AND (2+2^4-8/4 > 6 OR False) 5+9*3^2-4 >10 AND (2+16-8/4 > 6 OR False) Next the outer parentheses is evaluated 5+9*3^2-4 > 10 AND (2+16-2 > 6 OR False) 5+9*3^2-4 > 10 AND (18-2 > 6 OR False) 5+9*3^2-4 > 10 AND (16 > 6 OR False) Changing Precedence (cont.) Elementary Programming with C/Session 3/ * of 41 5+9*3^2-4 > 10 AND (True OR False) 5+9*3^2-4 > 10 AND True 5+9*9-4>10 AND True The expression to the left is evaluated as per the conventions 5+81-4>10 AND True 86-4>10 AND True 82>10 AND True 13) True AND True 14) True Changing Precedence (cont.) Elementary Programming with C/Session 3/ * of 41 The Assignment Operator The assignment operator(=) can be used with any valid C expression  (Left value) (Right value) Elementary Programming with C/Session 3/ * of 41 Multiple Assignment Many variables can be assigned the same value in a single statement  However, you cannot do this : Elementary Programming with C/Session 3/ * of 41 Type Conversion Elementary Programming with C/Session 3/ * of 41 Casts An expression can be forced to be of a certain type by using a cast. The general syntax of cast: (type) cast type  any valid C data type Example: float x,f; f = 3.14159; x = (int) f;	//The value of x will be 3 (integer) The integer value returned by (int)f is converted back to floating point when it crossed the assignment operator. The value of f itself is not changed. Input and Output in C * / of 41 Input and Output in 'C' Elementary Programming with C/Session 4/ * of 41 Standard Input/Output The standard library has functions for I/O that handle input, output, and character and string manipulation Standard input is usually the keyboard Standard output is usually the monitor (also called the console) Input and Output can be rerouted from or to files instead of the  standard devices Elementary Programming with C/Session 4/ * of 41 The Header File #include - This is a preprocessor command stdio.h is a file and is called the header file Contains the macros for many of the input/output functions used in ‘C’ printf(), scanf(), putchar(), getchar() functions are designed such that, they require the macros in stdio.h for proper execution Elementary Programming with C/Session 4/ * of 41 printf () Used to display data on the standard output – console 	Syntax printf ( “control string”, argument list); The argument list consists of constants, variables, expressions or functions separated by commas. The control string must always be enclosed within double quotes. It consists of constants, variables, expressions or functions separated by commas. Text characters: printable characters Format Commands: % sign + format code Nonprinting Characters: tabs, blanks and new lines Elementary Programming with C/Session 4/ * of 41 Format codes-1 In the above table c, d, f, lf, e, g, u, s, o and x are the type specifiers Elementary Programming with C/Session 4/ * of 41 Format codes-2 Elementary Programming with C/Session 4/ * of 41 Control String Special Characters Elementary Programming with C/Session 4/ * of 41 control strings & format codes Elementary Programming with C/Session 4/ * of 41 Example for printf() #include 	void main() 	{ 	int a = 10; 	float b = 24.67892345; 	char ch = ‘A’; 	 	printf(“Integer data = %d”, a); 	printf(“Float Data = %f”,b); 	printf(“Character = %c”,ch); 	printf(“This prints the string”); 	printf(“%s”,”This also prints a string”); 	 } Program to display integer, float , character and string Elementary Programming with C/Session 4/ * of 41 Modifiers in printf()-1 1. ‘-‘ Modifier The data item will be left-justified within its field, the item will be printed beginning from the leftmost position of its field . 2. Field Width Modifier Can be used with type float, double or char array (string). The field width modifier, which is an integer, defines , defines the minimum field width for the data item. Elementary Programming with C/Session 4/ * of 41 3. Precision Modifier This modifier can be used with type float, double or char array (string). If used with data type float or double, the digit string indicates the maximum number of digits to be printed to the right of the decimal. 4. ‘0’ Modifier The default padding in a field is done with spaces. If the user wishes to pad a field with zeroes this modifier must be used 5. ‘l’ Modifier This modifier can be used to display integers as long int or a double precision argument. The corresponding format code is %ld Modifiers in printf()-2 Elementary Programming with C/Session 4/ * of 41 6. ‘h’ Modifier This modifier is used to display short integers. The corresponding format code is %hd 7. ‘*’ Modifier If the user does not want to specify the field width in advance, but wants the program to specify it, this modifier is used Modifiers in printf()-3 Elementary Programming with C/Session 4/ * of 41 Example for modifiers /* This program demonstrate the use of Modifiers in printf() */ #include void main() { 	printf(“The number 555 in various forms:\n”); 	printf(“Without any modifier: \n”); 	printf(“[%d]\n”,555); 	printf(“With – modifier :\n”); 	printf(“[%-d]\n”,555); 	printf(“With digit string 10 as modifier :\n”); 	printf(“[%10d]\n”,555); 	printf(“With 0 as modifier : \n”); 	printf(“[%0d]\n”,555); 	printf(“With 0 and digit string 10 as modifiers :\n”); 	printf(“[%010d]\n”,555); 	printf(“With -, 0 and digit string 10 as modifiers: \n”); 	printf(“[%-010d]\n”,555); } Elementary Programming with C/Session 4/ * of 41 scanf() Is used to accept data. The general format of scanf() function scanf(“control string”, argument list); The format used in the printf() statement are used with the same syntax in the scanf() statements too Elementary Programming with C/Session 4/ * of 41 Example for scanf() #include 	void main() 	{ 	int a; 	float d; 	char ch, name[40]; 	 	printf(“Please enter the data\n”); 	scanf(“%d %f %c %s”, &a, &d, &ch, name); 	 	printf(“\n The values accepted are : 	%d, %f, %c, %s”, a, d, ch, name); 	} Elementary Programming with C/Session 4/ * of 41 Buffered I/O A buffer is a temporary storage area, either in the memory, or on the controller card for the device Used to read and write ASCII characters Buffered I/O can be further subdivided into: Console I/O, Buffered File I/O Console I/O functions direct their operations to the standard input and output of the system In ‘C’ the simplest console I/O functions are: getchar() putchar() Elementary Programming with C/Session 4/ * of 41 getchar() Has no argument, but the parentheses - must still be present Used to read input data, a character at a time from the keyboard Buffers characters until the user types a carriage return #include void main() { 	char letter; 	printf(“\nPlease enter any character:“); 	letter = getchar(); 	printf(“\nThe character entered by you is %c“, letter); } Elementary Programming with C/Session 4/ * of 41 putchar() Character output function in ‘C’ requires an argument Elementary Programming with C/Session 4/ * of 41 putchar() /* This program demonstrates the use of constants and escape sequences in putchar() */ #include void main() { 	putchar(‘H’); putchar(‘\n’); 	putchar(‘\t’); 	putchar(‘E’); putchar(‘\n’); 	putchar(‘\t’); putchar(‘\t’); 	putchar(‘L’); putchar(‘\n’); 	putchar(‘\t’); putchar(‘\t’); putchar(‘\t’); 	putchar(‘L’); putchar(‘\n’); 	putchar(‘\t’); putchar(‘\t’); putchar(‘\t’); 	putchar(‘\t’); 	putchar(‘O’); } Example 

File đính kèm:

  • pptBài giảng Lập trình C - Session 3 Operators, Expressions and InputOutput in C.ppt
Tài liệu liên quan