0% found this document useful (0 votes)
12 views14 pages

Common C Programming Errors and Fixes

The document identifies various errors in C programming code segments, including incorrect array declarations, initialization statements, loop structures, conditional statements, input statements, and variable declarations. Each error is accompanied by a correction and explanation. The document serves as a guide for debugging common syntax and logical errors in C programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views14 pages

Common C Programming Errors and Fixes

The document identifies various errors in C programming code segments, including incorrect array declarations, initialization statements, loop structures, conditional statements, input statements, and variable declarations. Each error is accompanied by a correction and explanation. The document serves as a guide for debugging common syntax and logical errors in C programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

1.

identify errors,if any,in each of the following array declaration statements, assuming that
ROW and
COLUMN are declared as symbolic constants.
(a)int score (100);
(b)float values [10,15];
(c)char name[15];
(d) float average[ROW],[COLUMN];
(e) double salary [i+ROW]
(f) long int number[ROW]
(g) int sum[];
(h)int array x[COLUMN];
ANS :
(a) Incorrect - Parentheses used instead of square brackets.

Correct Code:

int score[100];

(b) Incorrect - Invalid array size format.

Correct Code:

float values[10][15];

(c) Correct

(d) Incorrect - Missing second array name.

Correct Code:

float average[ROW][COLUMN];

(e) Incorrect - Array size must be constant.

Correct Code:

double salary[ROW];
(f)- Correct

(g) incorrect - Size not specified.

Correct Code:

int sum[10];

(h) Incorrect - Space in variable name.

Correct Code:

int arrayx[COLUMN];

2. IDENTIFY ERRORS,IF ANY,IN EACH OF THE FOLLOWING INITIALIZATION


STATEMENTS.
(a) int number[]={0,0,0,0,0};
(b)float item[3][2]={0,1,2,3,4,5};
(c)char word[]={‘A’,’R’,’R’,’A’,’Y’};
(d)int m[2,4]={(0,0,0,0) (1,1,1,1)};
(e)float result[10]=0;

ANS:
(A) Correct
(B) Correct
(C) Incorrect - Uses wrong quotation marks.
Correct: {'A','R','R','A','Y'}
(D) Incorrect - Wrong brackets and separators.
Correct: int m[2][4]={{0,0,0,0},{1,1,1,1}};

(E) Incorrect - Cannot assign single value like this.


Correct: float result[10]={0};
[Link] that the arrays A and Bare declared as follows:
Int A[5][4];
Float B[4];
Find the errors (if any )in the following program segments.
(a) for(i=1;i<4;i++)
Scanf(“%f”,B[i]);
(b) for(i=1;i<=5;i++)
For(j=1;j<=4;j++)
A[i][j]=0;
(c)for(i=0;i<=4;i++)
B[i]=B[i]+I;
(d) for(i=4;i>=0;i--)
For(j=0;j<4;j++)
A[i][j]=B[j]+1.0;

ANS:
(A) Incorrect - scanf needs address operator.
Correct: scanf("%f",&B[i]);
(B) Incorrect - Array index starts from 0, exceeds bounds
(C) Incorrect – B size is 4, index goes out of rang
(D) correct

[Link] is the error in the following program?


Main()
{
Int x;
Float y[];
…….}

ANS: as type name Int , Float are not valid .


#include<stdio.h>
int main()
{
int x;
float y[50];
return 0;
}

5.
what is the output of the following program?
main()
{
int m[]={1,2,3,4,5}
int x,y=0;
for(x=0;x<5;x++)
y=y+m[x};
printf("%d",y);
}
ANS: Incorrect - Missing semicolon after array declaration , m[x} should be m[x].

correct code :
int main()
{
int m[]={1,2,3,4,5};
int x,y=0;
for(x=0;x<5;x++)
y=y+m[x];
printf("%d",y);
}
O/P: 15

6.
What is the output of the following program ?
main()
{
chart string [] = “HELLO WORLD”;
int m;
for(m = 0; string [m] != ‘\0’;m++)
if ( (m%2) == 0)
printf(“%c”, string [m] );
}
ANS:
Correct Code:
#include <stdio.h>
int main()
{
char string[]="HELLO WORLD";
int m;
for(m=0;string[m]!='\0';m++)
if(m%2==0)
printf("%c",string[m]);
}

Output:
HLOWRD

7. Find errors, if any, in the following code segments:


a. char str[10]
strncpy(str,”GOD”,3);
printf(“%s”,str);
char str[10];
strncpy(str, "GOD", 3);
str[3] = '\0'; // Null-terminate the string
printf("%s", str);

b. char str[10];
strcpy(str,”Balagurusamy”);

c. if strstr(“Balagurusamy”,”guru”)==0);
printf(“Substring is found”);

