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

How many types of error are there in this code?

6 Answers

8 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 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 1 vote
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.
1 1 vote
1a -> 1 Lexical Error
Printf -> 2 Syntax Error
x -> 1 Semantic Error
0 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

 

0 0 votes

there will be two types of errors-  lexical and syntax error

error 1  LEXICAL ERROR-    tokens are meaningful lexemes, so while making tokens the analyzer won’t be able to make 

“1a”  as a single token. Hence ERROR.

error 2 SYNTAX ERROR-  as undeclared variable “x” is used so syntactically incorrect.

Position:
Show:

Related questions

0 0 votes
1 1 answer
2.6k
2.6k views
Mohit Aggarwal asked Dec 1, 2021
2,582 views
what is the difference between lexical error and syntax error?
6 6 votes
7 7 answers
5.3k
5.3k views
Na462 asked Oct 5, 2018
5,310 views
5 5 votes
2 2 answers
4.9k
4.9k views
nakshatra asked Dec 11, 2015
4,858 views
What is the difference in between lexical, syntax and semantic errors?Please give appropriate example also.
7 7 votes
4 4 answers
4.5k
4.5k views
itsvkp1 asked Nov 1, 2017
4,463 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...