Showing posts with label Programs. Show all posts
Showing posts with label Programs. Show all posts

Tuesday, 3 May 2016

Program to check whether data in your system is stored in little endian format or big endian format

0

Think of computer memory as cells which are 1 byte long. In order to store 2 byte hex-data (90AB), there are two possibilities: 

1. Big endianIn this, you store the most significant byte in the smallest address. Examples include Motorola 68000 series, Xilinx MicroblazeSuperH, IBM z/Architecture or Atmel AVR32
                                                    Address value      Data
                                                       1001                      90
                                                       1002                      AB

2. Little endianIn this, you store the least significant byte in the smallest address.
Example is Intel x86 and x86-64 series of processors, that's why it is also known as Intel Convention.
                                                   Address value      Data
                                                       1001                      AB
                                                       1002                      90


Here's the code to test your machine's endianness:
#include<stdio.h>
#include<stdlib.h>
#define CPU_VENDOR_OS "intel-gnu-linux"
int main()
{          union
            {
                   short s;
                   char c[sizeof(short)];
            }un1;
            un1.s=0x0102;
            printf("%s\n",CPU_VENDOR_OS);
            if(sizeof(short)==2)
           {
                  if(un.c[0]==1 && un.c[1]==2)
                  {
                         printf("Big endian\n");
                   }
                   else if(un.c[0]==2 && un.c[1]==1)
                   {
                        printf("Little endian\n");
                   }
                  else
                  {
                       printf("Unknown\n");
                  }
          }
         return 0;
}

Output: Intel processor, so output is little endian.



Wednesday, 9 March 2016

Identify whether identifier is valid or not

1


An identifier is used for any variable, function, data definition, etc. In the programming language C, an identifier is a combination of alphanumeric characters, the first being a letter of the alphabet or an underline, and the remaining being any letter of the alphabet, any numeric digit, or the underline.

This program is to identify whether the entered input is a valid identifier or not. Here goes the code:

#include<stdio.h> 
#include<conio.h> 
#include<ctype.h> // to use isdigit() and isalpha() functions
void main() {    
char a[10];  
int flag, i=1; 
for(int j=0; j<5; j++) {
    printf("\n Enter an identifier:");  
    gets(a);    
    if(isalpha(a[0]) || a[0]=='_')      
        flag=1;  
    else      
        printf("\n Not a valid identifier");    
    while(a[i]!='\0')    {   
        if(!isdigit(a[i])&&!isalpha(a[i])&&a[i]!='_') {         
            flag=0;         
            break;       
        }     
        i++;  
    }  
    if(flag==1)     
    printf("\n Valid identifier");    
    }
 


Output:



Saturday, 30 January 2016

Remove comments from file

0

Remove comments from a file. Store the new file without comments. Here I am assuming the file-name which has comments that is the source file is present in c drive with name src.cpp. While the file with the resultant content is named as res.cpp and will be created in c drive. 
This question needs basic knowledge of file handling.
C language has two types of comments.

 

1. Single line comment: 

This type of  comment starts with double slash (//) and it ends with enter character.

// This is a comment

2. Multi-line comment: 

This type of comment is used to multiple lines. It uses pair of slash and star to start and end a block of comment. 

/* This
is multi-line
comment */

Here goes the code:


#include <stdio.h>
#define INM 1       /* Reading multi-line comment */
#define INS 2       /* Reading single-line comment */
#define OUT 0       /* Not in comment */

int main() {
   int mode, i, l, j, k;
   FILE *src, *res; // File pointers

   /* File handling stuff: fopen is used to open a file */
   src = fopen("C:\\src.cpp", "rt");
   /* rt defines that opened file in reading mode */
   res = fopen("C:\\res.cpp", "wt");
   /* wt defines that opened file is in writing mode */
   
   mode = OUT;
   for (i = 1;;i++){
     
      if ((l = getc(src)) == EOF) {
         printf("File ended");
         break;
      }
      /* In comment section */     
      if (mode == OUT && l == '/') {
         if ((j = getc(src)) == EOF) {
            printf("File ended");
         }
         else if (j == '*') {
            mode = INM;
            /* entering into multi-line comment */
         }
         else if(j == '/'){
             mode = INS;
             /* entering into single-line comment */
         }
      }
      /* code to be written into new file */
      else if (mode == OUT) {
         putc(l, res);
      }
 
      /* Leaving comment */
      if (mode == INM && l == '*') {
         if ((j = getc(src)) == '/') {
            mode = OUT;
            /*end of multi-line comment*/
         }
      }
      if( mode == INS && l == '\n'){
          mode = OUT;
          /*end of single line comment*/
      }
   }
   
   /*close the file pointers*/
   fclose(src);
   fclose(res);
   return 0;
}


Results:

Contents of src.cpp

Don't remove this
// Single line comment : Remove this
/* Multi-line comment:
Remove this*/
Don't remove this!!


Contents of res.cpp

Don't remove this

Don't remove this!!


Thanks for reading!!