You are on page 1of 6

Chapter 2

Standard Input and Output


Whenever you're thinking about inputting values from the user or outputting values onto the screen, you MUST include the stdio.h header file at the top of the source code. (stdio.h stands for Standard nput and !utput".

Declaring Variables
#efore you can input or output values, variables must be declared before they are used $ithin the source code. The $ay to declare a variable is to state the data type, then the variable name, follo$ed by a semi%colon. When you declare variables, some of the computer's memory is reserved so that the value of that variable can be stored $ithin that memory &slot&. 'ost( )efer to the *+T+ T,-.S section if you really have to. /ere are some e0amples of some variable declarations1
char a; char beta; int x,y,z; 23 ,ou can declare more than one variable of the same type on one line4 32 float pi56.789: 23 ,ou can also assign a value to the variable like this4 32 double root957.878976;<96=6:

n >, you must declare all your variables at the start of main() % not after calling functions. ?or e0ample, this piece of code might @ust cause a compilation error1
#include <stdio.h> int main() { int x = 5; print (!x = "d#n!, x); int y = $; 23 &declaration is not allo$ed here& , says my compiler 32 printf(&y 5 AdBn&, y": return C: D

Inputting Values
To input a value, the easiest $ay is to use a library function called scan () . There are other functions like %etc() and %etchar() , but find that scan () is by far the easiest to use. /ere's an e0ample of ho$ scan () is used.
>opyright 'ogic !ption -vt. 'td.

