12_c-prog

O in unix obj in dos only if youre building in pieces

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: program) – a pre-linked compiled tidbit: . o in Unix, . obj in DOS – only if you’re building in pieces and linking later Winter 2008 5 Winter 2008 6 UCSD: Physics 121; 2008 UCSD: Physics 121; 2008 More on program Alternate form #include <stdio.h> #include <stdio.h> int main(void) { int i=53; int main(void) { int i; i=53; printf(“i = %04d\n”,i); return 0; } – semicolons delimit separate statements, but this program, while compact, is harder on the eyes – this time, we defined and assigned the variable in separate steps (more commonly done) – we shortened the print statement fluff – the format is now 4 characters wide, forcing leading zeros printf(“The illustrious variable, i, is %d\n”,i); return 0; } – – – – semicolons end each line within program spacing is not required, but makes for easier reading all variables must be declared before they are used could have simply said: i nt i; then declared later that i=53; – the \ n is a newline; the %d formats as decimal integer Winter 2008 Lecture 12 • output will be: i = 0053 – could compactify even more, if sadistic 7 Winter 2008 8 2 C-Programming, Part 1 02/26/2008 UCSD: Physics 121; 2008 UCSD: Physics 121; 2008 Variable types Output of previous program #include <stdio.h> int main(void) { char c; int i; long j; float x; double y; c i j x y = = = = = • Output looks like: // // // // // single byte typical integer long integer floating point (single precision) double precision c = 65 = 0x41, i = 356, j = 230948935, x = 3.141593, y = 3.141593 c = 100, i = 669918243, x = 3.14159274101257, y = 3.14159265358979 • Notes: – c “ wrapped” around 256 when assigned to be 356 – i couldn’t handle the large value, and also wrapped 'A'; 356; 230948935; 3.14159265358979; 3.14159265358979; • int i s actually the same as long on this machine – The f loat can’t handle the full precision set out – broke printf line: spacing irrelevant: semicolons do the work – The d, x, ld, f, and lf format codes correspond to decimal, hex, long decimal, float, and long float, respectively printf("c = %d...
View Full Document

This note was uploaded on 01/30/2014 for the course PHYS 121 taught by Professor Staff during the Winter '08 term at UCSD.

Ask a homework question - tutors are online