Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit 2 Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 66

Unit 2

____________________________________________________________________________
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.

High Level Languages:

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:

number = 1; // take input


while(number <= 10) // loop , until 10
{
printf(“%d\n”, number); // print that number
number = number + 1; // increment by 1
}

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.

Low Level Languages

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.

Two types of low-level language are:

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.

Machine code consists of only 0s and 1s which are binary numbers.

The machine language is the hardware-dependent programming language. The code in


machine language is the only computer program which is understandable by the computer and
directly executed by the Central processing unit (CPU). The machine language code is strictly
written in the numerical form.

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.

Advantage of Machine Language

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

LDA Loads a value from a memory address

STA Stores a value in a memory address

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

MOV Moves the contents of one memory address to another

In assembly language, programmers write programs as a series of mnemonics. Mnemonics are


much easier to understand and debug than machine code, giving programmers a simpler way of
directly controlling a computer.
Assembly language uses mnemonics to represent instructions. Writing in mnemonics is easy for
programmers because they are usually brief representations of the actual commands. They are
quicker to write than binary, and it is easier to spot mistakes.

The assembly language is machine-dependent, and the high-level language is


machine-independent. The computer is not able to understand the instructions in assembly
language, but the machine code is understandable to the computer. It is a challenging task for
the ordinary individual to write the program directly in machine language.

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.

Opcodes and Operands

Many machine code and assembly instructions contain two parts:


the opcode - this is the actual instruction
the operand - this is a value that the instruction uses or manipulates

Consider this set of program instructions:


Assembly language Opcode Operand Instruction

INP 1001 00000000 Input a number

STR 6 0011 00000110 Store it in address 06

LDR A1 0101 10100001 Load data from address A1

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.

Advantage of assembly language over machine language


● Easy to understand and use.
● Easy to locate the code and correct errors.
● We can easily modify the code.
● No worry about the addresses.

Limitations of assembly language


● Machine dependent
● Knowledge of hardware is required when we use the assembly language.

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.

Compilers have several advantages:


● Compiled programs run quickly, since they have already been translated. ● A compiled
program can be supplied as an executable file. An executable file is a file that is ready to
run. Since an executable file cannot be easily modified, programmers prefer to supply
executables rather than source code.
● Compilers optimise code. Optimised code can run quicker and take up less memory
space.

Compilers also have disadvantages:


● The source code must be re-compiled every time the programmer changes the program. ●
Source code compiled on one platform will not run on another - the machine code is specific
to the processor's architecture.

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.

Interpreters have several advantages


● Instructions are executed as soon as they are translated.
● Errors can be quickly spotted - once an error is found, the program stops running and the
user is notified at which part of the program the interpretation has failed. This makes
interpreters extremely useful when developing programs.

Interpreters also have several disadvantages:


● Interpreted programs run slowly as the processor has to wait for each instruction to be
translated before it can be executed.
● Additionally, the program has to be translated every time it is run.
● Interpreters do not produce an executable file that can be distributed. As a result, the
source code program has to be supplied, and this could be modified without permission. ●
Interpreters do not optimize code - the translated code is executed as it is.

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.

The C Programming Language:


C is a general-purpose, procedural, imperative computer programming language developed in
1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating
system. C is the most widely used computer language. It can be used to develop software like
operating systems, databases, compilers, and so on. C programming is an excellent language to
learn to program for beginners.

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

Some Facts about C:

● C was invented to write an operating system called UNIX.


● C is a successor of the B language which was introduced around the early 1970s. ● The
language was formalized in 1988 by the American National Standard Institute (ANSI).
● The UNIX OS was totally written in C.
● Today C is the most widely used and popular System Programming Language.
● Most of the state-of-the-art softwares have been implemented using C.

Structure of a C Program:
Consider the simple “Hello World” Program in C:

Let us break down it’s components one by one:

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.

The syntax for the #include directive in the C language is:

#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>

Type of Functions <stdlib.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.

Printf and Scanf Functions


printf() and scanf() functions are inbuilt library functions in C programming language which are
available in C library by default.

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.

GNU C Compiler Turbo C Compiler

It is a primitive, standard C It is an old, non-standard C compiler.


compiler.

It is used and it comes mostly It can be used with any operating system, such
with *nix Operating systems. as Windows, Linux.

It was developed by the It was developed by Borland.


Free Software Foundation.

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.

GNU Compiler supports C++14


