Input/output.

Slides:



Advertisements
Similar presentations
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Advertisements

Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Basic Input/Output and Variables Ethan Cerami New York
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
Programming in C #2 Input / Output.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Formatted Input and Output
Zhang Hongyi CSCI2100B Data Structures Tutorial 3
Chapter 9 C Formatted Input/Output
Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA
Chapter 2 - Introduction to C Programming
TMF1414 Introduction to Programming
Revision Lecture
Chapter 2 Overview of C.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter 18 I/O in C.
Introduction to C CSE 2031 Fall /3/ :33 AM.
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Programming in C Input / Output.
Input and Output Lecture 4.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Chapter 08- printf and scanf
Formatted Input/Output
Programming in C Input / Output.
CSI 121 Structured Programming Language Lecture 7: Input/Output
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Chapter 2 - Introduction to C Programming
Formatted Input/Output
Chapter 2 - Introduction to C Programming
INPUT & OUTPUT scanf & printf.
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Chapter 9 - Formatted Input/Output
Programming Assignment #1 12-Month Calendar—
Chapter 2 - Introduction to C Programming
Programming in C Input / Output.
Formatted Input/Output
Conversion Check your class notes and given examples at class.
Formatted Input/Output
Introduction to Java Applications
Formatted Input/Output
Chapter 2 - Introduction to C Programming
Introduction to C EECS May 2019.
Variables in C Topics Naming Variables Declaring Variables
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
EECE.2160 ECE Application Programming
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
EECE.2160 ECE Application Programming
Presentation transcript:

Input/output

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

Key words

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

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

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

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

Operation order, cont.

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

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

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

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

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

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.

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

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

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

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

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;

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

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

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); }

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); }

ASCII Codes

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

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!

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