[Link] s1[5],s2[10],
gets(s1,s2);

ANS : (a) incorrect – as the same variable used twice and Missing null character before printing.
correct code is :
#include<stdio.h>
#include<string.h>
int main(){
char str[10];
strncpy(str,"GOD",3);
printf("%s",str);
char st[10];
strncpy(st, "GOD", 3);
st[3] = '\0'; // Null-terminate the string
printf("%s", st);
return 0;
}
O/P : GOD GOD
(b) correct
(c)incorrect – no ; after if condition.
(d) incorrect – as char * gets (char* )
correct code is :
#include<stdio.h>
#include<string.h>
int main(){
char s1[5],s2[10];
gets(s1);
gets(s2);
return 0;
}

[Link] errors,if any in each of the following looping [Link] that all the variables
have been declared and assigned values.
(a) while(count!=10);
{
count=1;
sum=sum+x;
count=count+1;
}
(b)name=0;
do
{name=name+1;
printf("my name is john\n");}
while(name=1)
(c)do;
total=total+value;
scanf("%f",&value);
while(value!=999);
(d)for(x=1,x>10;x=x+1)
{
-------
____
_____ }
(e)m=1;
n=0;
for(;m+n<10;++n);
printf("Hello\n");
m=m+10

(f) for(p=10;p>0;)
p=p-1;
printf("%f",p);
ANS : (a) incorrect – as count , sum , x aren’t declared in the function.
correct code is :
#include<stdio.h>
#include<string.h>
int main()
{
int count , sum , x;
while(count!=10);
{
count=1;
sum=sum+x;
count=count+1;
}
return 0;
}

(b) incorrect – as name isn’t declared in the function , and the loop runs continuously.
correct code is :
#include<stdio.h>
#include<string.h>
int main()
{
int name=0;
do
{
name=name+1;
printf("my name is john\n");
}
while(name=1);

return 0;
}
(c) incorrect - do; is invalid syntax.
(D) incorrect – loop condition won’t executes .
(E) incorrect - Semicolon after for causes loop body to skip.
(F) incorrect - Missing braces {} – printf not inside loop.

[Link] errors ,if any,in each of the following segments:


(a)if (x+y=z&&y>0)
printf("");
(b)if (p<0)||(q<0)
printf("sign is negative");
(c) if (code>1);
a=b+c
else
a=0 ;

ANS :
(A) incorrect – = used instead of ==( as cmp is == )
(B) incorrect – Parentheses missing around condition.
(C) incorrect – after if condition , no ; should be placed .
[Link] the error,if any,in the following statements:
(a)if(x>=10)then
printf("\n");
(b) if x>=10
printf("0k");
(c) if (x=10)
printf("Good");
(d) if(x=<10)
printf("welcome");

ANS:
(A)INCORRECT – then doesn’t support in c .
(b) incorrect – missing parenthesis in C
(c) incorrect - = is used instead of ==
(D) incorrect - =< isn’t exist
[Link] errors,if any, in the following input statements.
(a)scanf("%c%f%d",city,&price,&year);
(b)scanf("%s%d",city,amount);
(c)scanf("%f,%d,&amount,&year);
(d)scanf(\n"%f",root);
(e)scanf("%c%d%id",*code,&count,Root);

ANS :

(a) incorrect – city should be address (&city).


(b) incorrect – Missing address for amount.
(c) incorrect – Wrong format string syntax.
(d) incorrect – Incorrect quotation placement.
(e) incorrect - %id incalid specifier

[Link] errors,if any,in the following declaration statements.


int x;
float letter,DIGIT;
double=p,q
exponent alpha,beta;
m,nz:INTEGER
short char c;
long int m;count;
long float temp;

ANS :
(a) correct
(b) correct
(c) incorrect - Variable names cannot appear on the left side of = in declarations
(d) incorrect - exponent is not a valid C data type.
(e) Incorrect : Pascal-style syntax used; not valid in C.
(f) Incorrect - short cannot be used with char.

(g)Incorrect - count has no data type.

(h)Incorrect - long float is not a valid data type in C.

[Link] syntax errors in the following [Link] corrections,what output would you
expect when you execute it?
#define PI 3.14159
main()
{
int R,C;
float perimeter;
float area;
C=PI
R=5;
perimeter=2.0*C*R
area=C*R*R;
printf("%f","%d",&perimeter,&area)
}
ANS : Missing semicolon after C = PI , C should be float, not int , printf format string is
incorrect , printf arguments are wrong.

correct code :
#include <stdio.h>

#define PI 3.14159

int main()

int R;

float C, perimeter, area;

C = PI;

R = 5;

perimeter = 2.0 * C * R;
area = C * R * R;

printf("Perimeter = %f\n", perimeter);

printf("Area = %f\n", area);

return 0;

You might also like