retagged by
7,014 views
12 votes
12 votes
int main()
{
    int 1a, b;
    Printf("\nGate 2018");
    Printf("%d",x);
}

How many types of error are there in this code?

retagged by

5 Answers

8 votes
8 votes
int main()
{
    int 1a, b; //syntax error
    Printf("\nGate 2018");
    Printf("%d",x); //x is not defined hence semantic error
}
  1.  A lexical error at line 3 "1a" because in C, the first character of a variable name should be either a letter or an underscore.
  2.  variable x is not defined anywhere in the program so it will give a semantic error

 

edited by
6 votes
6 votes
int main()
{
    int 1a, b;
    Printf("\nGate 2018");
    Printf("%d",x);
}

 

int 1a, b; $\Rightarrow$ error: invalid suffix "a" on integer constant


This gives a $lexical\space error$. This is because in C, the first character of a variable name should be either a letter or an underscore.

 

Printf("\nGate 2018"); $\Rightarrow$ warning: implicit declaration of function 'Printf'

$Semantic\space error$ at Printf. C is a case sensitive language and function used to print is printf. The program identifies 'Printf' as a new function and gives 'function not declared' error.

 

Printf("%d",x); $\Rightarrow$ error: 'x' undeclared (first use in this function)

This is a $semantic\space error$ again, as x is used here before declaring.

 

So, the $number\space of\space types$ of errors in this code is $2$. (Lexical error and Semantic error)

edited by
1 votes
1 votes
Considering the language to be C , there are 4 errors :

1. Identifier "1a" is not a valid declaration.

2. The 'P' in printf function should be in small letter.

3. Variable "x" should be declared first.

4. The function should return a value. So there must be a return statement.
0 votes
0 votes
int main()
{
    int 1a, b;/lexical error
    Printf("\nGate 2018");
    Printf("%d",x); //x is not defined hence semantic error
}
  1.  A lexical error at line 3 "1a" because in C, the first character of a variable name should be either a letter or an underscore.
  2.  variable x is not defined anywhere in the program so it will give a semantic error

 

Related questions

0 votes
0 votes
2 answers
1
Mohit Aggarwal asked Dec 1, 2021
1,329 views
what is the difference between lexical error and syntax error?
6 votes
6 votes
7 answers
2
Na462 asked Oct 5, 2018
3,409 views
5 votes
5 votes
2 answers
3
nakshatra asked Dec 11, 2015
3,757 views
What is the difference in between lexical, syntax and semantic errors?Please give appropriate example also.
7 votes
7 votes
4 answers
4
itsvkp1 asked Nov 1, 2017
3,205 views
if there is miss spelling in some keyword in a program then this misspelled keyword will be treated as lexical errors or it will be treated as a new identifier and accept...