How many bytes in a const int?
The storage size of an int in C is loosely defined, and may be either 2 bytes or, more commonly, 4 bytes.
Whether or not it is defined as const won't affect the size.
How primitive variables are stored on Stack?
Call-stacks are fixed-length and are allocated on a per-thread basis as threads are instantiated. The stack pointer CPU register keeps track of the next available address in the current thread's stack. The compiler computes the length of a function according to the number and type of its local variables (including formal arguments) plus the return address. When the function is invoked, the current stack pointer is adjusted by this amount, creating a "stack frame" specific to that function. Given the start address of the stack frame, the local variables and formal arguments can be referred to via constant offsets within the stack frame. When the function returns, the stack pointer is readjusted, effectively freeing the memory without actually releasing it back to the system. In this way, memory can be allocated and released on the stack with minimum cost.
What are the disadvantages of inline functions?
The inline functions is an optimization technique that is used by the compilers. The disadvantage of the inline functions is the increased binary size.
What must you do to evaluate a variable expression?
you have to undo what ever's being done to the variable :p
C program to find euler's number e?
#include <stdio.h>
long factorial(int n) {
long result = 1;
for (int i = 1; i <= n; ++i)
result *= i;
return result;
}
int main ()
{
double n=0;
int i;
for (i=0; i<=32; i++) {
n=(1.0/factorial(i))+n;
}
printf("%.32f\n", n);
}
//2.71828182845904553488480814849027
How do you enter a number as octal in c?
Not sure is what respect, but when you want a variable to be read as an octal put a 0 in front of the number.
a = 01, b = 02; Lets the compiler know that 1 and 2 are to be stored as octals.
To print an unsigned number as an octal use %o.
In c a pointer is a variable that points to or references a memory location in which data is stored. Each memory cell in the computer has an address that can be used to access that location so a pointer variable points to a memory location we can access and change the contents of this memory location via the pointer.
Pointer declaration
A pointer is a variable that contains the memory location of another variable. The syntax is as shown below. You start by specifying the type of data stored in the location identified by the pointer. The asterisk tells the compiler that you are creating a pointer variable. Finally you give the name of the variable. type * variable name Example: int *ptr; float *string;
import java.io.* ;
public class Roman
{
public static void main(String[] args)throws IOException
{
InputStreamReader reader = new InputStreamReader(System.in) ;
BufferedReader br = new BufferedReader (reader) ;
System.out.print("Enter any decimal number between 1 and 59 : ") ;
int n = Integer.parseInt(br.readLine()) ;
String c[] = {"I","II","III","IV","V","VI","VII","VIII","IX","X","L"} ;
String s = "" ;
int t ;
if(n <= 10)
System.out.println("The number '"+n+"' in ROMAN notation is '"+(c[n-1])+"'") ;
else if(n > 10 && n <= 59)
{
int y = n/10 ;
int r = n%10 ;
if(y 0)
s = s+c[10] ;
}
else
{
if(r!=0)
s = s+c[r-1] ;
}
System.out.println("The number '"+n+"' in ROMAN notation is '"+s+"'") ;
}
else
System.out.println("!! FATAL ERROR !! [ No. Greater than 59 ] ") ;
}
}
Program designed to swap the values of two variables?
You'll need 3 variables for this, here's a pseudo code for swapping values of 2 variables.
ALGORITHM SWAP
var1 = 10
var2 = 20
temp = 0
temp = var1 "temp = 10"
var1 = var2 "var1 = 20, originally 10"
var2 = temp "var2 = 10, originally 20"
END SWAP
Do one what has a value of eight A 512 B 32 C 4096 D 83?
If you add up all the digits, A has the value 8.
What is the smallest number of formal parameters that can be included in a function definition in C?
You can have a function with no parameters.
How do you convert cents to dollars in C without losing decimal places?
d= c/100;
c= c-d*100;
c=245 --> d=2, c=45
What is the difference between forward slash and backward slash in c?
forward slash - division operator
backward slash - special character (e.g. \n - newline) in C strings
How do you put battiries in a lazer pointer?
Usually At The Back Or Front Of A Laser Pointer You Can Screw It Off, Just Slot The Batteries In And BAM!!! It SHould Work, Be Carful that you have the right batteries though, they should be the type you get in a watch XD
Write a basic program to print even and odd numbers in between 1 to 10?
# include<stdio.h>
void main()
{
int i = 1;
while(i<11)
{
if(i%2==1)
{
printf("\n%d",i);
}
i++;
}
}
What are the Advantages of in line function in c plus plus?
Inline functions effectively remove the function call altogether. However, just because a function is declared inline is no guarantee it will be inline expanded. it is merely a hint to the compiler that the function should considered for inline expansion, nothing more.
To understand how inline expansion helps, consider the following code:
void print(int x) {
std::cout << x << std::endl; }
int main()
{
int x=5;
print(x);
x*=3;
print(x);
print(++x);
return(0);
}
If the print() function could be inline expanded, it would be the same as if you'd written the following:
int main()
{
int x=5;
std::cout << x << std::endl;
x*=3;
std::cout << x << std::endl;
std::cout << ++x << std::endl;
return(0);
}
Our code may be a little larger, but it will execute that much faster because we've completely eliminated all three function calls. But while we could expand functions inline manually, as shown above, it makes no sense to do so. In a larger program where the function might be called several dozen times, it becomes that much harder to manage the inline code because there are so many instances of it. By keeping the function and declaring it inline, we can easily modify the function's behaviour because the code is all in one place. More fundamentally, however, if we manually expand functions inline, our code becomes that much larger, and performance is directly related to code size.
By giving the compiler hints as to which functions should be inline expanded, we allow the compiler to decide which is better in each case. The compiler uses complex algorithms to determine the best compromise between code size and performance. A function that is called many times but has little actual code is an ideal candidate for inline expansion, but then so is any function, regardless of complexity, that is called just the once. In the latter case the function may not be absolutely necessary, but functions can help make the code that uses them that little bit more readable and easier to understand, with much less need for commentary.
Even recursive functions can be inline expanded, but usually there is a limit to the depth of recursion (16 being a common limit). This can greatly increase the performance of the function at the expense of code size, which may affect other areas of the program. However, it is not something that should concern the programmer. The compiler is a better judge of what should be expanded and what should not. Even so, some compilers also include a non-standard force_inline flag that can override the compiler's decision, but this is best used sparingly, and only when the programmer is absolutely sure the performance gained will not cause problems elsewhere due to the increased code size.
A backward slash is a slash that tips backwards (\), as opposed to a forward slash that tips forwards (/).
Write a program to find the sum of even numbers from 2 to50?
In Java:
sum = 0;
for (i = 2; i <= 50; i += 2)
sum += i;
Similar (perhaps identical?) in C.