C Introduction New
C Introduction New
C Programming
b) High Level Language: The programs in high level language are like human
language. For eg, instead of writing “add” instruction as in assembly lang.
program, in high level , we directly use ‘+’ symbol. Another advantage is
Jyoti Chandnani
programmer don’t have to keep track of register content. The programs in high
level lang. are easier to write, understand and debug. As machine cannot
understand the programs written in high level language, we need a translator
called compiler to translate the programs in high level lang to machine
language.
c) Middle level language: It lies between the low level and high level
programming language .It is user friendly and closely related to machine
language.
C Character Set
A character denotes any alphabet, digit or special symbol used to represent
information. Following are the valid alphabets, numbers and special symbols allowed
in C.
• Alphabets - A, B, ….., Y, Z a, b, ……, y, z
• Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
• Special symbols - ~ ‘ ! @ # % ^ & * ( ) _ - + = | \ { }
[]:;"'<>,.?/
Constants
Constant is a value that doesn’t change.
Types of Constants
1. Integer Constants
2. Character Constants
3. Float Constants
4. String Constants
5. Symbolic Constants
1. Integer Constant
Rules :
1. At one digit should be there.
2. It cannot have decimal number.
3. It can be positive or negative.
4. by default its positive number.
5. -32767 to +32767 and -2147483648 to +2147483648(depending on compiler)
Eg. 12,5,6,32,-78
Jyoti Chandnani
2. Real Constant
1. Floating point numbers, numbers with decimals
2. Two types are: fractional form or exponential form.
3. It must have at least one digit.
4. It must have a decimal point.
5. Default sign is positive. It can be both positive and negative number.
6. No commas and spaces are allowed.
e.g: 12.5,36.456,-259.5 etc.
3. Character Constant
1. Single alphabet, single digit or single special character, space.
2. Enclosed in single quotes.
Eg. ‘a’ , ‘A’ , ‘=’ , ‘3’ etc.
4. String Constant
String means a group of characters
Eg. "Jyoti" "123456789" "2135.145"
Variables
When we want to perform some manipulations or operations on data in a program,
we need some storage that will store the data. For e.g. to add two numbers in a
program (suppose 5 +6), we need three locations, two for the input numbers (5 and
6) and one for the result (11). For storing data and performing operations and
manipulation, we need variable.
A Variable is a named memory location .It is called variable because the data stored
in the memory can be changed.
For e.g., To add two numbers (5 and 6) , we need three locations
So we can name the locations that will store 5 and 6 as num1 and num2 and the
locations that will store the sum can be named as sum or res.
Datatypes in C
Datatype of a variable denotes the type of the value that will be stored in the
variable. Different datatype will require different amount of memory, the range of
values that can be stored in the memory and the operations that can be performed
on specific types.
Let us briefly describe them one by one:
Following are the examples of some very common data types used in C:
• char: The most basic data type in C. It stores a single character and requires a
single byte of memory in almost all compilers.
• int: As the name suggests, an int variable is used to store an integer.
• float: It is used to store decimal numbers (numbers with floating point value)
with single precision.
• double: It is used to store decimal numbers (numbers with floating point value)
with double precision.
Jyoti Chandnani
float 4 %f
double 8 %lf
C Keywords
Keywords are the reserved words in C. These words have some pre-defined
functionality which cannot be changed. Since each keyword has some specific
function, so they cannot be used for another purpose like as a variable or function
name. There are 32 keywords in C language.
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
Installation of TurboC
Step 1: Download Turbo C++ 3.2.
Step 2: Extract “Turbo C++ 3.2 zip” file in the same directory.
Step 3: Run “Setup.exe” file and follow the instructions.
Step 4: You will get the Install Shield Wizard to Turbo C++. Click Next to continue.
Structure of C program
/*comments */
// Sample program
preprocessors
main() function{ }
myfunc()
{
statements 1
statement 2
}
Jyoti Chandnani
First C Program:
We will write our first C Program :
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“\n Hello , Welcome to C Learning “);
getch();
}
→The first line in the code #includes<stdio.h> is preprocessor command which tells
the compiler to include the header file stdio.h before actual compilation. stdio.h is a
header file for standard input output which contains the definition of printf() and
scanf() functions . Similarly, conio.h is also a header file which include definition of
clrscr() and getch() used in the program.
→Every C program starts execution from the main () function and finish execution at
the end of the main ().
→void is a data type which means no return value .Here the main() does not return
any value to the OS(Operating System).
→clrscr() is an in-built function which is used to clear the output screen. It is
available in header file conio.h
→printf() is also an in-built function available in stdio.h header file. It is used to print
on the standard output.
→getch() is also an in-built function in conio.h which is used to hold the output
screen unless a user press any character from keyboard.
NOTE :
1. C language is case-sensitive means ‘x’ and ‘X’ are different.
2. Each instruction in C is written as a statement.
3. All statement should be in same order in which we want to display and execute.
4. Every statement in C program is terminated with (;) semicolon which is also called
statement terminator.
5. We can write two statements in single line separated by semicolon.
6. White spaces and tabs are ignored by the compiler that why it is also called Free
Form Language.
Jyoti Chandnani
Escape sequences
• \n - New line
• \r - Carriage return
• \b - Backspace
• \t - Horizontal tab
• \" - Quotation mark
• \v - Vertical tab
• \' - Apostrophe
• \\ - Backslash
• \? - Question mark
• \0 - Null
E.g.
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
printf("\n C Programming");
printf("\r CPP");
getch();
return 0;
}
-Format string contains placeholders for variables that we intend to receive from
keyboard.
-A & sign comes before each variable name that comes in variable listing.
-Character strings are exceptions from this rule. They will not come with this sign
before them.
Note: You are not allowed to insert any additional characters in format string other
than placeholders and some special characters.
float a;
int n;
scanf("%d%f",&n,&a);
Pay attention that scanf function has no error checking capabilities built in it.
Programmer is responsible for validating input data (type, range etc.) and preventing
errors.
Comments in C
Comments are given to describe the purpose of the program or instructions.
Comments are never compiled. It is a good practice to start a program with comment
telling about the program, date and who has written it.
Any number of comments can be given in a program.
Single line comments can start with // (double front slashes).
e.g : // description of the program
Multiline comments should be enclosed in /* your comments */.
e.g. /* comments enclosed in these characters are multiline comments */.
Jyoti Chandnani
Operators in C
Forms of Operators.
1. Unary- If operator is applied on one operand, it is called Unary operator.
2. Binary : operator is applied on two operands.
3. Ternary : operators are applied on three operands.
Types of Operator:
Assignment Operators:
The function of this operator is to assign the value or values in variable on right hand
side of an expression to the variables on the left hand side.
e.g. a=5 →
a=l*b*0.5; → datatype of L.H.S. should match the datatype of R.H.S.
si= (prin*rate*time);
Jyoti Chandnani
Arithmetic Operator
These are used for performing mathematical operations on numeric values. They are
unary or binary.
Rules :
Parenthesis can be used in same manner as algebraic expressions
Parentheses are at the ‘highest level of precedence’. In case of nested parenthesis
the innermost parenthesis are evaluated first.
Apart from these binary arithmetic operators C contains two more Unary Operators
E.g. n1=8;
n1++; //n1=n1+1
will change n1 value to 9.
Decrement operator or -- will decrement the value of a variable by 1
It can be only applied on numeric value stored in variable. It cannot be directly
operate on constants like --5 will be invalid.
n2=8;
n2--; //n2=n2-1
Will make n2 value to 7
Predecrement will first decrement and then assign the value to variable.
Postdecrement will assign the value to variable and then decrement.
Relational operators
These operators are used for comparing two values whether they are greater or less
than or equal or not equal to other. These operators will either return TRUE or FALSE
value. In C, False is treated as 0 and TRUE means 1.
Logical operator
These operators are used to combine two or more conditions in a program. These
operators return TRUE or FALSE values. The AND (&&), OR(||) and NOT(!) are logical
operators.
AND (&&) returns TRUE when both the conditions are TRUE. If any of condition or
both conditions is FALSE, it returns FALSE.
For eg.: x=10 ,y=10, Z=50
(x==y && x!=Z) --> (true && true) --> true
(x!=y && x!=Z) --> (false && true) --> false
OR(||) return TRUE if any of the one condition or both conditions are TRUE. If both
conditions are FALSE, it returns FALSE.
For Eg :
x=10, y=10, z=50
NOT(!) will complement the condition and return FALSE if condition is TRUE or
returns TRUE if condition is FALSE. It is Unary operator.
Truth table for ! operator:
A b
T F
F T
Bitwise Operators
These are used to perform bit level operations i.e. each operand is first converted to
bit and then operation is performed bit-by-bit.
Operator Functionality
& (AND) Gives 1 in result when both the bits in operands are 1 ,otherwise
gives 0
|(OR) Gives 1 in the result when any one of the bit in operand is 1,if
both bits are 0,it gives 0
^(XOR) Gives 1 if both bits in operand are different, if both bits in
operand are same ,it gives 0
~(complement) Reverse the bits, makes 1 to 0, and 0 to 1
<< Left Shift, it will shift the bits to left by operand specified
>> Right shift, it will shift the bits to right by operand specified
Examples:
Let a=25 and b=39
Then a and b in binary are
27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1
1. A=5 = 4+1 = 0101
0 1 0 1
3. 125=64+32+16+8+4+1 = 01111101
128 64 32 16 8 4 2 1
0 1 1 1 1 1 0 1
More examples
& (AND)
A=8 → 00001000
B=16→00010000
A&B 00000000
Truth Table of AND (&)
A B A&B
1 1 1
1 0 0
0 1 0
0 0 0
OR (|)
Jyoti Chandnani
A B A|B
1 1 1
1 0 1
0 1 1
0 0 0
A=8 → 00001000
B=16→00010000
A|B 00011000
XOR (^)
A B A^B
1 1 0
1 0 1
0 1 1
0 0 0
A=8 → 00001000
B=16→00010000
A^B 00011000
Eg : A=8 → 00001000
~A → 11110111 →
8 = 00001000 (In Binary)
Bitwise complement Operation of ~8
00001000
________
~ 11110111 = 247 (In decimal)
128+64+32+16+0+4+2+1 = 247
The bitwise complement of 8 is 247 (in decimal). The 2's complement of 247 is -9.
Hence, the output is -9 instead of 247.
#include <stdio.h>
void main()
{
printf("Output = %d\n",~35); -(35+1)
printf("Output = %d\n",~-12); -(-12+1)
return 0;
}
Output
Output = -36
Output = 11
Shorthand operators
These operators are used to assign a value to a variable. The left hand side of these
operators contain a variable and right hand side contains a value. The datatype of
value and variable must be same.
Comma Operator :
It is used to separate a pair of expressions.
Generally comma operator is used in for loop for eg.: for(i=0,j=1;i<10;i++)
Conditional Operator:
It is C’s only ternary operator, it takes three operands. The operands together with
conditional operators form a conditional expression.
If the condition is true first expression will be evaluated and if the condition if false
second expression will be evaluated.
For eg: x=(y<20)? 9 : 10 ; → means if y is less than 20 , then x=9 else x=10;
When constants and variables are mixed in an expression, they are converted to the
same type. That is automatic type conversion takes place.
Rules for type conversion:
All chars and short ints are converted to int. All floats are converted to doubles .
means values of small datatype can be converted to large datatype.
Apart from automatic conversion what if we want to convert the type of variable?
What if int is to be converted to float. Yes it can be done by using cast operator.
.
Syntax : (type) expression → type is any C datatype
For example, if you want to make sure that the expression a/5 would evaluate to
type of float you write it as
Cast is an unary operator and has the same precedence as any other unary operator.
The use of cast operator id=s explained in the following example:
main()
{
int num;
printf(“%f%f%f\n”,(float)num/2,(float)num/3,(float)num/3);
}
The cast operator in this example will ensure that fractional part is also displayed on
the screen.
Jyoti Chandnani
Size of Operator
C provides a compile-time unary operator called sizeof that can be used to compute
the size of any object. The expression such as:
Sizeof object and sizeof(type name)
Result in an unsigned integer value equal to the size of the specified object or type in
bytes. Actually the resultant integer is the number of bytes required to store an
object of this type of its operand. An object can be a variable or array or structure.
An array and structure are data structures provided in C, introduced in latter units. A
type name can be the name of any basic type like int or double or a derived type like
a structure or a pointer.
For example,
sizeof(char) = 1 bytes
sizeof(int) = 2 bytes
Priority of Operators
C uses a certain hierarchy to solve such kind of mixed expression. The hierarchy ans
associatively of the operators discussed so far is summarized in Table 6. The
operators written in the same line have the same priority. The higher precedence
operators are written first.
Precedence of the operators
Operators Associativity
() Left to right
!,++,--,(type),sizeof Right to left
/,%,* Left to right
+- <<=>>= Left to right
== != Left to right
&& Left to right
|| Left to right
?: Right to left
=+=- Right to left
=*=/+%=&&=||+
, Left to right
Special Symbols: Some of the symbols have special functionality which is described
as –
1. [ ] :-used as subscripts to refer to the elements of the array
2. { } :- marks the start and end of a block which have more than one instruction
3. () :- used to denote function
4. Semicolon (;) :-used as a statement terminator.
5. *:- used as a pointer variable.