All Exams Test series for 1 year @ ₹349 only
Question

Consider the following C statements:
char *str1 = "Hello; /* Statement S1 */
char *str2 = "Hello;"; /* Statement S2 */
int *str3 = "Hello"; /* Statement S3 */

Which of the following options is/are correct?

The correct answer is
S1 has a lexical error and S3 has a semantic error

The given question involves understanding errors in C statements. Let's analyze each statement one by one:

  1. Statement S1: char *str1 = "Hello;
    This line is intended to assign a string to the pointer str1. However, the string is not terminated with a closing double quote. This causes a lexical error, as the lexer (lexical analyzer) will not be able to find an end to the string literal.
  2. Statement S2: char *str2 = "Hello;";
    This is a correctly written statement. It assigns the string "Hello;" to the character pointer str2. The semicolon inside the string is part of the string itself, and the statement ends properly with a semicolon outside the string. There are no lexical or syntactic errors here.
  3. Statement S3: int *str3 = "Hello";
    This line attempts to assign a string to an integer pointer. While syntactically correct, it does not make semantic sense, leading to a semantic error. A string should be assigned to a character pointer, not an integer pointer, as the data types are incompatible.

Now, evaluating the options, the correct answer is:

  • S1 has a lexical error and S3 has a semantic error.
    This matches our analysis: S1 is missing a closing quote, causing a lexical error, and S3 has a semantic type mismatch.

Other options are incorrect based on the analysis above. S2 does not have any errors.

Was this answer helpful?

Important Questions from Lexical Analysis

  1. What is the output of a lexical analyzer?

  2. Which of the following are applications of symbol table ?

    (A) Storage allocation

    (B) Checking type compatability

    (C) Suppressing duplicate error messages

    Choose the correct answer from the options given below:

  3. Arrange the given compilation process in the correct order.

    a. Linking

    b. Assembling

    c. Compiling

    d. Pre-processing

  4. Which ONE of the following statements is FALSE regarding the symbol table?

  5. A lexical analyzer uses the following token definitions
    • $letter \rightarrow [A-Z a-z]$
    • $digit \rightarrow [0-9]$
    • $id \rightarrow letter(letter \mid digit)^*$
    • $number \rightarrow digit^+$
    • $ws \rightarrow (blank \mid tab \mid newline)^+$

    For the string given below,

    $x1 \quad 23mm \quad 78 \quad y \quad 7z \quad zz5 \quad 14A \quad 8H \quad AaYcD$

    the number of tokens (excluding $ws$) that will be produced by the lexical analyzer is __________. (answer in integer)
Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App