Question

In: Computer Science

Question 2. Explain preprocessor directives in C++. Describe Error and pragma directives and write code snippets....

Question 2.

Explain preprocessor directives in C++.

  1. Describe Error and pragma directives and write code snippets.
  2. Describe define, and undef, directives.
  3. Write the code which shows if constant “VALUE” is not defined then define it and replace its value to 5 on runtime. Display the value of x.

int x = VALUE;

Solutions

Expert Solution

The preprocessor director in c++ gives the instructions to compiler for preprocessing the data or information before the actual compilation begins. It is the lines that which is included in the program and is invoked by the compiler for doing the process before the compilation. It begins with the # symbol. Only whitespace character will be there before the preprocessor and we cannot write anything before that. Also they do not end with a semicolon .

Example is #define ,#include ,etc

#define PI 3.14 - when we define this PI with 3.14 and in the program at compiling time instead of PI the value 3.14 is replaced.

Error directives is used to stop or aborting the compilation when an error is been found in the program.

Syntax is #error message

Example: we are using the sqrt () function and we do not use the math.h header file, then we get the error.

Now include the math function,

Here we get the right output when the #define cmath is added.

pragma directives helps us to specify functions which is required for the run before the execution of main().

#include <stdio.h>

void func();

#pragma startup func

void func()

{

Printf("Inside func()\n");

}

int main()

{

printf("inside main()\n");

Return 0;

}

Define directives are used for defining some constant . The constant will be replaced by the value at compile time

Example

undef is used to tell for removing the macro definition. Here I have used under PI after defining PI. We get an error.

The code is shown below,

#include<stdio.h>

#define VALUE 5


int main()
{
int x=VALUE;
printf("%d",x);

return 0;
}

​​​​​​

Hope this helps you. Please give a thumbs up if it helped you. Comment for more doubts regards to the same. Thanks for posting


Related Solutions

For each of the following Visual Basic code snippets, identify the syntax error.   If intX >...
For each of the following Visual Basic code snippets, identify the syntax error.   If intX > 100   lblResult.Text = "Invalid Data" End If   Dim str As String = "Hello" Dim intLength As Integer intLength = Length(str) If intZ < 10 Then   lblResult.Text = "Invalid Data"   Dim str As String = "123" If str.IsNumeric Then   lblResult.Text = "It is a number." End If   Select Case intX   Case < 0     lblResult.Text = "Value too low."   Case > 100     lblResult.Text = "Value too high."   Case Else     lblResult.Text = "Value...
A pragma and an intrinsic function are both statements embedded in C source code. What is...
A pragma and an intrinsic function are both statements embedded in C source code. What is the primary difference in how the compiler handles these two types statements?
This is a numerical methods question using MATLAB. Which of the following code snippets finds the...
This is a numerical methods question using MATLAB. Which of the following code snippets finds the forward difference estimate of the derivative at each of the x values. Assume x and y have been previously defined, for example as y=[10,20,25, 27.5, 30]; x = [0.3,0.5, 0.8, 0.9, 1]; (d is the derivative variable name) Although not necessarily so, there may be more than one correct answer. a) for k=1:length(y)-1 d(k)=(y(k+1)-y(k))/(x(k+1)-x(k)); end d(k+1)=NaN b) for k=1:length(y) d(k)=(y(k+1)-y(k))/(x(k+1)-x(k)); end c) d(1)=NaN; for...
PYTHON Write the time complexity of the code snippets in Big O notation. Also, add one/two...
PYTHON Write the time complexity of the code snippets in Big O notation. Also, add one/two lines to explain your answer. 1a int a = 0, b = 0; for (int i = 0; i< N; i++) {     a = a + i; } for (int j = 0; j < N; j++) {     b = b + j; } 1b bool isPrime(int N) { if (N == 1) return false; for (x = 2; x <= sqrt(N);...
Explain how you can secure passwords and other sensitive data on a website. Provide code snippets...
Explain how you can secure passwords and other sensitive data on a website. Provide code snippets to explain your approach. Your answer can be based on the use of PHP/MySQL or ASP.NET/SQL based solution. Explain how online advertisements will be stored and served.
Write one difference for items below a.     char versus Character b.     Source code versus Byte code c.    Complier error...
Write one difference for items below a.     char versus Character b.     Source code versus Byte code c.    Complier error versus Runtime error d.   char [ ] versus String [ ]
From this information: the following code, explain which statements are instructions, which are assembly directives, which...
From this information: the following code, explain which statements are instructions, which are assembly directives, which are labels, and which are comments: ;"this"program"adds"3"to address $2000"and"increments"Accumulator"B """""""" org"$4000 ldaa"$2000 adda"#3 ;" add"3"to"(A)" staa"$2000 incb ; increment"B
Error detection/correction C Objective: To check a Hamming code for a single-bit error, and to report...
Error detection/correction C Objective: To check a Hamming code for a single-bit error, and to report and correct the error(if any) Inputs: 1.The maximum length of a Hamming code 2.The parity of the check bits (even=0, odd=1) 3.The Hamming code as a binary string of 0’s and 1’s Outputs: 1.The original parity bits (highest index to lowest index, left to right) 2.The new parity bits (highest index to lowest index, left to right) 3.The bit-wise difference between the original parity...
**Add comments to existing ARM code to explain steps** Question that my code answers: Write an...
**Add comments to existing ARM code to explain steps** Question that my code answers: Write an ARM assembly program to calculate the value of the following function: f(y) = 3y^2 – 2y + 10 when y = 3. My Code below, that needs comments added: FunSolve.s LDR R6,=y LDR R1,[R6] MOV R2,#5 MOV R3,#6 MUL R4,R1,R1 MUL R4,R4,R2 MUL R5,R1,R3 SUB R4,R4,R5 ADD R4,R4,#8 st B st y DCD 3
1st question: explain Living wills and advanced directives applied in nursing practice in wa 2nd question:...
1st question: explain Living wills and advanced directives applied in nursing practice in wa 2nd question: explain  Guardianship applied in nursing practice in wa
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT