Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

1.2. What is C?

A tool called a compiler is then used to convert the high-level code into machine language. A program can be written in C and compiled for any computer, it's up to the compiler to get the hardware-specific instructions right.

To see just how readable C is compared to Assembly language, take a look at the following tiny program written in each:

Example 1-1. C vs. Assembly language

          .section  .rodata
.LC0:
          .string   "Tax Due: %d\n"
          .text
          .align 2
.globl main
          .type     main,@function
main:
          pushl     %ebp
          movl      %esp, %ebp
          subl      $24, %esp
          andl      $-16, %esp
          movl      $0, %eax
          subl      %eax, %esp
          movl      $1000, %eax
          movl      $400, %edx
          movl      $0x3e6147ae, -12(%ebp)
          subl      %edx, %eax
          pushl     %eax
          fildl     (%esp)
          leal      4(%esp), %esp
          fmuls     -12(%ebp)
          fnstcw    -18(%ebp)
          movw      -18(%ebp), %ax
          movb      $12, %ah
          movw      %ax, -20(%ebp)
          fldcw     -20(%ebp)
          fistpl    -16(%ebp)
          fldcw     -18(%ebp)
          subl      $8, %esp
          pushl     -16(%ebp)
          pushl     $.LC0
          call      printf
          addl      $16, %esp
          movl      $1, %eax
          leave
          ret
.Lfe1:
          .size     main,.Lfe1-main
     
And the program in C:

#include <stdio.h>

int
main()
{
  int wages = 1000;
  int tax_allowance = 400;
  float tax_rate = 0.22;
  int tax_due;

  tax_due = (wages - tax_allowance) * tax_rate;

  printf("Tax Due: %d euro\n", tax_due);

  return 0;
}
     

Which did you find easier to understand, even without knowing C. The output of both programs is the same: "Tax Due: 131 euro". The Assembly code shown is written in the "80386" instruction set, it will not work on machines that use a different instruction set. The C code can be compiled for practically any computer.

 
 
  Published under the terms of the GNU General Public License Design by Interspire