Download presentation
Presentation is loading. Please wait.
1
Conversion Check your class notes and given examples at class.
2
Check your class notes and given examples at class.
Floating point Check your class notes and given examples at class.
3
Choose the biased exponent
(32-bits)(floating point numbers IEEE-754) -11 26 Binary to decimal(32-bit-floating point IEEE-754)
4
Conversion Decimal number to signed integer(32-bit)(2-compliment for negative numbers) -64 208 64 Binary to decimal
5
Write a program which reads seven characters
Q Write a program which reads seven characters
6
/* This program reads 7 characters */
/* This program reads seven characters */ #include <stdio.h> int main (void) { /* declarations */ char letter1, letter2, letter3, letter4,letter5,letter6,letter7; printf ("Enter a name: "); scanf ("%c%c%c%c%c%c%c", &letter1, &letter2, &letter3, &letter4,&letter5, &letter6, &letter7); printf ("You entered: %c%c%c%c%c%c%c", letter1, letter2, letter3, letter4,letter5,letter6,letter7); return (0); }
7
scanf( ) function To get input from the user.
Defined in <stdio.h> e.g., scanf("%f", &var); /* get a value for var from the user */ %f tells the program the type of the data &var the address of var in memory. scanf("%f %d %c", &var1, &var2, &var3); /* get values for var1, var2, var3 */ /* var1 is float */ /* var2 is integer */ /* var3 is character */
8
NOTE for scanf( ): scanf( ) does not use the variable name. It uses the variable address. The value entered by the user at the keyboard. Never put the &sign infront of a variable in a printf!
9
Examples Partial ‘C’ Code … printf(“This is a C Message.”);
printf("What appears bet. The double quote is printed as message on screen.") Output on the Screen This is a C Message. What appears bet. The double quote is printed as a message on screen. Note: there are 2 printf statements but messages are not printed on 2 separate lines. They are printed one after each other.
10
Examples Partial ‘C’ Code …
printf(“This is also a \”C\” Message!\n”); printf(“Number 5 is equal to %d”, 5); Output on the Screen This is also a “C” Message! Number 5 is equal to 5. Note: printing a numerical constant; %d is replaced by the constant to be printed. Note:using \” will cause a double quote to be printed directly to the screen.
11
Examples Partial ‘C’ Code …
printf(“Number %d is not equal to %d.”, 5, 7); Output on the Screen Number 5 is not equal to 7. Note: printing 2 numerical constants; first %d is replaced by the 1st constant to be printed and the second %d is replaced by the 2nd constant to be printed.
12
Examples Partial ‘C’ Code …
printf(“This is how to print a character, for example: 'a' is %c.”, 'a'); printf("This is how to print a string, for example: CPS125 is %s.", "CPS125“); Output on the Screen This is how to print a character, for example: 'a' is a. This is how to print a string, for example: CPS125 is CPS125.
13
Examples Partial ‘C’ Code …
printf("We can also print a combination of numbers, %d, characters, %c, and strings, %s.", 25, 'X‘ , "CMTH140"); Output on the Screen We can also print a combination of numbers, 25, characters, X, and CMTH140. NOTE: arguments are separated by commas.
14
Examples Partial ‘C’ Code … int iNTvar=5; float fLOATvar=10.3;
char cHARvar=’X’; printf(“This is how to print different variables, for example: iNTvar is %d, fLOATvar is %f, cHARvar is %c.”, iNTvar, fLOATvar, cHARvar); Output on the Screen This is how to print different variables, for example: iNTvar is 5, fLOATvar is 10.3, and cHARvar is X.
15
Example Partial ‘C’ Code … Output on the Screen int iNTvar;
float fLOATvar; char cHARvar; printf(“This is how to print different variables, for example: iNTvar is %d, fLOATvar is %f, cHARvar is %c.”, iNTvar, fLOATvar, cHARvar); Output on the Screen This is how to print different variables, for example: iNTvar is -108, fLOATvar is , and cHARvar is a.
16
Format specifiers for PRINTF( ) functions
Purpose %d, %i Signed decimal integer %f Signed floating point %lf Signed double %e Signed floating point using e notation %c A single character %s Strings
17
Variables Variable like a memory cell, can only contain one value at a time. Putting a value in a variable that contains another value destroys the previous value. Putting certain values in a variable can lead to inaccuracies Cancellation error: happens when the magnitude of the operands are too different. Ex: would give Arithmetic underflow: happens when a number too small appears as 0. Ex: * would give 0.0
18
Question? Q) What happens if we try to print a variable which is declared but is not initialized (i.e., we didn’t assign any value to it)? Can we assume that if the variable is integer then ZERO will be printed out?
19
Answer if we declare variables and don’t assign values to them, then try to print them, we would see unexpected values on the screen.
20
Arithmetic Operators in C
Denote Example What it does + Addition 2+8 Add numbers & return the result 2+8 returns 10 - Subtraction 12-8 Subtract numbers & return the result 12-8 returns 4 * Multiplication 5*8 Multiply numbers & return the result 5*8 returns 40 / Division 40/5 Divide numbers & return the result 40/5 returns 8 % Remainder (modulo) 7%3 Divide numbers & return the remainder 7%3 Returns 1 NOTE: Unary operation is an operation that ONLY requires one operand, for example, -2.
21
Assignment Operators A simple assignment operator or a combination of assignment operator (=) with some other arithmetic operator, Example, count=count + 1; /* count count+1 */ count += 1; /* count count+1 */
22
Assignment Operators NOTE:
Denote Example Operation = Assignment X=X+2 += Addition Assignment X+=2; X=X+2; -= Subtraction Assignment X-=2; X=X-2; *= Multiplication Assignment X*=2; X=X*2; /= Division Assignment X/=2; X=X/2; %= Remainder Assignment X%=2; X=X%2; NOTE: The choice of using either X+=2 or X=X+2; depends on the individual programming practice. Both assignments are correct thus you would use the method that is most convenient to you.
23
Increment/Decrement Operators
Increment and decrement operators are unary operators and they can be in the form of postfix or prefix. Operation Of Increment and Decrement Operators Operator Operation Counter++ (postfix) Do the current operation then increment Counter ++Counter (prefix) increment Counter then do the current operation Counter-- Do the current operation then Decrement Counter --Counter
24
Example:HOMEWORK Partial Code #1 Partial Code #2 Partial Code #3 …
int count=0; count=count+1; count++; ++count; Partial Code #4 Partial Code #5 Partial Code #6 … int w=0, x=2, y=3, z; x = x + y++; z = y + ++x ... int count=0, x=2, y=3, z; x = x + ++y; z = y + x++; ++y; x++; x = x + y; z = y + x;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.