C Programming Basics
C Programming Basics
Its size affects speed, power, and capability. Primary memory and secondary memory are
two types of memories in the computer. Functions of the memory unit are −
• It stores all the data and the instructions required for processing.
• It stores the final results of processing before these results are released to an output
device.
• All inputs and outputs are transmitted through the main memory.
Cache Memory
• Cache memory is a very high speed semiconductor device that can speed up the CPU.
• It is used to hold those parts of data and program which are most frequently used by
the CPU.
• The parts of data and programs are transferred from the disk to cache memory by the
operating system, from where the CPU can access them.
Secondary Memory
• The memory is also known as external memory or non-volatile.
• CPU directly does not access these memory it access via input-output routines.
• The contents of secondary memories are first transferred to the main memory, and
then the CPU can access it. For example, disk, CD-ROM, DVD, etc.
Control Unit
• This unit controls the operations of all parts of the computer but does not carry out
any actual data processing operations.
• It is responsible for controlling the transfer of data and instructions among other
units of a computer.
• It obtains the instructions from the memory, interprets them, and directs the
operation of the computer.
• Arithmetic Section
• Logic Section
Arithmetic Section
• Function of arithmetic section is to perform arithmetic operations like addition,
subtraction, multiplication, and division.
• All complex operations are done by making repetitive use of the above operations.
Logic Section
Function of logic section is to perform logic operations such as comparing, selecting,
matching, and merging of data.
Operating System
• It is an interface between the user and computer system.
• It keeps the track of who is using the resource, granting resource requests and
mediating conflicting requests from different programs and users.
Compiler
• A compiler is computer software that translates computer code written in one
programming language into another programming language.
• Compilers are a type of translator that supports digital devices, primarily computers.
• Compilers are primarily used for programs that translate source code from a high-level
programming language to a lower level language to create an executable program
Number System
Decimal Number System
• The number system that we use in our day-to-day life is the decimal number system.
Decimal number system has base 10 as it uses 10 digits from 0 to 9.
• In decimal number system, the successive positions to the left of the decimal point
represent units, tens, hundreds, thousands, and so on.
• Each position represents a specific power of the base (10). For example, the decimal
number 1234 consists of the digit 4 in the units position, 3 in the tens position, 2 in the
hundreds position, and 1 in the thousands position. Its value can be written as
= 1000 + 200 + 30 + 4
= 1234
• Each position in a binary number represents a 0 power of the base (2). Example 20
• Each position in an octal number represents a 0 power of the base (8). Example 80
• Last position in an octal number represents a x power of the base (8). Example
8x where x represents the last position – 1
= 530510
• Letters represent the numbers starting from 10 means A = 10. B = 11, C = 12, D =
13, E = 14, F = 15
= 10646310
#define MAX 25
Global variable declaration part
Void main()
{
Local variables
Executable statements
- --
}
User defines functions
{
Statement
…
}
Properties of a Language
• To be understandable
• Need a structure
• To be meaningful
Basics of a computer
Compilation: In unix, the process can be done using gcc filename.c. After
using the command, the source code can be converted into machine code.
Applications
Variables
We can store information in a variable. Named memory location is called a
variable. A variable occupies some memory space.
Eg: Seating arrangement in an examination hall.
Seating arrangement in a cinema theatre.
Identifier: Every variable has some name. It is called identifier.
Declaration of a variable and its Syntax: datatype identifier
float 4 bytes
Tokens
Keywords Constants variables Operators Special Characters
Int 3.125 name + #
Constants: Constants are those that are not changed their values during the
execution of the program. They are:
III +, - L -> R
#include<stdio.h>
void main()
{
int a = 6, b = 8, c = 2, d, x, y, z;
d = -a + b * c;
x = (a + b) * c;
y = a * b / c;
z = a * (b / c);
Relational Operators
Example Program 1
#include<stdio.h>
void main()
{
int i=10, j, k;
j = i++;
printf(“%d\t%d\n”,i, j);
k = ++i;
printf(“%d\t%d\n”,k,i);
}
Example Program2
void main()
{
int i =10, j, k;
j = i--;
printf(“%d\t%d\n”, i, j);
k = --i;
printf(“%d\t%d\n”, i, k);
}
Logical AND and Logical OR
Logical && or Logical || can give whether the statements are true or false.
They don’t support mathematical values.
Similarly, the result of the logical || will be true if at least one of the operands
is true. Moreover, while evaluating the logical OR operation if the first operand
is identified to be true then the second operand will not be evaluated.
A B A && B A || B
F F F F
F T F T
T F F T
T T T T
Bitwise Operators
C has a distinction of supporting special operators known as bitwise operators
for manipulation of data at bit level. The ability provided by the bitwise
operators to deal with data items at bit level will in turn help us to perform
low level functions. These operators will work on only integers.
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise Exclusive OR
~ One’s Complement
<< Shift Left
>> Shift Right
Bitwise AND
Dr.V.Sreenivasulu KMIT Page 18
Syntax: Operand1 & Operand2
Where operand1 and operand2 are integer expressions. The value of the
expression would also be integer. If the corresponding bits of operand1 and
operand2 are both 1’s then the bits in the corresponding position of the result
would be 1, otherwise it would be be 0.
P Q P|Q
0 0 0
0 1 0
1 0 0
1 1 1
Bitwise OR
Syntax Operand1 | Operand2
where operand1 and operand2 are integer expressions. The value of the
expression will also be an integer. If the corresponding bits of operand1 and
operand2 are both 0s, then the bit in the corresponding position of the result
would be 0. Otherwise, it will be 1.
P Q P|Q
0 0 0
0 1 1
1 0 1
1 1 1
Bitwise Exclusive OR
Syntax Operand1 ^ Operand2
where operand1 and operand2 are integer expressions. The value of the
expression will also be an integer. If any one of the bits of operand1 and
operand2 is 1 then the bit in the corresponding position of the result will be 1,
otherwise it will be 0.
P Q P|Q
0 0 0
0 1 1
1 0 1
1 1 0
// Bitwise AND & Operator
#include<stdio.h>
void main()
{
int x = 5;
int y = 4;
int z;
z = x ^ y;
printf("The value of z is \t%d\n",z);
}
The bitwise shift left operator is used to shift the given number of bits of an
integer towards left. The vacant positions on the right of the integer will be
filled with zeros. The syntax is as follows.
Example:
int x = 5;
The bitwise shift right operator is used to shift the given number of bits of an
integer towards right. The vacant positions on the left of the integer will be
filled with zeros. The syntax is as follows.
Example:
int x = 5;
The bitwise one’s complement operator is used to invert the bits of an integer.
That is, it replaces all ones by zeroes and all zeroes by ones of an integer.
int x = 5;
Conditional Operators
In the above format, first expression is evaluated if it is true, then the value of
expression-2 is assigned to expression otherwise, the value of expression-3 is
assigned to expression.
Example: int x = 5, y = 10, large;
Large = (x > y) ? x : y;
Program
#include<stdio.h>
void main( )
{
int x, y, large;
printf(“Enter x value and y value..”);
scanf(“%d%d”,&x,&y);
large = (x > y) ? x: y;
printf(“The large value is %d”, large);
}
The if statement is used to control the flow of statements. The general form of
if statement is
If ( Conditional - expression)
}
Statement;
#include<stdio.h>
void main( )
{
int number;
printf(“ Enter a number …”);
scanf(“%d”,&number);
if (number > 0)
{
Printf(“The given one is a positive number\n”);
}
}
Two Way Selection
It is very clear that the simple if statement will not sufficient to implement
selection which involves complex conditions. We can use variation of if
structures depending on the degree of the complexity of the conditions to be
checked.
if( conditional - expression)
{
Statement block1;
}
else
{
statement- block2;
}
statements;
.
.
.
if ..else
Here, on if..else is enclosed within another if..else. the ei..else which enclosed
another is called outer if. The if..else which is enclosed within another is called
inner if..else. statement – 1, statement – 2 and statement – 3 are three block of
statements for execution proceeds.
else if ladder
There is another way of putting ifs together when multipath decisions are
involved. A multipath decision is a chain of ifs in which the statement
associated with each else is an if.
if (conditional – expression)
statement1
else if ( conditional – expression 2)
statement - 2
else if (conditional – expression 3)
statement - 3
else if (conditional – expression 4)
statement - 4
else default-statement;
statement – x;
Example: Read marks of a student and determine whether he got first class,
second class, third class or failed.
void main()
{
int marks;
printf(“Enter marks of a students..”);
void main( )
{
int x, y, z, ch;
switch ( ch )
{
case 1: z = x + y;
break;
case 2: z = x - y;
break;
case 1: z = x * y;
break;
case 1: z = x / y;
break;
i. while loop
ii. for loop
iii. do..while
While loop
i. initialization
ii. condition
iii. increment/decrement
Example1: A program that prints numbers from 1 to n;
#include<stdio.h>
void main( )
{
int num,i;
printf (“Read n from the key board”);
scanf(“%d”,&num);
i=1;
printf(“Printing numbers from 1 ot %d”, num);
while(i < num)
{
printf(“%d”, i);
i++;
}
For loop
The for loop is most common in major programming languages. However, the
for loop in C language is very flexible and very powerful. Generally, the for loop
is used to repeat the execution of a statement for fixed number of times.
===