Friday 27 December 2013

swap two variable in C

Q: Swap two variables in C?

1. Using Temporary Variable

#include <stdio.h>

int main(int argc, char *argv[]){
    int temp,num1,num2;
    printf("Enter Two Number: ");
    scanf(" %d %d", &num1, &num2);
    printf("Before Swap num1=%d and num2=%d\n", num1, num2);
    temp = num1;
    num1 = num2;
    num2 = temp;
    printf("After Swap num1=%d and num2=%d\n", num1, num2);
    return 0;
}
2. Not Using Temporary Variable

#include <stdio.h>

int main(int argc, char *argv[]){
    int num1,num2;
    printf("Enter Two Number: ");
    scanf(" %d %d", &num1, &num2);
    printf("Before Swap num1=%d and num2=%d\n", num1, num2);
    num1+=num2;
    num2 =num1-num2;
    num1 = num1-num2;
    printf("After Swap num1=%d and num2=%d\n", num1, num2);
    return 0;
}
/***********************************
* NOTE MEMORY OVER FLOW CAN HAPPEN *
***********************************/
3. Not Using Temporary Variable/Using Bitwise operator

#include <stdio.h>

int main(int argc, char *argv[]){
    int num1,num2;
    printf("Enter Two Number: ");
    scanf(" %d %d", &num1, &num2);
    printf("Before Swap num1=%d and num2=%d\n", num1, num2);
    num1^=num2;
    num2^=num1;
    num1^=num2;
    printf("After Swap num1=%d and num2=%d\n", num1, num2);
    return 0;
}
4. Using Bitwise operator and input From Command Line

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
    int num1; 
    int num2;
    if(argc != 3){
        printf("\nUSAGE:: %s <num1> <num2>\n",argv[0]);
        exit(-1);
    }
    num1 = atoi(argv[1]);
    num2 = atoi(argv[2]);
    printf("Before Swap num1=%d and num2=%d\n", num1, num2);
    num1^=num2;
    num2^=num1;
    num1^=num2;
    printf("After Swap num1=%d and num2=%d\n", num1, num2);
    return 0;
}

finding a number is even or odd using c

Q: Write a C program to Find whether a Number is Odd or Even?

Finding Odd or Even Using % operator.

#include<stdio.h>

int main(int argc, char * argv[]){
        int num=0;
        printf("\nInput a Number for Test:");
        scanf(" %d",&num);
        if(num%2 == 0){
                printf("%d IS EVEN\n",num);
        }
        else{
                printf("%d IS ODD\n", num);
        }
        return 0;
}
Compiling and Running Under Linux

rajesh@ubuntu:~/rajesh/c$ gcc iseven.c -o isev
rajesh@ubuntu:~/rajesh/c$ ./isev

Input a Number for Test:20
20 IS EVEN
rajesh@ubuntu:~/rajesh/c$ ./isev 

Input a Number for Test:11
11 IS ODD
Finding Odd or Even Using bit-wise operator.

#include<stdio.h>

int main(int argc, char * argv[]){
        int num=0;
        printf("\nInput a Number for Test:");
        scanf(" %d",&num);

        if(!(num&0x01)){
                printf("%d IS EVEN\n",num);
        }
        else{
                printf("%d IS ODD\n", num);
        }
        return 0;
}
Compiling and Running Under Linux

rajesh@ubuntu:~/rajesh/c$ gcc iseven1.c -o isev1
rajesh@ubuntu:~/rajesh/c$ ./isev1

Input a Number for Test:20
20 IS EVEN
rajesh@ubuntu:~/rajesh/c$ ./isev1 

Input a Number for Test:7
7 IS ODD
Finding Odd or Even Using bit-wise operator.Getting Input from Command Line.

#include<stdio.h>

int main(int argc, char * argv[]){
        int num=atoi(argv[1]);

        if(!(num&0x01)){
                printf("%d IS EVEN\n",num);
        }
        else{
                printf("%d IS ODD\n", num);
        }
        return 0;
}
Compiling and Running Under Linux

rajesh@ubuntu:~/rajesh/c$ gcc iseven2.c -o isev2
rajesh@ubuntu:~/rajesh/c$ ./isev2 12
12 IS EVEN
rajesh@ubuntu:~/rajesh/c$ ./isev2 13
13 IS ODD

Monday 23 December 2013

Hello World For ARM

Hello World For ARM LPC2148


#include&ltLPC214x.h&gt
int main(){
    printf("Hello World!!!\n");
    return 0;
}

LPC2148

A Separate Page For LPC2148 Micro-Controller.

Executing shell commands using C


We Can Use system() function to execute a command

        #include <stdlib.h>
        int system(const char *command);

Small Example Code Snipet

int main(int argc, char *argv[]){
        const char *command = "ls";
        system(command);
        return 0;
}
system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed.
During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.

