chapter_3

This preview shows pages 1–15. Sign up to view the full content.

Administrative Issues TA Office hours: Thursdays: 4:00 PM till 5:30 PM Fridays: 3:00 PM till 4:30 PM Location: ECC Labs TA for ECE 3331 is Fouad Shehab E-mail: [email protected]

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Characters and Integers Floating-Point Variabeles Arithmetic Operators Relational and Logical Operators and the Assignment Operator Real World Application: Statistical Measures
Characters and Integers The definition of Character char c; creates a 1-byte cell that can hold one character. c = ‘x’; Note: the character referenced is enclosed in single quotation marks. #include <stdio.h> main ( ) { char c, d; c = ‘x’; printf ( “Please enter one character: “ ); scanf ( “%c”, &d ); /* %c-reads the next character in scanf () */ printf ( “c = %c, ”, c); /* %c is format descriptor to print one character in printf ( ) */ printf ( “d = %c\n”, d); }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
If the input is W The output is c = x, d = W Reading and writing chars using scanf() and printf( ) char ASCII W 87 W 87 d Cell in memory Input Scanf(“%c”, &d); ASCII char 87 W printf(“%c”, &d); 87 W d Cell in memory output
In scanf ( ), %d skip white space until an integer is found. But %c does not skip white space. #include <stdio.h> main ( ) { char c1, c2, c3, c4; int x; scanf ( “%d%c%c%c%c”, &x, &c1, &c2, &c3, &c4); printf ( “%d%c%c%c%c”, x, c1, c2, c3, c4 ); } If the input is If the input is 12 345 12 3 45 the output the output ????? ?????

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
In scanf ( ), %d skip white space until an integer is found. But %c does not skip white space. #include <stdio.h> main ( ) { char c1, c2, c3, c4; int x; scanf ( “%d%c%c%c%c”, &x, &c1, &c2, &c3, &c4); printf ( “%d%c%c%c%c”, x, c1, c2, c3, c4 ); } If the input is If the input is 12 345 12 3 45 the output the output 12 345 12 3 4
int i; scanf ( “%c”, &i ); Is this correct? printf ( “%c”, i ); Is this correct? #include <stdio.h> main ( ) { char c; c = 121; printf (“The character of int %d is %c.\n”, c, c ); } The output is (ASCII code) The character of int 121 is y.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
int i; scanf ( “%c”, &i ); /* Logical Error - for the format descriptor %c, the corresponding argument must by the address of char*/ printf ( “%c”, i ); /* correct */ The printf can exchange char and int because system converts char to int before passing the value to printf.
#include <stdio.h> main ( ) { char c; c = 121; printf (“The character of int %d is %c.\n”, c, c ); } Output is ????

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
#include <stdio.h> main ( ) { char c; c = 121; printf (“The character of int %d is %c.\n”, c, c ); } The output is (ASCII code): The character of int 121 is y.
#include <stdio.h> main ( ) { char c; c = ‘y’; printf (“The character of int %d is %c.\n”, c, c ); } The output is ????

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
#include <stdio.h> main ( ) { char c; c = ‘y’; printf (“The character of int %d is %c.\n”, c, c ); } The output is The character of int 121 is y.
#include <stdio.h> main ( ) { char n; int i = 9; n = ‘9’; printf (“The integer of character %c is %d.\n”, n, n ); printf (“The character of integer %d is %c.\n”, i, i ); }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
The integer of character 9 is 57. The character of integer 9 is
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}