Class3 ProgrammingC Winter24!25!1
Class3 ProgrammingC Winter24!25!1
Code as if the guy who ends up maintaining your code will be a violent psychopath
who knows where you live - Martin Golding.
Variable Naming
1. Begin with a letter or underscore ( _ ) and followed by any combination of letters (upper or
lowercase), underscores, or the digits 09.
(d) int, oat etc. - reserved names. These mean specic meaning for the compiler. Hence
these cannot be used.
int
1. Integer constant consists of a sequence of one or more digits
2. Minus sign preceding the sequence indicates that the value is negative.
4. 2,000: NO.
1
(a) 050 is octal and in decimal represents 5 × 81 + 0 × 80 = 40 in decimal.
7. Hexadecimal (base 16): integer constant preceded by a zero and the letter x.
oat
Type oat can be used for storing values containing decimal places.
2. %f in printf.
double
1. extended precision type, similar to oat, with more signicant digits.
char
1. exactly one byte in size.
_Bool
1. stores just 0 and 1.
3. %i can be used.
2
Type Speciers[2]
1. Within each of the groups above, the dierence between types is only their size (i.e., how
much they occupy in memory): the rst type in each group is the smallest, and the last is
the largest, with each type being at least as large as the one preceding it in the same group.
Other than that, the types in a group have the same properties.
2. Only char has fundamental data size specied. All other depend on the system and the
compiler.
3. For integer types, having more representable values means that the range of values they can
represent is greater; for example, a 16-bit unsigned integer would be able to represent 65536
distinct values in the range 0 to 65535, while its signed counterpart would be able to represent,
on most cases, values between -32768 and 32767.
4. For oating -- point types, the size aects their precision, by having more or less bits for their
signicant and exponent.
5. The properties of fundamental types in a particular system and compiler implementation can
be obtained by using the numeric_limits classes (see standard header <limits>).
6. If for some reason, types of specic sizes are needed, the library denes certain xed-size type
aliases in header <cstdint>.
3
Storage Classes
auto
Default storage class of any type of variable
extern
global variable visible all across the program
static
visibility of local variable, but lifetime of extern
once declared inside a function, does not get destroyed after execution of the function ⇒ value
is retained and can be used during future function calls. (e.g. can count how may times a
function is called)
register
similar to auto, but stored in CPU register for faster execution
Precedence
Each operator in C has a precedence. Evaluation of an expression generally proceeds from left
to right. However, the operations of multiplication and division are given precedence over the
operations of addition and subtraction.
#i n c l u d e < s t d i o . h>
int main ( void )
{
result = a = b; // subtraction
printf (" a = b = %i \ n " , result );
result = b * c; // multiplication
printf ("b * c = %i \ n " , result );
4
result = a / c; // division
printf (" a / c = %i \ n " , result );
result = a + b * c; // precedence
printf (" a + b * c = %i \ n " , result );
// p r i n t f (" a * b + c * d = %i \ n " , a * b + c * d);
return 0;
}
1. Note - try to do division by zero as integer and as oat and see what result comes up.
#i n c l u d e < s t d i o . h>
int a = 25;
int b = 2;
float c = 25.0;
float d = 2.0;
return 0;
}
Unary Operator
1. High precedence
#i n c l u d e < s t d i o . h>
5
int main ( void )
{
int a = 25 , b = 5, c = 10 , d = 7;
return 0;
}
float f1 = 123.125 , f2 ;
int i1 , i2 = = 150;
char c = 'a ' ;
return 0;
}
6
123.125000 assigned to an int produces 123
=150 assigned to a float produces=150.000000
=150 divided by 100 produces =1.000000
=150 divided by 1 0 0 . 0 p r o d u c e s = 1 . 5 0 0 0 0 0
( float ) =150 d i v i d e d by 1 0 0 p r o d u c e s = 1 . 5 0 0 0 0 0
2. has a higher precedence than all the arithmetic operators except the unary minus and unary
plus.
4. Another example:
Assignment Operators
c o u n t += 1 ;
is equivalent to
count = count + 1 ;
The eect of the so -- called plus equals operator += is to add the expression on the right side
of the operator to the expression on the left side of the operator and to store the result back into
the variable on the left-hand side of the operator.
Any of the arithmetic operators, including +, , Ö, /, and % can be combined.
is equivalent to
a = a / ( b+c ) ;
The eect of the so-called plus equals operator += is to add the expression on the right side
of the operator to the expression on the left side of the operator and to store the result back into
the variable on the left-hand side of the operator. So, the previous statement is equivalent to this
statement:
7
count = count + 10;
A slightly more involved expression is:
a /= b + c;
which divides a by whatever appears to the right of the equal signor by the sum of b and
cand stores the result in a.The addition is performed rst because the addition operator has
higher precedence than the assignment operator.
a = a / (b+c);
Logical operators
#i n c l u d e < s t d i o . h>
main ( ) {
/* L o g i c a l AND o p e r a t o r */
if ( a && b )
{
p r i n t f (" Line 1 = Condition is t r u e \n" );
}
/* L o g i c a l OR o p e r a t o r */
if ( a || b )
{
p r i n t f (" Line 2 = Condition is t r u e \n" );
}
a = 0; b = 10;
if ( a && b )
{
p r i n t f (" Line 3 = Condition is t r u e \n" );
}
else
{
p r i n t f (" Line 3 = Condition is not t r u e \n" );
}
/* L o g i c a l NOT o p e r a t o r */
if ( ! ( a && b ) )
{
p r i n t f (" Line 4 = Condition is t r u e \n" );
}
}
8
Shift Operators
1. << (left shift): Takes two numbers, left shifts the bits of the rst operand, the second operand
decides the number of places to shift. Or in other words left shifting an integer x with an
integer y (x<<y) is equivalent to multiplying x with 2^y (2 raise to power y).
2. >> (right shift) Takes two numbers, right shifts the bits of the rst operand, the second
operand decides the number of places to shift.Similarly right shifting (x>>y) is equivalent to
dividing x with 2^y.
#i n c l u d e <s t d i o . h>
int main ( ) {
return 0;
}
Program Looping
9
Problem 1: Find sum of eight integers starting from 1.
#i n c l u d e < s t d i o . h>
sumEight = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 ; // t r i a n g u l a r number
return 0;
}
Relational Operators
== Checks if the values of two operands are equal or not, if yes then (A==B) is not true
condition becomes true.
!= Checks if the values of two operands are equal or not, if values are not (A!=B) is true
equal then condition becomes true.
> Checks if the value of left operand is greater than the value of right (A>B) is not true
operand, if yes then condition becomes true.
< Checks if the value of left operand is less than the value of right (A<B) is true
operand, if yes then condition becomes true.
>= Checks if the value of left operand is greater than or equal to the value (A>=B) is not true
of right operand, if yes then condition becomes true.
<= Checks if the value of left operand is less than or equal to the value of (A<=B) is true
right operand, if yes then condition becomes true.
The relational operators have lower precedence than all arithmetic operators. That means,
a<b+c
is evaluated as,
a <(b+c )
#i n c l u d e < s t d i o . h>
10
{
int n, sumTwoHum = 0 ;
return 0;
}
The for statement provides the mechanism that enables you to avoid having to explicitly write out
each integer from 1 to 200. The general format of the for statement is as follows:
The program statement is executed as many times as specied by the parameters set up in the for
statement.
1. The three expressions that are enclosed within the parentheses - init_expression, loop_condition,
and loop_expression - set up the environment for the program loop.
2. The program statement that immediately follows can be any valid C program statement and
constitutes the body of the loop.
3. This statement is executed as many times as specied by the parameters set up in the for
statement.
4. The rst component of the for statement, labeled init_expression, is used to set the initial
values before the loop begins. Here n is initialized to 1.
5. Second, we have a relational operator that forms the loop condition. The loop is run as long
as this condition is met. Here we have n <= 200.
6. The program statement sumTwoHun = sumTwoHun + n, is run as long as the above relational
operation is TRUE.
7. The nal component of the for statement contains an expression that is evaluated each time
after the body of the loop is executed.
8. Semi colon ends the loop.
11
for ( n =0; n <=200; ++n ) {
sumTwoHun = sumTwoHun + n ;
}
return 0;
}
1. Increment operator is used to increment the current value of the variable by adding integer 1.
4. There are two ways of doing the increment operator. The pre -- increment (as given in the
above example), and the post -- increment (n++).
5. The pre -- increment operator is used to increment the value of the variable before using in
the expression.
6. Post increment operator is used to increment the value of the variable as soon as after executing
expression completely in which post -- increment is used.
7. Note that in the above program it does not matter how you use this operator. But there are
cases where this matter, e.g.
#i n c l u d e < s t d i o . h>
int main ( void ){
int n =4 , p =4;
i n t m=++n ;
int o=p++;
return 0;
}
12
Questions
1. Write a program to calculate the nth triangular number, where n is to be taken as user input.
3. Write a program to nd all the prime numbers among the rst 1000 integers.
References
[1] http://www.tutorialspoint.com/cprogramming/c_relational_operators.htm
[2] http://www.c4learn.com/c-programming/c-increment-operator/
[2] http://www.cplusplus.com/doc/tutorial/variables/
13