[Courses] C Programming For Absolute Beginners, Lesson 2: Fun With Printf, Scanf, Puts, and Variables
Sachin Divekar
ssd532 at gmail.com
Wed Feb 29 08:01:04 UTC 2012
On Wed, Feb 29, 2012 at 11:48 AM, Jacinta Richardson <
jarich at perltraining.com.au> wrote:
> On 29/02/12 16:56, Sachin Divekar wrote:
>
> #include<stdio.h>
> int main()
> {
>
> void flush_i(void){
>
> int c;
> while (c != '\n' && c != EOF){
> c = getchar();
> }
> }
>
> int a, b, c;
> printf("Please enter any number up to three digits:" );
> scanf("%d", &a);
>
> flush_i();
>
> printf("You entered %d. Now enter another number up to three
> digits:\n", a );
> scanf("%d", &b);
>
> c = a + b;
> printf("%d + %d = %d\n", a, b, c);
>
> return 0;
> }
>
>
> It's usually considered good form to create functions separately, rather
> than embedding them inside other functions. To do this we do need one
extra
> line, which is where we give the function prototype (header) so that when
C
> is compiling the code it knows what that function takes and returns.
>
> // using flush_i() to clear input stream buffer
>
> #include<stdio.h>
>
> /* our prototype */
>
> void flush_i(void);
>
> /* Now our main function */
>
> int main() {
>
> int a, b, c;
> printf("Please enter any number up to three digits:" );
> scanf("%d", &a);
>
> flush_i();
>
> printf("You entered %d. Now enter another number up to three
> digits:\n", a );
> scanf("%d", &b);
>
> c = a + b;
> printf("%d + %d = %d\n", a, b, c);
>
> return 0;
> }
>
> void flush_i(void){
> int c;
> while (c != '\n' && c != EOF){
> c = getchar();
> }
> }
>
>
Dear Jacinta,
Thanks for the tip. This is how we, noobs, learn best practices from you
guys.
A quick read on Wikipedia about function
prototype<http://en.wikipedia.org/wiki/Function_prototype#Uses>convinced
me the importance
of declaring function prototype. And prototype declarations in header files
for
standard library functions satisfies the rule that we
should declare prototype for all the functions we use.
For example stdio.h has prototypes for standard library functions for I/O
like printf etc.,
On my system the prototype for printf is as following (I am not getting the
meaning of this
declaration. It is way above my expertise).
extern int printf (__const char *__restrict __format, ...);
--
Regards,
Sachin Divekar
More information about the Courses
mailing list