KEMBAR78
Computer Languages Classification | PDF | Assembly Language | Programming
0% found this document useful (0 votes)
76 views46 pages

Computer Languages Classification

The document provides a comprehensive overview of computer languages, classifying them into low-level, middle-level, and high-level languages, each with distinct characteristics, advantages, and disadvantages. It also explains the roles of compilers and interpreters in translating high-level code into machine language, highlighting their differences. Additionally, it covers C programming, including its tokens, character set, keywords, identifiers, and data types.

Uploaded by

dark.i.nawaz9
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)
76 views46 pages

Computer Languages Classification

The document provides a comprehensive overview of computer languages, classifying them into low-level, middle-level, and high-level languages, each with distinct characteristics, advantages, and disadvantages. It also explains the roles of compilers and interpreters in translating high-level code into machine language, highlighting their differences. Additionally, it covers C programming, including its tokens, character set, keywords, identifiers, and data types.

Uploaded by

dark.i.nawaz9
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/ 46

Computer Languages Classification

Over the years, computer languages have been evolved from Low-Level to High-Level Languages. In the
earliest days of computers, only Binary Language was used to write programs. The computer languages
are classified as follows...

Low-Level Language (Machine Language)

Low-Level language is the only language which can be understood by the computer. Binary Language is
an example of a low-level language. Low-level language is also known as Machine Language. The binary
language contains only two symbols 1 & 0. All the instructions of binary language are written in the form
of binary numbers 1's & 0's. A computer can directly understand the binary language. Machine language
is also known as the Machine Code.

As the CPU directly understands the binary language instructions, it does not require any translator. CPU
directly starts executing the binary language instructions and takes very less time to execute the
instructions as it does not require any translation. Low-level language is considered as the First
Generation Language (1GL).

Advantages

 A computer can easily understand the low-level language.

 Low-level language instructions are executed directly without any translation.

 Low-level language instructions require very less time for their execution.

Disadvantages

 Low-level language instructions are very difficult to use and understand.

 Low-level language instructions are machine-dependent, that means a program written for a
particular machine does not execute on another machine.
 In low-level language, there is more chance for errors and it is very difficult to find errors, debug
and modify.

Middle-Level Language (Assembly Language)

Middle-level language is a computer language in which the instructions are created using symbols such
as letters, digits and special characters. Assembly language is an example of middle-level language. In
assembly language, we use predefined words called mnemonics. Binary code instructions in low-level
language are replaced with mnemonics and operands in middle-level language. But the computer
cannot understand mnemonics, so we use a translator called Assembler to translate mnemonics into
binary language. Assembler is a translator which takes assembly code as input and produces machine
code as output. That means, the computer cannot understand middle-level language, so it needs to be
translated into a low-level language to make it understandable by the computer. Assembler is used to
translate middle-level language into low-level language.

Advantages

 Writing instructions in a middle-level language is easier than writing instructions in a low-level


language.

 Middle-level language is more readable compared to low-level language.

 Easy to understand, find errors and modify.

Disadvantages
 Middle-level language is specific to a particular machine architecture, that means it is machine-
dependent.

 Middle-level language needs to be translated into low-level language.

 Middle-level language executes slower compared to low-level language.

High-Level Language

A high-level language is a computer language which can be understood by the users. The high-level
language is very similar to human languages and has a set of grammar rules that are used to make
instructions more easily. Every high-level language has a set of predefined words known as Keywords
and a set of rules known as Syntax to create instructions. The high-level language is easier to understand
for the users but the computer can not understand it. High-level language needs to be converted into
the low-level language to make it understandable by the computer. We use Compiler or interpreter to
convert high-level language to low-level language.

Languages like COBOL, FORTRAN, BASIC, C, C++, JAVA, etc., are examples of high-level languages. All
these programming languages use human-understandable language like English to write program
instructions. These instructions are converted to low-level language by the compiler so that it can be
understood by the computer.

Advantages

 Writing instructions in a high-level language is easier.


 A high-level language is more readable and understandable.

 The programs created using high-level language runs on different machines with little change or
no change.

 Easy to understand, create programs, find errors and modify.

Disadvantages

 High-level language needs to be translated into low-level language.

 High-level language executes slower compared to middle and low-level languages.

Understanding Computer Languages

The following figure provides a few key points related to computer languages.

From the above figure, we can observe the following key points...

 The programming languages like C, C++, Java, etc., are written in High-level language which is
more comfortable for the developers.

 A high-level language is closer to the users.

 Low-level language is closer to the computer. Computer hardware can understand only the low-
level language (Machine Language).

 The program written in the high-level language needs to be converted to low-level language to
make communication between the user and the computer.

 Middle-level language is not closer to both user and computer. We can consider it as a
