1 10/14/2024
Language Translator
Language Translator
Translators are system programms, which translate programs written
in any high, or assembly language into 1’s and 0’s or machine language.
Program code Language Machine code
translator
There are three types of language processors or translators
Assembler
Compiler
Interpreter
Language Translator
Assembler
The software that translates assembly code into the computer’s
machine code is called assembler
A program written by a programmer in assembly language is called
source program
After this source code has been converted into object code
Language Translator
Complier
A compiler is a computer software that translates a whole program,
called the source code at once into machine code (object code)
Interpreter
An interpreter in another type of translator used for translating a
language program into machine code
It takes one statement of program and translates it into a machine
instruction which is immediately executed by computer
TRANSLATION PROCESS
Source Program / code
Target Program / code
Programs
• Source Program:
• The program we write in English langue using the editor is called Source
program.
• This program is given to the compiler for translation.
• Target Program:
• The program i.e. produced by the compiler after translating the source
program is called target program.
• This program is in binary form.
Variable ,keyword, constant, data type
Variable:
an entity that may change its value during program execution is
called variable.
Keyword:
are words that meaning is already defined to the language.
Constant:
an entity that cant be changed during program execution.
Data type:
are used to show the type of data .
Data Type
data type is used to
• Identify the type of a variable when the variable is declared
• Identify the type of the return value of a function
• Identify the type of a parameter passed to function
Data Type (continued)
When the compiler encounters a declaration for a variable, it sets up a
memory location for it
An operator used on a variable or variables is legal only if
The operator is defined in that programming language for a variable
of that type
The variable or variables involved with the operator are of the same
or compatible types
Classifications of Data Types
Built-in data types
Fundamental data types (int, char, double,
float, void,bool)
Programmer-defined data types
Structure
Union
Enumeration
Fundamental Data Types
void – used to denote the type with no values
int – used to denote an integer type
char – used to denote a character type
float, double – used to denote a floating point type
int *, float *, char * – used to denote a pointer
type, which is a memory address type
bool- boolean values (true,false)
Constant & Variable
A constant is an entity or value that does not change during the
execution of a program.
an entity that may vary during program execution is called a variable.
Variable names are names given to locations in memory.
These locations can contain integer, real or character constants.
Rules for Constructing Identifiers in C
The first character in the variable name must be an alphabet(A-Z,a-z)
or underscore.
Capital letters A-Z, lowercase letters a-z, digits 0-9, and the
underscore character
First character must be a letter or underscore
There can be no embedded blanks
Keywords cannot be used as identifiers
Identifiers are case sensitive
No commas or blanks are allowed within a variable name.
No special symbol other than an underscore (as in gross_sal)
can be used in a variable name.
Example:
si_int Identifiers refer to the names of data types,
m_hra
pop_e_89 constants, variables, and functions
C constants can be divided into two major categories
Primary Constant
Secondary Constant
These constants are further categorized as shown in below figure
Rules for constructing Integer Constants
An integer constant must have at least one digit
It must not have a decimal point
It can be either positive or negative
If no sign precedes an integer constant, it is assumed to be positive
No commas or blanks are allowed within an integer constant
The allowable range for integer constant is -32768 to 32767
Example: 25, +134, 0, -600
Rules for constructing Character Constants
A character constant is a single alphabet, a single digit or a single
special symbol enclosed within single quotes
The maximum length of a character constant can be 1 character
Example:
‘A’, ‘l’, ‘5’, ‘=’, ‘ ’, ‘.’
Definitions
Initialization
Declaration
Variable
Constant
Terminator
Data type
Assignment operator
Int a =4;
Simple C Program
/* A first C Program*/
#include <stdio.h>
void main()
{
printf("Hello World \n");
}
Preprocessor Directives
The first thing to be checked by the compiler.
Starts with ‘#’.
Tell the compiler about specific options that it needs to
be aware of during compilation.
There are a few compiler directives.
#include <stdio.h>
Tell the compiler to include the file stdio.h during compilation
Anything in the header file is considered a part of the program
Simple C Program
Line 1: #include <stdio.h>
As part of compilation, the C compiler runs a program called
the C preprocessor.
The preprocessor is able to add and remove code from your
source file.
In this case, the directive #include tells the preprocessor to
include code from the file stdio.h.
This file contains declarations for functions that the program
needs to use. A declaration for the printf function is in this file.
Simple C Program
Line 2: void main()
This statement declares the main function.
A C program can contain many functions but must always
have one main function.
A function is a self-contained module of code that can
accomplish some task.
The "void" specifies the return type of main. In this case,
nothing is returned to the operating system.
Simple C Program
Line 3: {
This opening bracket denotes the start of the program.
Simple C Program
Line 4: printf("Hello World From About\n");
Printf is a function from a standard C library that is used to print
strings to the standard output, normally your screen.
The compiler links code from these standard libraries to the code
you have written to produce the final executable.
The "\n" is a special format modifier that tells the printf to put
a line feed at the end of the line.
If there were another printf in this program, its string would
print on the next line.
Simple C Program
Line 5: }
This closing bracket denotes the end of the program.
Structure of a C program
#include <stdio.h> Preprocessor directive (header file)
void main (void)
{
printf(“\nHello World\n”); Program statement
}
#include <stdio.h>
#define VALUE 10 } Preprocessor directive
int global_var;
Global variable declaration
Comments
void main (void)
{
/* This is the beginning of the program */
int local_var; Local variable declaration
local_var = 5;
global_var = local_var + VALUE; Variable definition
printf (“Total sum is: %d\n”, global_var); // Print out the result
}
Comments
Explanations or annotations that are included in a
program for documentation and clarification purpose.
Completely ignored by the compiler during compilation
and have no effect on program execution.
Starts with ‘/*’ and ends with ‘*/’
Some compiler support comments starting with ‘//’
Statements
A specification of an action to be taken by the computer
as the program executes.
In the previous example, there are 2 lines following
variable declaration and variable definition that
terminate with semicolon ‘;’.
global_var = local_var + VALUE;
printf (“Total sum is: %d\n”, global_var);
Each line is a statement.
Basic Functions
A C program consists of one or more functions that contain
a group of statements which perform a specific task.
A C program must at least have one function: the function
main.
We can create our own function or use the functions that has
been created in the library, in which case we have to include
the appropriate header file (example: stdio.h).
In this section, we will learn a few functions that are pre-
defined in the header file stdio.h
These functions are:
printf()
scanf()
In addition to those functions, we will also learn about Format
Specifier and Escape Sequence which are used with printf()
and scanf().
printf()
Used to send data to the standard output (usually the
monitor) to be printed according to specific format.
The general form of printf( ) function is,
printf ( “format string", <list of variables> ) ;
<format string> can contain,
%f for printing real values
%d for printing integer values
%c for printing character values
Control string is a combination of text, format specifier and
escape sequence.
Example:
printf(“Thank you”);
printf (“Total sum is: %d\n”, global_var);
%d is a format specifier
\n is an escape sequence
scanf()
Read data from the standard input device (usually keyboard) and
store it in a variable.
General format:
scanf(“Control string”, &variable);
The general format is pretty much the same as printf() except that
it passes the address of the variable (notice the & sign) instead of
the variable itself to the second function argument.
Example:
int age;
printf(“Enter your age: “);
scanf(“%d”, &age);
Summary
The three primary constants and variable types in C are
integer, float and character.
A variable name can be of maximum 31 characters.
Do not use a keyword as a variable name.
An expression may contain any sequence of constants,
variables and operators.
Input/output in C can be achieved using scanf( ) and
printf( ) functions.