(a)
#include<stdio.h>
int main()
{
int i;
char a[] = "Hello";
while (a != '\0')
{
printf("%c", *a);
a++;
}
return 0;
}
Error: 'a' is an array of characters, which is initialized at the time of declaration, so it cannot be modified. i.e. writing 'a++' is illegal.
(b)
#include<stdio.h>
int main()
{
double dval;
scanf("%f", &dval);
printf("\nDouble Value = %lf", dval);
return 0;
}
Error: Wrong format specifier used, in scanf()
(c)
#include<stdio.h>
int main()
{
int ival;
scanf("%d\n", &n);
printf("\nInteger Value = %d", ival);
return 0;
}
Error: n is undefined in scanf().
(d)
#include<stdio.h>
int main()
{
char *mess[5];
for (i = 0; i < 5; i++)
scanf("%s", mess[i]);
return 0;
}
Error:
1. i is undefined.
2. We cannot save the string in a char type pointer from scanf() function.
(e)
#include<stdio.h>
int main()
{
int dd, mm, yy;
printf("\nEnter day, month and year\n");
scanf("%d%*c%d%*c%d", &dd, &mm, &yy);
printf("The date is: %d - %d - %d", dd, mm, yy);
return 0;
}
Error: No error, but the output dd-mm-yy (as entered) Now, what is %*c in scanf() function. %*c means after each %d is that, after every integer entered, space or Enter key hit, is read from the keyboard but ignored and next read item is stored in the variable.
(f)
#include<stdio.h>
int main()
{
char text;
sprintf(text, "%4d\t%2.2f\n%s", 12, 3.452, "Merry Go Round");
printf("\n%s", text);
return 0;
}
Error: sprintf()
the first argument should be of the array of characters char*, instead of char type.
(g)
#include<stdio.h>
int main()
{
char buffer[50];
int no = 97;
double val = 2.34174;
char name[10] = "Shweta";
sprintf(buffer, "%d %lf %s", no, val, name);
printf("\n%s", buffer);
sscanf(buffer, "%4d %2.2lf %s", &no, &val, name);
printf("\n%s", buffer);
printf("\n%d %lf %s", no, val, name);
return 0;
}
Error: No error but the warning, "format specifier" should not be used in scanf()
or sscanf()
functions.