

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Programming samples and exercises in c language. It includes explanations of macros, functions, and simple programs. Students can practice coding and understanding the logic of c programming.
Typology: Exams
1 / 2
This page cannot be seen from the preview
Don't miss anything!


Version of June 3, 2002 8:19 am Page 1 of 2
int main(int argc, char *argv[]) { int sum = 0; int i;
for(i = 1; i < argc; i++) sum += argv[i]; printf(“%d\n”, sum);
return(0); } a. Suppose this program is invoked as sum 1 2 3 What are the values of argc and the elements of argv? Draw a picture if that would help you indicate what the values are. Please note I want the value of argc, argv, and each element of argv, and so forth, either as a number or character, or as arrows in a picture (for addresses). b. When I ran this programs, I got 2147431340. Why does the program print the incorrect value, and how would you fix it? (If you cannot write the code, for partial credit simply say what you would do.)
variable value if max is macro value if max is function
a
b
c
d
e
Version of June 3, 2002 8:19 am Page 2 of 2
a. What does the following function do? Remember, its purpose can be stated in a succinct sentence; for par- tial credit, you can describe what each line does, but for full credit, you must state its function in one very short sentence. int mystery(char a[]) { register int i;
for(i = 0; a[i]; i++) if (!isdigit(a[i])) return(0); return(1); } b. Rewrite the function using pointers rather than an array.
for(n = 0, p = charray; p; p++){ switch(p){ case '–': s = -s; break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': n = n * 10 + (*p - '0'); break; } } n = s * n;