combination of both high-level language and low-level language.
Compilers and interpreters
Both compilers and interpreters are the language processors used to convert software codes written in
high-level language into machine language codes. Compilers and interpreters are types of system
software. They are required because a computer cannot process a code written in high-level
programming language like C, C++, Java, etc. Therefore, we convert a HLL code into machine code for
execution.

In this article, we will highlight all the major differences between a compiler and an interpreter. Let's
start with some basics so that it will become easier to understand their differences.

What is a Compiler?

A language processor that converts a program written in high-level language into machine language,
entire program at once, is called a compiler. Thus, the input of a compiler is a high-level language code
(called source code), while its output is a machine language code (called object code).
A compiler scans whole program and then check it for syntactic and semantic error, once the code is
checked for errors, it is converted into an object code. Then, it can be processed by the machine to
perform the corresponding task. The common programming languages that use compiler are C, C++, C#,
etc.

Advantages of Compiler

There are various advantages of the compiler which are as follows −

 A compiler translates a program in a single run.

 It consumes less time.

 CPU utilization is more.

 Both syntactic and semantic errors can be checked concurrently.

 It is easily supported by many high-level languages like C, C++, JAVA, etc.

What is an Interpreter?

A language translator that converts a high-level language program into a machine language program,
one line at a time, is referred to as an interpreter. Interpreters converts the codes slower than compiler.
This is because the interpreter can scan and translate only one statement of the program at a time.
Therefore, interpreters convert the source code into machine code during the execution of the program.

Interpreters do not generate an object code corresponding to the source code. However, these are
relatively easy to use and execute the code. The programming languages that use interpreters are Perl,
Ruby, Python, METLAB, etc.

Advantages of Interpreter

There are various advantages of the interpreter which are as follows −


 An interpreter translates the program line by line.

 The interpreter is smaller in size.

 It is flexible.

 Error localization is easier.

 The interpreter facilitates the implementation of computer programming language constructs.

Difference between Compiler and Interpreter

The following table highlights all the significant differences between a Compiler and an Interpreter −

Parameter Compiler Interpreter

Program Compilers scan the entire program in one The program is interpreted/translated one line at a time.
scanning go.

Error detection As and when scanning is performed, all the One line of code is scanned, and errors encountered are
errors are shown in the end together, not shown.
line by line.

Object code Compilers convert the source code to object Interpreters do not convert the source code into object
code. code.

Execution time The execution time of compiler is less, hence It is not preferred due to its slow speed. Usually,
it is preferred. interpreter is slow, and hence takes more time to execute
the object code.

Need of source Compiler doesn’t require the source code for It requires the source code for execution later.
code execution later.

Programming Programming languages that use compilers Programming languages that uses interpreter include
languages include C, C++, C#, etc.. Python, Ruby, Perl, MATLAB, etc.

Types of errors Compiler can check syntactic and semantic Interpreter checks the syntactic errors only.
detected errors in the program simultaneously.

Size Compiler are larger in size. Interpreters are smaller in size.

Flexibility Compilers are not flexible. Interpreters are relatively flexible.


Efficiency Compilers are more efficient. Interpreters are less efficient.

Conclusion

The most significant difference between a compiler and an interpreter is that a compiler scans the entire
program in one go, while an interpreter scans the program line by line. However, both are used in
different applications depending on the requirements of the programming language.

C programming
C programming is a general-purpose, procedure-oriented programming language. It is both machine-
independent and structured. C is a high-level programming language developed by Dennis Ritchie in the
early 1970s. It is now one of the most popular and influential programming languages worldwide. C is
popular for its simplicity, efficiency, and versatility, it has powerful features including low-level memory
access, a rich set of operators, and a modular framework. Apart from its importance with respect to
evolution of computer programming technologies, the design of C language has a profound influence on
most of the other programming languages that are in use today.

The languages that are influenced by C include Java, PHP, JavaScript, C#, Python and many more. These
languages have designed their syntax, control structures and other basic features from C.

C supports and run on different hardware and operating systems due to its portability.

Following are the applications and software used to create and execute C programs.

1. Turbo C

2. Turbo C++

3. GNU C

4. Code Blocks

5. Net Beans

C Tokens
Every C program is a collection of instructions and every instruction is a collection of some individual
units. Every smallest individual unit of a c program is called token. Every instruction in a c program is a
collection of tokens. Tokens are used to construct c programs and they are said to the basic building
blocks of a c program.

In a c program tokens may contain the following...


1. Keywords

2. Identifiers

3. Operators

4. Special Symbols

5. Constants

6. Strings

7. Data values

In a C program, a collection of all the keywords, identifiers, operators, special symbols, constants,
strings, and data values are called tokens.

C Character Set
As every language contains a set of characters used to construct words, statements, etc., C language
also has a set of characters which include alphabets, digits, and special symbols. C language supports a
total of 256 characters.

