This document contains examples of using nested printf and scanf statements in C programming. The first example shows how to define a main function using a macro. The next examples demonstrate how nested printf statements work, printing the number of digits in the results of inner statements. Similarly, nested scanf examples show how values are input for variables in inner statements and then used in outer code. The final example mixes scanf and printf to input multiple values and then print the number input. Overall, the document provides examples of complex nested input/output statements in C for educational purposes.
This document contains examples of using nested printf and scanf statements in C programming. The first example shows how to define a main function using a macro. The next examples demonstrate how nested printf statements work, printing the number of digits in the results of inner statements. Similarly, nested scanf examples show how values are input for variables in inner statements and then used in outer code. The final example mixes scanf and printf to input multiple values and then print the number input. Overall, the document provides examples of complex nested input/output statements in C for educational purposes.
This document contains examples of using nested printf and scanf statements in C programming. The first example shows how to define a main function using a macro. The next examples demonstrate how nested printf statements work, printing the number of digits in the results of inner statements. Similarly, nested scanf examples show how values are input for variables in inner statements and then used in outer code. The final example mixes scanf and printf to input multiple values and then print the number input. Overall, the document provides examples of complex nested input/output statements in C for educational purposes.
C – Language Tricks ---------------------------------------------------------------------------------------------------------------------------------- // C program without using main () function #include<stdio.h> #include<conio.h> #define begin main void begin () { printf ("Hello Disha Computers"); } Output: Hello Disha Computers ------------------------------------------------------------------------------------------------------------------------ // Nested printf () statement example #include<stdio.h> #include<conio.h> void main () { int x = 1987; printf ("%d", printf ("%d", printf ("%d", x))); } Output: 198741 Explanation: o Firstly, innermost (third) printf is executed which results in printing 1987. o Secondly, middle (second) printf is executed which print total number of digits in 1987 i.e 4. o Finally, outer (first) printf is executed which print total number of digits in 4 i.e. 1. ------------------------------------------------------------------------------------------------------------------------ // Another nested printf () statement example #include<stdio.h> #include<conio.h> void main () { int x = 247; printf ("%d", printf ("%d", printf ("%d", x))); } Output: 24731 Explanation: o Firstly, innermost (third) printf is executed which results in printing 247. o Secondly, middle (second) printf is executed which print total number of digits in 247 i.e 3. o Finally, outer (first) printf is executed which print total number of digits in 3 i.e. 1. ------------------------------------------------------------------------------------------------------------------------ // Nested scanf () statement example #include<stdio.h> #include<conio.h> void main () { int a; clrscr(); scanf("%d",scanf("%d",&a)); printf("Value of a is=%d",a); getch(); } Output: 24 67 Value of a is=24 Explanation: o Firstly, second scanf is executed which inputs 24 for variable a. o Secondly, first scanf is executed which inputs 67. o Finally, printf is executed which prints value of variable a i.e. 24. ------------------------------------------------------------------------------------------------------------------------ // scanf () inside printf () statement example #include<stdio.h> #include<conio.h> void main () { int a, b, c; printf ("%d", scanf ("%d %d %d", &a, &a, &b)); } Output: 23 93 7 (suppose use inputted 3 values for variables a, b and c) 3 Explanation: o Firstly, scanf is executed which accepts three input from user, for example 23 is inputted for variable a, 93 is for variable b and 7 for variable c. o Secondly, printf is executed which print total number of values inputted by scanf i.e. 3. So final output is 3. ==========================================================================