0% found this document useful (0 votes)
2 views

Class3 ProgrammingC Winter24!25!1

The document serves as an introduction to C programming, covering essential topics such as data types, variable naming conventions, arithmetic operations, and storage classes. It explains the five basic data types (int, float, double, char, and _Bool) along with their characteristics and usage. Additionally, it discusses operators, type conversions, and provides examples to illustrate the concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Class3 ProgrammingC Winter24!25!1

The document serves as an introduction to C programming, covering essential topics such as data types, variable naming conventions, arithmetic operations, and storage classes. It explains the five basic data types (int, float, double, char, and _Bool) along with their characteristics and usage. Additionally, it discusses operators, type conversions, and provides examples to illustrate the concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

EC2025E INTRODUCTION TO C PROGRAMMING

Department of Electronics and Communication Engineering


National Institute of Technology Calicut
Winter 202425
Class 3

Code as if the guy who ends up maintaining your code will be a violent psychopath
who knows where you live - Martin Golding.

Data Types, Arithmetic Operations

Variable Naming
1. Begin with a letter or underscore ( _ ) and followed by any combination of letters (upper or
lowercase), underscores, or the digits 09.

2. C language is case sensitive. Sum is dierent from sum or suM or sUm.

3. The following are not allowed:

(a) $ is not a valid character: $um - NO

(b) Embedded spaces are not permitted: s um - NO

(c) Cannot start with a number: 3um - NO

(d) int, oat etc. - reserved names. These mean specic meaning for the compiler. Hence
these cannot be used.

4. A strategy I follow: sumInteger, sumFloat.

5. Pick names that reect the intended use of the variable.

Data Types and Constants


Five basic data types: int, oat, double, char, and _Bool.

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.

3. 23, 2345, -100, 0

4. 2,000: NO.

5. Storage: 32 bit or 64bit, typically depends on your computer.

6. Octal notation: precede number with 0.

1
(a) 050 is octal and in decimal represents 5 × 81 + 0 × 80 = 40 in decimal.

(b) %o or %#o in printf.

7. Hexadecimal (base 16): integer constant preceded by a zero and the letter x.

(a) 10F is represented as either 0x10F or 0X10F.

(b) %x or %#x in printf.

oat
Type oat can be used for storing values containing decimal places.

1. 3.45, 3., .45 are all examples.

2. %f in printf.

3. Scientic notation: 234.5 → 2.345e2 → 2.345 value of Mantissa, 2 value of exponent.

4. %e print is scientic format, %g automates between two formats.

double
1. extended precision type, similar to oat, with more signicant digits.

2. use %f, %e or %g with printf.

char
1. exactly one byte in size.

2. can be used to store a single character.

3. 'a', ';', '0' are all character constant.

_Bool
1. stores just 0 and 1.

2. convention is to have 0 indicate false and 1 indicate true.

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

ˆ Visibility is restricted to the function in which it is declared

ˆ lifetime limited till the time its container function executes

extern
ˆ global variable  visible all across the program

ˆ external variable declared outside of the function

ˆ accessible inside function block

ˆ lifetime = lifetime of 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

Working with Arithmetic Expressions


+, -, *, / are the binary operators.

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 )
{

int a = 100; int b = 2; int c = 25; int d = 4; int result ;

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.

Use the parenthesis


Use this liberally to alter the order of evaluation. (a + b) * c and a + (b * c) result in dierent
results.

// More arithmetic expressions

#i n c l u d e < s t d i o . h>

int main ( void )


{

int a = 25;
int b = 2;
float c = 25.0;
float d = 2.0;

printf ("6 + a / 5 * b = %i \ n " , 6 + a / 5 * b);


printf (" a / b * b = %i \ n " , a / b * b);
printf (" c / d * d = %f \ n " , c / d * d);
printf (" = a = %i \ n " , =a ) ;

return 0;
}

Unary Operator
1. High precedence

2. operates on a single value.

The Modulus Operator


// The modulus operator

#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;

printf ( " a %% b = %i \ n " , a % b);


printf ( " a %% c = %i \ n " , a % c );
printf ( " a %% d = %i \ n " , a % d);
printf (" a / d * d + a %% d = %i \ n " , a / d * d + a % d);

return 0;
}

1. % denes the modulus operator.

2. works only with integer values.