Every C program contains statements. These statements are constructed using words and these words
are constructed using characters from C character set. C language character set contains the following
set of characters...

1. Alphabets

2. Digits

3. Special Symbols

Alphabets

C language supports all the alphabets from the English language. Lower and upper case letters
together support 52 alphabets.

lower case letters - a to z

UPPER CASE LETTERS - A to Z

Digits

C language supports 10 digits which are used to construct numerical values in C language.

Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Special Symbols
C language supports a rich set of special symbols that include symbols to perform mathematical
operations, to check conditions, white spaces, backspaces, and other special symbols.

Special Symbols - ~ @ # $ % ^ & * ( ) _ - + = { } [ ] ; : ' " / ? . > , < \ | tab newline space NULL bell
backspace verticaltab etc.,

Every character in C language has its equivalent ASCII (American Standard Code for Information
Interchange) value.

C Keywords
As every language has words to construct statements, C programming also has words with a specific
meaning which are used to construct c program instructions. In the C programming language,
keywords are special words with predefined meaning. Keywords are also known as reserved words in
C programming language.

In the C programming language, there are 32 keywords. All the 32 keywords have their meaning which
is already known to the compiler.

Keywords are the reserved words with predefined meaning which already known to the compiler

Whenever C compiler come across a keyword, automatically it understands its meaning.

Properties of Keywords

1. All the keywords in C programming language are defined as lowercase letters so they must be
used only in lowercase letters

2. Every keyword has a specific meaning, users can not change that meaning.

3. Keywords can not be used as user-defined names like variable, functions, arrays, pointers,
etc...

4. Every keyword in C programming language represents something or specifies some kind of


action to be performed by the compiler.

The following table specifies all the 32 keywords with their meaning...
C Identifiers
In C programming language, programmers can specify their name to a variable, array, pointer, function,
etc... An identifier is a collection of characters which acts as the name of variable, function, array,
pointer, structure, etc... In other words, an identifier can be defined as the user-defined name to
identify an entity uniquely in the c programming language that name may be of the variable name,
function name, array name, pointer name, structure name or a label.

The identifier is a user-defined name of an entity to identify it uniquely during the program execution

Example

int marks;
char studentName[30];

Here, marks and studentName are identifiers.

Rules for Creating Identifiers

1. An identifier can contain letters (UPPERCASE and lowercase), numerics & underscore symbol
only.

2. An identifier should not start with a numerical value. It can start with a letter or an underscore.

3. We should not use any special symbols in between the identifier even whitespace. However, the
only underscore symbol is allowed.

4. Keywords should not be used as identifiers.

5. There is no limit for the length of an identifier. However, the compiler considers the first 31
characters only.

6. An identifier must be unique in its scope.

Rules for Creating Identifiers for better programming

The following are the commonly used rules for creating identifiers for better programming...

1. The identifier must be meaningful to describe the entity.


2. Since starting with an underscore may create conflict with system names, so we avoid starting
an identifier with an underscore.

3. We start every identifier with a lowercase letter. If an identifier contains more than one word
then the first word starts with a lowercase letter and second word onwards first letter is used as
an UPPERCASE letter. We can also use an underscore to separate multiple words in an identifier.
C data types
Data used in c program is classified into different types based on its properties. In the C programming
language, a data type can be defined as a set of values with similar characteristics. All the values in a
data type have the same properties.

Data types in the c programming language are used to specify what kind of value can be stored in a
variable. The memory size and type of the value of a variable are determined by the variable data type.
In a c program, each variable or constant or array must have a data type and this data type specifies how
much memory is to be allocated and what type of values are to be stored in that variable or constant or
array. The formal definition of a data type is as follows...

The Data type is a set of value with predefined characteristics. data types are used to declare variable,
constants, arrays, pointers, and functions.

In the c programming language, data types are classified as follows...

1. Primary data types (Basic data types OR Predefined data types)

2. Derived data types (Secondary data types OR User-defined data types)

3. Enumeration data types

4. Void data type


Primary data types

The primary data types in the C programming language are the basic data types. All the primary data
types are already defined in the system. Primary data types are also called as Built-In data types. The
following are the primary data types in c programming language...

1. Integer data type

2. Floating Point data type

3. Double data type

4. Character data type

Integer Data type

The integer data type is a set of whole numbers. Every integer value does not have the decimal value.
We use the keyword "int" to represent integer data type in c. We use the keyword int to declare the
variables and to specify the return type of a function. The integer data type is used with different type
modifiers like short, long, signed and unsigned. The following table provides complete details about the
integer data type.
Floating Point data types

Floating-point data types are a set of numbers with the decimal value. Every floating-point value must
contain the decimal value. The floating-point data type has two variants...

 float

 double