A Complete Program.

QUESTION::
Write a C program to take set of commands separated by semicolons(;).
Execute each commands sequentially and wait till excution of commands over.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void executeCommand(const char *command)
{
        printf("\n=====================\n");
        system(command);
        printf("=====================\n");
}

int main(int argc, char *argv[]){
        char commands[1024];
        char *tempPos, *itrCommand;
        char command[256];
        int i;
        if(argc > 1)
        {
                memset(commands,0x00,sizeof(commands));
                for(i=1;i<argc;i++)
                {
                        strcat(commands,argv[i]);
                }
        }else{
                memset(commands,0x00,sizeof(commands));
                printf("Enter Commands Separated By Semicolon.\n");
                scanf(" %[^\n]s",commands);
        }
        printf("commands : %s\n",commands);
        itrCommand = commands;
        while(tempPos = strchr(itrCommand,';')){
                (*tempPos)=0x00;
                memset(command,0x00,sizeof(command));
                strcpy(command,itrCommand);
                executeCommand(command);
                itrCommand = tempPos+1;
                tempPos = NULL;
        }
        if((*itrCommand)!=0x00)
        {
                memset(command,0x00,sizeof(command));
                strcpy(command,itrCommand);
                executeCommand(command);
        }
        return 0;
}

Compilation and Excution In Linux System

rajesh@ubuntu:~/rajesh/Linux$ gcc system.c -o exe_using_C
rajesh@ubuntu:~/rajesh/Linux$ ./exe_using_C
Enter Commands Separated By Semicolon.
echo Rajesh;ls
commands : echo Rajesh;ls

=====================
Rajesh
=====================

=====================
a.out  executing.txt  exe_using_C  system.c
=====================
rajesh@ubuntu:~/rajesh/Linux$

Wednesday 4 December 2013

Assembly Code To Blink LEDs




The Delay Generation ASM Routine For Generating Delay


;file: delays.asm
;ALL DELAYS ROUTINES HERE
DELAY_SEG SEGMENT CODE
RSEG DELAY_SEG
;DELAY OF 1MS SUBROUTINE
DELAY1MS:
MOV R7,#250
DJNZ R7,$
MOV R7,#247
DJNZ R7,$
RET
;DELAY OF 100MS SUBROUTINE
DELAY100MS:
MOV R6,#99;1MUS
L1:
ACALL DELAY1MS ;99MS
DJNZ R6,L1;198MUS
MOV R6,#250;1MUS
DJNZ R6,$;500US
MOV R6,#147;1US
DJNZ R6,$;294US
NOP
RET;1US
;DELAY 0F 1SEC SUB ROUTINE
DELAY1S:
MOV R5,#9
L2:
ACALL DELAY100MS
DJNZ R5,L2
MOV R5,#99
L3:
ACALL DELAY1MS
DJNZ R5,L3
MOV R5,#250
DJNZ R5,$
MOV R5,#138
DJNZ R5,$
RET


The Main ASM Routine For Operation


;file:main.asm
;Glow Alternate Leds Connected to A Port 0
$INCLUDE(delays.asm)
cseg at 0
MOV A,#0ffH
MOV A,#055H
MAIN:
MOV P2,A
LCALL DELAY1S
LCALL DELAY1S
XRL A,#0FFH
SJMP MAIN
END

Download Keil Project And Proteous Project For This

Thursday 3 October 2013

Interface One Led with 8051

Interfacing one Led to 8051 Micro-controller


Description About Interfacing Led to 8051

Led is a simple 1 bit out put device. So we need only one bit(or pin) to operate it.
Led can be configured as active low(i.e Glow when pin is Low ) or It can be configured as
active high(i.e Glow when pin is High).
In above figure it is configured as active low. So For Glowing of led the bit must be 0
and 1 for off the led.

Code for The Above configuration


  #include "./include/reg51.h"  /*contain 8051 specific definition*/
  #include "./include/delays_header.h" /* contain delay generation declaration*/
  /*AS LED IS CONFIGURED AS A NEGATIVE LOGIC DEVICE */
  #define ON 0
  #define OFF 1
  sbit LED1=P0^0;

  void main()
  {
   while(1)/*super loop for embedded application*/
   {
    LED1=ON;
    /*provide some delay so that led in ON state for some time*/
    delay_500_ms();
    LED1=OFF;
    /*provide some delay so that led in OFF state for some time*/
    delay_500_ms();
   }
  }

  

Links For Download Complete keil's Project

The above Link contain single Led as well as 8 leds(full port). Also contain Proteus Simulator Example.

Wednesday 4 September 2013

PRINT BINARY DATA IN C

PRINT BINARY DATA IN C
==============================================