and C++17. Turbo's C++ 4.5 compiler does not support the
latest C++ standards.

Initial Release in the year 1987.


Initial Release in the year 1990.
The GCC Compiler

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.

And, What is GNU?


It is an Operating System and a collection of free software made to be "Unix like" without using
Unix. (GNU stands for "GNU's not Unix!"). GCC was developed as a part of the GNU Project.

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.

A Short note on different ways of creating C Programs on Windows

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.

Let us discuss the different ways to program with C in Windows.

● 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.

Getting Started with C Programming:


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 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.

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 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.

Tokens are of following types:


1. Keywords
2. Identifiers
3. Literals
4. Operators
5. Punctuators

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

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

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 and Constants:


A constant is a value assigned to the variable which will remain the same throughout the
program, i.e., the constant value cannot be changed. For example, const int =10; is a constant
integer expression in which 10 is an integer literal.
There are two ways of declaring constant:
● Using const keyword
● Using #define pre-processor
Constant Example

Integer constant 10, 11, 34, etc.

Floating-point constant 45.6, 67.8, 11.2, etc.


Octal constant 011, 088, 022, etc.

Hexadecimal constant 0x1a, 0x4b, 0x6b, etc.

Character constant 'a', 'b', 'c', etc.

String constant "java", "c++", ".net", etc.

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.

For example: 56, 78

Octal number (base 8)


It is defined as a number in which 0 is followed by digits such as 0,1,2,3,4,5,6,7. For example,
012, 034, 055, etc.
For example: 045, 076, 06210

Hexadecimal number (base 16)


It is defined as a number in which 0x or 0X is followed by the hexadecimal digits (i.e., digits from
0 to 9, alphabetical characters a, A, b, B, c, C, d, D, e, E, f, F).

For example: 0x23A, 0Xb4C, 0xFEA

Binary-literal (base 2)
0b or 0B followed by one or more binary digits(0, 1).

For example: 0b101, 0B111

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.

For example: 1.2, +9.0, -4.5

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.

Syntax of float literal in exponential form

[+/-] <Mantissa> <e/E> [+/-] <Exponent>

For example: +1e23, -9e2, +2e-25

Rules for creating an exponential notation

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.

Representation of character literal


A character literal can be represented in the following ways:
● It can be represented by specifying a single character within single quotes. For example,
'a', 'b', etc.
● We can specify the escape sequence character within single quotes to represent a
character literal. For example, '\n', '\a', '\b'.
● We can also use the ASCII in integer to represent a character literal. For example, the
ascii value of 65 is 'A'.
● The octal and hexadecimal notation can be used as an escape sequence to represent a
character literal. For example, '\023', '\0x12'.

String literal

A string literal represents multiple characters enclosed within double-quotes. It contains an


additional character, i.e., '\0' (null character), which gets automatically inserted. This null
character specifies the termination of the string.

Escape Sequence Characters in C

An escape sequence in C language is a sequence of characters that doesn't represent itself


when used inside string literal or character.

It is composed of two or more characters starting with backslash \. For example: \n represents a
new line.

List of Escape Sequences in C:

Escape Sequence Meaning


\a Alarm or Beep

\b Backspace

\f Form Feed

\n New Line

\r Carriage Return

\t Tab (Horizontal)

\v Vertical Tab

\\ Backslash
\' Single Quote

\" Double Quote

\? Question Mark

\nnn octal number

\xhh hexadecimal number

\0 Null

C code to illustrate some of the escape sequences in C:

// C program to illustrate some of the escape sequences

