Unit 2
Unit 2
C Language Introduction
C is a procedural programming language initially developed by Dennis Ritchie in
the year 1972 at Bell Laboratories of AT&T Labs. It was mainly developed as a
system programming language to write the UNIX operating system.
Newer languages like Java, python offer more features than c programming
language but due to additional processing in these languages, their
performance rate gets down effectively.
C programming language as the middle-level language provides
programmers access to direct manipulation with the computer hardware but
higher-level languages do not allow this.
That’s one of the reasons C language is considered the first choice to start
learning programming languages. It’s fast because statically typed languages
are faster than dynamically typed languages.
3. Modularity
4. Statically Type
5. General-Purpose Language
Robust libraries and functions in C help even a beginner coder to code with
ease.
8. Middle-Level Language
9. Portability
1. Documentation
This section consists of the description of the program, the name of the
program, and the creation date and time of the program. \
It is specified at the start of the program in the form of comments.
Documentation can be represented as:
// description, name of the program, programmer name, date, time etc.
or
/*
description, name of the program, programmer name, date, time etc.
*/
Anything written as comments will be treated as documentation of the program
and this will not interfere with the given code. Basically, it gives an overview to
the reader of the program.
2. Preprocessor Section
3. Definition
Preprocessors are the programs that process our source code before the
process of compilation.
There are multiple steps which are involved in the writing and execution of
the program.
Preprocessor directives start with the ‘#’ symbol. The #define preprocessor
is used to create a constant throughout the program.
Whenever this name is encountered by the compiler, it is replaced by the
actual piece of defined code.
Example:
#define PI 3.142
4. Global Declaration
5. Main() Function
Every C program must have a main function. The main() function of the
program is written in this section.
Operations like declaration and execution are performed inside the curly
braces of the main program.
The return type of the main() function can be int as well as void too.
void() main tells the compiler that the program will not return any value.
The int main() tells the compiler that the program will return an integer
value.
Example:
void main()
or
int main()
6. Sub Programs
Every file that contains a C program must be saved with ‘.c’ extension.
This is necessary for the compiler to understand that this is a C program file.
Suppose a program file is named, first.c. The file first.c is called the source
file which keeps the code of the program.
Now, when we compile the file, the C compiler looks for errors. If the C
compiler reports no error, then it stores the file as a .obj file of the same name,
called the object file. So, here it will create the [Link]. This .obj file is not
executable.
The process is continued by the Linker which finally gives a .exe file which is
executable.
Linker:
First of all, let us know that library functions are not a part of any C program
but of the C software.
Thus, the compiler doesn’t know the operation of any function, whether it be
printf or scanf.
The definitions of these functions are stored in their respective library which
the compiler should be able to link.
This is what the Linker does. So, when we write #include, it includes stdio.h
library which gives access to Standard Input and Output. The linker links the
object files to the library functions and the program becomes a .exe file. Here,
[Link] will be created which is in an executable format.
Loader:
Whenever we give the command to execute a particular program, the loader
comes into work.
The loader will load the .exe file in RAM and inform the CPU with the
starting point of the address where this program is loaded.
CPU Registers
Types of Tokens in C
The tokens of C language can be classified into six types based on the functions
they are used to perform. The types of C tokens are as follows:
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Special Symbols
6. Operators
1. Keywords
Definition:
The keywords are pre-defined or reserved words in a programming language. Each
keyword is meant to perform a specific function in a program.
Since keywords are referred names for a compiler, they can’t be used as
variable names because by doing so, we are trying to assign a new meaning to
the keyword which is not allowed.
You cannot redefine keywords. However, you can specify the text to be
substituted for keywords before compilation by using C preprocessor
directives. C language supports 32 keywords which are given below:
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Note: The number of keywords may change depending on the version of C you are
using. For example, keywords present in ANSI C are 32 while in C11, it was
increased to 44. Moreover, in the latest c23, it is increased to around 54.
[Link]
Definition:
Identifiers are used as the general terminology for the naming of variables,
functions, and arrays.
These are user-defined names consisting of an arbitrarily long sequence of
letters and digits with either a letter or the underscore(_) as a first character.
Identifier names must differ in spelling and case from any keywords.
You cannot use keywords as identifiers; they are reserved for special use.
Once declared, you can use the identifier in later program statements to refer
to the associated value.
Rules for Naming Identifiers
Certain rules should be followed while naming c identifiers which are as follows:
They must begin with a letter or underscore(_).
They must consist of only letters, digits, or underscore. No other special
character is allowed.
It should not be a keyword.
It must not contain white space.
It should be up to 31 characters long as only the first 31 characters are
significant.
Note: Identifiers are case-sensitive so names like variable and Variable will be
treated as different.
For example,
main: method name.
a: variable name.
3. Constants
Definition:
The constants refer to the variables with fixed values. They are like normal
variables but with the difference that their values cannot be modified in the
program once they are defined.
Constants may belong to any of the data types.
Examples of Constants in C
const int c_var = 20;
float x=567;
4. Strings
Strings are nothing but an array of characters ended with a null character (‘\0’).
This null character indicates the end of the string. Strings are always enclosed in
double quotes. Whereas, a character is enclosed in single quotes in C and C++.
Examples of String
char string[20] = {‘g’, ’e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘g’, ’e’, ‘e’, ‘k’, ‘s’, ‘\0’};
char string[20] = “geeksforgeeks”;
char string [] = “geeksforgeeks”;
5. Special Symbols
The following special symbols are used in C having some special meaning and
thus, cannot be used for some other purpose. Some of these are listed below:
Brackets[]: Opening and closing brackets are used as array element references.
These indicate single and multidimensional subscripts.
Parentheses(): These special symbols are used to indicate function calls and
function parameters.
Braces{}: These opening and ending curly braces mark the start and end of a
block of code containing more than one executable statement.
Comma (, ): It is used to separate more than one statement like for separating
parameters in function calls.
Colon(:): It is an operator that essentially invokes something called an
initialization list.
Semicolon(;): It is known as a statement terminator. It indicates the end of one
logical entity. That’s why each individual statement must be ended with a
semicolon.
Asterisk (*): It is used to create a pointer variable and for the multiplication of
variables.
Assignment operator(=): It is used to assign values and for logical operation
validation.
Pre-processor (#): The preprocessor is a macro processor that is used
automatically by the compiler to transform your program before actual
compilation.
Period (.): Used to access members of a structure or union.
Tilde(~): Bitwise One’s Complement Operator.
[Link]:
Definition:
Operators are symbols that trigger an action when applied to C variables and other
objects. The data items on which operators act are called operands.
Depending on the number of operands that an operator can act upon, operators can
be classified as follows:
Unary Operators: Those operators that require only a single operand to act
upon are known as unary operators.
For Example increment and decrement operators
Binary Operators: Those operators that require two operands to act upon are
called binary operators. Binary operators can further are classified into:
1. Arithmetic operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Bitwise Operator
1. Arithmetic Operations in C
The arithmetic operators are used to perform arithmetic/mathematical operations
on operands. There are 9 arithmetic operators in C language:
Operator Description
S. No. Symbol Syntax
Subtracts right
– Minus operand from left a–b
operand.
2
Multiply two
* Multiply numeric values. a*b
3
numeric values.
Returns the
remainder after
% Modulus diving the left a%b
operand with the
right operand.
5
Used to specify
+ Unary Plus the positive +a
values.
6
Increases the
++ Increment value of the a++
operand by 1.
8
Decreases the
— Decrement value of the a–
operand by 1.
9
Example of C Arithmetic Operators
C
// C program to illustrate the arithmatic operators
#include <stdio.h>
#include<conio.h>
void main()
{
int a = 25, b = 5;
clrscr();
getch0;
}
Output
a + b = 30
a - b = 20
a * b = 125
a/b=5
a%b=0
+a = 25
-a = -25
a++ = 25
a-- = 26
2. Relational Operators in C
The relational operators in C are used for the comparison of the two operands. All
these operators are binary operators that return true or false values as the result of
comparison.
These are a total of 6 relational operators in C:
Operator Description
S. No. Symbol Syntax
Operator Description
S. No. Symbol Syntax
Returns true if
the left
operand is less
< Less than a<b
than the right
operand. Else
false
1
Returns true if
the left
operand is
> Greater than greater than a>b
the right
operand. Else
false
2
Returns true if
the left
operand is less
Less than or
<= than or equal a <= b
equal to
to the right
operand. Else
false
3
Returns true if
the left
operand is
Greater than
>= greater than or a >= b
or equal to
equal to right
operand. Else
false
4
Operator Description
S. No. Symbol Syntax
Returns true if
both the
== Equal to a == b
operands are
equal.
5
Returns true if
both the
!= Not equal to a != b
operands are
NOT equal.
6
Example of C Relational Operators
C
// C program to illustrate the relational operators
#include <stdio.h>
#include<conio.h>
void main()
{
int a = 25, b = 5;
clrscr();
// using operators and printing results
printf("a < b : %d\n", a < b);
printf("a > b : %d\n", a > b);
printf("a <= b: %d\n", a <= b);
printf("a >= b: %d\n", a >= b);
printf("a == b: %d\n", a == b);
printf("a != b : %d\n", a != b);
getch();
}
Output
a<b :0
a>b :1
a <= b: 0
a >= b: 1
a == b: 0
a != b : 1
3. Logical Operator in C
Logical Operators are used to combine two or more conditions/constraints or to
complement the evaluation of the original condition in consideration. The result of
the operation of a logical operator is a Boolean value either true or false.
Operator Description
S. No. Symbol Syntax
Returns true if
both the
&& Logical AND a && b
operands are
true.
1
Returns true if
both or any of
|| Logical OR a || b
the operand is
true.
2
Returns true if
! Logical NOT the operand is !a
false.
3
Example of Logical Operators in C
C
// C program to illustrate the logical operators
#include <stdio.h>
#include<conio.h>
void main()
{
int a = 25, b = 5;
clrscr();
// using operators and printing results
printf("a && b : %d\n", a && b);
printf("a || b : %d\n", a || b);
printf("!a: %d\n", !a);
getch();
}
Output
a && b : 1
a || b : 1
!a: 0
4. Bitwise Operators in C
The Bitwise operators are used to perform bit-level operations on the operands.
The operators are first converted to bit-level and then the calculation is performed
on the operands. Mathematical operations such as addition, subtraction,
multiplication, etc. can be performed at the bit level for faster processing.
There are 6 bitwise operators in C:
Operator Description
S. No. Symbol Syntax
Performs bit-
by-bit AND
& Bitwise AND operation and a&b
returns the
result.
1
Performs bit-
by-bit OR
| Bitwise OR operation and a|b
returns the
result.
2
Operator Description
S. No. Symbol Syntax
Performs bit-
by-bit XOR
^ Bitwise XOR operation and a^b
returns the
result.
3
Shifts the
number in
binary form
Bitwise by one place
<< a << b
Leftshift in the
operation and
returns the
result.
5
Shifts the
number in
binary form
Bitwise by one place
>> a >> b
Rightshilft in the
operation and
returns the
result.
6
Example of Bitwise Operators
C
// C program to illustrate the bitwise operators
#include <stdio.h>
void main()
{
getch();
}
Output
a & b: 1
a | b: 29
a ^ b: 28
~a: -26
c >> : 2
c << b: 40
5. Assignment Operators in C
Assignment operators are used to assign value to a variable.
The left side operand of the assignment operator is a variable and the right
side operand of the assignment operator is a value.
The value on the right side must be of the same data type as the variable on
the left side otherwise the compiler will raise an error.
The assignment operators can be combined with some other operators in C to
provide multiple operations using single operator. These operators are called
compound operators.
In C, there are 11 assignment operators :
Operator Description
S. No. Symbol Syntax
Assign the
value of the
Simple
= right operand a=b
Assignment
to the left
operand.
1
Subtract the
right operand
and left
Minus and
-= operand and a -= b
assign
assign this
value to the
left operand.
3
Multiply the
right operand
and left
Multiply and
*= operand and a *= b
assign
assign this
value to the
left operand.
4
the right
operand and
assign this
value to the
left operand.
Assign the
remainder in
the division of
Modulus and
%= left operand a %= b
assign
with the right
operand to the
left operand.
6
Performs
bitwise AND
AND and and assigns
&= assign this value to a &= b
the left
operand.
7
Performs
bitwise OR
OR and and assigns
|= assign this value to a |= b
the left
operand.
8
this value to
the left
operand.
getch();
}
Output
a = b: 5
a += b: 10
a -= b: 5
a *= b: 25
a /= b: 5
a %= b: 0
a &= b: 0
a |= b: 5
6. Other Operators
Apart from the above operators, there are some other operators available in C used
to perform some specific tasks. Some of them are discussed here:
i. sizeof Operator
sizeof is much used in the C programming language.
It is a compile-time unary operator which can be used to compute the size of its
operand.
The result of sizeof is of the unsigned integral type which is usually denoted by
size_t.
Basically, the sizeof the operator is used to compute the size of the variable or
datatype.
Syntax
sizeof (operand)
7. Variables
Definition:
A variable in C language is the name associated with some memory location
to store data of different types.
There are many types of variables in C depending on the scope, storage class,
lifetime, type of data they store, etc.
A variable is the basic building block of a C program that can be used in
expressions as a substitute in place of the value it stores.
Syntax
The syntax to declare a variable in C specifies the name and the type of the
variable.
data_type variable_name = value; // defining single variable
or
data_type variable_name1, variable_name2; // defining multiple variable
Here,
data_type: Type of data that a variable can store.
variable_name: Name of the variable given by the user.
value: value assigned to the variable by the user.
Example
int var; // integer variable
char a; // character variable
float fff; // float variables
Note: C is a strongly typed language so all the variables types must be specified
before using them.
1. Variable Declaration
Variable declaration in C tells the compiler about the existence of the variable with
the given name and data [Link] the variable is declared, an entry in symbol
table is created and memory will be allocated at the time of initialization of the
variable.
2. C Variable Definition
In the definition of a C variable, the compiler allocates some memory and some
value to it. A defined variable will contain some random garbage value till it is not
initialized.
Example
int var;
char var2;
Note: Most of the modern C compilers declare and define the variable in single
step. Although we can declare a variable in C by using extern keyword, it is not
required in most of the cases. To know more about variable declaration and
definition, click here.
3. Variable Initialization
Initialization of a variable is the process where the user assigns some meaningful
value to the variable when creating the variable.
Example
int var = 10; // variable declaration and definition (i.e. Vairable Initialization)
// assignment
defined_var = 12;
getch();
}
Output
Defined_var: 0
Value of defined_var after assignment: 12
Value of ini_var: 25
Variable Types
The C variables can be classified into the following types:
1. Local Variables
2. Global Variables
1. Local Variables in C:
A Local variable in C is a variable that is declared inside a function or a
block of code.
Its scope is limited to the block or function in which it is declared.
Example of Local Variable in C
C
// C program to declare and print local variable inside a function.
#include <stdio.h>
#include<conio.h>
void demo()
{
int x = 10; // local variable
printf("%d", x);
}
void main()
{
demo();
getch();
}
Output
10
In the above code, x can be used only in the scope of function(). Using it in the
main function will give an error.
2. Global Variables in C
A Global variable in C is a variable that is declared outside the function or a
block of code.
Its scope is the whole program i.e. we can access the global variable anywhere
in the C program after it is declared.
Example of Global Variable in C
C
// C program to demonstrate use of global variable
#include <stdio.h>
#include<conio.h>
int x = 20; // global variable
void function1()
{
printf("Function 1: %d\n", x);
}
void function2()
{
printf("Function 2: %d\n", x);
}
void main()
{
clrscr();
function1();
function2();
getch();
}
Output
Function 1: 20
Function 2: 20
Data Types in C
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.
Let's see the basic data types. Its size is given according to 32-bit architecture.
Int:
Integers are entire numbers without any fractional or decimal parts, and
the int data type is used to represent them.
It is frequently applied to variables that include values, such as counts,
indices, or other numerical numbers.
The int data type may represent both positive and negative numbers because
it is signed by default.
An int takes up 4 bytes of memory on most devices, allowing it to store values
between around -2 billion and +2 billion.
Char:
Float:
To represent integers, use the floating data type.
Floating numbers can be used to represent fractional units or numbers with
decimal places.
The float type is usually used for variables that require very good precision
but may not be very precise.
It can store values with an accuracy of about 6 decimal places and a range of
about 3.4 x 1038 in 4 bytes of memory.
Double:
Use two data types to represent two floating integers. When additional
precision is needed, such as in scientific calculations or financial applications,
it provides greater accuracy compared to float.
Double type, which uses 8 bytes of memory and has an accuracy of about 15
decimal places, yields larger values. C treats floating point numbers as
doubles by default if no explicit type is supplied.
In the example above, we declare four variables: an int variable for the person's
age, a char variable for the student's grade, a float variable for the temperature
reading, and two variables for the number pi.
Array:
Definition:
Syntax:
Data_type var[size];
#include <stdio.h>
#include<conio.h>
void main()
{
int numbers[5]; // Declares an integer array with a size of 5 elements
getch();
}
Output:
Values in the array: 10 20 30 40 50
Pointer:
A pointer is a derived data type that keeps track of another data type's
memory address.
When a pointer is declared, the data type it refers to is stated first, and then
the variable name is preceded by an asterisk (*).
Syntax:
Data_type *ptr_var;
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,*ptr;
a=20;
clrscr();
ptr=&a;
printf(“Values of ptr=%d\n”,*ptr);
getch();
}
Structure:
A structure is a derived data type that enables the creation of composite data types
by allowing the grouping of many data types under a single name. It gives you the
ability to create your own unique data structures by fusing together variables of
various sorts.
1. A structure's members or fields are used to refer to each variable within it.
2. Any data type, including different structures, can be a member of a structure.
3. A structure's members can be accessed by using the dot (.) operator.\
Syntax:
struct Structure_name
Data_type var1,var2;
}structure_variable;
Example:
#include<stdio.h>
#include<conio.h>
struct add
{
int a,b;
}s;
void main()
{
int sum;
s.a=10;
s.b=20;
clrscr();
sum=s.a+s.b;
printf("Addition of two number=%d\n",sum);
getch();
}
Union:
A derived data type called a union enables you to store various data types in
the same memory address.
In contrast to structures, where each member has a separate memory space,
members of a union all share a single memory space.
A value can only be held by one member of a union at any given moment.
When you need to represent many data types interchangeably, unions come in
handy.
Like structures, you can access the members of a union by using the dot
(.) operator.
Syntax:
union Union_name
Data_type var1,var2…..varn;
}union_variable;
Example:
#include<stdio.h>
#include<conio.h>
unio add
{
int a,b;
}u;
void main()
{
int sum;
u.a=10;
u.b=20;
clrscr();
sum=u.a+u.b;
printf("Addition of two number=%d\n",sum);
getch();
}
enum Enum_name
Values;
};
Here is an example of how to define and use an enumeration in C:
#include <stdio.h>
#include<conio.h>
// Define an enumeration for days of the week
enum DaysOfWeek
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
};
void main()
{
// Declare a variable of type enum DaysOfWeek
enum DaysOfWeek today;
getch();
}
Output:
Today is 2
void Data Type:
The void data type in the C language is used to denote the lack of a particular
type.
Function return types, function parameters, and pointers are three situations
where it is frequently utilized.
A void function executes a task or action and ends rather than returning a value.
The parameter void can be used to indicate that a function accepts no arguments.
Example:
Syntax:
#define const_name value
Example:
// C Program to define a constant using #define
#include <stdio.h>
#define pi 3.142
void main()
{
Formatted I/O functions are used to take various inputs from the user and
display multiple outputs to the user.
These types of I/O functions can help to display the output to the user in
different formats using the format specifiers.
These I/O supports all data types like int, float, char, and many more.
int/signed
1 %d used for I/O signed integer value
int
5 %ld long int Used for I/O long signed integer value
unsigned
6 %u Used for I/O unsigned integer value
int
1. printf():
printf() function is used in a C program to display any value like float, integer,
character, string, etc on the console screen. It is a pre-defined function that is
already declared in the stdio.h(header file).
Syntax 1:
printf(“Format Specifier”, var1, var2, …., varn);
Example:
// C program to implement printf() function
#include <stdio.h>
#include<conio.h>
void main()
int a;
a = 20;
printf("%d", a);
getch();
Output
20
2. scanf():
scanf() function is used in the C program for reading or taking any value
from the keyboard by the user, these values can be of any data type like
integer, float, character, string, and many more.
This function is declared in stdio.h(header file), that’s why it is also a pre-
defined function.
In scanf() function we use &(address-of operator) which is used to store the
variable value on the memory location of that variable.
Syntax:
scanf(“Format Specifier”, &var1, &var2, …., &varn);
Example:
#include <stdio.h>
#include<conio.h>
void main()
int num1;
scanf("%d", &num1);
getch();
Output
Enter a integer number: You have entered 0
Output:
Enter a integer number: 56
You have entered 56
1. getch():
getch() function reads a single character from the keyboard by the user but doesn’t
display that character on the console screen and immediately returned without
pressing enter key. This function is declared in conio.h(header file). getch() is also
used for hold the screen.
Syntax:
getch();
or
Example:
C
#include <conio.h>
#include <stdio.h>
void main()
getch();
Output:
Enter any character:
2. getchar():
The getchar() function is used to read only a first single character from the
keyboard whether multiple characters is typed by the user and this function
reads one character at one time until and unless the enter key is pressed. This
function is declared in stdio.h(header file)
Syntax:
Variable-name = getchar();
Example:
C
#include <conio.h>
#include <stdio.h>
void main()
{
// Declaring a char type variable
char ch;
clrscr();
ch = getchar();
printf("%c", ch);
getch();
Output:
Enter the character: a
a
3. putchar():
The putchar() function is used to display a single character at a time by passing
that character directly to it or by passing a variable that has already stored a
character. This function is declared in stdio.h(header file)
Syntax:
putchar(variable_name);
Example:
C
#include <conio.h>
#include <stdio.h>
void main()
char ch;
// Reads a character
ch = getchar();
putchar(ch);
getch();
Output:
Enter any character: Z
Z
4. gets():
gets() function reads a group of characters or strings from the keyboard by
the user and these characters get stored in a character array.
This function allows us to write space-separated texts or strings. This
function is declared in stdio.h(header file).
Syntax:
char str[length of string in number]; //Declare a char type variable of any
length
gets(str);
Example:
// C program to implement the gets() function
#include <conio.h>
#include <stdio.h>
void main()
char name[50];
gets(name);
getch();
Output:
Please enter some texts: Welcome to the class
You have entered: Welcome to the class
5. puts():
In C programming puts() function is used to display a group of characters or
strings which is already stored in a character array.
This function is declared in stdio.h(header file).
Syntax:
puts(identifier_name );
Example:
#include <stdio.h>
#include<conio.h>
void main()
char name[50];
gets(name);
// Displays string
puts(name);
getch();
Output:
Enter your text: Welcome to the class
Your text is: Welcome to the class
Escape Sequence in C
The escape sequence in C is the characters or the sequence of characters
that can be used inside the string literal.
The purpose of the escape sequence is to represent the characters that
cannot be used normally using the keyboard. Some escape sequence
characters are the part of ASCII charset but some are not.
Different escape sequences represent different characters but the output is
dependent on the compiler you are using.
Escape Sequence List
The table below lists some common escape sequences in C language.
Escape
Sequence Name Description
\n New Line It moves the cursor to the start of the next line.
Hexadecimal
\xhh It represents the hexadecimal number.
Number
Out of all these escape sequences, \n and \0 are used the most. In fact, escape
sequences like \f, \a, are not even used by programmers nowadays.
Escape Sequence in C Examples
The following are the escape sequence examples that demonstrate how to use
different escape sequences in C language.
1. Example to demonstrate how to use \a escape sequence in C
// C program to illustrate \a escape sequence
#include <stdio.h>
void main()
{
clrscr();
// output may depend upon the compiler
printf("My mobile number "
"is 7\a8\a7\a3\a9\a2\a3\a4\a0\a8\a");
getch();
}
Output
My mobile number is 7873923408
2. Example to demonstrate how to use \b escape sequence in C
C
// C program to illustrate \b escape sequence
#include <stdio.h>
#include<conio.h>
void main()
{
clrscr();
// \b - backspace character transfers the cursor one character back with or
without //deleting on different compilers.
printf("Hello \b\b\b\b\b\bHi Hello");
getch();
}
Output
Hello Hi Hello
3. Example to demonstrate how to use \n escape sequence in C
Output
Hello Hiiii
The escape sequence “\t” is very frequently used in loop-based pattern printing
programs.
printf("Welcome to class");
getch ();
}
Output
Hello friends
Welcome to GFG
6. Example to demonstrate how to use \r escape sequence in C
Output
WelcomeHello Hii
7. Example to demonstrate how to use \\ escape sequence in C
// C program to illustrate \\(Backslash)
// escape sequence to print backslash.
#include <stdio.h>
#include<conio.h>
void main()
{
// Here we are using \,It contains two escape sequence means \ and \n.
printf("Hello\\Hi");
getch ();
}
Output
Hello\Hi
Explanation: It contains two ‘\’ which means we want print ‘\’ as output.
Output
' Hello Hi
" Hello Hi
9. Example to demonstrate how to use \? escape sequence in C
// C program to illustrate
// \? escape sequence
#include <stdio.h>
int main(void)
{
// Here we are using \?, which is
// used for the presentation of trigraph
// in the early of C programming. But
// now we don't have any use of it.
printf("\?\?!\n");
return 0;
}
Output
??!
10. Example to demonstrate how to use \ooo escape sequence in C
// C program to illustrate \OOO escape sequence
#include <stdio.h>
int main(void)
{
// we are using \OOO escape sequence, here
// each O in "OOO" is one to three octal
// digits(0....7).
char* s = "A\072\065";
printf("%s", s);
return 0;
}
Output
A:5
Explanation: Here 000 is one to three octal digits(0….7) means there must be at
least one octal digit after \ and a maximum of three. Here 072 is the octal notation,
first, it is converted to decimal notation which is the ASCII value of char ‘:’. At the
place of \072, there is: and the output is A:5.
Output
BJ
computational problems :
Operator
Precedence Description Associativity
. Dot Operator
* Dereference Operator
than or equal to
12 || Logical OR Left-to-Right
= Assignment
Multiplication, division
*= , /=
assignment
void main()
{
// Verifying the result of the same expression
Clrscr();
printf("100 / 5 % 2 = %d", 100 / 5 % 2);
getch();
}
Output
100 / 5 % 2 = 0
Type Conversion in
Definition:
Type conversion in C is the process of converting one data type to another.
The type conversion is only performed to those data types where conversion
is possible. Type conversion is performed by a compiler.
In type conversion, the destination data type can’t be smaller than the source
data type.
Type conversion is done at compile time and it is also called widening
conversion because the destination data type can’t be smaller than the source
data type.
There are two types of Conversion:
1. Implicit Type Conversion/Widening type casting:
Also known as ‘automatic type conversion’.
A. Done by the compiler on its own, without any external trigger from the user.
B. Generally takes place when in an expression more than one data type is present.
In such conditions type conversion (type promotion) takes place to avoid loss of
data.
C. All the data types of the variables are upgraded to the data type of the variable
with the largest data type.
bool -> char -> short int -> int ->
unsigned int -> long -> unsigned ->
long long -> float -> double -> long double
D. It is possible for implicit conversions to lose information, signs can be lost
(when signed is implicitly converted to unsigned), and overflow can occur (when
long is implicitly converted to float).
#include <stdio.h>
#include<conio.h>
void main()
x = x + y;
getch();
Output
x = 107
This process is also called type casting and it is user-defined. Here the user can
typecast the result to make it of a particular data type.
The syntax in C Programming:
(type) expression
Type indicated the data type to which the final result is converted.
Example no 2
C
#include<stdio.h>
#include<conio.h>
void main()
double x = 1.2;
clrscr();
getch();
Output
sum = 2