scan (!"d!, &x);

#asically, the user types an integer and presses return. The value they typed in $ill be stored in the memory slot reserved for x. is kno$n as a ?!)M+T S-.> ? .). There are many format specifiers. + particular one should be used, depending on the desired input of the user. /ere $e assume that an integer is going to be entered. /ere are @ust some of the format specifiers1
"d "c "d " "e "s

character format specifier integer format specifier float and double format specifier scientific notation format specifier string format specifier

,ou can input more than one value at a time. /ave a think about this line and $hat it does1
scan (!"c "d "d!, &x, &y, &z);

+ssuming the variables x, y and z have been declared $ith char , int and int respectively, the user can input 6 values separated by spaces. Eotice that the Fuote marks surround the format specifiers $here as the &address of variables& are separated from one another $ith a comma.

Outputting Values
To output a value, $e use a common library function called print () . /ere's an e0ample1
print (!'he (alue o x is "d#n!, x);

+lmost all te0t $ithin the Fuote marks is printed out onto the screen. nstead of printing out the format specifier, the numerical value of x is printed out (assuming that you've declared x as an integer". The #n is a special character % the ne$line character. #asically, this is like pressing return on a $ord processor. f the program $as to print out another line, it $ould be on the ne0t one. Eotice that the ne$line character is inside the double Fuote marks. /ave a look at an e0ample program $ritten in > and try and figure it out before reading my comments.
#include <stdio.h> int main() { int a, b, c;
>opyright 'ogic !ption -vt. 'td.

print (!)nter t*o inte%ers separated by a space !); print (!and + *ill *or, out their sum-#n!); scan (!"d "d!, &a, &b); c = a . b; print (!"d plus "d is e/ual to "d#n!, a, b, c); 1 return 0;

Why don't you try copying the above code and pasting it into your > compiler( This only $orks for Windo$s based ones mind4 +lternatively, copy the code (highlight and hold >trl and >", open up Eotepad and paste it in. Then save $ith the e0tension .c and compile the UE G $ay. ,ou have to rename the file if it is saved $ith the .t0t e0tension. /ere's the same program, this time $ith comments in1
#include <stdio.h> int main(" H int a, b, c: 23 *on't forget to include the header file4 32

23 *eclare variables 32

printf(&.nter t$o integers separated by a space &": printf(&and $ill $ork out their sum4Bn&": scanf(&Ad Ad&, Ia, Ib": c 5 a J b: 23 Work out the sum 32 23 -rint it out 32

printf(&Ad plus Ad is eFual to AdBn&, a, b, c": return C: D

n print , notice that the order of the e0pressions after the code enclosed in the double Fuote marks matters. The value of the first e0pression, a in this case, is printed out by the first format specifier and so on (see belo$".

More on Format Specifiers


/o$ to display a floating point number rounded to 9 decimal places.

Minimum Field Width


Suppose you $ant the program to display something that takes up a minimum number of spaces on the screen. ,ou can achieve this by adding an integer value after the percentage sign. ?or e0ample, if you $ant to display an integer using a minimum of K spaces, you'd $rite "2d in your
>opyright 'ogic !ption -vt. 'td.

print

statement. /ere's a more advanced e0ample1

#include <stdio.h> int main() { int x = 345; print print print print print print print print print print 1 (!6rintin% (!6rintin% (!6rintin% (!6rintin% (!6rintin% (!6rintin% (!6rintin% (!6rintin% (!6rintin% (!6rintin% 345 345 345 345 345 345 345 345 345 345 usin% usin% usin% usin% usin% usin% usin% usin% usin% usin% ""0d ""3d ""4d ""5d ""$d ""5d ""7d ""8d ""2d ""9d displays displays displays displays displays displays displays displays displays displays "0d#n!, "3d#n!, "4d#n!, "5d#n!, "$d#n!, "5d#n!, "7d#n!, "8d#n!, "2d#n!, "9d#n!, x); x); x); x); x); x); x); x); x); x);

return 0;

f you compile and run the program, it should display something like this1
6rintin% 6rintin% 6rintin% 6rintin% 6rintin% 6rintin% 6rintin% 6rintin% 6rintin% 6rintin% 345 345 345 345 345 345 345 345 345 345 usin% usin% usin% usin% usin% usin% usin% usin% usin% usin% "0d "3d "4d "5d "$d "5d "7d "8d "2d "9d displays displays displays displays displays displays displays displays displays displays 345 345 345 345 345 345 345 345 345 345

Eotice that in the first 8 cases, 796 is displayed in the same $ay as $hen you normally use "d. Why( Simple % the number of spaces on the screen that 796 can be displayed is greater than or eFual to 6. #ut also, if you $rite "09d, the program $ill display Leros before the number itself. n the above e0ample, it $ill display1
6rintin% 345 usin% "09d displays 000000345

+n advantage of using this, is that you can count $hat's the minimum field of the number4

Making It Look Neater


The output from the e0ample above doesn't look very neat does it(4 That's because the numbers are aligned to the right of the minimum field. n other $ords, 7,9 and 6 are the digits in the
>opyright 'ogic !ption -vt. 'td.

furthest 6 spaces of the minimum field. To align your output on the left, you'd stick a minus sign before the number in the format specifier. #ut if you do this to the previous e0ample, all the output lines $ill be the same. Try compiling this e0ample1
#include <stdio.h> int main() { int x = 34; int y = 345; int z = 345$5; print print print print print print print print print print print print 1 (!6rintin% (!6rintin% (!6rintin% (!6rintin% (!6rintin% (!6rintin% (!6rintin% (!6rintin% (!6rintin% (!6rintin% (!6rintin% (!6rintin% 34 34 34 34 345 345 345 345 usin% usin% usin% usin% usin% usin% usin% usin% ""9d #t#t displays "9d#n!, x); ""09d #t#t displays "09d#n!, x); "":9d #t#t displays ":9d#n!, x); "":09d #t displays ":09d#n!, x); ""9d #t#t displays "9d#n!, y); ""09d #t displays "09d#n!, y); "":9d #t displays ":9d#n!, y); "":09d #t displays ":09d#n!, y); ""9d #t displays "9d#n!, z); ""09d #t displays "09d#n!, z); "":9d #t displays ":9d#n!, z); "":09d #t displays ":09d#n!, z);

345$5 345$5 345$5 345$5

usin% usin% usin% usin%

return 0;

More Precision
,ou can gain more control $ith the displaying of integers by placing a dot, follo$ed by an integer, after the minimum field specifier. The dot and this integer is kno$n as a precision specifier. The integer you add specifies the ma0imum field $idth $hen displaying an integer or string. This is $hen $e should really move onto floating point numbers like mentioned earlier. f you're using " , the format specifier for floating point numbers, you can control the number of decimal places that is displayed ($hich is < by default". /o$( #y using the precision specifier. This time, the number after the dot is the number of decimal places. The number before the dot is still the minimum field $idth. >ompile this lot and you'll see $hat mean1
#include <stdio.h>
>opyright 'ogic !ption -vt. 'td.

int main() { loat x = 5.3$3594; print print print print print print print print print print 1 (!6rintin% (!6rintin% (!6rintin% (!6rintin% (!6rintin% (!6rintin% (!6rintin% (!6rintin% (!6rintin% (!6rintin% 5.3$3594 5.3$3594 5.3$3594 5.3$3594 5.3$3594 5.3$3594 5.3$3594 5.3$3594 5.3$3594 5.3$3594 usin% usin% usin% usin% usin% usin% usin% usin% usin% usin% "" #t displays " #n!, x); ""3.3 #t displays "3.3 #n!, x); ""3.4 #t displays "3.4 #n!, x); ""5.5 #t displays "5.5 #n!, x); ""$.$ #t displays "$.$ #n!, x); ""$.5 #t displays "$.5 #n!, x); ""09.5 displays "09.5 #n!, x); "":09.5 displays ":09.5 #n!, x); ""9.5 displays "9.5 #n!, x); "":9.5 displays ":9.5 #n!, x);

return 0;

>opyright 'ogic !ption -vt. 'td.

<

You might also like