Unit 2 Notes
Unit 2 Notes
Unit 2 Notes
____________________________________________________________________________
Structure of C programs, keywords and identifiers, constants, variables, Data types,
enumerated data types, Strings. Declarations of variables, scope and life of variables.
Various types of operators and expressions. Programming errors and their handling.
____________________________________________________________________________
Computer Programming
A computer program is a list of instructions that enable a computer to perform a specific task.
Computer programs can be written in high and low level languages, depending on the task and
the hardware being used.
The instructions that tell a computer what to do are written in machine code. Machine code is a
series of numbers written in binary. Each number represents a different instruction.
Programmers find machine code difficult to learn, program in and debug. As a result, the
majority of programmers write programs in high-level programming languages. These languages
are close to natural language - the spoken and written language of humans.
For example, Python uses 'print', ‘if’, 'input' and 'while' statements - all words from the English
language - to form instructions. In fact, instructions often look like abbreviated English
sentences.
A high level programming language is a language that allows you to tell a computer to do
something, but in a syntax that is easy and intuitive for you to understand. It is a totally different
language from what a computer understands, ie. The Machine Language.
Compare this simple C program with its comments that are written in English:
Programmers write in high-level languages because they are easier to understand and are less
complex than machine code. They allow the programmer to focus on what needs to be done,
rather than on how the computer actually works.
For example, in many high-level languages, to place a message on the screen, a programmer
would use the statement 'print'. The programmer might not know how the computer actually
generates the message. They just need to know how to use the 'print' statement.
Many types of high-level language exist and are in common use today, including:
●C
● Python
● Java
● C++
● C#
● Visual Basic
● JavaScript
Source code
Source code is the term given to a set of instructions that are written in human readable
programming language. Source code must be translated into machine code before a computer
can understand and execute it.
The low-level programming language provides little or no abstraction. The low-level language is
the machine-dependent language. We can say that the low level of language is near to the
machine and far away from the programmer. The execution time of the low-level language is
very fast, and it is the advantage of the low-level language.
Low-level languages are languages that sit close to the computer's instruction set. An instruction
set is the set of instructions that the processor understands.
machine code = the actual Binary language, sequence of 0s and 1s that the computer
understands. Eg. 01010 10101110010
assembly language = It is the low-level language for microprocessors and other programmable
devices that uses codes known as mnemonics to represent the machine language instructions.
Machine Code
Machine code is the set of instructions that a CPU understands directly and can act upon. A
program written in machine code would consist of only 0s and 1s - binary. This is very difficult to
write and debug. Even a very simple program could have thousands of 0s and 1s in it.
It is a hardware dependent language. Each processor has kept its own instruction set, and these
instructions are the patterns of bits. There is the class of processors using the same structure,
which is specified as an instruction set — any instruction can be divided into two parts: the
operator or opcode and operand.
The starting bits are known as the operator or opcode whose role is to identify the kind of
operation that is required to be performed. The remaining bits are called operand, whose
purpose is to show the location of activity.
The machine-level language or binary language can be executed very fast because it is not
required to translate from one language to another. It can run directly.
Limitations of machine language
● Machine dependent.
● Difficult to program.
● High error level when we write a program in this language.
● Difficult to modify the code.
Assembly language
Assembly language sits between machine code and high-level language in terms of ease of use.
While high-level languages use statements to form instructions, assembly language uses
mnemonics - short abbreviations. Each mnemonic directly corresponds with a machine code
instruction. Here are some examples of mnemonics:
Mnemonic Action
ADD Adds the value held in a memory address to the value held in the accumulator
SUB Subtracts from the accumulator the value held in a memory address
The programs are mainly written in a high-level language like c, python, java, etc. That high-level
language code is known as source code. This source code is never executed directly by the
computer; it must be converted into the machine language to be performed, so that we need the
program translator softwares for this purpose.
ADD #10 0010 00001010 Add the number 10 to the loaded address
Both opcode and operand values are ultimately represented in binary. However, values may also
be represented in hexadecimal when programming as this number system can represent larger
values in fewer characters, for example, denary 250 is 11111010 in binary, but only FA in
hexadecimal. Hexadecimal is therefore easier to read and understand by humans.
Translators
Any program written in a high-level language is known as source code. However, computers
cannot understand source code. Before it can be run, source code must first be translated into a
form which a computer understands.
A translator is a program that converts source code into machine code. Generally, there are
three types of translator:
● compilers
● interpreters
● assemblers
Compilers
A compiler takes the source code as a whole and translates it into machine code all in one go.
Once converted, the object code can be run unassisted at any time. This process is called
compilation.
Interpreters
An interpreter translates source code into machine code one instruction at a time. It is similar to
a human translator translating what a person says into another language, sentence by sentence,
as they speak. The resulting machine code is then executed immediately. The process is called
interpretation.
Assemblers
Assemblers are a third type of translator. The purpose of an assembler is to translate assembly
language into machine code.
Whereas compilers and interpreters generate many machine code instructions for each
high-level instruction, assemblers create one machine code instruction for each assembly
instruction.
C programming language is a MUST for students and working professionals to become a great
Software Engineer specially when they are working in Software Development Domain. I will list
down some of the key advantages of learning C Programming:
● Easy to learn
● Structured language
● It produces efficient programs
● It can handle low-level activities
● It can be compiled on a variety of computer platforms
Structure of a C Program:
Consider the simple “Hello World” Program in C:
In the C Programming Language, the #include directive tells the preprocessor to insert the
contents of another file into the source code at the point where the #include directive is found.
Include directives are typically used to include the C header files for C functions that are held
outside of the current source file.
#include <header_file>
In C language, header files contain the set of predefined standard library functions. The
“#include” preprocessing directive is used to include the header files with “.h” extension in the
program.
Header Files:
In C language, header files contain the set of predefined standard library functions. The
“#include” preprocessing directive is used to include the header files with “.h” extension in the
program.
Here is the table that displays some of the header files in C language.
<locale.h>
Header
File
<math.h>
<assert.h> <setjmp.h>
<ctype.h>
<signal.h>
<stdarg.h> Variable Argument List
Functions
<stdio.h>
<string.h>
Diagnostics Functions
<time.h>
Character Handling
Functions Localization
Input/Output Functions
Functions
General Utility
Mathematics Functions
Functions String
Nonlocal Jump
Functions
Functions Signal
Date and Time
Handling Functions Functions
Main Function in C
A main is a predefined keyword or function in C. It is the first function of every C program that is
responsible for starting the execution and termination of the program. It is a special function that
always starts executing code from the 'main' having 'int' or 'void' as return data type. In other
words, a main() function is an entry point of the programming code to start its execution.
These functions are declared and related macros are defined in “stdio.h” which is a header file in
C language.
We have to include the “stdio.h” file to make use of these printf() and scanf() library functions in
C language.
printf() function is used to print the character, string, float, integer, octal and hexadecimal values
onto the output screen. We use printf() function with %d format specifier to display the value of
an integer variable. Similarly %c is used to display character, %f for float variable, %s for string
variable, %lf for double and %x for hexadecimal variable. To generate a newline,we use “\n” in
the C printf() statement. We will learn about escape sequences and format specifiers in details
ahead.
Compiling a C program:
C needs a compiler to convert it into an executable code so that the program can be run on our
machine.
Different Compilers available: GCC, mingw, Turbo C etc. We will discuss these below.
It is used and it comes mostly It can be used with any operating system, such
with *nix Operating systems. as Windows, Linux.
Users need to use it via a It has menus and mouse-based interaction facilities
command-line interface. as well, which makes it a Graphical User Interface
IDE.
The GNU Compiler Collection (GCC) is an optimizing compiler produced by the GNU Project
supporting various programming languages, hardware architectures and operating systems. The
Free Software Foundation (FSF) distributes GCC as free software under the GNU General
Public License (GNU GPL). GCC is a key component of the GNU toolchain and the standard
compiler for most projects related to GNU and the Linux kernel. When it was first released in
1987 by Richard Stallman, GCC 1.0 was named the GNU C Compiler since it only handled the C
programming language. It was extended to compile C++ in December of that year. Front ends
were later developed for Objective-C, Objective-C++, Fortran, Ada, D and Go, among others.
What is MinGW?
MinGW stands for "Minimalist GNU for Windows" It is essentially a tool set that includes some
GNU software, including a port of GCC.
In summary, MinGW contains GCC which is in the collection of GNU free software. MinGW is a
free and open source software development environment to create Windows applications. It is
licensed under the GNU General Public License. MinGW includes a simplified port of the GNU
Compiler Collection (GCC), a compiler system for Linux. It also provides developer utilities for
Windows like an assembler, linker, etc. Freely distributable Windows-specific header files and
libraries that support the use of the Windows application programming interfaces (API) are also
provided by MinGW. Unlike many of the platforms that GCC is found on, Windows lacks most of
the common utilities and tools, so those are included so that a lot of open source software
originally developed in POSIX environments can be compiled.
Running C Programs in Linux is easy, most Linux distributions come shipped with GCC, or you
can install one easily using the respective package managers (like apt for Ubuntu).
C programming being closely related to UNIX makes it slightly troublesome when used on
non-UNIX/LINUX operating systems. Since MacOS is based on BSD Linux, it is relatively easy
to do C programming in it. But programming with C in Windows is more problematic.
● Turbo C/ Borland C
● Microsoft Visual Studio/Microsoft Visual C++
● MinGW Based IDEs like Code::Blocks
● Online C Compilers
There are still people who use Turbo C or Borland C on older versions of Windows but the use
of such outdated IDEs is not a good practice.
Serious C programmers on Windows typically use Microsoft Visual Studio (Microsoft Visual C++
available as part of Visual Studio), an IDE that’s often called MSVC. Microsoft Visual C++ is
Microsoft's partial implementation of the C and full implementation C++ compiler and associated
languages-services and specific tools for integration with the Visual Studio IDE. It can compile
either in C mode or C++ mode.
Another way is to use IDEs based on MinGW compilers for Windows like Code::Blocks or Dev
C++. Yet another way is to use online C compilers like:
https://www.onlinegdb.com/online_c_compiler
https://www.programiz.com/c-programming/online-compiler/
While in most of the IDEs compiling and running a C code is just a click of a button, let’s see the
process in GCC.
Below are the steps we use on an Ubuntu machine with a gcc compiler.
We first create a C program using an editor (like nano or vim) and save the file as filename.c
Then compile it using the below command:
gcc<space><filename of the c file>
This will create an executable/ object file a.out, on successful compilation.
To run our code, we need to execute the a.out file, as shown below:
./ <filename of the executable file/object file>
By default, gcc will name the generated executable/object file with the name a.out , we can use
an option , -o with gcc, to give it another name. Also, the option -Wall enables all compiler’s
warning messages. This option is recommended to generate better code.
Every C program contains statements. These statements are constructed using words and
these words are constructed using characters from the C character set. C language character
set contains the following set of characters:
● Alphabets
● Digits
● Special Symbols
Alphabets: C language supports all the alphabets from the English language. Lower and upper
case letters together support 52 alphabets.
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 vertical tab etc.,
Note: Every character in C language has its equivalent ASCII (American Standard Code for
Information Interchange) value.
Tokens in C:
We can define the token as the smallest individual element in C. For `example, we cannot create
a sentence without using words; similarly, we cannot create a program in C without using tokens
in C. Therefore, we can say that tokens in C is the building block or the basic component for
creating a program in C language.
Keywords
Keywords in C can be defined as the pre-defined or the reserved words having its own
importance, and each keyword has its own functionality. Since keywords are the pre-defined
words used by the compiler, they cannot be used as the variable names. If the keywords are
used as the variable names, it means that we are assigning a different meaning to the keyword,
which is not allowed. C language supports the following keywords given below:
auto double int struct
do if static while
Identifiers
Identifiers in C are used for naming variables, functions, arrays, structures, etc. Identifiers in C
are the user-defined words. It can be composed of uppercase letters, lowercase letters,
underscore, or digits, but the starting letter should be either an underscore or an alphabet.
Identifiers cannot be used as keywords. Rules for constructing identifiers in C are given below:
● 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.
● 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.
Literals are the constant values assigned to the constant variables. We can say that the literals
represent fixed values that cannot be modified.
There are four types of literals that exist in C programming:
● Integer literal
● Float literal
● Character literal
● String literal
Integer literal
It is a numeric literal that represents only integer type values. It represents the value neither in
fractional nor exponential part. It can be specified in the following four ways: Decimal number
(base 10)
It is defined by representing the digits between 0 to 9. For example, 45, 67, etc.
Binary-literal (base 2)
0b or 0B followed by one or more binary digits(0, 1).
Float literal
It is a literal that contains only floating-point values or real numbers. These real numbers contain
the number of parts such as integer part, real part, exponential part, and fractional part. The
floating-point literal must be specified either in decimal or in exponential form. Let's understand
these forms in brief.
Decimal form
The decimal form must contain either decimal point, exponential part, or both. If it does not
contain either of these, then the compiler will throw an error. The decimal notation can be
prefixed either by '+' or '-' symbol that specifies the positive and negative numbers.
Exponential form
The exponential form is useful when we want to represent the number, which is having a big
magnitude. It contains two parts, i.e., mantissa and exponent. For example, the number is
2340000000000, and it can be expressed as 2.34e12 in an exponential form.
The following are the rules for creating a float literal in exponential notation:
● In exponential notation, the mantissa can be specified either in decimal or fractional form.
● An exponent can be written in both uppercase and lowercase, i.e., e and E. ● We can use
both the signs, i.e., positive and negative, before the mantissa and exponent.
● Spaces are not allowed
Character literal
A character literal contains a single character enclosed within single quotes. If multiple
characters are assigned to the variable, then we need to create a character array. If we try to
store more than one character in a variable, then the warning of a multi-character constant will
be generated.
String literal
It is composed of two or more characters starting with backslash \. For example: \n represents a
new line.
\b Backspace
\f Form Feed
\n New Line
\r Carriage Return
\t Tab (Horizontal)
\v Vertical Tab
\\ Backslash
\' Single Quote
\? Question Mark
\0 Null
#include <stdio.h>
int main(void)
{
// \n - newline
printf("\n new line escape sequence");
printf("\n first line");
printf ("\n Second line \n");
// \? question mark
printf("\nThis is a question\?");
// \\ to represent \ - Backslash
printf(" \n C:\\user\\bin\\file");
// vertical tab \v
printf("\n usage of vertical tab escape sequence");
printf(" \n \v Mayank \t Ramnani ");
return (0);
}
Additionally, you will hear a bell sound when you run this code, due to \a. C code to illustrate
Literals in C:
#include <stdio.h>
#define X 20 //declaring constants using #define
#define PI 3.14
#define Y 10
int main()
{
Output:
Variables and Data Types in C
A variable is an identifier which is used to store some value. Constants can never change at the
time of execution. Variables can change during the execution of a program and update the value
stored inside it.
A single variable can be used at multiple locations in a program. A variable name must be
meaningful. It should represent the purpose of the variable.
Example: Height, age, are the meaningful variables that represent the purpose it is being used
for. Height variable can be used to store a height value. Age variable can be used to store the
age of a person.
A variable must be declared first before it is used somewhere inside the program. A variable
name is formed using characters, digits and an underscore.
Following are the rules that must be followed while creating a variable:
We can also say a variable is a named memory location in C, a variable is associated with three
things:
● Value
● Address/Location
● Data Type
Declaration of Variables:
int a, b;
a = b = 10; //right to left-> assignment operator
int a, b = 10, c = 20;
int a=b=10;
But ,
int a,b;
a=b=10; is fine (Why?)
Similarly,
float a=1.5,b=a+3.1;
Is alright, but
float b=a+3.1, a= 1.5; is NOT.
Because here we are trying to use “a” even before defining it.
int a;
printf("Enter the value of a");
scanf("%d", &a);
Data types in C
All the data types defined by C are made up of units of memory called bytes. On most computer
architectures a byte is made up of eight bits, each bit stores a one or a zero. These eight bits
with two states give 256 combinations (28). So an integer which takes up four bytes can store a
number between 0 to 4,294,967,295 (0 and 232). However, integer variables use the first bit to
store whether the number is positive or negative so their value will be between -2,147,483,648
and + 2,147,483,647.
Format Specifiers in C:
SPECIFIER USED FOR
%c a single character
%s a string
%n prints nothing
%% the % symbol
#include <stdio.h>
//C code to illustrate the format specifiers in C
int main() {
char ch = 'B';
printf("%c\n", ch); //printing character data
printf("%d\n", ch); //printing character as an integer - gets
ASCII value
//print decimal or integer data with d and i
int x = 45, y = 90;
printf("%d\n", x);
printf("%i\n", y);
float f = 12.67;
printf("%f\n", f); //print float value
printf("%e\n", f); //print in scientific notation
printf("%E\n", f); //also prints in scientific notation
unsigned int num=21;
printf("Num is %u",num); //print unsigned integer
short int num2= -2;
printf("\nnum2 is %hi",num2);//print short int
unsigned short int num3=5;
printf("\nnum 3 is %hu", num3);//print short unsigned integer
int a = 67;
printf("\n%o\n", a); //print in octal format
printf("%x\n", a); //print in hex format
char str[] = "Hello World";
printf("%s\n", str); //print a string
int marks=100;
printf("He scored %d %%",marks); //print % symbol
return 0;
}
Sizeof operator
Sizeof is a much used operator in the C. It is a compile time unary operator which can be used
to compute the size of its operand. The result of sizeof is of unsigned integral type. To find out
the size of each data type on your machine, compile and run this program. It uses sizeof() to tell
us how many bytes a data type takes up.
#include <stdio.h>
int main()
{
printf("sizeof(char) == %d\n", sizeof(char));
printf("sizeof(short) == %d\n", sizeof(short));
printf("sizeof(int) == %d\n", sizeof(int));
printf("sizeof(long) == %d\n", sizeof(long));
printf("sizeof(long long) == %d\n", sizeof(long long));
return 0;
}
Now what if we store a number greater than the range of a data type? C will calculate the
difference between the highest number that can be represented and the number we are trying to
assign and starts counting that from the lower range, and then assign the number, for eg, if we
try to assign an int a value of 2147483648 (range is 2147483647) it will start counting (the
difference: 1) from lowest number possible that is -2147483648 and so the variable will contain
-2147483648. Similarly for other data types as shown in below C code.
#include<stdio.h>
int main(){
int a= 2147483648; //range: 2,147,483,647
unsigned int b= 4294967296;//range: 4294967295
short int c=32768; //32,767
short unsigned int d= 65536; //65535
printf("a is %d\n", a);
printf("\nb is %u\n", b);
printf("\nc is %hi\n", c);
printf("\nd is %hu\n", d);
return 0;
}
And what happens if we try to assign a negative value to an unsigned int, say -x, then C will
assign the highest number in range - x as the result.
#include<stdio.h>
#include<math.h> //header file that contains various mathematical
functions
int main(){
unsigned int a = -23; //range: 4,294,967,295
unsigned int b= 4294967296;
printf("answer %u\n", a);
printf(" max range of unsigned int is %u\t",((unsigned int
)pow(2,sizeof(a)*8)));
printf("\n calculated ans : %u",(unsigned int )pow(2,sizeof(a)*8)+ 1
- 23);
printf("\nb is %u\n", b);
return 0;
}
Modifiers are keywords in c which changes the meaning of basic data type in c. It specifies the
amount of memory space to be allocated for a variable. Modifiers are prefixed with basic data
types to modify the memory allocated for a variable. For example, storage space for int data
type is 4 byte. We can increase the range by using long int which is 8 byte. We can decrease the
range by using a short int which is 2 byte.
● long
● short
● signed
● unsigned
● long long
long:
This can be used to increase the size of the current data type to 2 more bytes, which can be
applied on int or double data types. For example, if for the specific C compiler int occupies 2
byte of memory if we use long with integer variable then it occupies 4 byte of memory.
Syntax:
short:
In general int data type occupies different memory spaces for a different operating
system/compiler; to reduce the allocated memory space to 2 Bytes, short keyword can be used.
short int a; --> occupies 2 bytes of memory space in every operating system.
unsigned:
This keyword can be used to make the accepting values of a data type a positive data type.
Syntax
unsigned int a =100; // right
unsigned int a=-100; // wrong
Note: as we saw, assigning a negative value is wrong but it might or might not lead to
compilation error , as it depends on compiler to compiler.
Signed:
This keyword accepts both negative or positive value and this is default properties or data type
modifiers for every data type.
Example
void is an incomplete type. It means "nothing" or "no type". You can think of void as absent.
For example, if a function is not returning anything, its return type should be void. Note
that, you cannot create variables of void type.
The enum in C is also known as the enumerated type. It is a user-defined data type that consists
of integer values, and it provides meaningful names to these values. The use of enum in C
makes the program easy to understand and maintain. The enum is defined by using the enum
keyword.
In the above declaration, we define the enum named as a flag containing 'N' integer constants.
The default value of integer_const1 is 0, integer_const2 is 1, and so on. We can also change the
default value of the integer constants at the time of the declaration.
For example:
enum fruits{mango, apple, strawberry, papaya};
enum fruits{
mango=2,
apple=1,
strawberry=5,
papaya=7,
};
As we know that in C language, we need to declare the variable of a pre-defined type such as
int, float, char, etc. Similarly, we can declare the variable of a user-defined data type, such as
enum. Let's see how we can declare the variable of an enum type.
enum status{false,true};
In the above statement, we have declared the 's' variable of type status.
enum status{false,true} s;
In this case, the default value of false will be equal to 0, and the value of true will be equal to 1.
#include <stdio.h>
enum weekdays{Sunday=1, Monday, Tuesday, Wednesday, Thursday,
Friday, Saturday};
int main()
{
enum weekdays w; // variable declaration of weekdays type
w=Monday; // assigning value of Monday to w.
printf("The value of w is %d",w);
return 0;
}
In the above code, we create an enum type named weekdays, and it contains the name of all
the seven days. We have assigned 1 value to the Sunday, and all other names will be given a
value as the previous value plus one.
The enum is used when we want our variable to have only a set of values. For example, we
create a direction variable. As we know that four directions exist (North, South, East, West), so
this direction variable will have four possible values. But the variable can hold only one value at
a time.
Life Time - Life time of any variable is the time for which the particular variable outlives in
memory during running of the program.
Scope - The scope of any variable is actually a subset of life time. A variable may be in the
memory but may not be accessible though. So, the area of our program where we can actually
access our entity (variable in this case) is the scope of that variable.
The scope of any variable can be broadly categorized into three categories :
Global scope : When a variable is defined outside all functions. It is then available to all the
functions of the program and all the blocks the program contains.
Local scope : When a variable is defined inside a function or a block, then it is locally
accessible within the block and hence it is a local variable.
}
void func1()
{
printf("Global inside func1 is %d",global); // Would print the global
variable successfully.
Local variable is 10
Let us consider the same program again but this time, let us uncomment line no. 14 of our
function func1() which tries to access local which was declared in main() inside another function
func1():
It would produce an error, as the variable named local is actually local to the function main() and hence
can be accessed only through that function not from any other function, but for global variables, it can
be accessed by all functions/ blocks a program is using.
Here our variable named global has global scope and lifetime, hence it outlives in memory till
the program execution is completed and can be accessed by any function / block locally too. But
in case of our local variable, it may outlive in memory till program execution is completed but it
can be accessed from only within the function or block it is defined in.
Operators in C
* multiplication
/ division
C Code:
// Working of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
The operators +, - and * computes addition, subtraction, and multiplication respectively as you
might have expected. In normal calculation, 9/4 = 2.25. However, the output is 2 in the program.
It is because both the variables a and b are integers. Hence, the output is also an integer. The
compiler neglects the term after the decimal point and shows answer 2 instead of 2.25.
The modulo operator % computes the remainder. When a=9 is divided by b=4, the remainder is
1. The % operator can only be used with integers.
C programming has two operators increment ++ and decrement -- to change the value of an
operand (constant or variable) by 1.
Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These
two operators are unary operators, meaning they only operate on a single operand.
return 0;
}
Here, the operators ++ and -- are used as prefixes. These two operators can also be used as
postfixes like a++ and a--. We will now study the difference between these two:
pre-increment/pre-decrement versus post-increment/ post-decrement operators.
The pre-increment operator is represented as the double plus (++a) symbol, appended before
the variable's name. The pre-increment operator is used to increment the value of an operand by
1 before using it in the mathematical expression. In other words, the value of a variable is first
incremented, and then the updated value is used in the expression.
Post-increment is an increment operator, represented as the double plus (a++) symbol followed
by an operator 'a'. It increments the value of the operand by 1 after using it in the mathematical
expression. In other words, the variable's original value is used in the expression first, and then
the post-increment operator updates the operand value by 1.
Similarly, there are some properties of the decrement operator, as follows: ● The
decrement operator is used to decrease the current value of the variable by 1. ● We
can only use these operators with variables.
● It is the operator represented by the double minus (--) symbol.
It is of two types:
● Pre Decrement
● Post Decrement operators.
The Pre Decrement Operator decreases the operand value by 1 before assigning it to the
mathematical expression. In other words, the original value of the operand first decreases, and
then a new value is assigned to the other variable.
Post decrement operator is used to decrease the original value of the operand by 1 after
assigning to the expression.In other words, the original value of the operand is first assigned into
the other variable, and then it is decremented.
#include <stdio.h>
#include <conio.h>
int main ()
{
// declaration of the variables
int a = 7;
int b = 2;
a/b
%= a %= b a = a%b
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}
C
Relational Operators
A relational operator checks the relationship between two operands. If the relation is true, it
returns 1; if the relation is false, it returns value 0.
0 > Greater than 5 > 3 is evaluated to 1 < Less than 5 < 3 is evaluated to
return 0;
}
C Logical Operators
An expression containing a logical operator returns either 0 or 1 depending upon whether the
expression results true or false. Logical operators are commonly used in decision making in C
programming.
Operator Meaning Example
int main()
&& Logical AND. True only if all operands {
int a = 5, b = 5, c = 10,
are true
result;
If c = 5 and d = 2 then, expression
|| Logical OR. True only if either one ((c==5) && (d>5)) equals to 0.
operand
is true
If c = 5 and d = 2 then, expression
! Logical NOT. True ((c==5) || (d>5)) equals 1.
only if the operand is
0
If c = 5 then, expression !(c==5) equals
to 0.
// Working of logical operators
#include <stdio.h>
return 0;
}
C Bitwise Operators
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
The output of bitwise AND is 1 if the corresponding bits of two operands is 1. If either bit of an
operand is 0, the result of the corresponding bit is evaluated to 0.
Let us suppose the bitwise AND operation of two integers 12 and 25.
The result of the bitwise XOR operator is 1 if the corresponding bits of two operands are
opposite. It is denoted by ^.
Bitwise complement operator is an unary operator (works on only one operand). It changes 1 to
0 and 0 to 1. It is denoted by ~.
For any integer n, the bitwise complement of n will be -(n+1). To understand this, you should
have the knowledge of 2's complement.
2's Complement
Two's complement is an operation on binary numbers. The 2's complement of a number is equal
to the complement of that number plus 1. For example:
Right shift operator shifts all bits towards the right by a certain number of specified bits. It is
denoted by >>.
Left shift operator shifts all bits towards the left by a certain number of specified bits. The bit
positions that have been vacated by the left shift operator are filled with 0. The symbol of the left
shift operator is <<.
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf("\nOutput of Bitwise AND = %d", a&b);
printf("\nOutput of bitwise OR = %d", a|b);
printf("\nOutput of Bitwise XOR = %d", a^b);
printf("\nOutput of Bitwise compliment = %d\n",~35);
int num=212;
printf("\nOutput of right shift by 1 = %d", num>>1);
printf("\nOutput of left shift by 1 = %d", num<<1);
return 0;
}
Other Operators
Comma Operator
Comma operators are used to link related expressions together. For example:
int a, c = 5, d;
We have already discussed this in the section “Declaration of variables”.
The sizeof is a unary operator that returns the size of data (constants, variables, array, structure,
etc). We have already discussed this.
The conditional operator is also known as a ternary operator. The conditional statements are the
decision-making statements which depend upon the output of the expression. It is represented
by two symbols, i.e., '?' and ':'.
As a conditional operator works on three operands, it is also known as the ternary operator.
The behavior of the conditional operator is similar to the 'if-else' statement as the 'if-else'
statement is also a decision-making statement.
● In
the above syntax, the expression1 is a Boolean condition that can be either true or false
value.
● If the expression1 results into a true value, then the expression2 will execute.
● The expression2 is said to be true only when it returns a non-zero value. ● If
the expression1 returns false value then the expression3 will execute. ● The
expression3 is said to be false only when it returns zero value.
#include <stdio.h>
int main()
{
int age; // variable declaration
printf("Enter your age");
scanf("%d",&age); // taking user input for age variable
(age>=18)? (printf("eligible for voting")) : (printf("not
eligible for voting")); // conditional operator
return 0;
}
Operator
Precedence and Associativity in C
Operator precedence determines which operator is performed first in an expression with more
than one operator with different precedence.
Operators Associativity
Operators Associativity is used when two operators of the same precedence appear in an
expression. Associativity can be either Left to Right or Right to Left.
For example: ‘*’ and ‘/’ have same precedence and their associativity is Left to Right, so the
expression “100 / 10 * 10” is treated as “(100 / 10) * 10”.
Operators Precedence and Associativity are two characteristics of operators that determine the
evaluation order of sub-expressions in absence of brackets.
100 + 200 / 10 - 3 * 10
1) Associativity is only used when there are two or more operators of the same precedence. The
point to note is that associativity doesn’t define the order in which operands of a single operator
are evaluated.
The table below shows the C operators are listed in order of precedence (highest to lowest).
Their associativity indicates in what order operators of equal precedence in an expression are
applied.
Programming Errors in C
Errors are the problems or the faults that occur in the program, which makes the behavior of the
program abnormal, and experienced developers can also make these faults. Programming
errors are also known as the bugs or faults, and the process of removing these bugs is known
as debugging.
These errors are detected either during the time of compilation or execution. Thus, the errors
must be removed from the program for the successful execution of the program.
1. Compile Time Errors: Errors that are detected at compile time. Compilers can detect
them.
1.1 Syntax Errors
1.2 Semantic Errors
2. Runtime Errors
3. Logical Errors
Syntax error
Syntax errors are also known as the compilation errors as they occurred at the compilation time,
or we can say that the syntax errors are thrown by the compilers. These errors mainly occur due
to the mistakes while typing or do not follow the syntax of the specified programming language.
These mistakes are generally made by beginners only because they are new to the language.
These errors can be easily debugged or corrected.
For example:
In the above output, we observe that the code throws the error that 'a' is undeclared. This error is nothing
but the syntax error only.
Semantic error
Semantic errors are the errors that occurred when the statements are not understandable by the
compiler.
Type incompatibility
int b = "hello";
Errors in expressions
int a, b, c;
a+b = c;
In the above code, we use the statement a+b =c, which is incorrect as we cannot use the two operands
on the left-side.
Runtime Errors
Sometimes the errors exist during the execution-time even after the successful compilation
known as run-time errors. When the program is running, and it is not able to perform the
operation is the main cause of the run-time error. The division by zero is the common example of
the run-time error. These errors are very difficult to find, as the compiler does not point to these
errors.
You can notice that the code terminates abruptly on runtime and returns a non-zero exit code,
signifying the abnormal termination of the program.
Logical Errors
On compilation and execution of a program, desired output is not obtained when certain input
values are given. These types of errors which provide incorrect output but appear to be error
free are called logical errors. These are one of the most common errors made by beginners of
programming. These errors solely depend on the logical thinking of the programmer and are
easy to detect if we follow the line of execution and determine why the program takes that path
of execution.