Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 35

1

C program

2
Compiler
 Compiler : Compiler is special type of program that transforms source
code(human writable) into machine code(machine readable) comprising of just
two digits – 1s and 0s (target language).
 The high-level language is converted into binary language in various phases. A
compiler is a program that converts high-level language to assembly language.
Similarly, an assembler is a program that converts the assembly language to
machine-level language.

Source file Object file


3
Compilation Flow

4
Interpreter
 Interpreter: An interpreter, like a compiler, translates high-level language into
low-level machine language. interpreter reads a statement from the input,
converts it to an intermediate code, executes it, then takes the next statement in
sequence. If an error occurs, an interpreter stops execution and reports it.
 However, a compiler will convert all source code into machine code (create an
exe) before program run.
 Interpreters convert source code into machine code when the program is run.
 Compiled code runs faster while interpreted code runs slower.

5
Linker And Loader
 Linker: In high level languages built in header files or libraries are stored.
These libraries are predefined and these contain basic functions which are
essential for executing the program. These functions are linked to the libraries
by a program called Linker.
 Loader: Loader is a part of operating system and is responsible for loading
executable files into memory and execute them.

6
Debugger
 A debugger or debugging tool is a computer program used
to test and debug other programs (the "target" program).

 The main use of a debugger is to run the target program under controlled
conditions that permit the programmer to track its operations in progress and
monitor changes in computer resources.

Debugging is a methodical process of finding and reducing the number of


bugs (or defects) in a computer program, thus making it behave as originally
expected.

7
Indentations and Comments
 In computer programming, an indentation style is a convention governing
the indentation of blocks of code to convey program structure.

 Indentations increase the Readability and Understandability of a program.

8
Data types, variables,
Constants
C Language Reserved Words
Keywords are the words whose meaning has already been explained to the C
compiler.
The table below lists all keywords reserved by the C language. When the current
programming language is C or C++, these keywords cannot be abbreviated, used as
variable names, or used as any other type of identifiers.

auto else long switch


break enum register typedef
case extern return union
char float short unsigned
const for signed void
continue goto sizeof volatile
default if static while
do int struct double
Datatypes

Datatypes:

A data type specifies the type of data that a variable can store such as integer, floating,
character, etc.
The memory size of the basic data types may change according to 32 or 64-bit operating
system.

11
Data Types
Primary Derived User defined
int String structure

char Array union

float Pointer enum

void function typedef

12
Primary Data Type:
 Int: It is used to store integer values.
  Eg : int a ,b;
int number = 100;
 Char: It stores single character such as ‘a’, ‘Z’, ‘@’ etc. including number,
symbol or special character. It takes 1 byte (8-bits) to store each character.
char a = 'A';
 Float:It stores real numbers with precision upto 6 decimal places. It takes 4
bytes of memory and is also known as floating point number.
Eg: float num_1 = 100.6543;
 Double:It stores real numbers with precision upto 15 decimal places. It takes 8
bytes of memory.
Eg: double num = 180.715586;
Data Type and their Description

14
Derived datatypes
2. Derived datatypes : These data type are derived from
basic data type. Variables of derived data type allow us to
store multiple values of same type in one variable but
never allows to store multiple values of different types.

Types of derived datatype:


a.Array
b.Pointer
c.String etc

Eg: In C language it can be achieve by array.


int arr [] ={10,20,30};
int arr [] ={10,’a’,”W”};//invalid
  15
User defined data types
3.User defined data types : User defined data types related variables
allows us to store multiple values either of same type or different type or
both. This is a data type whose variable can hold more than one value of
dissimilar type.

In C language, user defined data types can :


a.struct,
b.enum,
c.typedef,
d.union etc.

Eg: In C language it is achieved by structure.


Struct emp
{
int e_id;
char e_name[20]; 16

float e_sal;
};
Modifiers
Modifiers: Modifiers are prefixed with basic data types to modify (either
increase or decrease) the amount of storage space allocated to a variable.

For example, storage space for int data type is 4 byte for 32 bit processor.
We can increase the range by using long int which is 8 byte. We can
decrease the range by using short int which is 2 byte.

 Data type modifiers are classified into following types.


long
short
unsigned
signed

17
Identifier
 In C language identifiers are the names given to variables, constants, functions
and user-define data. Identifiers must be unique.
Rules for an Identifier:
 the first character of an identifier should be either an alphabet or an
underscore, and then it can be followed by any of the character, digit, or
underscore.
 It should not begin with any numerical digit.
 In identifiers, both uppercase and lowercase letters are distinct. Therefore, we