We use the keyword "float" to represent floating-point data type and "double" to represent double data
type in c. Both float and double are similar but they differ in the number of decimal places. The float
value contains 6 decimal places whereas double value contains 15 or 19 decimal places. The following
table provides complete details about floating-point data types.

Character data type


The character data type is a set of characters enclosed in single quotations. The following table provides
complete details about the character data type.

The following table provides complete information about all the data types in c programming language...

void data type

The void data type means nothing or no value. Generally, the void is used to specify a function which
does not return any value. We also use the void data type to specify empty parameters of a function.

Enumerated data type

An enumerated data type is a user-defined data type that consists of integer constants and each integer
constant is given a name. The keyword "enum" is used to define the enumerated data type.
Derived data types

Derived data types are user-defined data types. The derived data types are also called as user-defined
data types or secondary data types. In the c programming language, the derived data types are created
using the following concepts...

 Arrays

 Structures

 Unions

 Enumeration

C Variables
Variables in a c programming language are the named memory locations where the user can store
different values of the same datatype during the program execution. That means a variable is a name
given to a memory location in which we can store different values of the same data type. In other
words, a variable can be defined as a storage container to hold values of the same datatype during the
program execution. The formal definition of a data type is as follows...

Variable is a name given to a memory location where we can store different values of the same
datatype during the program execution.

Every variable in c programming language must be declared in the declaration section before it is used.
Every variable must have a datatype that determines the range and type of values be stored and the size
of the memory to be allocated.

A variable name may contain letters, digits and underscore symbol. The following are the rules to specify
a variable name...

1. Variable name should not start with a digit.

2. Keywords should not be used as variable names.

3. A variable name should not contain any special symbols except underscore(_).

4. A variable name can be of any length but compiler considers only the first 31 characters of the
variable name.

Declaration of Variable
Declaration of a variable tells the compiler to allocate the required amount of memory with the
specified variable name and allows only specified datatype values into that memory location. In C
programming language, the declaration can be performed either before the function as global variables
or inside any block or function. But it must be at the beginning of block or function.
Declaration Syntax:

datatype variableName;

Example

int number;

The above declaration tells to the compiler that allocates 2 bytes of memory with the name number and
allows only integer values into that memory location.

C Constants
In C programming language, a constant is similar to the variable but the constant hold only one value
during the program execution. That means, once a value is assigned to the constant, that value can't be
changed during the program execution. Once the value is assigned to the constant, it is fixed throughout
the program. A constant can be defined as follows...

A constant is a named memory location which holds only one value throughout the program
execution.

In C programming language, a constant can be of any data type like integer, floating-point, character,
string and double, etc.,

Integer constants
An integer constant can be a decimal integer or octal integer or hexadecimal integer. A decimal integer
value is specified as direct integer value whereas octal integer value is prefixed with 'o' and hexadecimal
value is prefixed with 'OX'.
An integer constant can also be unsigned type of integer constant or long type of integer constant.
Unsigned integer constant value is suffixed with 'u' and long integer constant value is suffixed with 'l'
whereas unsigned long integer constant value is suffixed with 'ul'.

Example

125 -----> Decimal Integer Constant


O76 -----> Octal Integer Constant
OX3A -----> Hexa Decimal Integer Constant
50u -----> Unsigned Integer Constant
30l -----> Long Integer Constant
100ul -----> Unsigned Long Integer Constant

Floating Point constants

A floating-point constant must contain both integer and decimal parts. Some times it may also contain
the exponent part. When a floating-point constant is represented in exponent form, the value must be
suffixed with 'e' or 'E'.

Example
The floating-point value 3.14 is represented as 3E-14 in exponent form.

Character Constants

A character constant is a symbol enclosed in single quotation. A character constant has a maximum
length of one character.

Example

'A'
'2'
'+'

In the C programming language, there are some predefined character constants called escape
sequences. Every escape sequence has its own special functionality and every escape sequence is
prefixed with '\' symbol. These escape sequences are used in output function called 'printf()'.

String Constants

A string constant is a collection of characters, digits, special symbols and escape sequences that are
enclosed in double quotations.

We define string constant in a single line as follows...


"This is btechsmartclass"

We can define string constant using multiple lines as follows...


" This\
is\
btechsmartclass "

We can also define string constant by separating it with white space as follows...
"This" "is" "btechsmartclass"

All the above three defines the same string constant.

Creating constants in C

In a c programming language, constants can be created using two concepts...

1. Using the 'const' keyword

2. Using '#define' preprocessor

Using the 'const' keyword

We create a constant of any datatype using 'const' keyword. To create a constant, we prefix the variable
declaration with 'const' keyword.
The general syntax for creating constant using 'const' keyword is as follows...

const datatype constantName ;


OR

const datatype constantName = value ;

Example

const int x = 10 ;

Here, 'x' is a integer constant with fixed value 10.

Example Program

#include<stdio.h>