#include <stdio.h>
int main(void)
{
// \n - newline
printf("\n new line escape sequence");
printf("\n first line");
printf ("\n Second line \n");

//\t for tab space


printf(" \n 34543 \t 345435 ");
printf(" \n 123 \t 678 ");
//\b for backspace
printf("\n backspace escape sequence tutorial");
printf(" \n watch\b carefully the execution");

/*\r for carriage return


This is the escape sequence to position the cursor at the beginning
of the line. */
printf("\n demo code below");
printf(" \r remove");
printf("\n done with example");

//\a- audible bell to generate a bell sound to denote the execution


of the program
printf("\n here is the demo ");
printf(" \n bell sound\a");
// \' single quote
printf("\nThis is under \'single qoutes\'");
printf("\nThis is under \"double quotes\" ");

// \? 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()
{

// constant integer literal


const int intVal = 010; //8 in octal
const int intValinHex=0xF; //15 in Hexadecimal
const int intValBin=0b101; //5 in Binary
const float floatVal = 4.14;
const float floatVal2= 414e-2; //4.14 in exponential
const char a= 'z';
const char b=65;
const char c='\023';
printf("%f is Pi",PI);
printf("\n%d is Y",Y);
printf("\nInteger Literal:%d \n",intVal);
//intVal=10; //re-assignment of a constant variable will give error
printf("Integer Literal:%d \n",intValinHex);
printf("Integer Literal:%d \n",intValBin);
printf("20 times intVal is:%d",X*intVal);
printf("\nfloatVal is:%f",floatVal);
printf("\nfloatVal2 is:%f",floatVal2);
printf("Char Literal a:%c \n",a);
printf("Char Literal b:%c \n",b);
printf("Char Literal c:%c \n",c);
return 0;
}

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.

Rules for naming a variable in C

Following are the rules that must be followed while creating a variable:

● A variable name should consist of only characters, digits and an underscore. ● A


variable name should not begin with a number.
● A variable name should not consist of whitespace.
● A variable name should not consist of a keyword.
● ‘C’ is a case sensitive language that means a variable named ‘age’ and ‘AGE’ are different.

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:

Declaring and initializing a variable:

Method 1 (Declaring the variable and then initializing it)


int a;
a = 5;

Method 2 (Declaring and Initializing the variable together):


int a = 5;

Method 3 (Declaring multiple variables simultaneously and then initializing them


separately)

int a, b; //comma -Punctuator


a = 5;
b = 10;

Method 4 (Declaring multiple variables simultaneously and then initializing them


simultaneously)

int a, b;
a = b = 10; //right to left-> assignment operator
int a, b = 10, c = 20;

Note: here you cannot do directly

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.

Method 5 (Dynamic Initialization : Value is being assigned to a variable at run time.)

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.

Data Type Size Value Range

char 1 byte -128 to 127 or 0 to 255

unsigned char 1 byte 0 to 255

signed char 1 byte -128 to 127

int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647

unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295

short 2 byte -32,768 to 32,767


unsigned short 2 byte 0 to 65,535

long 4 byte -2,147,483,648 to 2,147,483,647

unsigned long 4 byte 0 to 4,294,967,295

Format Specifiers in C:
SPECIFIER USED FOR

%c a single character

%s a string

%hi short (signed)

%hu short (unsigned)

%Lf long double

%n prints nothing

%d a decimal integer (assumes base 10)


%i a decimal integer (detects the base automatically)

%o an octal (base 8) integer

%x a hexadecimal (base 16) integer

%p an address (or pointer)

%f a floating point number for floats

%u int unsigned decimal

%e a floating point number in scientific notation

%E a floating point number in scientific notation

%% the % symbol

C code to illustrate some of the format Specifiers:

#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;
}

Data Type modifiers in C

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.

There are five data type modifiers in C Programming Language:

● 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:

long a; --> by default which represents long int.

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

int a=10; // right


int a=-10; // right
signed int a=10; // right
signed int a=-10; // right
Note: in real time no need to write signed keyword explicitly for any data type.

Void data type:

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.

Enumeration data 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.

The following is the way to define the enum in C:

enum flag{integer_const1, integer_const2,.....integter_constN};

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};

The default value of mango is 0, apple is 1, strawberry is 2, and papaya is 3. If we want to


change these default values, then we can do as given below:

enum fruits{
mango=2,
apple=1,
strawberry=5,
papaya=7,
};

Enumerated type declaration

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.

Suppose we create the enum of type status as shown below:

enum status{false,true};

Now, we create the variable of status type:


enum status s; // creating a variable of the status type.

In the above statement, we have declared the 's' variable of type status.

To create a variable, the above two statements can be written as:

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.

Why do we use enum?

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.

Scope and Lifetime of variables in C

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.

Let's understand this with help of an example :

Consider this snippet of program :


#include<stdio.h>
int global = 100; // global variable declared void
main()
{
int local = 10; // local variable declared
printf("Global variable is %d",global);
printf("Local variable is %d",local);
func1();

}
void func1()
{
printf("Global inside func1 is %d",global); // Would print the global
variable successfully.

/*printf("Local inside func1 is %d", local); /* It would produce an


error if uncommented, because local is a local variable and can be
accessed only inside main function. */
}
Here we have a variable named global which is defined outside all the functions (generally at the
top of the program) , it is defined even outside main, and hence would be available to every
function defined in the program , here we consider only one function func1() .

