Introduction To C
Introduction To C
Programming
By:
Dr. Abhishek Singh
Outline of Topics
• Hardware/Software interface
– Layers of the Machine
– Kinds of Software
• Computer Languages
• Syntax, Semantics, Grammars
• What happens to your program?
– Compilation, Linking, Execution
– Program errors
• Compilation vs. Interpretation etc.
Software Categories
• System SW
– Programs written for computer systems
• Compilers, operating systems, …
• Application SW
– Programs written for computer users
• Word-processors, spreadsheets, & other
application packages
A Layered View of the Computer
Application Programs
Word-Processors, Spreadsheets,
Sample
Assembly
code for
Printing
Hello World
High-level language
• A programming language which use statements
consisting of English-like keywords such as "FOR",
"PRINT" or “IF“, ... etc.
• Each statement corresponds to several machine
language instructions (one-to-many correspondence).
• Much easier to program than in assembly language.
• Data are referenced using descriptive names
• Operations can be described using familiar symbols
• Example:
Cost := Price + Tax
Syntax & Semantics
• Syntax:
– The structure of strings in some language. A
language's syntax is described by a grammar.
– Examples:
• Binary number
<binary_number> = <bit> | <bit> <binary_number>
<bit> =0|1
• Identifier
<identifier> = <letter> {<letter> | <digit> }
<letter> =a|b|...|z
<digit =0|1|...|9
• Semantics:
– The meaning of the language
Syntax & Grammars
• Syntax descriptions for a PL are
themselves written in a formal language.
– E.g. Backus-Naur Form (BNF)
• The formal language is not a PL but it can
be implemented by a compiler to enforce
grammar restrictions.
• Some PLs look more like grammar
descriptions than like instructions.
Compilers & Programs
• Compiler
– A program that converts another program from
some source language (or high-level
programming language / HLL) to machine
language (object code).
– Some compilers output assembly language
which is then converted to machine language by
a separate assembler.
– Is distinguished from an assembler by the fact
that each input statement, in general,
correspond to more than one machine
instruction.
Compilation into Assembly L
Source Assembly
Program Compiler Language
• Executable program
– Output from linker/loader
– Machine language program linked with necessary
libraries & other files
– Files usually have extension ‘.exe’
What is a Linker?
• A program that pulls other programs together so
that they can run.
• Most programs are very large and consist of
several modules.
• Even small programs use existing code provided
by the programming environment called libraries.
• The linker pulls everything together, makes sure
that references to other parts of the program
(code) are resolved.
Running Programs
• Steps that the computer goes through to run a
program:
Memory
Machine language
program
(executable file)
Input Data Data entered CPU
during execution
Computed results
Program Output
Program Execution
• Steps taken by the CPU to run a program
(instructions are in machine language):
1. Fetch an instruction
2. Decode (interpret) the instruction
3. Retrieve data, if needed
4. Execute (perform) actual processing
5. Store the results, if needed
Program Errors
• Syntax Errors:
– Errors in grammar of the language
• Runtime error:
– When there are no syntax errors, but the program
can’t complete execution
• Divide by zero
• Invalid input data
• Logical errors:
– The program completes execution, but delivers
incorrect results
– Incorrect usage of parentheses
Compilation
Source Target
Program Compiler Program
Interpreter Output
Input
Intermediate
Program
VM Output
Input
Java Compiler
bytecode
ML Interpreter
2. What is the minimum number of temporary variables required to swap the values of two
variables?
a)1 b) 2 c) 3 d) 0
3. C is a
a.High level language
b.Low level language
c.High level language with some low level features
d.Low level language with some high level features
4.If integer needs two bytes of starage, then max value of an unsigned integer is:
do if static while
Variables
• A variable is nothing but a name given to a storage area that our programs can
manipulate. Each variable in C has a specific type, which determines the size and
layout of the variable's memory; the range of values that can be stored within that
memory; and the set of operations that can be applied to the variable.
• The name of a variable can be composed of letters, digits, and the underscore
character. It must begin with either a letter or an underscore. Upper and lowercase
letters are distinct because C is case-sensitive. There are following basic variable
types −
Type Description
• char Typically a single octet(one byte). This is an integer type.
• int The most natural size of integer for the machine.
• float A single-precision floating point value.
• double A double-precision floating point value.
• void Represents the absence of type.
Constants
Integer constants
• A integer constant is a numeric constant (associated with number)
without any fractional or exponential part. There are three types of
integer constants in C programming:
Floating-point constants
• A floating point constant is a numeric constant that has either
a fractional form or an exponent form. For example:
2.0,0.0000234,-0.22E-5
Character constants
• A character constant is a constant which uses single quotation
around characters. For example: 'a', 'l', 'm', 'F'
String constants
• String constants are the constants which are enclosed in a pair
of double-quote marks. For example: "good" ,"x","Earth is
round\n"
Escape Sequences
Sometimes, it is necessary to use characters which cannot be typed or has special
meaning in C programming. For example: newline(enter), tab, question mark etc. In
order to use these characters, escape sequence is used.
• For example: \n is used for newline. The backslash ( \ ) causes "escape" from the
normal way the characters are interpreted by the compiler.Escape
Sequences Character
• \b Backspace
• \f Form feed
• \n Newline
• \r Return We will see and evaluate
• \t Horizontal tab these escape sequences
• \v Vertical tab throughout the syllabus
• \\ Backslash
• \' Single quotation mark
• \" Double quotation mark
• \? Question mark
• \0 Null character
Operators in C:An operator is a symbol which operates on a
value or a variable. For example: + is an operator to perform
addition.
statement 3; Statement 3
else
Default statemen
default statement;
Next statement
Switch statement
Switch is a multi-way decision making statement
which selects one of the several alternatives based
on the value of single variable or expression.
It is mainly used to replace multiple if-else-if
statement.
The if-else-if statement causes performance
degradation as several conditions need to be
evaluated before a particular condition is satisfied.
Syntax: switch (expression)
{
case constant1 : statement (s); [break;]
case constant2 : statement (s); [break;]
…………………………………….
default: statement (s)
}
Break statement
Break statement terminates the
execution of the loop in which it is
defined.
The control is transferred
immediately to the next executable
statement after the loop.
It is mostly used to exit early from
the loop by skipping the remaining
statements of loop or switch control
structures.
Syntax: break;
Looping Structures
When we want to repeat a group of
statements a no. of times, loops are used.
These loops are executed until the
condition is true.
When condition becomes false, control
terminates the loop and moves on to next
instruction immediately after the loop.
Various looping structures are-
while
do – while
for
LOOPING STATEMENTS
Loop is divided into two parts:
Body of the loop
Control of loop
is equivalent to:
expr1;
while (expr2) {
statement;
expr3;
}
Various other ways of writing same
for loops
Also: c += 1;
Also: c++;
Also: ++c;
/* Preincrementing and postincrementing */
#include <stdio.h>
int main()
{
int c;
c = 5;
printf("%d\n", c);
printf("%d\n",c++); /*post increment*/
printf("%d\n\n", c);
:
continued
c = 5;
printf("%d\n", c);
printf("%d\n",++c); /*pre-increme
nt*/
printf("%d\n", c);
return 0;
}
Output:
5
5
6
5
6
6
Decrementing
Take 1 from c by writing:
c = c - 1;
Also: c -= 1;
Also: c--;
Also: --c;
Continue statement
Like break ,continue statement also skips the
remaining statements of the body of the loop
where it is defined but instead of terminating
the loop, the control is transferred to the
beginning of the loop for next iteration.
The loop continues until the test condition of
the loop become false.
Syntax: continue;
E.g. for (m=1;m<=3;m++)
{ Output:
for (n=1;n<=2;n++) 1 2
{ 2 1
if (m==n) 3 1
continue; 3 2
printf(“ m=%d n=%d”);
}
}
goto Statement
An unconditional control statement that causes
the control to jump to a different location in the
program without checking any condition.
It is normally used to alter the normal sequence
of program execution by transferring control to
some other part of the program.
So it is also called jump statement.
Syntax: goto label;
Label represents an identifier which is used to
label the destination statement to which the
control should be transferred.
label : statement;
The goto statement causes the control to be
shifted either in forward direction or in a
backward direction .
exit() function
C provides a run time library function
exit() which when encountered in a
program causes the program to
terminating without executing any
statement following it.
Syntax: exit(status);
Status is an integer variable or
constant.
If the status is 0,then program normally
terminates without any errors.
A non-zero status indicates abnormal
termination of the program.
The exit() function is defined in the
process.h header file.
Difference b/w exit() & break