c Glossary g - i
goto : The infamous goto! Never use it. Now if you argue, it has been used in Linux Operating system, they have their reasons. Mostly for optimizations it is used.
statement;
If the conditional expression given in the brackets is true, then the statement is executed. Else nothing happens.
else
{
printf("Greater or equal to 3")
}
In the code segment here depending on char variable option, we are adding, subtracting , multiplying or dividing 2 numbers. If the option is 'd', first we are checking if the denominator is non-zero. If it is zero, the program crashes with floating point error. Also we are enclosing the condition of n2 not equal to 0 in braces. If not, last else would apply to if (n2 !=0) and the answer will be wrong.
int : int is a datatype. Integer data types are normally stored in 2's complement notation and THE SIZE OF AN INTEGER IS THE WORD SIZE OF THE COMPUTER.
You have different versions of int viz
int,
short int,
The specification just says that short int is shorter or same size as int and long int is larger or same size as int. But most compilers store short int with 2 bytes and long ints with 4 bytes.
Basically goto , goes to a statement given by the label. The label must be in the same compilation unit.
Whenever you have the urge to use goto statement, think of break - to leave a loop, return to leave a function and exit to stop the program.
Never ever use goto statement. So don't expect me to write a sample program here.
if: if is used to conditionally execute one or more statements.
Syntax :
if (condn_expn)statement;
If the conditional expression given in the brackets is true, then the statement is executed. Else nothing happens.
In the place of statement, a block can also be used which is enclosed in curly brackets. It is advisable to wrap your statement in braces, even if there is one statement to avoid bugs.
if (a<3)
{
printf("Less than 3");
b=a;
}
You can optionally have an else block. If the condition is false, then else part is executed.
if (a<3)
{
printf("Less than 3");
b=a;
}else
{
printf("Greater or equal to 3")
}
In a nested if statement, else applies to the innermost if statement.
If there is a set of mutually exclusive conditions, then it is more efficient to use else if ladder than a set of individual if statements.
if(option == 'a' )
ans = n1+n2;
else if (option == 's')
ans = n1-n2;
else if (option == 'm')
ans = n1 * n2;
else if (option == 'd')
{
if (n2 != 0)
ans = n1/n2;
}
else
printf("Wrong option\n");
|
In the code segment here depending on char variable option, we are adding, subtracting , multiplying or dividing 2 numbers. If the option is 'd', first we are checking if the denominator is non-zero. If it is zero, the program crashes with floating point error. Also we are enclosing the condition of n2 not equal to 0 in braces. If not, last else would apply to if (n2 !=0) and the answer will be wrong.
int : int is a datatype. Integer data types are normally stored in 2's complement notation and THE SIZE OF AN INTEGER IS THE WORD SIZE OF THE COMPUTER.
You have different versions of int viz
int,
short int,
long int,
unsigned int
short unsigned int
long unsigned int
The specification just says that short int is shorter or same size as int and long int is larger or same size as int. But most compilers store short int with 2 bytes and long ints with 4 bytes.
Most compilers on 32 bit machines will have the following ranges. You can find the ranges of integer types for your compiler in the header file limits.h
Type/keyword | No. of Bytes | Range |
int | 4 | -2,147,483,648 -> +2,147,483,647 (2gb) |
short int | 2 | -32,768 -> +32,767 (16kb) |
long int | 4 | -2,147,483,648 -> +2,147,483,647 |
short unsigned int | 2 | 0 -> +65,535 (16kb) |
long unsigned int | 4 | 0 -> +4,294,967,295 (4 Gb) |
unsigned int | 4 | 0 -> +4,294,967,295 (4 Gb) |
Note that even a short int is 2 bytes in size. For smaller sizes, you can even use a signed char.
Most people do not realize that an int divided by another int gives truncated answer.
int m = 5;
int n = 2;
printf("%f\n",m/n);//will print 2.00 instead of 2.5
float a=m/n;
printf("%f\n",a);//still prints 2.0 as m/n gives 2 then
//this is converted to float
a = m/(n*1.0);
printf("%f\n",a);//printfs 2.5 because n*1.0 is a double hence double arithemetic
a = (float)m/n;
printf("%f\n",a);//prints 2.5 because m is converted to float
|
In an expression in the program , all operands are converted to long, if one operands is long and others are integer types. All operands are converted to int if one operand is int and others are smaller integer. If one operand is float or double, then all operands are converted into double.
Reference : LIX polytechnic int datatype
Comments
Post a Comment