- What will be the output of the following statement ? int a=10; printf(“%d &i”,a,10);
a) error
b) 10
c) 1010
d) none of these - Which of the following special symbol allowed in a variable name?
A. (asterisk)
B. I (pipeline)
C. – (hyphen)
D. _ (underscore)
The correct answer and explanation is:
1. What will be the output of the following statement?
int a = 10;
printf("%d &i", a, 10);
Answer: a) error
Explanation:
In this C code snippet, there is a syntax issue within the printf() function. The format string provided is "%d &i", but the variables supplied are a and 10.
- The format string
"%d &i"expects:%dto format an integer, which corresponds to the variablea(with a value of 10).&iappears to be attempting to print the address of the variablei, but no variable namediis defined in theprintf()function call. Sinceiis not declared, the program will fail to compile and result in an error.
Therefore, the correct answer is a) error.
2. Which of the following special symbols is allowed in a variable name?
- A. (asterisk)
- B. | (pipeline)
- C. – (hyphen)
- D. _ (underscore)
Answer: D) _ (underscore)
Explanation:
In C and many other programming languages, variable names must adhere to specific rules regarding which characters are allowed. Let’s analyze the options:
- A. (asterisk): The asterisk
*is not allowed in variable names. It is used as a dereference operator for pointers in C, and it cannot be part of a variable name. - B. | (pipeline): The pipeline
|character is also not allowed in variable names. It is used as a bitwise OR operator in C, and it cannot be used in identifiers. - C. – (hyphen): The hyphen
-is not allowed in variable names because it is used as a subtraction operator. If used in a variable name, it would create ambiguity in the syntax, as the language would not know if you are subtracting something. - D. _ (underscore): The underscore
_is the only special symbol allowed in variable names. It is commonly used to separate words in a multi-word variable name (e.g.,my_variable_name). It is a valid character in variable names and is frequently used in programming for readability, especially in languages that follow the “snake_case” naming convention.
General Rules for Variable Names:
- A variable name can contain letters (
a-z,A-Z), digits (0-9), and the underscore_. - A variable name cannot start with a digit.
- Special characters like
*,|,-, and others (apart from underscore) are not allowed in variable names. - Keywords reserved by the language (e.g.,
int,return,if) cannot be used as variable names.
Thus, the correct answer is D) _ (underscore).