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
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.