Integer and Floating -- Point Conversions


// Basic conversions in C
#i n c l u d e < s t d i o . h>

int main ( void )


{

float f1 = 123.125 , f2 ;

int i1 , i2 = = 150;
char c = 'a ' ;

i1 = f1 ; // floating to integer conversion


printf ("% f assigned to an int p r o d u c e s %i \ n " , f1 , i1 );

f1 = i2 ; // integer to floating conversion


printf ("% i assigned to a float p r o d u c e s %f \ n " , i2 , f1 ) ;

f1 = i2 / 100; // integer divided by integer and assigned to float = very v


printf ("% i divided by 100 p r o d u c e s %f \ n " , i2 , f1 ) ;

f2 = i2 / 100.0; // integer divided by a float


printf ("% i divided by 100.0 p r o d u c e s %f \ n " , i2 , f2 ) ;

f2 = ( fl oat ) i2 / 100; // type cast operator


printf ( " ( f l o a t ) %i divided by 100 p r o d u c e s %f \ n " , i2 , f2 ) ;

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

The Type Cast Operator


1. Unary operator

2. has a higher precedence than all the arithmetic operators except the unary minus and unary
plus.

3. Another way to typecast

( int ) 29.55 + ( int ) 21.99

will be evaluated as 29 + 21.

4. Another example:

( float ) 6 / ( float ) 4 // my preferred way !

will give oat output.

5. C++ usage =⇒ average = oat (sum)/oat (i);

(a) or average = sum/oat (i);

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.

Precedence in assignment operator


a /= b + c ;

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 ( ) {

int a = 5; int b = 20; int c ;

/* 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" );
}

/* lets change the value of a and b */

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.

/ * C++ Program to demonstrate use of left shift operator */

#i n c l u d e <s t d i o . h>
int main ( ) {

unsigned char a = 5; //(00000101)


unsigned char b = 9; //(00001001);
// u n s i g n e d char a = 5, b = 9;

p r i n t f ( " a<<1 = %d \ n " , a < <1);


// The result is 00001010

p r i n t f ( " b<<1 = %d \ n " , b < <1);


// The result is 00010010

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>

int main ( void )


{
int sumEight ;

sumEight = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 ; // t r i a n g u l a r number

printf ( " The sum of eight numbers from 1 i s %i \ n " , sumEight ) ;

return 0;
}

Problem 2: Find sum of integers from 1 to 200?

Relational Operators

Operator Description Example A=10, B=20

== 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.

Table 1: Relational Operators[1].

The relational operators have lower precedence than all arithmetic operators. That means,

a<b+c

is evaluated as,

a <(b+c )

The for Statement


We will modify the above program using for statement.

#i n c l u d e < s t d i o . h>

int main ( void )

10
{
int n, sumTwoHum = 0 ;

for ( n =0; n <=200; n=n+1)


sumTwoHun = sumTwoHun + n ;

printf ( " The sum of 200 numbers from 1 i s %i \ n " , sumTwoHun ) ;


// output will be 20100

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:

for ( initial_expression , loop_condition , loop_expression )


pr ogr am statement ;

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.

The increment operator


#i n c l u d e < s t d i o . h>

int main ( void )


{
int n, sumTwoHun = 0 ;

11
for ( n =0; n <=200; ++n ) {
sumTwoHun = sumTwoHun + n ;
}

printf ( " The sum of 200 numbers from 1 i s %i \ n " , sumTwoHun ) ;


// output will be 20100

return 0;
}

1. Increment operator is used to increment the current value of the variable by adding integer 1.

2. Note that the value in the memory is updated by this operation.

3. Can be applied only on variables. ++5 wont work.

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.

Consider the following program [2],

#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++;

p r i n t f (" the value of n in p r e =i n c r e m e n t i s %i and value o f m i s :% i \ n " , n, m


p r i n t f (" the value of p in p o s t =i n c r e m e n t i s %i and value of o i s :% i \ n " , p,

return 0;
}

Output of the above program will be thus:

the value of n in p r e =i n c r e m e n t is 5 and value of m i s :5

the value of p in p o s t =i n c r e m e n t is 5 and value of o is :4

12
Questions

1. Write a program to calculate the nth triangular number, where n is to be taken as user input.

2. Write a program to check if integer m is prime, where m 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

You might also like