can say that identifiers are case sensitive.
 Commas or blank spaces cannot be specified within an identifier.
 Keywords cannot be represented as an identifier.
  18
Rules for writing an identifier
 The length of the identifiers should not be more than 31 characters.
 Identifiers should be written in such a way that it is meaningful, short, and
easy to read.
 Example of valid identifiers: total, sum, average, _m _, sum_1, etc

19
Variables
 Variables: A variable is a name of the memory location. It is used to store
data. It’s value can be changed, and it can be reused many times.
 Note: We need to specify the data type of variable(identifier) to store any data
in it.

5
 Syntax: datatype variavle_name;
 For Example:
int myVariable= 5; // it means myVariable is assigned value ”5”
float b= 3.5; // it means b is assigned ”3.5”
myVari
char c = ‘A’; // it means c is assigned ”A” able

Here, a, b, c are variables. The int, float, char are the data types

20
Variables
Rules for naming variables in C:
 A variable can have alphabets, digits, and underscore.
 A variable name can start with the alphabet, and underscore only. It can't start
with a digit.
 No whitespace is allowed within the variable name.
 A variable name must not be any reserved word or keyword.
 e.g. int, float, etc.
 The maximum allowable length of variable name is 31 characters.
 We must create meaningful variable name, this enhance readability of the
program.

21
Variables Declaration
Variables may be declared in a few ways:

22
Variable and Datatypes use in prog
#include <stdio.h>

int main(void)
{
Data
Types Variable
float radius, area, PI;
Declarations
//Calculate area of circle
PI = 3.1416;
radius = 12.0;
area = PI * radius * radius;
Variables in
printf("Area = %f", area); use
Return 0;
} 23
Types of Variable
Local variable
Local variables are declared within the body of a function, and can only be
used within that function.

Static variable
A variable that is declared with the static keyword is called static variable. It
retains its value between multiple function calls. Static variable has a default
initial value zero, and is initialized only once in its lifetime.

Global variable
A global variable declaration looks normal, but is located outside any of the
program's functions. So it is accessible to all functions.
24
Types of Variable Example

An example
int value=20;//global variable  
void function1()
{  
int x=10;//local variable  
static int y=10;//static variable  
}
25
Variable Storage

Variables example in C
int X = 8; int Y = 7;

26
Constants
Constants : Constants refer to fixed values. They are also called as literals.
Constants may be belonging to any of the data type. A constant is an entity that
doesnot change.
 
Syntax:
const data_type variable_name; (or) const data_type *variable_name;
 
Primary constants
Integer constant
Real constant
Character constant

27
Integer Constant

Integer Constant: It is used to store integer values


Rules for constructing ineger constants:
 Integer constant must have at least one digit.
 It must not have decimal point.
 It can be either positive or negative.
 No commas or blanks are allowed within integer constant
 The allowable range of integer constant is -32768 to 32767(16 bit).
Eg: -1, +4, 2, 45,345 etc.

28
Real Constant
Real constant: It stores real numbers with precision upto 6 decimal places. Real constant are
often called floating point constant.
Rules for constructing Real constants:
 Real constant must have at least one digit.
 It must have decimal point.
 It can be either positive or negative.
 No commas or blanks are allowed within Real constant
 Default sign is positive.
 Range -3.4e38 to +3.4e38.
Eg: -234.5 , .+4567.23456,2900.3433 etc
29
Character Constant
Character constant: must be enclosed within single inverted commas ,both the
inverted commas should point to the left ’A’ valid and ‘A’ is not.
The maximum length of a character constant can be 1 character.
Eg: ’a’
‘A’//invalid

30
String Constant

String constant: These are a sequence of characters enclosed in double quotes, and
they may include letters, digits, special characters, and blank spaces. It is again to be
noted that "G" and 'G' are different - because "G" represents a string as it is enclosed
within a pair of double quotes, whereas 'G' represents a single character.
Example:
"Hello!", "2015", "2+1"

31
Printf
The printf() function can be instructed to print integers, floats and string properly.
The general syntax is
printf( “format”, variables);

An example
int stud_id = 5200;
printf(“ ID is %d \n”,stud_id);

32
Format specifiers -

33
escape sequence
Why “\n” : It introduces a new line on the terminal screen.

escape sequence

34
Scanf ()
Description:
The C library which allows the programmer to accept input
from a keyboard. The following program illustrates the use of
this function

#include <stdio.h>
main() {
int pin;
printf("Please type in your PIN\n");
scanf(“%d “,&pin);
printf("Your access code is %d\n",pin);
}

35

You might also like