Presentation is loading. Please wait.

Presentation is loading. Please wait.

Input/output.

Similar presentations


Presentation on theme: "Input/output."— Presentation transcript:

1 Input/output

2 Rules of variable name A variable name can have letters (‘a’..’z’, ‘A’..’Z’), digits (0…9) and underscore (_) only. The first letter of a variable should be either a letter or an underscore. However, it is discouraged to start variable name with an underscore. It is because variable name that starts with an underscore can conflict with system name and may cause error. There is no rule on how long a variable can be. However, only the first 31 characters of a variable are checked by the compiler. So, the first 31 letters of two variables in a program should be different. Do not use keywords

3 Key words

4 8. Sizeof()  the unary operator sizeof generates the size of a variable or datatype, measured in the number of char size storage units required for the type

5 Sizeof(), cont. #include<stdio.h> int main() { int ivar = 100;
char cvar = 'a'; float fvar = 10.10; printf("%d", sizeof(ivar)); printf("%d", sizeof(cvar)); printf("%d", sizeof(fvar)); return 0; }

6 Sizeof(), cont. #include<stdio.h> int main() {
printf("%d", sizeof(int)); printf("%d", sizeof(char)); printf("%d", sizeof(float)); return 0; }

7 Operation order A simple example: 12-9/3=??

8 Operation order, cont.

9 Output – printf() The basic function call to printf( ) is of the form
printf(“literal,format”,arg1,arg2,… ); where the format is a string containing literals to be printed conversion specifications

10 printf( ) conversions Conversions specifications begin with % and end with a conversion character. Between the % and the conversion character MAY be, in order A minus sign specifying left-justification The minimum field width A period separating the field width and precision The precision that specifies Number of digits after the decimal for a floating point Minimum number of digits for an integer An h for “short” or an l (letter ell) for long See K&R section 7.2 and appendix section B1.2

11 Common printf( ) Conversions
%d -- the int argument is printed as a decimal number %u -- the int argument is printed as an unsigned number %s -- prints characters from the string until ‘\0’ is seen or the number of characters in the (optional) precision have been printed (more on this later) %f -- the double argument is printed as a floating point number %x, %X -- the int argument is printed as a hexadecimal number %c - the int argument is printed as a single character %p - the pointer argument is printed

12 printf( ) Examples int anInt = 5678; double aDouble = 4.123; /* what is the output from each printf( ) statement? */ printf ("%d is a large number\n", anInt); printf ("%8d is a large number\n", anInt); printf ("%-8d is a large number\n", anInt); printf ("%10.2f is a double\n", aDouble); printf("The sum of %d and %8.4f is %12.2f\n", anInt, aDouble, anInt + aDouble);

13 Keyboard Input- scanf Calling scanf( ) is similar to calling printf( )
scanf( format, arg1, arg2, ... ) The format string has a similar structure to the format string in printf( ). The arguments are the addresses of the variables into which the input is store. See K & R section 7.4 and Appendix section B1.3 for a detailed description of scanf( )

14 scanf( ) format string The scanf( ) format string usually contains conversion specifications that tell scanf( ) how to interpret the next “input field”. An input field is a string of non-whitespace characters. The format string usually contains Blanks or tabs which are ignored Ordinary characters which are expected to match the next (non-whitespace) character input by the user Conversion specifications usually consisting % character indicating the beginning of the conversion An optional h, l (ell) or L A conversion character which indicates how the input field is to be interpreted.

15 Common scanf( ) conversions
%d -- a decimal (integer) number %u - an unsigned decimal (integer) number %x -- a hexadecimal number The matching argument is the address of an int May be preceded by h to indicate that the argument is the address of a short or by l (ell) to indicate that the argument is the address of a long rather than an int %f, %e -- a floating point number with optional sign, optional decimal point, and optional exponent The matching argument is the address of a float May be preceded by l (ell) to indicate the argument is of the address of a double rather than a float %s -- a word (a string delimited by white space, not an entire line) The matching argument is the address of a char or the name of a char array The caller must insure the array is large enough to for the input string and the terminating \0 character More on this later %c - a single character The matching arguments is the address of a char Does not skip over white-space

16 scanf( ) examples int age; double gpa; printf(“Input your age: “);
scanf( “%d”, &age ); /* note & */ printf(“ input your gpa: “); scanf (“%lf”, &gpa );

17 Program to Add Two Integers
#include <stdio.h> int main() { int firstNumber, secondNumber, sumOfTwoNumbers; printf("Enter two integers: "); // Two integers entered by user is stored using scanf() scanf("%d %d", &firstNumber, &secondNumber); // sum of two numbers in stored in variable sumOfTwoNumbers = firstNumber + secondNumber; // Displays sum printf("%d + %d = %d", firstNumber, secondNumber, sumOfTwoNumbers); return 0; }

18 Program to Swap Numbers Using Temporary Variable
#include <stdio.h> int main() { double firstNumber, secondNumber, temporaryVariable; printf("Enter first number: "); scanf("%lf", &firstNumber); printf("Enter second number: "); scanf("%lf",&secondNumber); // Value of firstNumber is assigned to temporaryVariable temporaryVariable = firstNumber; // Value of secondNumber is assigned to firstNumber firstNumber = secondNumber; secondNumber = temporaryVariable; printf("\nAfter swapping, firstNumber = %.2lf\n", firstNumber); printf("After swapping, secondNumber = %.2lf", secondNumber); return 0; }

19 Program to Check Even, using if
#include <stdio.h> int main() { int number; printf("Enter an integer: "); scanf("%d", &number); //True if the number is perfectly divisible by 2 if(number % 2 == 0) printf("%d is even.", number); } return 0;

20 Program to Check Even or Odd, using ?:
#include <stdio.h> int main() { int number; printf("Enter an integer: "); scanf("%d", &number); (number % 2 == 0) ? printf("%d is even.", number) : printf("%d is odd.", number); return 0; }

21 Check if a Number is Positive or Negative
#include <stdio.h> int main() { int number; printf("Enter an integer: "); scanf("%d", &number); (number >= 0) ? printf("%d is positive.", number) : printf("%d is negative.", number); return 0; }

22 Area of a Circle #include<stdio.h> #define P 3.1415 int main() {
   float radius, area;    printf("\nEnter the radius of Circle : ");    scanf("%d", &radius);    area = P* radius * radius;    printf("\nArea of Circle : %f", area);    return (0); }

23 Area of a Circle #include<stdio.h> int main() {
const float P=3.1415;    float radius, area;    printf("\nEnter the radius of Circle : ");    scanf("%d", &radius);    area = P* radius * radius;    printf("\nArea of Circle : %f", area);    return (0); }

24 ASCII Codes

25 Program to Print ASCII Value
#include <stdio.h> int main() { int code; printf("Enter a number: "); // Reads number input from the user scanf("%d", &code); // %d displays the integer value of a character // %c displays the actual character printf("ASCII value of %d = %c", code, code); return 0; }

26 getch(), getche(),getchar()
#include <stdio.h> int main() { char ch=getch(); printf(“%d",ch); } #include <stdio.h> int main() { char ch=getche(); printf("\n%d",ch); } Without display! With display! #include <stdio.h> int main() { char ch=getchar(); printf(“%d",ch); } With display & wait for enter!

27 putch(), putchar() #include <stdio.h> int main() {
char ch=getchar(); putch(ch); } int putc(int c, FILE *stream);  writes the character to the specified FILE #include <stdio.h> int main() { char ch=getchar(); putchar(ch); } int putchar(int c);  writes the character to the console


Download ppt "Input/output."

Similar presentations


Ads by Google