/*** This Program Can Print Binary Data as hex string in to
 * stdout or any stream by simple modification.
 * Date: 04/09/2013
 * Author: Rajesh kumar Sahoo.
 * File Name: binprinter.c
 */

#include <stdio.h>
/**
 * This is the main function designed to print binary data.
 */
int print_bin_data(const void *bin_buf,const int len)
{
        int i;
        unsigned char uch;
        const char *buf;
        buf = (const char *) bin_buf;
        for(i=0;i<len;i++){
                uch = buf[i];
                fprintf(stdout,"%02X",uch); // change stdout to required stream
        }
        printf("\n");
}

int main(int argc, char *argv[])
{
        short int ssL_num_one=-1;
        int siL_num=100;
        long int slL_num_two=2000;
        float fL_float_one=10.056;
        printf("short %d in memory as: ",ssL_num_one);
        print_bin_data(&ssL_num_one, sizeof(ssL_num_one));
        printf("integer %d in memory as: ",siL_num);
        print_bin_data(&siL_num, sizeof(siL_num));
        printf("long %ld in memory as: ",slL_num_two);
        print_bin_data(&slL_num_two, sizeof(slL_num_two));
        printf("float %f in memory as",fL_float_one);
        print_bin_data(&fL_float_one, sizeof(fL_float_one));
        return 0;
}

:::::::compile and executing::::::::::

$ gcc binprinter.c -o bin_print
$./bin_print

:::::::output::::

short 10 in memory as: FFFF
integer 100 in memory as: 64000000
long 2000 in memory as: FFFFFFD0070000
float 10.056000 in memory as60FFFFFFE52041

Wednesday 1 May 2013

A Template Class In C


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct templateStructure
{
        signed int mcsi_row;
        signed int mcsi_col;
        signed int mcsi_type;
        char *mcpc_ptr;
        //void (*init)(templateStructure *,const char*,const int,const int);
        void (*mcfn_get)(struct templateStructure *,const int ,const int,void *);
        void (*mcfn_set)(struct templateStructure *,const int,const int,const void *);
};
typedef struct templateStructure template_t;
typedef template_t* template_p;

void fnG_set(template_p StL_obj,const int iL_row,const int iL_col,const void *val)
{
        char *pvL_tmpPtr;
        pvL_tmpPtr =StL_obj->mcpc_ptr + (((StL_obj->mcsi_col*StL_obj->mcsi_type)*iL_row)+(iL_col*StL_obj->mcsi_type));
        memcpy(pvL_tmpPtr,val,StL_obj->mcsi_type);
}

void fnG_get(template_p StL_obj,const int iL_row,const int iL_col,void *val)
{
        char *pvL_tmpPtr;
        pvL_tmpPtr =StL_obj->mcpc_ptr + ((((StL_obj->mcsi_col)*(StL_obj->mcsi_type))*(iL_row))+((iL_col)*(StL_obj->mcsi_type)));
        memcpy(val,pvL_tmpPtr,StL_obj->mcsi_type);
}

int fnG_initTemplate(template_p StL_obj,const char *pcL_type,const int iL_row,const int iL_col)
{
        StL_obj->mcsi_row=iL_row;
        StL_obj->mcsi_col=iL_col;
        if(strcmp(pcL_type,"int")==0)
        {
                StL_obj->mcsi_type=sizeof(int);
                StL_obj->mcpc_ptr=(char *)malloc((sizeof(int)*iL_row)*iL_col);
        }
        else if(strcmp(pcL_type,"char")==0)
        {
                StL_obj->mcsi_type=sizeof(char);
                StL_obj->mcpc_ptr=(char *)malloc((sizeof(char)*iL_row)*iL_col);
        }
        else if(strcmp(pcL_type,"float")==0)
        {
                StL_obj->mcsi_type=sizeof(float);
                StL_obj->mcpc_ptr=(char *)malloc((sizeof(float)*iL_row)*iL_col);
        }
        StL_obj->mcfn_get=fnG_get;
        StL_obj->mcfn_set=fnG_set;
}

int main(int argc,char* argv[],char* envp[])
{
        char val='a';
        char val1='z';
//      int val=1000;
//      int val1=999;
        template_t myObj;
        printf("val=%c and val1=%c\n",val,val1);
        //printf("val=%d and val1=%d\n",val,val1);
        fnG_initTemplate(&myObj,"char",4,3);//use to define different type at run time 
        //fnG_initTemplate(&myObj,"int",4,3);//use to define different type at run time 
        myObj.mcfn_set(&myObj,0,3,&val1);
        printf("val=%c and val1=%c \n",val,val1);
        myObj.mcfn_get(&myObj,0,3,&val);
        printf("val=%c and val1=%c \n",val,val1);
        //printf("val=%d and val1=%d\n",val,val1);
        return 0;
}