As we can see, output of the above program would be :


Global variable is 100

Local variable is 10

Global inside func1 is 100

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

An operator is a symbol that operates on a value or a variable. For example: + is an operator to


perform addition. C has a wide range of operators to perform various operations.
C Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction,
multiplication, division etc on numerical values (constants and variables).

Operator Meaning of Operator

+ addition or unary plus

- subtraction or unary minus

* multiplication
/ division

% remainder after division (modulo 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.

Suppose a = 5.0, b = 2.0, c = 5 and d = 2. Then in C programming,


// Either one of the operands is a floating-point number
a/b = 2.5
a/d = 2.5
c/b = 2.5

// Both operands are integers


c/d = 2

C Increment and Decrement Operators

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.

// Working of increment and decrement operators


#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;

printf("++a = %d \n", ++a);


printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);

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.

There are some properties of the increment operator, as follows:


● The increment operator is used to increase the current value of the variable by 1.
● We can only use these operators with variables.
● It is the operator represented by the double plus (++) symbol.

There are two types of the increment operators


● Pre-increment operator
● Post-increment operator

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.

Consider the below C codes to better understand the concept.

#include <stdio.h>
#include <conio.h>
int main ()
{
// declaration of the variables
int a = 7;
int b = 2;

// print the value of the increment operator


printf (" Before using the pre-increment operator\n " );
printf (" The value of a is %d \n", a);
printf (" The value of b is %d \n", b);
// use pre-increment operator
b = ++a;
printf (" After using the pre-increment operator\n"); printf (" The
value of a is %d \n", a);
printf (" The value of b is %d \n", b);

// print the value of the increment operator


printf (" Before using the post-increment operator " ); printf (" \n
The value of a is %d ", a);
printf (" \n The value of b is %d ", b);
// use post increment operator
b = a++;
printf (" \n\n After using the post-increment operator "); printf ("
\n The value of a is %d ", a);
printf (" \n The value of b is %d ", b);
return 0;
}
#include <stdio.h>
#include <conio.h>
int main ()
{
// declare integer variables
int a, b, c, d, x;

// initialization of the variables


a = 5;
b = 7;
c = 12;
d = 15;

// use pre-increment operator in the mathematical expression


x = ++a + ++b + ++c + ++d;
printf ( " The value of x is: %d ", x);

// print the updated value of a, b, c, and d


printf (" \n The updated value of a = %d, b = %d, c = %d and d =
%d ", a, b, c, d);

// use post-increment operator in the mathematical expression


x = a++ + b++ + c++ + d++;
printf ( "\n The value of x is: %d ", x);

// print the updated value of a, b, c, and d


printf (" \n The updated value of a = %d, b = %d, c = %d and d =
%d ", a, b, c, d);
return 0;
}
C Assignment
Operators
An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =.

Operator Example Same as = a = b a = b

+= a += b a = a+b -= a -= b a = a-b *= a *= b a = a*b /= a /= b a =

a/b

%= a %= b a = a%b

// Working of assignment operators


#include <stdio.h>
int main()
{
int a = 5, c;

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.

Relational operators are used in decision making and loops.


Operator Meaning of Operator Example == Equal to 5 == 3 is evaluated to

0 > Greater than 5 > 3 is evaluated to 1 < Less than 5 < 3 is evaluated to

0 != Not equal to 5 != 3 is evaluated to 1 >= Greater than or equal to 5 >=

3 is evaluated to 1 <= Less than or equal to 5 <= 3 is evaluated to 0

// Working of relational operators


#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;

printf("%d == %d is %d \n", a, b, a == b);


printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);

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>

printf("The values of a b and c are %d ,%d and %d


respectively:\n",a,b,c);

result = (a == b) && (c > b);


printf("(a == b) && (c > b) is %d \n", result);

result = (a == b) && (c < b);


printf("(a == b) && (c < b) is %d \n", result);

result = (a == b) || (c < b);


printf("(a == b) || (c < b) is %d \n", result);

result = (a != b) || (c < b);


printf("(a != b) || (c < b) is %d \n", result);

result = !(a != b);


printf("!(a != b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);

return 0;
}

Explanation of logical operator program


● (a == b) && (c > 5) evaluates to 1 because both operands (a == b) and (c > b) is 1 (true).
● (a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false). ● (a == b) || (c < b)
evaluates to 1 because (a = b) is 1 (true).
● (a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c < b) are 0 (false). ●
!(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b) is 1 (true). ●
!(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0 (false).

C Bitwise Operators

During computation, mathematical operations like: addition, subtraction, multiplication, division,


etc are converted to bit-level which makes processing faster and saves power.

Bitwise operators are used in C programming to perform bit-level operations.

Operators Meaning of operators

& Bitwise AND

| Bitwise OR
^ Bitwise exclusive OR

~ Bitwise complement

<< Shift left

>> Shift right

Bitwise AND operator &

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.

12 = 00001100 (In Binary)


25 = 00011001 (In Binary)

Bit Operation of 12 and 25


00001100
&00011001
________
00001000 = 8 (In decimal)
Bitwise OR operator |

The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. In C


Programming, bitwise OR operator is denoted by |.

12 = 00001100 (In Binary)


25 = 00011001 (In Binary)

Bitwise OR Operation of 12 and 25


00001100
| 00011001
________
00011101 = 29 (In decimal)

Bitwise XOR (exclusive OR) operator ^

The result of the bitwise XOR operator is 1 if the corresponding bits of two operands are
opposite. It is denoted by ^.

12 = 00001100 (In Binary)


25 = 00011001 (In Binary)

Bitwise XOR Operation of 12 and 25


00001100
^ 00011001
________
00010101 = 21 (In decimal)

Bitwise complement operator ~

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 ~.

35 = 00100011 (In Binary)

Bitwise complement Operation of 35


~ 00100011
________
11011100 = 220 (In decimal)

Twist in bitwise complement operator in C Programming

The bitwise complement of 35 (~35) is -36 instead of 220, but why?

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:

Decimal Binary 2's complement


0 00000000 -(11111111+1) = -00000000 = -0(decimal)
1 00000001 -(11111110+1) = -11111111 = -256(decimal)
12 00001100 -(11110011+1) = -11110100 = -244(decimal)
220 11011100 -(00100011+1) = -00100100 = -36(decimal)

Note: Overflow is ignored while computing 2's complement.


The bitwise complement of 35 is 220 (in decimal). The 2's complement of 220 is -36. Hence, the
output is -36 instead of 220.

Bitwise complement of any number N is -(N+1). Here's how:

bitwise complement of N = ~N (represented in 2's complement form)


2'complement of ~N= -(~(~N)+1) = -(N+1)
Shift Operators in C programming

There are two shift operators in C programming:


● Right shift operator
● Left shift operator.

Right Shift Operator

Right shift operator shifts all bits towards the right by a certain number of specified bits. It is
denoted by >>.

212 = 11010100 (In binary)


212>>2 = 00110101 (In binary) [Right shift by two bits]
212>>7 = 00000001 (In binary)
212>>8 = 00000000
212>>0 = 11010100 (No Shift)

Left Shift Operator

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 <<.

212 = 11010100 (In binary)


212<<1 = 110101000 (In binary) [Left shift by one bit]
212<<0 = 11010100 (Shift by 0)
212<<4 = 110101000000 (In binary) =3392(In decimal)

C code to illustrate Bitwise operators

#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 operator

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.

Syntax of a conditional operator


Expression1? expression2: expression3;

The pictorial representation of the above syntax is shown below:

● 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.

For example: Solve


10 + 20 * 30
10 + 20 * 30 is calculated as 10 + (20 * 30)
and not as (10 + 20) * 30

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.

For example: Solve

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.

2) All operators with the same precedence have same associativity


This is necessary, otherwise, there won’t be any way for the compiler to decide the evaluation
order of expressions which have two operators of the same precedence and different
associativity. For example + and – have the same associativity.

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:

If we want to declare the variable of type integer,

int a; // this is the correct form


Int a; // this is an incorrect form.

Commonly occurred syntax errors are:

● If we miss the parenthesis (}) while writing the code.


● Displaying the value of a variable without its declaration.
● If we miss the semicolon (;) at the end of the statement.

Consider the code below :

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.

The following can be the cases for the semantic error:

Use of a un-initialized variable.


int i;
i=i+2;

Type incompatibility
int b = "hello";

Errors in expressions
int a, b, c;
a+b = c;

Array index out of bound


int a[10];
a[10] = 34;

Consider the following code:

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.

You might also like