0% found this document useful (0 votes)
315 views

Lab 2

The document provides instructions for a lab assignment involving formatting strings in C programs. It instructs the user to check if a directory called LAB2 exists, remove it if it does, and create the directory. It then provides two programs to type (fmti.c and fmtf.c) that demonstrate various formatting specifiers and examines their output. It also provides two programs with errors (fun.c and err.c) and instructs the user to compile and identify the errors, modifying err.c to remove errors.

Uploaded by

venkatesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
315 views

Lab 2

The document provides instructions for a lab assignment involving formatting strings in C programs. It instructs the user to check if a directory called LAB2 exists, remove it if it does, and create the directory. It then provides two programs to type (fmti.c and fmtf.c) that demonstrate various formatting specifiers and examines their output. It also provides two programs with errors (fun.c and err.c) and instructs the user to compile and identify the errors, modifying err.c to remove errors.

Uploaded by

venkatesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

LAB II

a Login to default directory and see if the directory LAB2 exists. [Hint. ls]

b If the directory LAB2 exists, then remove it. [Hint. To remove the directory LAB2,
the following steps are needed (i) Go to that directory (cd LAB2); (ii) Remove its
content (rm -i *); (iii) Go back to the previous directory (cd ..); (iv) Remove the
directory (rmdir LAB2).]

c Create the directory LAB2 (mkdir LAB2) and go to the directory (cd LAB2).

1. Type the program in a file fmti.c and examine/understand the output.

#include<stdio.h>
int main(void)
{ int a=123,b=-123,c=12345;
printf("%2d\n",c);
printf("%10.2d\n",c);
printf("%-10.2d\n",c);
printf("%-7d\n",a);
printf("%07.2d\n",a);
printf("%07d\n",a);
printf("%+0-9.4d\n",a);
printf("%+09.4d\n",a);
printf("%+07d\n",a);
printf("%+07.4d\n",a);
printf("%+-07.4d\n",a);
printf("%-08d\n",b);
printf("%-08.2d\n",b);
printf("%-8.4d\n",b);
return 0;
}

2. Type the program in a file fmtf.c and examine/understand the output.

#include <stdio.h>
int main(void)
{ double a=12345.6789;
printf("\nFormatting with %%e or %%E\n");
printf("%e\n",a);
printf("%5e\n",a);
printf("%5.2E\n",a);
printf("%5.0E\n",a);
printf("%#5.0E\n",a);
printf("%05e\n",a);
printf("%010.2e\n",a);
printf("%+010.1e\n",a);
printf("\nFormatting with %%lf\n");
printf("%lf\n",a);
printf("%5lf\n",a);
printf("%4.2lf\n",a);
printf("%10.2lf\n",a);
printf("%-10.2lf\n",a);
printf("%10.0lf\n",a);
printf("%#10.0lf\n",a);
printf("%+010.2lf\n",-a);
printf("\nFormatting with %%g \n");
printf("%g\n",a);
printf("%9g\n",a);
printf("%4.3g\n",a);
printf("%4.5g\n",a);
printf("%#4.5g\n",a);
printf("%#9.5g\n",a);
printf("%5.4g\n",a);
return(0);
}

3. Find the errors (if any) in the following program: (You may type the program in a file
fun.c and identify the erros during compilation)

/* Is it a C program?*/
#include <stdio.h>
int
main(
)
{
int a,b
,c;
a=
2.45;
b
=a+2;
printf(
"Enter an integer:");
scanf(
"%d",
&c);
printf(
"%d %d %d\n",a,
b,c);
return
0;
}

4. Find the errors (if any) in the following program assuming that the #define statements
are correct. (You may type the program in a file err.c and identify the erros during
compilation). Modify the code so that it becomes error free.

/* There are errors in /*the*/ code.*/


#include <stdio.h>
#define CUBE(X) (X)*(X)*(X)
#define SQ(X) (X)*(X);
int main()
{
double Int,x,_x1,2xb,x-y,y3z;
int Float,char,a,b,c,d,pa@b,qa.b;
char int,u,_2v,w=t;
a=2,b=3;
a+b;
c=a+b;
a+b=1;
b-a==c;
d=w;
a=CUBE(d);
b=SQ(d)
u=d+62;
c=u-1;
u=’y’;
_2v=z;
y3z=CUBE(c);
x=SQ(c);
_x1=SQ(c)*2;
c=x+u;
return 0;
}

You might also like