#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
 char opcode[10],operand[10],label[10],a[10],ad[10],symbol[10],ch;
char code[10][10],code1[10][10]={"33","44","53","57"};
 char mnemonic[10][10]={"START","LDA","STA","LDCH","STCH","END"};
 char mnemonic1[10][10]={"LDA","STA","LDCH","STCH"};
 int locctr,start,length,i=0,j=0,k,l=0;
 int st,diff,address,add,len,actual_len,finaddr,prevaddr;
 FILE *fp1,*fp2,*fp3,*fp4,*fp5,*fp6,*fp7;
 clrscr();
 fp1=fopen("INPUT.DAT","r");
 fp2=fopen("SYMTAB.DAT","w");
 fp3=fopen("INETERMED.DAT","w");
 fscanf(fp1,"%s%s%s",label,opcode,operand);
 if(strcmp(opcode,"START")==0)
  {
   start=atoi(operand);
   locctr=start;
   fprintf(fp3,"%s\t%s\t%s\n",label,opcode,operand);
   fscanf(fp1,"%s%s%s",label,opcode,operand);
  }
 else
  locctr=0;
 while(strcmp(opcode,"END")!=0)
  {
   fprintf(fp3,"%d",locctr);
   if(strcmp(label,"**")!=0)
     fprintf(fp2,"%s\t%d\n",label,locctr);
   strcpy(code[i],mnemonic[j]);
   while(strcmp(mnemonic[j],"END")!=0)
    {
     if(strcmp(opcode,mnemonic[j])==0)
     {
      locctr+=3;
      break;
     }
     strcpy(code[i],mnemonic[j]);
     j++;
    }
   if(strcmp(opcode,"WORD")==0)
    locctr+=3;
   else if(strcmp(opcode,"RESW")==0)
    locctr+=(3*(atoi(operand)));
   else if(strcmp(opcode,"RESB")==0)
    locctr+=(atoi(operand));
   else if(strcmp(opcode,"BYTE")==0)
    ++locctr;
   fprintf(fp3,"\t%s\t%s\t%s\n",label,opcode,operand);
   fscanf(fp1,"%s%s%s",label,opcode,operand);
  }
  fprintf(fp3,"%d\t%s\t%s\t%s\n",locctr,label,opcode,operand);
  length=locctr-start;
  fcloseall();
  printf("\n\nThe contents of Input file:\n\n");
  fp1=fopen("INPUT.DAT","r");
  ch=fgetc(fp1);
  while(ch!=EOF)
   {
    printf("%c",ch);
    ch=fgetc(fp1);
   }
  printf("\n\nLength of the input program is %d.",length);
  printf("\n\nThe contents of Symbol Table:\n\n");
  fp2=fopen("SYMTAB.DAT","r");
  ch=fgetc(fp2);
  while(ch!=EOF)
   {
    printf("%c",ch);
    ch=fgetc(fp2);
   }
  fcloseall();
  fp4=fopen("ASSMLIST.DAT","w");
  fp5=fopen("SYMTAB.DAT","r");
  fp6=fopen("INTERMED.DAT","r");
  fp7=fopen("OBJCODE.DAT","w");
  fscanf(fp6,"%s%s%s",label,opcode,operand);
  while(strcmp(opcode,"END")!=0)
  {
   prevaddr=address;
   fscanf(fp6,"%d%s%s%s",&address,label,opcode,operand);
  }
  finaddr=address;
  fclose(fp6);
  fp6=fopen("INTERMED.DAT","r");
  fscanf(fp6,"%s%s%s",label,opcode,operand);
  if(strcmp(opcode,"START")==0)
  {
   fprintf(fp4,"\t%s\t%s\t%s\n",label,opcode,operand);
   fprintf(fp7,"H^%s^00%s^00%d\n",label,operand,finaddr);
   fscanf(fp6,"%d%s%s%s",&address,label,opcode,operand);
   st=address;
   diff=prevaddr-st;
   fprintf(fp7,"T^00%d^%d",address,diff);
  }
  while(strcmp(opcode,"END")!=0)
  {
   if(strcmp(opcode,"BYTE")==0)
   {
    fprintf(fp4,"%d\t%s\t%s\t%s\t",address,label,opcode,operand);
    len=strlen(operand);
    actual_len=len-3;
    fprintf(fp7,"^");
    for(k=2;k<(actual_len+2);k++)
    {
     itoa(operand[k],ad,16);
     fprintf(fp4,"%s",ad);
     fprintf(fp7,"%s",ad);
    }
    fprintf(fp4,"\n");
   }
   else if(strcmp(opcode,"WORD")==0)
   {
    len=strlen(operand);
    itoa(atoi(operand),a,10);
    fprintf(fp4,"%d\t%s\t%s\t%s\t00000%s\n",address,label,opcode,operand,a);
    fprintf(fp7,"^00000%s",a);
   }
   else if((strcmp(opcode,"RESB")==0)||(strcmp(opcode,"RESW")==0))
    fprintf(fp4,"%d\t%s\t%s\t%s\n",address,label,opcode,operand);
   else
   {
    while(strcmp(opcode,mnemonic1[l])!=0)
     l++;
    if(strcmp(operand,"COPY")==0)
     fprintf(fp4,"%d\t%s\t%s\t%s\t%s0000\n",address,label,opcode,operand,code1[l]);
    else
    {
     rewind(fp5);
     fscanf(fp5,"%s%d",symbol,&add);
      while(strcmp(operand,symbol)!=0)
       fscanf(fp5,"%s%d",symbol,&add);
     fprintf(fp4,"%d\t%s\t%s\t%s\t%s%d\n",address,label,opcode,operand,code1[l],add);
     fprintf(fp7,"^%s%d",code1[l],add);
    }
   }
   fscanf(fp6,"%d%s%s%s",&address,label,opcode,operand);
  }
  fprintf(fp4,"%d\t%s\t%s\t%s\n",address,label,opcode,operand);
  fprintf(fp7,"\nE^00%d",st);
  printf("\nObject Program has been generated.");
  fcloseall();
  printf("\n\nObject Program:\n\n");
  fp7=fopen("OBJCODE.DAT","r");
  ch=fgetc(fp7);
  while(ch!=EOF)
  {
   printf("%c",ch);
   ch=fgetc(fp7);
  }
  fcloseall();
  getch();
} 

