49_pdfsam_cs2022_lectures - Command line arguments Command...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Macro pitfalls “Search-and-replace” can be problematic It’s easy to write macros that have unintended consequences #include <stdio.h> #define sum_sq(a,b) (a)*(a) + (b)*(b) int main() { double num1 = 3.0, ssq; ssq = sum_sq(num+3, 5.0); return 0; } ssq = (num+3)*(num+3)+(5.0)*(5.0); Before Processing After Processing
Background image of page 1

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

View Full DocumentRight Arrow Icon
Macro pitfalls “Search-and-replace” can be problematic It’s easy to write macros that have unintended consequences #include <stdio.h> #define sum_sq(a,b) (a)*(a) + (b)*(b) int main() { double num1 = 3.0, ssq; ssq = sum_sq( rand() , 5.0); return 0; } Before Processing After Processing
Background image of page 2
More preprocessor features The preprocessor has many other features check Kernighan and Ritchie's book for details
Background image of page 3

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

View Full DocumentRight Arrow Icon
Command-line arguments
Background image of page 4
Background image of page 5

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

View Full DocumentRight Arrow Icon
Background image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Command line arguments Command line arguments are passed as parameters to main() Full prototype of main: int main(int argc, char *argv) argc contains the number of command line arguments argv is an array of strings, one per command line argument e.g. for the command line above: argc = 4 argv[0]: myprogram argv[1]: -x argv[2]: file1.txt argv[3]: file2.txta C:\&gt; myprogram -x file1.txt file2.txt Command line arguments example #include &lt;stdio.h&gt; int main(int argc, char *argv) { int j; for(j=0; j&lt;argc; j++) printf(%d %s\n, j, argv[j]); return 0; } C:\&gt; program 12 -x test.txt 0 program 1 12 2 -x 3 test.text...
View Full Document

This note was uploaded on 01/10/2010 for the course CS 2022 at Cornell University (Engineering School).

Page1 / 6

49_pdfsam_cs2022_lectures - Command line arguments Command...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online