notes2 - What weve learned so far Basic structure of a C...

Info icon This preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Basic structure of a C program Printing output: printf Reading in input: scanf Basic arithmetic operations Decision making: if statements Debugging programs • Algorithms Reading for this week • Chap. 3 • Section 4.10
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
We used the following scanf and printf statements last week: – scanf( “%d”, &integer1 ); – printf( “Sum is %d\n\n”, sum ); – Here integer1 and sum were variables, and %d indicated the format of the data (we used %d for integer). Other format types: – %d = decimal integer (can also be of the form %.3d , %5d or %5.3d ) – %f = floating point number (can also be of the form %.2f or %6.2f ) – %lf = double (use %f in printf statement, %lf in scanf statement) – %c = character – %s = string (in printf only)
Image of page 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 /* More on printf */ #include <stdio.h> /* function main begins program execution */ int main() { float pie = 3.1416; printf( "\n%.3d is an integer.", 15 ); printf( "\n%.3f is a floating point number.", pie ); printf( "\n%s is a string\n", "aabbbcc" ); return 0; /* indicate that program ended successfully */ } /* end function main */
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Output for program on previous page: % a.out 015 is an integer. 3.142 is a floating point number. aabbbcc is a string. %
Image of page 4
if (num1 % 7 == 0 ) printf( “%d is divisible by 7.\n”, num1 ); num1 % 7 == 0 print “num1 is…” true false
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon