0% found this document useful (0 votes)
6 views25 pages

C Notes

C notes ..it's useful for engineering students and make great impact in coding and useful for software engineer

Uploaded by

anjugamarulvelan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views25 pages

C Notes

C notes ..it's useful for engineering students and make great impact in coding and useful for software engineer

Uploaded by

anjugamarulvelan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

TYPES OF PROGRAMMING LANGUAGES:

1. Low Level Programming Languages


• Machine language( only 0’s and 1’s)
• Assembly language(related to computer architecture)
2. High Level Programming Languages
• C
• C++
• JAVA
• PYTHON

C can work in low level and can interact with computer hardware.

C is a Structure Oriented programming language.

Short History of C language

In 1972 Dennis Ritchie developed C was a successor of Ken Thompson’s B language.

In 1989, ANSI C Standard

In 1999, C99 C Updated versions

In 2011, C11

In 2018, C18

Compiler:

Compiler transforms code written in a high-level programming language into the


machine code, at once, before program runs.

Interpreter:

Interpreter coverts each high-level program statement, one by one, into the machine
code, during program runs.

Note: Compiled code runs faster while interpreted code runs slower.

EXE.file:

An executable file is a type of computer file that runs a program when it is opened. This
means it executes code or a series of instructions contained in the file.
TOPICS IN C:

❖ C Tokens
❖ Constants and Variable
❖ Data Types
❖ Operators
❖ Control Statements
❖ Looping statements
❖ Functions
❖ Pointers
❖ Arrays
❖ Strings
❖ Structure and Union
❖ File management
❖ Dynamic memory allocation
❖ Graphics

C Tokens:

Tokens are the smallest elements of a program, which are meaningful to the compiler. The
following are the types of tokens: Keywords, Identifiers, Constant, Strings, Operators.
C TOKENS

Keywords Constants Strings Operators

int 15 “hello” +
while -22 “rubi” *
char 0.26 ++
Identifiers Special Symbols
total { }
marks [ ]

Constants and Variable:

Constants are fixed value cannot be modified by the program once it is defined.

Ex:

const int var=5;


keyword datatype name of the constant value

int a=10;

variable

DATATYPES

DATA TYPES

Primitive User defined

int array

char (1 byte) pointer

float(4 byte) structure

double( 8 byte) union

long int( 4 byte)

integer:

int – whole numbers (3,25,75)

Size- 2bytes ( 1 byte=8 bit)

Range → -32768 to +32767

Range calculation:

1-1

2-2

3-4

4-8

5-16

6-32
7-64

8-128

9-256

10-512

11-1024

12-2048

13-4096

14-8192

15-16384

16-32768

Bit→ is the smallest unit of storage. A bit stores just a 0 or 1.

Byte → collection of 8 bits. One byte can one character. Ex. ‘a’,’x’…

char

size - 1 byte

range→ -128 to +127

float

size - 4 byte

range→ -3.4E-38 to 3.4E+38

double

size - 8 byte

range →-1.7E-308 to 1.7E+308

long int

size – 4 byte

range → -2,147,483,648 to +2,147,483,647


Format Specifier in C:
Data type Format specifier
int %d or %i
long int %ld
float %f
double %lf
char %c
String (or) character array %s
Address %u
Octal %o
Hexadecimal %x
Pointer %p
Character set:

Alphabets A,B,….Z

a,b,……z

Digits 0,1,2,3,4,5,6,7,8,9

Special Symbol { } [ ] ( )

+-/ != ?

Simple Program:

Write a program to print your name.

#include< stdio.h>

#include<conio.h>

void main()

clrscr();

printf(“Rubi”);

getch();

Example for float:

#include <stdio.h>

void main()

float a=3.1,b=6.8,c;

c=a+b;

printf("c=%f",c);

}
Output:

9.900000

Operators

 Assignment Operator
 Arithmetic operator
Simple Arithmetic operators
Modulus operator
Increment and decrement operator
 Relational Operator
 Logical operator
 Bitwise Operator
 Ternary operator

1. Assignment Operator

<variable>=<value>
Ex: a=10;

2. Arithmetic operator

Addition +,
Subtraction -,
Multiplication *, 3
Division /, 3 10
Modules %, 9
Ex: a=10,b=3 1
c=a+b; c=a-b; c=a*b; c=a/b c=a%b;

c=13 c=7 c=30 c=3 c=1

Example program:

#include <stdio.h> c=a-(a/b)*b;

void main()

int a=10,b=3,c;

c=a+b;

printf("sum=%d\n",c);
c=a-b;

printf("sub=%d\n",c);

c=a*b;

printf("mul=%d\n",c);

c=a/b;

printf("div=%d\n",c);

c=a%b;

printf("mod=%d",c);

Output:
sum=13

sub=7

mul=30

div=3

mod=1

3. Relational operator

Equal to ==
Not equal to != used in conditions
Greater than > (if ..else stmt)
Less than <
Greater than or equal to >=
Less than or equal to <=

4. Logical operator

Logical AND && → two condition must be true


Logical OR || → either one condition must be true
Logical NOT ! → both conditions false

5. Ternary operator (or) Conditional operator


(expression1)? expression2: expression3

Ex:

if(a>b) true false


x=a; x=(a>b)?a:b;
else
x=b; true
6. Bitwise operator

Operator Description

& Bitwise AND

| Bitwise OR

^ Bitwise exclusive OR

>> Right Shift

<< Left shift

>>> Unsigned Right Shift

Bitwise AND

a=10 b= 2 convert 10 and 2 to binary

c= a&b; 10 →1010 2→0010

1010 AND Gate Truth Table


0010
A B Output
0010
1 1 1
1 0 0
C=2 0 1 0
0 0 0

8421
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7 32 16 8 4 2 1
1000 8 1 0 1 0 10 →42
1001 9
1010 10
1011 11
1100 12
1101 13
1110 14
1111 15
Bitwise OR

a=10 b= 2 convert 10 and 2 to binary

c= a|b; 10 →1010 2→0010

1010 AND Gate Truth Table


0010
A B Output
1010
1 1 1
1 0 1
C=10 0 1 1
0 0 0

Bitwise XOR

a=10 b= 2 convert 10 and 2 to binary

c= a^b; 10 →1010 2→0010

1010 AND Gate Truth Table


0010
A B Output
1000
1 1 0
1 0 1
C=8 0 1 1
0 0 0

Right Shift:
a=10,b=2

c=a>>b; convert 10 to binary number 1010

c=2 b value is 2 so delete 2 numbers from the right side

1010 remaining value is 2.

Left Shift:

a=10,b=2

c=a<<b; convert 10 to binary number 1010

c=40 b value is 2 so add 2 zero’s to the right side

101000 answer is 40. Convert 101000 to decimal number.

Example program:

#include <stdio.h>

void main()

int a=10,b=2,c;

c=a&b;

printf("And=%d\n",c);

c=a|b;

printf("or=%d\n",c);

c=a^b;

printf("xor=%d\n",c);

c=a>>b;

printf("right shift=%d\n",c);

c=a<<b;
printf("left shift=%d",c);

Output:
And=2

or=10

xor=8

right shift=2

left shift=40

7. Increment and decrement operator:


Post increment a++ post decrement a--
Per increment ++a per decrement --a
Ex: a=10
a=a+1(post) a=1+a(per) a=a-1(post) a=1-a(per)

Control Statements:
Decision Control Statements

1. Simple if

if(condition)

…Stmt..;

2. If..else
if(condition)
{
….stmt;
}
else
{
…stmt;
}
3. Nested if..else
if(condition)
{
if(condition)
{
..stmt;
}
else
{
…stmt;
}
}
else
{
…stmt;
}
4. Else..if ladder
if(condition)
{
…stmt;
}
else if(condition)
{
….stmt;
}
else if(condition)
{
…..stmt;
}
.
..

else
{
…stmt;
}

Example program:

#include <stdio.h>

void main()

int a;

printf("Enter a no:");

scanf("%d",&a);

if(a%2==0)

printf("no. is even");

else

printf("no.is odd");
}

Output:
Enter a no:5

no.is odd

Example program to find greatest among three numbers:

Nested if..else

#include <stdio.h>

void main()

int num1, num2, num3;

printf("Enter the values of num1, num2 and num3\n");

scanf("%d %d %d", &num1, &num2, &num3);

printf("num1 = %d\t num2 = %d\t num3 = %d\n", num1, num2, num3);

if (num1 > num2)

if (num1 > num3)

printf("num1 is the greatest \n");

else

printf("num3 is the greatest \n");

}
}

else if (num2 > num3)

printf("num2 is the greatest \n");

else

printf("num3 is the greatest \n");

}
Enter the values of num1, num2 and num3

25

38

29

num1 = 25 num2 = 38 num3 = 29

Num2 is greatest

Example program to find greatest among three numbers:

Else…if Ladder

#include <stdio.h>

void main()

int num1, num2, num3;

printf("Enter the values of num1, num2 and num3\n");

scanf("%d %d %d", &num1, &num2, &num3);

printf("num1 = %d\tnum2 = %d\tnum3 = %d\n", num1, num2, num3);

if((num1>num2)&&(num1>num3))

printf("\n Num1 is greatest");


else if((num2>num1)&&(num2>num3))

printf("\n Num2 is greatest");

else

printf("num3 is the greatest \n");

}
Enter the values of num1, num2 and num3

54

12

37

num1 = 54 num2 = 12 num3 = 37

Num1 is greatest
Looping Statements:
1. While loop

while(condition)
{
…..
….. the blocks run till the condition is true
Block of stmts;
……

2. While infinite loop

while(1)
{
…….
Block of stmts; block run infinite times
……..
}
Note:A loop that repeats indefinitely and never terminates is called an
Infinite loop.

3. Do…..while

do
{
………. This block executes once and continues if the
………. while condition is true. If the while condition
Block of stmts; is false it terminates the loop.
……….
}while(condition);

4. Do….while infinite loop

do
{
…….
Block of stmts;
…..
}while(1);

5. For Loop:

3.b) if false

3.a) if true

1. 2. 5.

for (initialization ; condition ; increment/decerment)


{
//body of the loop
// statements to be executed 4
6. }
//statements outside the loop
6. For loop as while :

for(;condition;)

//block of stmt;

7. For

for(initialization;condition;)
{
//block of stmts;
}
8. For

for(;condition;increment/decrement)
{
//block of stmts;
}
9. Infinite for loop:

for(;;)
{
//block of stmts;
}

Example of while loop:

Sum of digits:

Ex:

Input:

N=125

Output:

Sum of digits:8
Step 1: Review the problem carefully and understand what you are asked to do.
Step 2: Determine what information is given as Input and what result must be produced as
Output.
Step 3: Assign names to each input and output items.
n as input
sum as output
n=125
sum=1+2+5=8
Step 4: Determine the manner of processing that must be done on the input data to come up
with the desired output(i.e., determine what formulas are needed to manipulate the given
data).
1 2 5

1 + 2 + 5

So the logic is we must split the number as digits and then add them.

First we have to split the one’s digit from the given number and then ten’s and so on….

12 / if we do modules by10 with the given input then we


10 125 n will get one’s digit 5 and by division we get the remaining
120 digits of the given input12 .
r 5 % So, we are going to repeat this process again and again for
that we use while loop.

1 / if we do modules by10 with the given input then we


10 12 n will get ten’s digit 2 and by division we get the remaining
10 digits of the given input1 .
r 2 %
0 / if we do modules by10 with the given input then we
10 1 n will get last digit 1 and by division we get the remaining
digits of the given input 0 .
r 1 %

Finally after the loop gets completed the input will become 0.

sum=0;
while(n>0) condition while(n!=0)
{
r=n%10;
n=n/10;
sum=sum+r;
}
Iteration
while(125>0)
{
r=n%10;
5=125%10;
n=n/10;
12=125/10;
sum=sum+r;
5=0+5;
}

while(12>0)
{
r=n%10;
2=12%10;
n=n/10;
1=12/10;
sum=sum+r;
7=5+2;
}

while(1>0)
{
r=n%10;
1=1%10;
n=n/10;
0=1/10;
sum=sum+r;
8=7+1;
}
Now we can write the program for Sum of digits:

#include<stdio.h>
int main()
{
int n,sum=0,r;
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("Sum of digitsis=%d",sum);
return 0;
}

Enter a number:125

Sum of digits is=8

Armstrong number:

Armstrong number is a number that is equal to the sum of cubes of its digits. For
example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers.
Ex:

Input:

N=153

Output:

Given number is an Armstrong number.

Step 1: Review the problem carefully and understand what you are asked to do.
Step 2: Determine what information is given as Input and what result must be produced as
Output.
Step 3: Assign names to each input and output items.
n as input
sum as output
n=153
sum should be the same value of n.(we have compare the sum with the given number)
Step 4: Determine the manner of processing that must be done on the input data to come up
with the desired output(i.e., determine what formulas are needed to manipulate the given
data).
1 5 3

1*1*1 + 5*5*5 + 3*3*3

1 + 125 + 27

153

So the logic is we must split the number as digits and cube them, finally add the cube values.

First we have to split the one’s digit from the given number and then ten’s and so on….

15 / if we do modules by10 with the given input then we


10 153 n will get one’s digit 3 and by division we get the remaining
150 digits of the given input15 .
r 3 % So, we are going to repeat this process again and again for
that we use while loop.

1 / if we do modules by10 with the given input then we


10 15 n will get ten’s digit 5 and by division we get the remaining
10 digits of the given input1 .
r 5 %

0 / if we do modules by10 with the given input then we


10 1 n will get last digit 1 and by division we get the remaining
digits of the given input 0 .
r 1 %

Finally after the loop gets completed the input will become 0.
sum=0;
while(n>0) condition while(n!=0)
{
r=n%10;
n=n/10;
sum=sum+(r*r*r);
}
Iteration
while(153>0)
{
r=n%10;
3=153%10;
n=n/10;
15=153/10;
sum=sum+(r*r*r);
27=0+27;
}

while(15>0)
{
r=n%10;
5=15%10;
n=n/10;
1=15/10;
sum=sum+(r*r*r);
152=27+125;
}

while(1>0)
{
r=n%10;
1=1%10;
n=n/10;
0=1/10;
sum=sum+(r*r*r);
153=152+1;
}
Now we can write the program for Armstrong number:
#include<stdio.h>
int main()
{
int n,sum=0,r,temp;
printf("Enter a number:");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(sum==temp)
printf("Given number is an Armstrong number");
else
printf("not an Armstrong number");
return 0;
}

Enter a number:153

Given number is an Armstrong number

You might also like