12_c-prog

14f y 14lfncixy return 0 winter 2008 9 winter

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: = 0x%02x, i = %d, j = %ld, x = %f, y = %lf\n",c,c,i,j,x,y); c = i; i = 9259852835; printf("c = %d, i = %d, x = %.14f, y = %.14lf\n",c,i,x,y); return 0; } Winter 2008 9 Winter 2008 UCSD: Physics 121; 2008 Feeding data to the program UCSD: Physics 121; 2008 #include <stdio.h> #include <stdlib.h> • Command line arguments allow the same program to be run repeatedly with different inputs (very handy) • How to do it: 10 // for printf(),sscanf() // for exit() int main(int argc, char* argv) { int int_val; double dbl_val; – main() now takes arguments: traditionally argc a nd a rgv – argc is the number of command line arguments if (argc > 2) { sscanf(argv[1],"%lf",&dbl_val); sscanf(argv[2],"%d",&int_val); } else { printf("usage: %s double_val int_val\n",argv[0]); exit(-1); } • minimum is one: the command itself – argv is an array of strings (words) • one for each of the space-separated blocks of text following the command on the command line – C arrays are numbered starting at zero – The command line entry: one_ray -10.0 1.0 0.0 has: printf("Got double_val = %f; int_val = %d\n",dbl_val,int_val); • argc = 4 • argv[0] = one_ray; argv[1] = -10.0; etc. return 0; } Winter 2008 Lecture 12 11 Winter 2008 12 3 C-Programming, Part 1 02/26/2008 UCSD: Physics 121; 2008 UCSD: Physics 121; 2008 Result For Loops int k,count; • If I run simply prog_name, without arguments, I get: prog_name count = 0; for (k=0; k < 10; k++) { count += 1; count %= 4; printf (“count = %d\n”,count); } – usage: prog_name double_val int_val – normally, these would be given more descriptive names, like initial_x_position and number_of_trials • If I run prog_name 3.14 8, I get: prog_name – Got double_val = 3.140000; int_val = 8 • Notes: • Note that: – declared more than one integer on same line (common practice) – k starts at zero, remains less than 10 (will stop at 9), increments by one each time through loop – we needed a new header file for...
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