C (Chap 2-IDE and Introduction To C)
C (Chap 2-IDE and Introduction To C)
Structure of c program:
#include<header file>
int main() //main function
{
Statements-1;
Statements-2;
return 0;
}
#include<header file>
● As we know that before using any variable we declare it first in our program and then only we use that
variable.
● Similarly, before using any predefined function, we will have to declare that function first in our program
and as we know declaration i.e. prototype of the functions are defined in the header file only.so when we
write the line #include<header file> then all the function which is declared in respective header file are
copied into our program and then we use those functions.
● For eg. stdio.h contains declaration of printf(),scanf() etc. so when we write #include<stdio.h> then
declaration of printf(),scanf() etc. are copied into the program at the beginning. Then we use these function
efficiently.
int main()
● main() function is mandatory function to write in any program.
● Execution of any program start from the main function only.
● It is the entry point for a program. Compiler starts the execution from main function only.
● int is the return type of main function that means main function will return an integer value at the end of
the program.
{ }
● { } curly brackets defines scope of a block.
● All the program should be written inside the curly brackets “{ }” only.
return 0;
● return 0; statement causes the termination of main function.
● 0 value indicates successful execution of the program.
● statement written after return 0; never get executed.
Output:
Good Morning
1. C program (source code) is sent to preprocessor first. The preprocessor is responsible to convert
preprocessor directives into their respective values. The preprocessor generates an expanded source code.
2. Expanded source code is sent to compiler which compiles the code and converts it into object code i.e.
machine code.
Tokens:
● Tokens in C is the most important element to be used in creating a program 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
auto break case char const continue default
do double else enum extern float for
3. Constants:
A constant is a data item which will not be changed during the execution of a program.
1. Integer constant:
An integer constant is a whole number without any decimal point.
E.g.: 10, 20,-10, 1000 etc.
3. Character constants:
A character constant is a single alphabet, a single digit or a single special symbol enclosed within single
quotes.
E.g.: ‘a’,’Z’,’B’,’+’,’@’ etc.
Escape sequence characters:
● An escape sequence character begins with a ‘\’ and is followed by one character.
● A backslash along with one character gives rise to special print effect by changing (escaping) the meaning
of the character. Since an escape sequence character starts with backslash, they are also called backslash
constants.
Note: The backslash characters such as ‘\a’, ‘\n’, ‘\t’, etc. are non-printable characters.
String constant:
A sequence of characters (i.e. collection of characters) enclosed within a pair of double quotes is called a
string constant.
e.g.: “sahyog”,”hello”,”morning” etc.
Variables:
● When we want to store any information (data) on our computer/laptop, we store it in the computer's
memory space. Instead of remembering the complex address of that memory space where we have stored
our data, our operating system provides us with an option to create folders, name them, so that it becomes
easier for us to find it and access it.
● Similarly, in C language, when we want to use some data value in our program, we can store it in a
memory space.
● A variable is a name of the memory location. It is used to store data. Its value can be changed.
● A variable in C language must be given a type, which defines what type of data the variable will hold.
Data types
2. char
● Character data type allows a variable to store only one character.
● “char” keyword is used to refer character data type.
● For example, ‘A’ can be stored using char data type. You can’t store more than one character using char
data type.
● We can also store numerical value too in char type but if will store numerical value then “ASCII” value
will be printed.
● Note:
● Every character has a corresponding ASCII value to it ranging from -128 to 127.
● Numbers as a character has their corresponding ASCII values too. For example, ‘1’ as char has ASCII value
49, ‘A’ has ASCII value 65.
3. float
● It is a keyword which is used to define floating point numbers. The floating point numbers are also called
real numbers. Normally they are associated with the variables (also called identifiers) to store floating
point numbers in memory locations.
● That is, using this data type both positive and negative floating point numbers can be stored in the
memory.
● Eg: 1.23, 2234.4532 etc.
Sahyog College of Management Studies, Thane (W)
4. Double:
● Double is also used to define floating point numbers.
● But double data type accepts 15 digit after decimal whereas float accepts 6 digits.
● Eg: 523.45353
Modifiers in C Language:
1. Size Modifier
2. Sign Modifier
1. Size Modifier:
● Size Modifiers are prefixed with basic data types to modify (either increase or decrease) the amount of
storage space allocated to a variable.
● Short, long, long long are the size modifiers.
● 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 short int which is 2 byte.
● short is only used with int type.
● long can be used with int and double.
● Any size modifier cannot be used with float and char data type.
● long long can not be used with float and double.
● Long long can be used with int type only.
2. Sign Modifier:
● Sign modifiers decides whether the variable will hold positive or negative number.
● signed & unsigned are the sign modifiers.
● In signed type we can store both positive as well as negative value but in unsigned type we can store only
positive value.
● By default the data type is signed only.
● Sign modifiers are used with int and char only.
● Sign modifiers cannot be used with float and double.
Size & Range of data type:
Data type Space Range Format
specifier
int 4 byte -2147483648 to 2147483647 %d
char 1 byte -128 to 127 %c
float 4 byte 6 digits %f
double 8 byte 15 digit %lf
short int 2 byte -32768 to 32767 %hi
long int 4 byte -2147483648 to 2147483647 %ld
long long int 8 byte -9223372036854775808 to 9223372036854775807 %lld
signed short int 2 byte -32768 to 32767 %hi
Note:
● By default int and char data type is of signed type that means we can store positive as well as negative
value in it.
● int and signed int are same data type.
● short int and signed short int are same.
● long long int and signed long long int are same.
● char and signed char are same.
Format specifier:
The format specifier is used during input and output. It is a way to tell the compiler what type of data is in a
variable during taking input using scanf() or printing using printf().
Some examples are %c, %d, %f, etc.
● Format specifier for hexadecimal -%x or %X
● Format specifier octal - %o
● Format specifier decimal/integer - %d
Note: Hexadecimal, Octal and Decimal Values comes under integer constant so we can store value of these
types into int datatype.
Eg: int a=0XA is valid (Hexadecimal value)
int b=010 is valid(Octal value)
int c=102 is valid(Decimal value)
%x or %X %o %d
1. Arithmetic Operator:
The operators that are used to perform arithmetic operations such as Addition, subtraction, multiplication etc.,
are called arithmetic operators.
The meaning of all the operators along with examples is shown below:
Arithmetic Description Example Result Priority Associativity
operator
+ Used for addition 10 + 20 30 2 Left to Right
- Used for subtraction 50-10 40 2 Left to Right
* Used for multiplication 10 * 20 200 1 Left to Right
/ Used for division and gives 10/5 2 1 Left to Right
quotient
% Used to get remainder. It is 4%2 0 1 Left to Right
read as modulus operator or
mod
Note: Modulus operator denoted by % is used only for integer values and not for Floating point numbers. This
operator returns the remainder after division.
For Example:
● a = b; /* Store the value of b into a */
● area = L * B; /* Compute the product and store in variable area */
● pi = 3.1416; /* Store the number 3.1416 using the variable pi */
Pre-increment Post-increment
a. Pre-increment Operator:
● If the increment operator ++ is placed before (pre) the operand, then the Operator is called pre-increment.
● As the name indicates, pre-increment means increment before (pre) the operand value is used. So, the
operand value is incremented by 1 first, and then this incremented value is used.
● Eg: ++a, ++b etc.
Output:
a=21
b=21
Output:
a=21
b=20
Decrement operator:
● ‘--’ is a decrement operator. This is an unary operator. It decrements the value of the operand by one.
● The decrement operator is classified into two categories as shown below:
Decrement Operator
Pre-decrement Post-decrement
a. Pre-decrement Operator:
● If the decrement operator - - is placed before (pre) the operand, then the Operator is called pre-decrement.
● As the name indicates, pre-decrement means decrement before (pre) the operand value is used. So, the
operand value is decremented by 1 first, and then this decremented value is used.
● Eg: --a, --b etc.
b. Post-increment Operator:
● If the decrement operator -- is placed after (post) the operand, then the operator is called post-decrement.
● As the name indicates, post-decrement means decrement after (post) the operand value is used. So,
operand value is used first and then the operand value is decremented by 1.
● Eg: a--, b-- etc.
4. Relational operators:
● The relational operators, also called comparison operators, are used to compare two operands. They are
used to find the relationship between two operands and hence are called relational operators.
● The two operands may be constants, variables or expressions. The relationship between these two
operands results in true (1) or false(0).
The relational operators and the meaning associated with them are shown in the following table:
Operator Description Example Priority Associativity
< Less than 10 < 20 1 Left to right
<= Less than or equal 12 <= 18 1 Left to right
> Greater than 15>10 1 Left to right
>= Greater than or equal 15>=3 1 Left to right
== Equal to 10 ==9 2 Left to right
!= Not equal to 5 !=2 2 Left to right
&& Logical And. If all the conditions (10<20) && (23>5)=1 2 Left to right
are true then result will be true (21<43) &&(5==5)=0
a. Logical AND: The result of logical ‘AND’ operator denoted by && is true if and only if both the operands
are evaluated to true. If one of the operands is evaluated to false.
Operand 1 Operand 2 Result
True(1) True(1) True(1)
True(1) False(0) False(0)
False(0) True(1) False(0)
False(0) False(0) False(0)
c. Logical Not: The logical ‘NOT’ denoted by ! can be true or false. The result is true if the operand is false
and the result is false if the operand is true.
Operand 1 Result
True(1) False(0)
False(0) True(1)
6. Conditional Operator:
● The conditional operator is also known as a ternary operator.
● As conditional operator works on three operands, so it is also known as the ternary operator.
● It is represented by two symbols, i.e. '?' and ':'.
● The behavior of the conditional operator is similar to the 'if-else' statement.
Syntax1:
Expression1? expression2: expression3;
Example2:
Suppose, A=30, B=12
C= (A<B) ? A : B;
printf(“Smallest no is %d”,C);
Output:
Smallest no is 12
7. Bit-wise Operator:
● In arithmetic-logic unit (which is within the CPU), mathematical operations like: addition, subtraction,
multiplication and division are done in bit-level.
● To perform bit-level operations in C programming, bitwise operators are used.
The bit-wise operators and the meaning associated with them are shown in the following table:
Operator Description Example Priority Associativity
& Bitwise AND. 12 & 15 3 Left to right
| Bitwise OR 5|2 5 Left to right
^ Bitwise XOR 11 ^ 10 4 Left to right
~ Bitwise NOT ~10 1 Left to right
<< Bitwise Left Shift 10<<1 2 Left to right
>> Bitwise Right Shift 12>>1 2 Left to right
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:
● 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)
Question 1:
Find out complement of 35 i.e. ~35.
Solution:
X Y X&Y X|Y X ^Y ~X
0 0 0 0 0 1
0 1 0 1 1 1
1 0 0 1 1 0
1 1 1 1 0 0
0 0 0 1 1 0 1 0 = 26
So,
13<<1=26
20 = 0 0 0 0 1 0 1 0 0
Right Shift 20 by 1 bit
Discarded
8. Special Operators:
Operator Description Example
, Comma Operator int a,b,c,d
Sizeof Sizeof operator Sizeof(int)
& Ampersand (address of) &operand
Operator
Sizeof operator:
● This operator is used to find the number of bytes occupied by a variable or a datatype in the computer
memory.
The syntax is shown below:
sizeof(variable_name / Data_type);
Example:
char ch;
sizeof(int); //4 byte
sizeof(ch); // 1 byte
Address of Operator (&):
● The "Address Of" Operator denoted by the ampersand character (&), & is a unary operator, which
returns the address of a variable.
Example:
int a;
&a ; //returns address of a
Program to display address of a variable
#include <stdio.h>
int main()
{
int a=20;
printf("Address of a in decimal :%d",&a);
printf("\nAddress of a in Hexadecimal :%X",&a);
return 0; }
Output:
Address of a in decimal :6487580
Address of a in Hexadecimal :62FE1C
Note:
1. Operators with 1 operand is known as unary operator.
&,++,--,~ are the unary operators.
2. Operators with 2 operands is known as binary operator.
Hierarchy of operators:
Precedence operators:
● In C language, each operator is associated with a priority value. Based on the priority, the expressions are
evaluated. A part of the expression with priority value 1 is evaluated first; part of the expression with
priority value 2 is evaluated next and so on.
● These priority values that are associated with various operators are called
Precedence of operators.
Associativity Operators:
● If all the operators in an expression have equal priority, then the direction order chosen (left to right
evaluation or right to left evaluation) to evaluate an expression is called associativity of operators.
Associativity Operators are classified into 2 categories:
1. Left Associative:
● In an expression, if there are two or more operators having the same priority and are evaluated from left-
to-right, then the operators are called Left to Right associative operators.
For example,
8 + 4 + 3 = 15
8-4 -3=1
Sahyog College of Management Studies, Thane (W)
In the above example + and – both are having equal priority so in this case according to associativity this
expression will be evaluated and associativity of + and – and left to right. So above expression will execute
from left to right.
2. Right Associative:
● In an expression, if there are two or more operators having the same priority and are evaluated from right-
to-left, then the operators are called Right to Left associative operators.
For Example,
i = j = k = 10;
Above expression will execute from right to left because 3 times = operator is used and associativity of = is
right to left.
****