#include<conio.h>

void main(){

int i = 9 ;

const int x = 10 ;

i = 15 ;

x = 100 ; // creates an error

printf("i = %d\nx = %d", i, x ) ;

The above program gives an error because we are trying to change the constant variable value (x = 100).

Using '#define' preprocessor

We can also create constants using '#define' preprocessor directive. When we create constant using this
preprocessor directive it must be defined at the beginning of the program (because all the preprocessor
directives must be written before the global declaration).
We use the following syntax to create constant using '#define' preprocessor directive...

#define CONSTANTNAME value

Example

#define PI 3.14

Here, PI is a constant with value 3.14

Example Program

#include<stdio.h>
#include<conio.h>

#defien PI 3.14

void main(){

int r, area ;

printf("Please enter the radius of circle : ") ;

scanf("%d", &r) ;

area = PI * (r * r) ;

printf("Area of the circle = %d", area) ;

C Storage Classes
In C programming language, storage classes are used to define things like storage location (whether
RAM or REGISTER), scope, lifetime and the default value of a variable.

In the C programming language, the memory of variables is allocated either in computer memory (RAM)
or CPU Registers. The allocation of memory depends on storage classes.

In C programming language, there are FOUR storage classes and they are as follows...

1. auto storage class

2. extern storage class

3. static storage class

4. register storage class

auto storage class

The default storage class of all local variables (variables declared inside block or function) is auto storage
class. Variable of auto storage class has the following properties...
Property Description

Keyword auto

Storage Computer Memory (RAM)

Default Value Garbage Value

Scope Local to the block in which the variable is defined

Life time Till the control remains within the block in which variable is defined

Example Program 1

#include<stdio.h>

#include<conio.h>

int main(){

int i;

auto char c;

float f;

printf("i = %d\tc = %c\tf = %f",i,c,f);

return 0;

Example Program 2

#include<stdio.h>

#include<conio.h>

int main(){

int a=10;

int a=20;
printf("%d",a);

printf(" %d",a);

return 0;

Example Program 3

#include<stdio.h>

#include<conio.h>

int main(){

int a=20;

printf("%d",a);

printf(" %d",a); //a is not visible here

return 0;

External storage class

The default storage class of all global varibles (variables declared outside function) is external storage
class. Variable of external storage class has the following properties...

Property Description

Keyword extern

Storage Computer Memory (RAM)

Default Value Zero

Scope Global to the program (i.e., Throughout the program)


Property Description

Life time As long as the program’s execution does not comes to end

Example Program 1

#include<stdio.h>

#include<conio.h>

int i; //By default it is extern variable

int main(){

printf("%d",i);

return 0;

Example Program 2

#include<stdio.h>

#include<conio.h>

extern int i; //extern variable

int main(){

printf("%d",i);

return 0;

Static storage class

The static storage class is used to create variables that hold value beyond its scope until the end of the
program. The static variable allows to initialize only once and can be modified any number of times.
Variable of static storage class has the following properties...

Property Description

Keyword static
Property Description

Storage Computer Memory (RAM)

Default Zero
Value

Scope Local to the block in which the variable is defined

Life time The value of the persists between different function calls (i.e., Initialization is done
only once)

Example Program 1

#include<stdio.h>

#include<conio.h>

static int a;

int main(){

printf("%d",a);

return 0;

Example Program 2

#include<stdio.h>

#include<conio.h>

static int i=10;

int main(){

i=25; //Assignment statement

printf("%d",i);

return 0;

}
Register storage class

The register storage class is used to specify the memory of the variable that has to be allocated in CPU
Registers. The memory of the register variable is allocated in CPU Register but not in Computer memory
(RAM). The register variables enable faster accessibility compared to other storage class variables. As
the number of registers inside the CPU is very less we can use very less number of register variables.
Variable of register storage class has the following properties...

Property Description

Keyword register

Storage CPU Register

Default Value Garbage Value

Scope Local to the block in which the variable is defined

Life time Till the control remains within the block in which variable is defined

Example Program 1

#include<stdio.h>

#include<conio.h>

int main(){

register int a,b;

scanf("%d%d",&a,&b);

printf("%d %d",a,b);

The following table provides detailed properties of all storage classes...


Storage Memory Default
Class Keyword Location Value Scope Life Time

Automatic auto Computer Garbage Local to the block Till the control remains
Memory Value in which the within the block in which
(RAM) variable has variable is defined
defined

External extern Computer Zero Global to the As long as the program’s


Memory program (i.e., execution does not come
(RAM) Throughout the to end
program)

Static static Computer Zero Local to the block The value of the persists
Memory in which the between different function
(RAM) variable has calls (i.e., Initialization is
defined done only once)

Register register CPU Register Garbage Local to the block Till the control remains
Value in which the within the block in which
variable has variable is defined
defined

C Output Functions
C programming language provides built-in functions to perform output operation. The output operations
are used to display data on user screen (output screen) or printer or any file. The c programming
language provides the following built-in output functions...

1. printf()

2. putchar()

3. puts()

4. fprintf()

printf() function

The printf() function is used to print string or data values or a combination of string and data values on
the output screen (User screen). The printf() function is built-in function defined in a header file called
"stdio.h". When we want to use printf() function in our program we need to include the respective
header file (stdio.h) using the #include statement. The printf() function has the following syntax...
Syntax:

printf("message to be display!!!");

Example Program

#include<stdio.h>

#include<conio.h>

void main(){

printf("Hello! Welcome to btechsmartclass!!!");

Output:

In the above example program, we used the printf() function to print a string on to the output screen.

The printf() function is also used to display data values. When we want to display data values we
use format string of the data value to be displayed.

Syntax:

printf("format string",variableName);

Example Program

#include<stdio.h>

#include<conio.h>

void main(){

int i = 10;

float x = 5.5;
printf("%d %f",i, x);

Output:

In the above example program, we used the printf() function to print data values of variables i and x on
to the output screen. Here i is a an integer variable so we have used format string %d and x is a float
variable so we have used format string %f.

The printf() function can also be used to display string along with data values.

Syntax:

printf("String format string",variableName);

Example Program

#include<stdio.h>

#include<conio.h>

void main(){

int i = 10;

float x = 5.5;

printf("Integer value = %d, float value = %f",i, x);

Output:
In the above program, we are displaying string along with data values.

Every function in the C programming language must have a return value. The printf() function also have
an integer as a return value. The printf() function returns an integer value equivalent to the total
number of characters it has printed.

Example Program

#include<stdio.h>

#include<conio.h>

void main(){

int i;

i = printf("btechsmartclass");

printf(" is %d number of characters.",i);

Output:

In the above program, first printf() function printing "btechsmartclass" which is of 15 characters. So it
returns integer value 15 to the variable "i". The value of "i" is printed in the second printf() function.

Formatted printf() function


Generally, when we write multiple printf() statements the result is displayed in a single line because the
printf() function displays the output in a single line. Consider the following example program...

Example Program

#include<stdio.h>

#include<conio.h>

void main(){

printf("Welcome to ");

printf("btechsmartclass ");

printf("the perfect website for learning");

Output:

In the above program, there are 3 printf() statements written in different lines but the output is
displayed in single line only.

To display the output in different lines or as we wish, we use some special characters called escape
sequences. Escape sequences are special characters with special functionality used in printf() function to
format the output according to the user requirement. In the C programming language, we have the
following escape sequences...

Escape sequence Meaning

\n Moves the cursor to New Line

\t Inserts Horizontal Tab (5 characters space)


Escape sequence Meaning

\v Inserts Vertical Tab (5 lines space)

\a Beep sound

\b Backspace (removes the previous character from its current position)

\\ Inserts Backward slash symbol

\? Inserts Question mark symbol

\' Inserts Single quotation mark symbol

\" Inserts Double quotation mark symbol

Consider the following example program...

Example Program

#include<stdio.h>

#include<conio.h>

void main(){

printf("Welcome to\n");

printf("btechsmartclass\n");

printf("the perfect website for learning");

Output:
putchar() function

The putchar() function is used to display a single character on the output screen. The putchar() functions
prints the character which is passed as a parameter to it and returns the same character as a return
value. This function is used to print only a single character. To print multiple characters we need to write
multiple times or use a looping statement. Consider the following example program...

Example Program

#include<stdio.h>

#include<conio.h>

void main(){

char ch = 'A';

putchar(ch);

Output:
puts() function

The puts() function is used to display a string on the output screen. The puts() functions prints a string or
sequence of characters till the newline. Consider the following example program...

Example Program

#include<stdio.h>

#include<conio.h>

void main(){

char name[30];

printf("\nEnter your favourite website: ");

gets(name);

puts(name);

Output:

fprintf() function

The fprintf() function is used with the concept of files. The fprintf() function is used to print a line into
the file. When you want to use fprintf() function the file must be opened in writting mode.

C Input Functions
C programming language provides built-in functions to perform input operations. The input operations
are used to read user values (input) from the keyboard. The c programming language provides the
following built-in input functions.
1. scanf()

2. getchar()

3. getch()

4. gets()

5. fscanf()

scanf() function

The scanf() function is used to read multiple data values of different data types from the keyboard. The
scanf() function is built-in function defined in a header file called "stdio.h". When we want to use scanf()
function in our program, we need to include the respective header file (stdio.h)
using #include statement. The scanf() function has the following syntax...

Syntax:

scanf("format strings",&variableNames);

Example Program

#include<stdio.h>

#include<conio.h>

void main(){

int i;

printf("\nEnter any integer value: ");

scanf("%d",&i);

printf("\nYou have entered %d number",i);

Output:
In the above example program, we used the scanf() function to read an integer value from the keyboard
and store it into variable 'i'.

The scanf function also used to read multiple data values of different or the same data types. Consider
the following example program...

Example Program

#include<stdio.h>

#include<conio.h>

void main(){

int i;

float x;

printf("\nEnter one integer followed by one float value : ");

scanf("%d%f",&i, &x);

printf("\ninteger = %d, float = %f",i, x);

Output:
In the above example program, we used the scanf() function to read one integer value and one float
value from the keyboard. Here 'i' is an integer variable so we have used format string %d, and 'x' is a
float variable so we have used format string %f.

The scanf() function returns an integer value equal to the total number of input values read using scanf
function.

Example Program

#include<stdio.h>

#include<conio.h>

void main(){

int i,a,b;

float x;

printf("\nEnter two integers and one float : ");

i = scanf("%d%d%f",&a, &b, &x);

printf("\nTotal inputs read : %d",i);

Output:
getchar() function

The getchar() function is used to read a character from the keyboard and return it to the program. This
function is used to read a single character. To read multiple characters we need to write multiple times
or use a looping statement. Consider the following example program...

Example Program

#include<stdio.h>

#include<conio.h>

void main(){

char ch;

printf("\nEnter any character : ");

ch = getchar();

printf("\nYou have entered : %c\n",ch);

Output:
getch() function

The getch() function is similar to getchar function. The getch() function is used to read a character from
the keyboard and return it to the program. This function is used to read a single character. To read
multiple characters we need to write multiple times or use a looping statement. Consider the following
example program...

Example Program

#include<stdio.h>

#include<conio.h>

void main(){

char ch;

printf("\nEnter any character : ");

ch = getch();

printf("\nYou have entered : %c",ch);

Output:
gets() function

The gets() function is used to read a line of string and stores it into a character array. The gets() function
reads a line of string or sequence of characters till a newline symbol enters. Consider the following
example program...

Example Program

#include<stdio.h>

#include<conio.h>

void main(){

char name[30];

printf("\nEnter your favourite website: ");

gets(name);

printf("%s",name);

Output:
fscanf() function

The fscanf() function is used with the concept of files. The fscanf() function is used to read data values
from a file. When you want to use fscanf() function the file must be opened in reading mode.

C Operators

An operator is a symbol used to perform arithmetic and logical operations in a program. That means an
operator is a special symbol that tells the compiler to perform mathematical or logical operations. C
programming language supports a rich set of operators that are classified as follows.

1. Arithmetic Operators

2. Relational Operators

3. Logical Operators

4. Increment & Decrement Operators

5. Assignment Operators

6. Bitwise Operators

7. Conditional Operator

8. Special Operators

Arithmetic Operators (+, -, *, /, %)

The arithmetic operators are the symbols that are used to perform basic mathematical operations like
addition, subtraction, multiplication, division and percentage modulo. The following table provides
information about arithmetic operators.
Operator Meaning Example

+ Addition 10 + 5 =

- Subtraction 10 - 5 =

* Multiplication 10 * 5 =

/ Division 10 / 5 =

% Remainder of the Division 5%2=

⇒ The addition operator can be used with numerical data types and character data type. When it is used
with numerical values, it performs mathematical addition and when it is used with character data type
values, it performs concatination (appending).

⇒ The remainder of the division operator is used with integer data type only.

Relational Operators (<, >, <=, >=, ==, !=)

The relational operators are the symbols that are used to compare two values. That means the
relational operators are used to check the relationship between two values. Every relational operator
has two results TRUE or FALSE. In simple words, the relational operators are used to define conditions in
a program. The following table provides information about relational operators.

Operator Meaning Example

< Returns TRUE if the first value is smaller than second value otherwise returns FALSE 10 < 5 is

> Returns TRUE if the first value is larger than second value otherwise returns FALSE 10 > 5 is

<= Returns TRUE if the first value is smaller than or equal to second value otherwise returns FALSE 10 <= 5

>= Returns TRUE if the first value is larger than or equal to second value otherwise returns FALSE 10 >= 5

== Returns TRUE if both values are equal otherwise returns FALSE 10 == 5


Operator Meaning Example

!= Returns TRUE if both values are not equal otherwise returns FALSE 10 != 5 i

Logical Operators (&&, ||, !)

The logical operators are the symbols that are used to combine multiple conditions into one condition.
The following table provides information about logical operators.

Operator Meaning Example

&& Logical AND - Returns TRUE if all conditions are TRUE otherwise 10 < 5 && 12 > 10 is
returns FALSE FALSE

|| Logical OR - Returns FALSE if all conditions are FALSE otherwise 10 < 5 || 12 > 10 is
returns TRUE TRUE

! Logical NOT - Returns TRUE if condition is FLASE and returns !(10 < 5 && 12 > 10) is
FALSE if it is TRUE TRUE

⇒ Logical AND - Returns TRUE only if all conditions are TRUE, if any of the conditions is FALSE then
complete condition becomes FALSE.
⇒ Logical OR - Returns FALSE only if all conditions are FALSE, if any of the conditions is TRUE then
complete condition becomes TRUE.

Increment & Decrement Operators (++ & --)

The increment and decrement operators are called unary operators because both need only one
operand. The increment operators adds one to the existing value of the operand and the decrement
operator subtracts one from the existing value of the operand. The following table provides information
about increment and decrement operators.

Operator Meaning Example

++ Increment - Adds one to existing value int a = 5;


a++; ⇒ a = 6

-- Decrement - Subtracts one from existing value int a = 5;


a--; ⇒ a = 4
The increment and decrement operators are used infront of the operand (++a) or after the operand
(a++). If it is used infront of the operand, we call it as pre-increment or pre-decrement and if it is used
after the operand, we call it as post-increment or post-decrement.

Pre-Increment or Pre-Decrement

In the case of pre-increment, the value of the variable is increased by one before the expression
evaluation. In the case of pre-decrement, the value of the variable is decreased by one before the
expression evaluation. That means, when we use pre-increment or pre-decrement, first the value of the
variable is incremented or decremented by one, then the modified value is used in the expression
evaluation.

Example Program

#include<stdio.h>

#include<conio.h>

void main(){

int i = 5,j;

j = ++i; // Pre-Increment

printf("i = %d, j = %d",i,j);

Output:

Post-Increment or Post-Decrement
In the case of post-increment, the value of the variable is increased by one after the expression
evaluation. In the case of post-decrement, the value of the variable is decreased by one after the
expression evaluation. That means, when we use post-increment or post-decrement, first the expression
is evaluated with existing value, then the value of the variable is incremented or decremented by one.

Example Program

#include<stdio.h>

#include<conio.h>

void main(){

int i = 5,j;

j = i++; // Post-Increment

printf("i = %d, j = %d",i,j);

Output:

Assignment Operators (=, +=, -=, *=, /=, %=)

The assignment operators are used to assign right-hand side value (Rvalue) to the left-hand side variable
(Lvalue). The assignment operator is used in different variants along with arithmetic operators. The
following table describes all the assignment operators in the C programming language.
Operator Meaning

= Assign the right-hand side value to left-hand side variable

+= Add both left and right-hand side values and store the result into left-hand side variable

-= Subtract right-hand side value from left-hand side variable value and store the result
into left-hand side variable

*= Multiply right-hand side value with left-hand side variable value and store the result
into left-hand side variable

/= Divide left-hand side variable value with right-hand side variable value and store the result
into the left-hand side variable

%= Divide left-hand side variable value with right-hand side variable value and store the remainder
into the left-hand side variable

Bitwise Operators (&, |, ^, ~, >>, <<)

The bitwise operators are used to perform bit-level operations in the c programming language. When
we use the bitwise operators, the operations are performed based on the binary values. The following
table describes all the bitwise operators in the C programming language.
Let us consider two variables A and B as A = 25 (11001) and B = 20 (10100).

Operator Meaning

& the result of Bitwise AND is 1 if all the bits are 1 otherwise it is 0

| the result of Bitwise OR is 0 if all the bits are 0 otherwise it is 1

^ the result of Bitwise XOR is 0 if all the bits are same otherwise it is 1
Operator Meaning

~ the result of Bitwise once complement is negation of the bit (Flipping)

<< the Bitwise left shift operator shifts all the bits to the left by the specified number of positions

>> the Bitwise right shift operator shifts all the bits to the right by the specified number of positions

Conditional Operator (?:)

The conditional operator is also called a ternary operator because it requires three operands. This
operator is used for decision making. In this operator, first we verify a condition, then we perform one
operation out of the two operations based on the condition result. If the condition is TRUE the first
option is performed, if the condition is FALSE the second option is performed. The conditional operator
is used with the following syntax.

Condition ? TRUE Part : FALSE Part;

Example

A = (10<15)?100:200; ⇒ A value is 100

Special Operators (sizeof, pointer, comma, dot, etc.)

The following are the special operators in c programming language.

sizeof operator

This operator is used to find the size of the memory (in bytes) allocated for a variable. This operator is
used with the following syntax.

sizeof(variableName);

Example

sizeof(A); ⇒ the result is 2 if A is an integer

Pointer operator (*)

This operator is used to define pointer variables in c programming language.

Comma operator (,)


This operator is used to separate variables while they are declaring, separate the expressions in function
calls, etc.

Dot operator (.)

This operator is used to access members of structure or union.

You might also like