C Language online compiler

Write, Run & Share C Language code online using OneCompiler's C online compiler for free. It's one of the robust, feature-rich online compilers for C language, running the latest C version which is C18. Getting started with the OneCompiler's C editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'C' and start coding!

Read inputs from stdin

OneCompiler's C online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample C program which takes name as input and print your name with hello.

#include <stdio.h>
int main()
{
    char name[50];
    printf("Enter name:");
    scanf("%s", name);
    printf("Hello %s \n" , name );
    return 0;
    
}

About C

C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system. The initial release of C Language was in the year 1972. Most of the desktop operating systems are written in C Language.

Key features:

  • Structured Programming
  • Popular system programming language
  • UNIX, MySQL and Oracle are completely written in C.
  • Supports variety of platforms
  • Efficient and also handle low-level activities.
  • As fast as assembly language and hence used as system development language.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition if-else is used.

if(conditional-expression) {
   // code
} else {
   // code
}

You can also use if-else for nested Ifs and if-else-if ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to if-else-if ladder.

switch(conditional-expression) {    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
...    
    
default:     
 // code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  // code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition) {  
 // code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {
  // code 
} while (condition); 

Arrays

Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.

Syntax

One dimentional Array:

data-type array-name[size];

Two dimensional array:

data-type array-name[size][size];

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

Two types of functions are present in C

  1. Library Functions:

Library functions are the in-built functions which are declared in header files like printf(),scanf(),puts(),gets() etc.,

  1. User defined functions:

User defined functions are the ones which are written by the programmer based on the requirement.

How to declare a Function

return_type function_name(parameters);

How to call a Function

function_name (parameters)

How to define a Function

return_type function_name(parameters) {  
  //code
}