Rule for Constructing Real Constants (floating point representation):
- Format:
<mantissa>e<exponent> - mantissa & exponent can have any sign; default is always positive
Ex:
+3.2e-5, 4.9e9, -0.3e+4
Rules for variable names:
- variable names is generally of length
1 to 31chars, but some compiler may allow more, upto247 chars. - $my_variable (❌) but my_variable (✅) is allowed.
Rules on comments
- Nested comments are not allowed
/* Cal of SI /* Author sam date 01/01/2002 */ */
- Comments can be written at any place
/* formula */ si = p * n * r / 100 ;
si = p * n * r / 100 ; /* formula */
si = p * n * r / /* formula */ 100 ;
Pay special attention to this type of declaration:
float b = a + 3.1, a = 1.5 ;
/* accessing a before it is defined, which is wrong!! */
int a, b, c, d ;
a = b = c = 10 ; /* OK */
int a = b = c = d = 10 ; // same problem ont okay(using b before defining it)
mod operator(%) rules:
- not appicable on
float - sign of the remainder is same as numerator
-5 % 2 = -1
5 % -2 = 1
Remember int / int -> int
so, k = 2/9 gives 0 and k = 9 / 2 gives 4
Example of arithmetic evaluation:
i = 2*3/4+4/4+8-2+5/8
i = 6/4+4/4+8-2+5/8
i = 1+4/4+8-2+5/8
i = 1 + 1+ 8 - 2 + 5 / 8
i = 1+1+8-2+0
i = 2+8-2+0
i = 10 - 2 + 0
i = 8+0
i = 8
a > b? g=a: g=b; what will happen?
L value required error & to fix this do a > b ? g=a : (g=b); or g = (a > b) ? a : b;
In absence of parentheses the compiler believes that b is being assigned to the result of the expression to the left of second =. Hence it reports an error.
Note: Assignment statements used with conditional operators must be enclosed within a pair of parenthesis.
While loop
Loop counter can be float also:
main( ){
float a = 10.0 ;
while ( a <= 10.5 ){
printf ( "\nRaindrops on roses..." ) ;
printf ( "...and whiskers on kittens" ) ;
a = a + 0.1 ;
}
}
can you use scanf() inside for loop?
Yes,
for ( scanf ( "%d", &i ) ; i <= 10 ; i++ )
printf ( "%d", i ) ;
A valid for loop
int i ;
for ( i = 0 ; i++ < 10 ; )
printf ( "%d\n", i ) ;
for ( i = 1, j = 2 ; j <= 10 ; j++ )
Remember: only one expression is allowed in test expression (here it is j <= 10 ). But this is also acceptable j <= 10 && i <= 20.
do-while loop example:
main( )
{
char another ;
int num ;
do
{
printf ( "Enter a number " ) ;
scanf ( "%d", &num ) ;
printf ( "square of %d is %d", num, num * num ) ;
printf ( "\nWant to enter another number y/n " ) ;
scanf ( " %c", &another ) ;
} while ( another == 'y' ) ;
}
What is the equivalent for-loop of the above program?
main( )
{
char another = 'y' ;
int num ;
for ( ; another == 'y' ; )
{
printf ( "Enter a number " ) ;
scanf ( "%d", &num ) ;
printf ( "square of %d is %d", num, num * num ) ;
printf ( "\nWant to enter another number y/n " ) ;
scanf ( " %c", &another ) ;
}
}
What is the equivalent while-loop of the above program?
main( )
{
char another = 'y' ;
int num ;
while ( another == 'y' )
{
printf ( "Enter a number " ) ;
scanf ( "%d", &num ) ;
printf ( "square of %d is %d", num, num * num ) ;
printf ( "\nWant to enter another number y/n " ) ;
scanf ( " %c", &another ) ;
}
}
Switch Rules!!
- The integer expression following the keyword
switchis any C expression that will yield an integer value. It could be an integer constant like 1, 2 or 3, or an expression that evaluates to an Integer. - The keyword
caseis followed by anintegeror acharacterconstant. The character will be replaced by their ASCII values in compilation time. - Each constant in each case must be different from all the others. so the program bellow is illegal
switch ( a ) { case 3 : ... case 1 + 2 : ... } - Float is not allowed in
case. - one cannot have a
casein a switch which looks like:case i <= 20: - use of
continuewill not take the control to the beginning of switch as one is likely to believe. - Cases can never have variable expressions (for example it is wrong to say
case a +3: )
Valid suitch statements
switch ( i + j * k )
switch ( 23 + 45 % 4 * k )
switch ( a < 4 && b > 7 )
Expressions can also be used in cases provided they are constant expressions. Thus case 3 + 7 is correct, however, case a + b is incorrect.
what will happen if you put any C statement inside switch but outside of any case?
switch ( i ){
printf ( "Hello" ) ;
case 1 :
j = 10 ;
break ;
case 2 :
j = 20 ;
break ;
}
here the printf() statement will never get executed, and the compiler won’t report an error.
Note: there is no need for a break statement after the default, since the control comes out of the switch anyway.
Why switch works faster than if-else ladder?
For switch the compiler generates a jump table at compilation time. As a result, during execution it simply refers the jump table to decide which case should be executed, rather than actually checking which case is
satisfied. As against this, if-elses are slower because they are evaluated at execution time. Also, loockup in jump table is much faster compared to checking every condition in if-else.A switch with 10 cases would work faster than an equivalent if-else ladder. Why?
If the 10th case is satisfied then jump table would be referred and statements for the 10th case would be executed. As against this, in an if-else ladder 10 conditions would be evaluated at execution time, which makes it slow.A switch with 2 cases would work slower than if-else ladder. Why?
If the conditions in the if-else were simple and less in number then if-else would work out faster than the lookup mechanism of a switch.goto hell
#include <stdio.h>
#include <stdlib.h>
int main() {
int goals;
printf("Enter a number");
scanf("%d", &goals);
if (goals <= 5) {
goto sos;
} else {
printf("%d", goals);
exit(0); /* <-- this is important otherwise it will go on executing printf("To err is human!") */
}
sos:
printf("To err is human!");
return 0;
}
Here sos is just a label it does not have any use without goto. If goto is not present, then compiler will treat the label’s content as simple c statements and execute them.
int main() {
sos:
printf("To err is human!");
}
output:
To err is human!
K & R way of defining methods:
void sum(a, b)
int a, b; // <--
{
printf("%d", a + b);
}
main(){
int a = 10,b = a;
sum(a, b);
}
return;
above statement returns a garbage value.
In C, arguments might be passed from right to left. And in some function call the order of passing arguments becomes an important consideration.
func(a, b, c);
In this call it doesn’t matter whether the arguments are passed from left to right or from right to left. However, what do you think the output of the program given below will be?
int a = 1 ;
printf ( "%d %d %d", a, ++a, a++ ) ;
Surprisingly, it outputs 3 3 1. This is because C’s calling convention is from right to left.
int i = 10, j = 20 ;
printf ( "%d %d %d \n", i, j ) ;
printf ( "%d", i, j ) ;
What will be the output?
No error is reported and the following will be displayed
10 20 1835285976 <--garbage value
10 <-- only i gets printed
C function, by default, always returns an integer value.