GridVids
Sign in with YouTube

C Programming Tutorial #05 I/O Statements: printf, scanf, Compound Statements

Mark watched
Don't Miss
Hide
Open in popup Open on YouTube

Xoviabcs 9 years ago
Chapters

C Programming Tutorial #05 I/O Statements, printf, scanf, simple & compound statements. In 5th lecture of C programming tutorial with Data structure & algorithms, you will formally learn about I/O statements ie. how a program interacts with the real world by taking in inputs and putting out outputs. You have already used some of these ideas in your earlier programming assignments. So, this lecture should be relatively easy. You will also learn about simple and compound statements in programs.

C Programming Tutorial/ Lectures/ Course with Data Structures & Algorithms @ https://goo.gl/K6VbgL Machine Learning Complete Tutorial/Lectures/Course from IIT (nptel) for GATE @ https://goo.gl/AurRXm Discrete Mathematics for Computer Science for GATE @ https://goo.gl/YJnA4B (IIT Lectures for GATE) Best Programming Courses @ https://goo.gl/MVVDXR Operating Systems Lecture/Tutorials from IIT for GATE CSE @ https://goo.gl/GMr3if MATLAB Tutorials for GATE @ https://goo.gl/EiPgCF

Output Statement: printf(format-string, var1, var2, …, varn); - Format-string is enclosed in double quotes - Format string indicates: - How many variables to expect - Type of the variables - How many columns to use for printing them (not very commonly used) - Any character string to be printed - Sometimes this would be the only output - Example : printf(“Hello World!”);

Example: int x; float y; x = 20; y = – 16.789; printf(“Value x=%d and value y=%f\n”, x, y); %d : print as integer %f: print as real value There are other specifiers too. The output: Value x=20 and value y=–16.789

Some printf Statements We Used: printf("Enter three numbers A, B and C: "); - Empty format specification. The text will be printed as it is printf("The product is %d x^2 + %d x + %d\n",p2,p1,p0); Output: The product is: 3 x^2 + 10 x + 4 - %d means print as integer - The three %d specifiers are matched to p2, p1 and p0 in that order - Notice the spaces in the specifier as well as the output - \n moves the cursor to the next line

Input Statement: scanf(format-string, &var1, &var2, …, &varn); - Format-string is enclosed in double quotes - Format string indicates: - How many variables to expect - Type of the data items to be stored in var1 etc - The symbol ‘&’ is used to specify the memory address where the value is to be stored

Input Statement Example: scanf("%d%d%d",&A,&B,&C); - Read three integers from the user - Store them in memory locations of A, B and C respectively scanf(“%d%f”,&marks, &averageMarks); If the user keys in 16 14.75 - 16 would be stored in the memory location of marks - 14.75 would be stored in the memory location of aveMarks - scanf skips over spaces if necessary to get the next input - Usually, space, comma, \n etc. are not used in the format specifier string of scanf

Other Format Specifiers: - Most commonly needed are %d and %f - Character %c - Exponent form %e - Example 1.523e2 = 152.3 - Several modifications to %f and %d are possible for printf - They control how much space is taken by the number vs. how much white space is introduced

Statements: Program: - Declaration and one or more statements - Assignment statement - Function calls etc. - Selection statement - Repetitive Statements

Simple Statements: - Any statement that is an expression or a function call - Examples: X = 2; X = 2 + 8; printf(“Hello, World!”); Y = sin(X); Generally, all simple statements are terminated by ‘;’

Compound Statements: - A group of declarations and statements collected together - Usually to form a single logical unit - Surrounded by braces - Also called a block

Compound Statements: - Usually come in two varieties - Conditionals If..else.. switch - Loops for while do..while