Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In a C source file, a programmer can use /* and */ to provide comments explainin

ID: 3724494 • Letter: I

Question

In a C source file, a programmer can use /* and */ to provide comments explaining a section of the code. /* is used to signal the opening of a comment and */ is used to signal the closing of a comment. A programmer can also use // to give a single line comment.
Design a pipe of sed and awk to remove all comments as well as blank lines, add line number to the following C source file and save the output to new_example.c

Design a awk program to calculate and print out the number of items (sum of the quantities) in the cart and the total price. The output should look like this #items: 9 Total : $72. 91

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
/* this is the main program
remove this line
and this line
*/

int main(int argc, char *argv[])
{
//this is another comment
char *path;
int numbers[10];
int *a1;
a1= malloc(10*sizeof(int));

float *a2;
a2 = malloc(10*sizeof(float));

a1[2] = 10;
a2[4] = 3.14;
free(a1 );
free(a2);

return 0;
}

using gcc to parse the C

$ sed 's/a/aA/g; s/__/aB/g; s/#/aC/g' file.c |
gcc -P -E - |
sed 's/aC/#/g; s/aB/__/g; s/aA/a/g' |
cat -n
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char *path;
int numbers[10];
int *a1;
a1= malloc(10*sizeof(int));
float *a2;
a2 = malloc(10*sizeof(float));
a1[2] = 10;
a2[4] = 3.14;
free(a1 );
free(a2);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote