0% found this document useful (0 votes)
4 views56 pages

Isp Lesson 3 Share

The document discusses the syntax and semantics of programming languages, emphasizing their importance in writing correct and efficient code. Syntax refers to the rules governing the structure of code, while semantics pertains to the meaning and behavior of the code during execution. Additionally, it covers structured programming concepts, including advantages and disadvantages, and outlines the basic structure of a C program, including preprocessor commands, functions, variable declarations, statements, and comments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views56 pages

Isp Lesson 3 Share

The document discusses the syntax and semantics of programming languages, emphasizing their importance in writing correct and efficient code. Syntax refers to the rules governing the structure of code, while semantics pertains to the meaning and behavior of the code during execution. Additionally, it covers structured programming concepts, including advantages and disadvantages, and outlines the basic structure of a C program, including preprocessor commands, functions, variable declarations, statements, and comments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

SYNTAX AND SEMANTICS

NKANNAN
SYNTAX AND SEMANTICS
• Programming Language Syntax and Semantics
• Programming languages are the foundation of software
development, enabling developers to write instructions that
computers can execute. Two fundamental aspects of any
programming language are its syntax and semantics.
Understanding these concepts is crucial for writing correct and
efficient code.
SYNTAX AND SEMANTICS
• SYNTAX:
• Syntax refers to the set of rules that define the structure of a
programming language. It dictates how programs are written,
including the arrangement of symbols, keywords, and
punctuation. Syntax is like the grammar of a programming
language—it ensures that code is written in a way that the
compiler or interpreter can understand.
SYNTAX AND SEMANTICS
• Key Characteristics of Syntax:
• Rules for writing code: Syntax defines how statements,
expressions, and declarations are formed.
• Symbols and punctuation: Syntax includes the use of
braces {}, parentheses (), semicolons ;, and other
symbols.
• Keywords: Reserved words like if, else, for, and while
are part of the syntax.
SYNTAX AND SEMANTICS
• Example of Syntax:

• Consider the following Python code:


• if x > 10:
• print("x is greater than 10")
SYNTAX AND SEMANTICS
• The if statement follows Python's syntax rules:
• The condition x > 10 is enclosed in parentheses
(optional in Python but required in some languages like
C).
• A colon : is used to indicate the start of the block.
• The indentation defines the scope of the if block.
SYNTAX AND SEMANTICS
• If the syntax is violated, the code will not compile or run.
For example:
• if x > 10
• print("x is greater than 10")
• This code will result in a syntax error because the colon :
is missing after the condition.
SYNTAX AND SEMANTICS
• SEMANTICS:
• Semantics refers to the meaning of the code—what the
program does when it is executed. While syntax ensures
the code is written correctly, semantics ensures the code
behaves as intended.
SYNTAX AND SEMANTICS
• Key Characteristics of Semantics:
• Behavior of the program: Semantics defines how the
program executes and what results it produces.
• Logic and algorithms: Semantics is concerned with the
correctness of the logic and the flow of the program.
• Runtime behavior: Semantics includes how variables,
functions, and data structures interact during
execution.
SYNTAX AND SEMANTICS
• Example of Semantics:
• Consider the following Python code:
•x = 5
• y = 10
•z = x + y
• print(z)
SYNTAX AND SEMANTICS
• The syntax is correct: variables are assigned values, and
the + operator is used to add them.
• The semantics of this code is to add the values of x and y
and print the result (15).
SYNTAX AND SEMANTICS
• If the semantics are incorrect, the program may run but
produce unintended results. For example:
•x=5
• y = 10
• z = x - y # Syntax is correct, but the semantics
(subtraction) may not be what was intended.
• print(z)
• Here, the program will output -5, which might not be the
desired behavior if the goal was to add the numbers.
SYNTAX AND SEMANTICS
• Relationship Between Syntax and Semantics
• Syntax is about form: It ensures the code is
structured correctly.
• Semantics is about meaning: It ensures the code
performs the intended operations.
SYNTAX AND SEMANTICS
• Importance of Syntax and Semantics
• Syntax ensures that the code is readable and can
be parsed by the compiler or interpreter.
• Semantics ensures that the code performs the
intended operations and produces the correct
results.
SYNTAX AND SEMANTICS
• Understanding both syntax and semantics is essential for writing
effective and error-free programs. While syntax errors are often caught
during compilation, semantic errors can be more subtle and may only
become apparent during runtime or testing.
SYNTAX AND SEMANTICS
• Syntax and semantics are two pillars of programming languages.
Syntax defines the rules for writing code, while semantics defines the
meaning and behavior of the code. Both are essential for creating
programs that are not only correct but also efficient and maintainable.
By mastering these concepts, developers can write code that is both
syntactically correct and semantically meaningful.
PROGRAM STRUCTURE
NKANNAN
STRUCTURED PROGRAMMING CONCEPT
• In structured programming design, programs
are broken into different functions these
functions are also known as modules,
subprogram, subroutines or procedures.
STRUCTURED PROGRAMMING CONCEPT
• Each function is design to do a specific task with its own
data and logic. Information can be passed from one
function to another function through parameters. A
function can have local data that cannot be accessed
outside the function’s scope. The result of this process
is that all the other different functions are synthesized in
another function. This function is known as main
function. Many of the high-level languages support
structured programming.
STRUCTURED PROGRAMMING CONCEPT
• Structured programming minimizes the chances of
the function affecting another. It allows for clearer
programs code. It made global variables to
disappear and replaced by the local variables. Due
to this change one can save the memory allocation
space occupied by the global variable.
STRUCTURED PROGRAMMING CONCEPT
• Its organization helps in the easy understanding of
programming logic. So that one can easily
understand the logic behind the programs. It also
helps the newcomers of any industrial technology
company to understand the programs created by
their senior workers of the industry. The languages
that support Structured programming approach
are:
STRUCTURED PROGRAMMING CONCEPT
•C
• C++
• Java
• C#
• Pascal
STRUCTURED PROGRAMMING CONCEPT -
Advantages
• It is user friendly and easy to understand.
• Similar to English vocabulary of words and
symbols.
• It is easier to learn.
• They require less time to write.
• They are easier to maintain.
STRUCTURED PROGRAMMING CONCEPT -
Advantages
• These are mainly problem oriented rather than machine
based.
• Program written in a higher-level language can be
translated into many machine languages and therefore
can run on any computer for which there exists an
appropriate translator.
• It is independent of machine on which it is used, i.e.,
programs developed in high level languages can be run on
any computer.
STRUCTURED PROGRAMMING CONCEPT -
Disadvantages
• Structured programming codes implemented in
high-level language has to be translated into the
machine language by translator and thus a price in
computer time is paid. The object code generated
by a translator might be inefficient compared to an
equivalent assembly language program.
STRUCTURED PROGRAMMING CONCEPT -
Disadvantages
• Data type are proceeds in many functions in a
structured program. When changes occur in those
data types, the corresponding change must be
made to every location that acts on those data
types within the program. This is really a very
timeconsuming task if the program is very large.
STRUCTURE OF A C PROGRAM
• The structure of a C program is a protocol (rules) to
the programmer, which he has to follow while
writing a C program. The general basic structure of
C program is shown in the code below.Based on
this structure, we can write a C program. Example:
STRUCTURE OF A C PROGRAM
• /* This program accepts a number and displays it to the
user*/
• #include <stdio.h>
• void main(void)
• { int number;
• printf( "Please enter a number: " );
• scanf( "%d", &number );
• printf( "You entered %d", number );
• return 0;
•}
STRUCTURE OF A C PROGRAM - Explanation
• #include: The part of the compiler which actually gets your
program from the source file is called the preprocessor.
• #include <stdio.h>:#include is a pre-processor directive. It is not
really part of our program, but instead it is an instruction to the
• compiler to make it do something. It tells the C compiler to
include the contents of a file (in this case the system file called
stdio.h).
STRUCTURE OF A C PROGRAM - Explanation
• The compiler knows it is a system file, and therefore must be
looked for in a special place, by the fact that the filename is
enclosed in <> characters <stdio.h>: stdio.h is the name of the
standard library definition file for all STanDard Input and Output
functions.
• The program will almost certainly want to send information to the
screen and read things from the keyboard, and stdio.h is the name
of the file in which the functions that we want to use are defined.
STRUCTURE OF A C PROGRAM - Explanation
• The function we want to use is called printf. The actual code of
printf will be tied in later by the linker.
• The ".h" portion of the filename is the language extension, which
denotes an include file.
STRUCTURE OF A C PROGRAM - Explanation
• void:This literally means that this means nothing. In this case, it is
referring to the function whose name follows. Void tells C
compiler that a given entity has no meaning and produces no
error.
STRUCTURE OF A C PROGRAM - Explanation
• main:In this example, the only function in the program is called
main. A C program is typically made up of large number of
functions. Each of these is given a name by the programmer and
they refer to each other as the program runs. C regards the name
main as a special case and will run this function first i.e. the
program execution starts from main.
STRUCTURE OF A C PROGRAM - Explanation
• (void): This is a pair of brackets enclosing the keyword void. It tells
the compiler that the function main has no parameters. A
parameter to a function gives the function something to work on.
STRUCTURE OF A C PROGRAM - Explanation
• { (Brace): This is a brace (or curly bracket). As the name implies,
braces come in packs of two - for every open brace there must be
a matching close one. Braces allow us to group pieces of program
together, often called a block.A block can contain the declaration
of variable used within it, followed by a sequence of program
statements. In this case the braces enclose the working parts of
the function main.
STRUCTURE OF A C PROGRAM - Explanation
• ; (semicolon): The semicolon marks the end of the list of variable
names, and also the end of that declaration statement.All
statements in C programs are separated by ";" (semicolon)
characters. The ";" character is actually very important. It tells the
compiler where a given statement ends.
STRUCTURE OF A C PROGRAM - Explanation
• If the compiler does not find one of these characters where it
expects to see one, then it will produce an error.
• scanf:In other programming languages, the printing and reading
functions are a part of the language. In C this is not the case;
instead they are defined as standard functions which are part of
the language specification, but are not a part of the language
itself.
STRUCTURE OF A C PROGRAM - Explanation
• The standard input/output library contains a number of functions
for formatted data transfer; the two we are going to use are scanf
(scan formatted) and printf (print formatted).
• printf:The printf function is the opposite of scanf.
STRUCTURE OF A C PROGRAM - Explanation
• It takes text and values from within the program and sends it out
onto the screen.
• Just like scanf, it is common to all versions of C and just like scanf,
it is described in the system file stdio.h.
• The first parameter to a printf is the format string, which contains
text, value descriptions and formatting instructions.
STRUCTURE OF A C PROGRAM – Files used
in C
• Source File- This file contains the source code of the program. The
file extension of any c file is .c. The file contains C source code
that defines the main function & maybe other functions.
STRUCTURE OF A C PROGRAM – Files used
in C
• Header File- A header file is a file with extension .h which contains
the C function declarations and macro definitions and to be
shared between several source files.
STRUCTURE OF A C PROGRAM – Files used
in C
• Object File- An object file is a file containing object code, with an
extension .o, meaning relocatable format machine code that is
usually not directly executable. Object files are produced by an
assembler, compiler, or other language translator, and used as
input to the linker, which in turn typically generates an executable
or library by combining parts of object files.
STRUCTURE OF A C PROGRAM – Files used
in C
• Executable File- The binary executable file is generated by the
linker. The linker links the various object files to produce a binary
file that can be directly executed.
STRUCTURE OF A C PROGRAM
• The C programming language was designed by Dennis Ritchie as a
systems programming language for Unix.
• A C program basically has the following structure:
• Preprocessor Commands/directive
• Functions
• Variable declarations
• Statements & Expression
• Comments
STRUCTURE OF A C PROGRAM –
Preprocessor Commands
• These commands tell the compiler to do preprocessing before doing
actual compilation. Like #include<stdio.h> is a preprocessor command
which tells C compiler to include stdio.h file before going to actual
compilation. The standard input and output header file (stdio.h) allows
the program to interact with the screen, keyboard and file system of the
computer.
• # include <stdio.h>
• # include <math.h>
• # include <stdlib.h>
• NB: Preprocessor directives are not actually part of the C language, but
rather instructions from the programmer to the compiler.
STRUCTURE OF A C PROGRAM –
Preprocessor Commands
STRUCTURE OF A C PROGRAM - Functions
• These are main building blocks of any C Program. Every C Program
will have one or more functions and there is one mandatory
function which is called the main()function. When this function is
prefixed with keyword int, it means this function returns an integer
value when it exits. This integer values is retuned using return
statement.
• The C programming language provides a set of built in functions.
In the above example printf() is a C built in function which is used
for printing anything on the screen
STRUCTURE OF A C PROGRAM - Functions
• A function is a group of statement that together performs a task. A
C program can be divide up into many separate functions but
logically the division usually is so each function performs a
specific task.
• A function declaration tells the compiler about a function’s name,
return typs, and parameters. A function definition provides the
actual body of the function.
• The general form a function definition in C programming language
is as follows:
STRUCTURE OF A C PROGRAM - Functions
STRUCTURE OF A C PROGRAM – Variable
Declarations
• In C, all variables must be declared before they are used. Thus C
is a strongly typed programming language. Variable declaration
ensures that appropriate memory space is reserved for the
variables. Variables are used to numbers, strings and complex
data for manipulations e.g
• Int x;
• Int num; int z;
STRUCTURE OF A C PROGRAM – Statements
& Expressions
• Expressions combine variables and constants to create new
values e.g
• X+Y;
• Statements in C are expressions, assignments, function calls, or
control flow statement which make up C programs
• An assignment statement users the assignment operator “=“ to
give a variable on the operator’s left side the value to the
operator’s right or the result of an expression on the right.
• Z=X+Y
STRUCTURE OF A C PROGRAM – Comments
• These are non-executable program statements meant to enhance
program readability and allow easier program maintenance. They
document the program. They are ignored by the compiler. These
are used to give additional useful information inside a C program.
All the comments will be put inside /*…..*/ or // for single line
comments as given in the example. A comment can span through
multiple lines
STRUCTURE OF A C PROGRAM - Comments
STRUCTURE OF A C PROGRAM – Escape
Sequences
• Escape sequences (also called back slash codes) are character
combinations that begin with a backslash symbol used to format
output and represent difficult to type characters. They include:
• \a Alert/bell
• \b Backspace
• \n New line
• \v Vertical tab
• \t Horizontal tab
• \\ Back Slash
• \” Double quote
• \0 Null
Sample Program

You might also like