1.
Into to Programming in C
C programming Language was introduced in the year of 1972 by Dennis
Ritchie of AT&T Bell Laboratories. Let’s start our first program, the
traditional Hello World!.. Program
/*Program to Print Hello World*/
#include<stdio.h>
void main( )
{
printf(“Hello
World!..”);
}
· Here the things given inside /* */ are called comments which will be
ignored by the compiler during compilation time
· When we write the pre-processor directive
#include<stdio.h> our pre-processor will also include the header file
stdio.h which contains the defenitions for standard input output functions. So
if we include stdio.h in our program, we will be able to use those standard
functions in our program. Similary there are also many standard and
userdefined header files and if we include them in our program we will be able
to use the functions defined int that in our program.
· Every C program starts its execution from main(
) function and we do call other userdefined or standard library functions
inside our main( ) function. We do write all our programs also inside the main(
)
· Every C Statements terminates with a
semicolon ‘ ; ‘
· printf( ) is the standard library function we
use to print something into the console.
Declaring Variable
· Variables are the programming elements which
we refer to store some data in the Main Memory.
· The basic syntax for declaring and
initializing variable is:
<data type> variable_name [= <value>];
Eg: int myVar = 5;
Eg: int myVar = 5;
char character;
character
= ‘A’;
float
x = 11.234;
Basic Data Types
·
int is used to
define integer numbers.
Eg :int x =6;
·
float is used to
define floating point numbers.
Eg: float y = 3.231;
·
double is used to
define BIG floating point numbers. It reserves twice the storage for the
number. On PCs this is likely to be 8 bytes.
Eg: double z = 234.2376546;
·
char defines
characters and they are usually enclosed in a single quotes and they could hold
onle a single character values.
Eg: char newChar = ‘A’;
For storing strings, we
use array of characters and they are being terminated by null character ‘\0’.
Eg: char name = {‘R’, ‘a’, ‘m’, ‘\0’};
Eg:
/*Program to Print Hello World*/
#include<stdio.h>
void main( )
{
int x, y, sum;
printf(“Enter two numbers : ”);
scanf(“%d %d”, &x,
&y);
sum = x + y;
printf(“Sum is %d”,
sum);
}
Basic Operators in C:
i.
Arithmetic Operators
Operator
|
Description
|
Example
|
+
|
Adds two operands
|
A + B will give 30
|
-
|
Subtracts second operand from the first
|
A - B will give -10
|
*
|
Multiply both operands
|
A * B will give 200
|
/
|
Divide numerator by denumerator
|
B / A will give 2
|
%
|
Modulus Operator and remainder of after an integer
division
|
B % A will give 0
|
++
|
Increment operator, increases integer value by one
|
A++ will give 11
|
--
|
Decrement operator, decreases integer value by one
|
A-- will give 9
|
ii.
Logical / Relational Operators:
Operator
|
Description
|
Example
|
==
|
Checks if the value of two operands is equal or not, if
yes then condition becomes true.
|
(A == B) is not true.
|
!=
|
Checks if the value of two operands is equal or not, if
values are not equal then condition becomes true.
|
(A != B) is true.
|
>
|
Checks if the value of left operand is greater than the
value of right operand, if yes then condition becomes true.
|
(A > B) is not true.
|
<
|
Checks if the value of left operand is less than the
value of right operand, if yes then condition becomes true.
|
(A < B) is true.
|
>=
|
Checks if the value of left operand is greater than or
equal to the value of right operand, if yes then condition becomes true.
|
(A >= B) is not true.
|
<=
|
Checks if the value of left operand is less than or
equal to the value of right operand, if yes then condition becomes true.
|
(A <= B) is true.
|
&&
|
Called Logical AND operator. If both the operands are non-zero
then then condition becomes true.
|
(A && B) is true.
|
||
|
Called Logical OR Operator. If any of the two operands
is non-zero then then condition becomes true.
|
(A || B) is true.
|
!
|
Called Logical NOT Operator. Use to reverses the
logical state of its operand. If a condition is true then Logical NOT
operator will make false.
|
!(A && B) is false.
|
iii.
Logical Operators
A logical operator operated between two relational or logical expressions
and returns a logical value.
&& - Logical AND
|| - Logical OR
! - Logical Negation / NOT
iv.
Bitwise Operators
Bitwise operator works on bits and performs bit by bit operation.
Assume if A = 60; and B = 13; Now in binary format they will be as
follows:
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100 – Bitwise ANDing
A|B = 0011 1101 – Bitwise ORing
A^B = 0011 0001 – Bitwise XORing
~A = 1100 0011 – Bitwise Negation operator
<< - Left shift
operator
>> - Right Shift
Operator
NB: Please
make yourself very clear about bitwise operators as we use them much frequently
while programming the microcontrollers.
·
sizeof( ) – operator is used to get the size (in bytes) which a
variable or a data type takes in memory.
Control Structures:
a. Sequential
Control Structure
Eg: Usual programs in which we execute statements one after the
other.
b. Selection
Control Structure
If statements in which a condition is being checked and if it is
true certain set of statements will get executed.
i.
Simple if statement:
if(condition)
{
--Statements--
}
ii.
If –Else Statements
if(condition)
{
--Statements--
}
else
{
--Statements--
}
iii.
If-Else-If Ladder
if(condition1)
{
--Statements--
}
else if(condition2)
{
--Statements--
}
else
{
--Statements--
}
iv.
Switch Statements
switch(expression)
{
case const1:
--Statements—
break;
case const2: --Statements—
break;
case const3: --Statements—
break;
default: --Statements—
break;
}
c. Iteration
/ Looping :
i.
For Loop
for(initialisation;
condition ; updation)
{
--Loop Body--
}
Eg:
for(int i = i; i<=10 ; i++)
{ /*Printing
Multiplication Table of 5 */
printf(“%d x 5 =
%d”, i, i*5);
}
ii.
While Loop – Loop Body will get executed till the condition goes
false
while(condition)
{
--Loop Body --
}
iii.
Do-While Loop : It is an exit controlled loop, ie. The loop body
is executed at least once even if the condition is always being false.
do
{
--Loop Body --
} while (condition);
Break
statement:
If we place a break statement inside
a loop after checking a condition and if it goes right, the control will come
out of the loop.
Eg:
for(int i = i; i<=10 ; i++)
{ /*Printing
Multiplication Table of 5 */
printf(“%d x 5 =
%d”, i, i*5);
if(i == 5)
break;
}
/*Here the loop will print only till 25 and later on i becomes 5
and it breaks the loop*/
Continue
Statement:
If we place a continue statement inside a loop after checking a
condition and if it goes right, the control leave rest of the loop body, update
the loop variable and then continue the loop
for(int i = i; i<=10 ; i++)
{ /*Printing
Multiplication Table of 5 */
printf(“%d x 5 =
%d”, i, i*5);
if(i == 5)
break;
}/*Here the loop will not print 5*5 */
Functions
Functions
are one of the most commonly used features in the C Programming language. It
helps users to separate blocks of codes which could be reused again and again.
A program using functions may look
something like this:
#include<stdio.h>
int myFunction(int , int);
void main( )
{
---------
---------
}
int myFunction(int x, int y)
{
int
z;
z
= (x+y)*x;
return
z;
}
·
Here the line int myFunction(int , int); is called function prototyping or
declaration where we do tells the compiler that a function with a name
myFunction exists and it takes two integer arguments. We need to add this if we
define the function just after the main( )
·
After the main we have
defined the function myFunction and we have given the function body.
·
The general syntax of
defining a function is:
<return-type>
<function-name>(<parameter-list>)
{
--Function
Body--
}
·
Return-type is the
data type of value returned by a function to the called program.
·
If a function does not
return a value, we do give its return type as void.
Example for functions:
int factorial(int x)
{
if(x
== 1 || x ==0)
return
1;
else
return
(x * factorial(x -1) );
}
·
This is an example of
a recursive function which does call itself. This is a powerful programming
technique but it is least preferred in the embedded programming as it takes a
lot of space in the system stack and memory is a main constraint in the
Embedded Systems.
Arrays:
·
Arrays are continuous
storage locations in memory which could be refered under a common name and
could be used to store a collection of data of same data types.
·
The syntax for
declaration of array is:
<data-type>
<array-name>[<number-of elements>];
Eg: int nos[20];
char
name[30];
·
The indexing of array
starts from 0, so if we want to access the first element, we give array[0];
·
There is no basic data
type in C for storing strings, so we use array of characters for storing
string. The end of the string should have a null character .ie ‘\0’.
Structures:
·
Structures are user
defined data types which could be used to group different data-types under a
common name.
Eg:
struct student
{
int
roll_no;
char
name[20];
char
place[20];
int
age;
};
·
We create the elements
of an array as follows:
struct student
s;
·
We use dot( . )
operator to access each element in a structure.
Eg: s.age
= 15;
n
= s.roll_no;
·
We could also create
array of structures : struct
student s[40];
Pointers:
·
Pointers are special
variables which do hold the address of another variable.
Eg:
int num = 25;
int * ptr ; /* Declaring a pointer
variable ptr */
ptr = # /* Assigning the
address of variable num to ptr */
print(“%d”, *ptr); /* Printing the
value at the address in ptr through dereferencing*/
·
Here we have created
an integer num and a pointer variable ptr which could hold an address of a
integer variable.
·
We have assigned the
address of num to ptr using the address of operator( & ).
·
Using the
dereferencing or vaule at operator ( * ) we have printed the value contained at
the in ptr, which is equivalent as the value of num as ptr now has the address
of variable num.
No comments:
Post a Comment