Presentation is loading. Please wait.

Presentation is loading. Please wait.

Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"

Similar presentations


Presentation on theme: "Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f""— Presentation transcript:

1 Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"

2 Department of Electronic & Electrical Engineering Reading data... Simple IO... scanf #include { int x; scanf("%d",&x); } "%d" is a control string %d is a slot for an integer & is the address of operator &x is the memory location to place the integer (where x is stored). scanf("%d",x); common mistake

3 Department of Electronic & Electrical Engineering Format string (scanf) common slots

4 Department of Electronic & Electrical Engineering scanf ---- WARNING --- scanf can be confusing to use especially if you do not enter your data correctly. scanf reads your input until it has filled all the slots (%s). If you have extra input characters that are not read by a scanf they will be read by the next scanf !

5 Department of Electronic & Electrical Engineering Writing data ( printf ) Similar to scanf but additional formatting options %f --- float e.g 34.1 %e --- with exponent 3.41e1 %d --- decimal (for int type) %c --- single character %s --- string (array of char) %p --- address / pointer We can also do: %10.4f --- print at least 10 characters (maybe spaces) with 4 digits after the decimal point.

6 Department of Electronic & Electrical Engineering Example using scanf float x,y; char c; scanf("%f %c %f",&x,&c,&y); printf("%f %c %f",x,c,y); There is a slot for each parameter The number and type of parameters must match the slots

7 Department of Electronic & Electrical Engineering DEMO: IO Dr Leonard writes a program to read in some numbers and print a result. Illustrating the use of scanf and printf.


Download ppt "Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f""

Similar presentations


Ads by Google