C Unit-1 PPT
C Unit-1 PPT
Language
Unit -1
Introduction of ‘C’
• C was originally developed in the 1970s, by Dennis Ritchie at
Bell Telephone Laboratories, Inc. to develop the UNIX
operating system.
• C programming is considered as the base for other
programming languages, that is why it is known as mother
language.
• C is a High level , general –purpose structured programming
language.
• Advantages of 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
The Character set of ‘C’
• C language consist of some characters set, numbers and some
special symbols.
• The character set of C consist of all the alphabets of English
language.
• C consist of
Alphabets a to z, A to Z
Numeric 0,1 to 9
Special Symbols {,},[,],?,+,-,*,/,%,!,;,and more
• The words formed from the character set are building blocks
of C and are sometimes known as tokens. These tokens
represent the individual entity of language. The following
different types of token are used in C
1) Identifiers 2)Keywords 3)Constants
4) Operators 5)Punctuation Symbols
Identifiers
do if static while
Variables
• A variable is a name of the memory location. It is used to store data.
Its value can be changed, and it can be reused many times.
• It is a way to represent memory location through symbol so that it
can be easily identified.
Integer Types:
The integer data type in C is used to store the whole numbers without
decimal values. Octal values, hexadecimal values, and decimal values can
be stored in int data type in C.
Range: −32,768 to 32,767
Size: 2 bytes
Character Types:
Character data type allows its variable to store only a single character.
Range: −128 to 127
Size: 1 bytes
Float Types:
Float data type in C is used to store decimal and exponential values. It is
used to store decimal numbers (numbers with floating point values)
with single precision
Range: 1.2E-38 to 3.4E+38
Size: 4 bytes
Double Types:
A Double data type in C is used to store decimal numbers (numbers with
floating point values) with double precision.
Range: 1.7E-308 to 1.7E+308
Size: 8 bytes
Constants
• A constant is a value or an identifier whose value cannot be altered in a
program.
• For example: 1, 2.5
Integer constants:
Character constants:
String constants:
These Constants are used in output functions. They have one character
which is preceded by a backslash (\). These are also called an escape
sequences.
Sequences Character
\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\' Single quotation mark
\" Double quotation mark
\? Question mark
\0 Null character
Operators in C
• An operator is a symbol that help us to perform specific
mathematical and logical computations on operands..
• C programming has wide range of operators to perform various
operations.
• Arithmetic Operators
• Unary or Increment and Decrement Operators
• Relational Operators
• Logical Operators
• Assignment Operators
• Shorthand Assignment Operators
• Conditional Operators
• Bitwise Operators
• Comma Operators
Arithmetic Operator:
•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.
•If the expression1 returns false value then the expression3 will
execute.
Comma Operator:
Example:
int a, b, c;
Example:
In the second statement, value of b will be 30, because 10, 20, 30 are
enclosed in braces, and braces has more priority than assignment (=)
operator. When multiple values are given with comma operator within the
braces, then right most value is considered as result of the expression.
Thus, 30 will be assigned to the variable b.
Bitwise Operators:
Bitwise operator works on bits and perform bit-by-bit operation. The truth
tables for &, |, and ^ is as follows −
p q p&q p|q p^q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Assume A = 60 and B = 13 in binary format, they will be as follows −
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
Operator Description Example
& Binary AND Operator copies a bit to the (A & B) = 12, i.e.,
result if it exists in both operands. 0000 1100
| Binary OR Operator copies a bit if it exists in (A | B) = 61, i.e.,
either operand. 0011 1101
^ Binary XOR Operator copies the bit if it is set (A ^ B) = 49, i.e.,
in one operand but not both. 0011 0001
~ Binary One's Complement Operator is unary (~A ) = ~(60), i.e,. -
and has the effect of 'flipping' bits. 0111101
<< Binary Left Shift Operator. The left operands
A << 2 = 240 i.e.,
value is moved left by the number of bits
1111 0000
specified by the right operand.
>> Binary Right Shift Operator. The left operands
value is moved right by the number of bits A >> 2 = 15 i.e.,
specified by the right operand. 0000 1111
First C Program
#include <stdio.h>
void main()
{
printf("Hello World.");
}
There are 2 ways to compile and run the c program, by menu and by
shortcut.
By menu:
• Click on the compile menu then compile sub menu to compile the c
program.
• Click on the run menu then run sub menu to run the c program.
By shortcut:
printf() function:
The printf() function is used for output. It prints the given statement to the
console.
printf("format specifier",argument_list);
scanf() function:
The scanf() function is used for input. It reads the input data from the console.
scanf("format specifier",&argument_list);
Program to print sum of 2 numbers
#include<stdio.h>
int main()
{
int x,y,result;
result=x+y;
printf("sum of 2 numbers:%d ",result);
return 0;
}
C Control Structures
• Branching
• Looping
• If statement
• If-else statement
• If else-if ladder
• Nested if
• Switch Statement
If Statement
• It takes an expression in parenthesis and an statement or block of
statements in curly braces.
• If the expression is true then the statement or block of statements gets
executed otherwise these statements are skipped.
• The syntax of the if statement is given below.
if(expression)
{
Block of statements;
}
• Example: To check a number is even. #include<stdio.h>
void main()
{
int number;
printf("Enter a number:");
scanf("%d",&number);
if(number%2==0)
{
printf("%d is even number",number);
}
If-else statement
if(expression)
{
Block of statements;
}
else
{
Block of statements;
}
Example: To check whether a number is even or odd.
#include<stdio.h>
void main()
{
int number;
printf("enter a number:");
scanf("%d",&number);
if(number%2==0)
{
printf("%d is even number",number);
}
else
{
printf("%d is odd number",number);
}
}
If else-if ladder Statement
• In if-else-if ladder statement, if a condition is true then the statements
defined in the if block will be executed, otherwise if some other condition is
true then the statements defined in the else-if block will be executed, at the
last if none of the condition is true then the statements defined in the else
block will be executed.
• The syntax of the if statement is given below.
if(expression)
{
Block of statements;
} else if(expression)
{
Block of statements;
} else if(expression)
{
Block of statements;
} else
{
Block of statements;}
Example: To calculate the grade of the student according to the specified marks.
#include <stdio.h>
void main()
{
int marks;
printf("Enter your marks?");
scanf("%d",&marks);
if(marks > 85 && marks <= 100)
{
printf("Congrats ! you scored grade A ..."); }
else if (marks > 60 && marks <= 85)
{
printf("You scored grade B + ..."); }
else if (marks > 40 && marks <= 60)
{
printf("You scored grade B ..."); }
else if (marks > 30 && marks <= 40)
{
printf("You scored grade C ..."); }
else
{
printf("Sorry you are fail ..."); }
}
Nested if statements
#include <stdio.h>
void main ()
{
int a = 100;
int b = 200;
if( a == 100 )
{
if( b == 200 )
{
printf("Value of a is 100 and b is 200\n" );
}
}
default:
code to be executed if all cases are not matched;
}
Example:
#include<stdio.h>
void main()
{
int number;
printf("enter a number:");
scanf("%d",&number);
switch(number)
{
case 10: printf("number is equals to 10");
break;
case 50: printf("number is equal to 50");
break;
case 100: printf("number is equal to 100");
break;
default: printf("number is not equal to 10, 50 or 100");
}
}
Looping
• do while
• while
• For
do while loop
• The do while loop is a post tested loop. Using the do-while loop, we can
repeat the execution of the statements. The do-while loop is mainly
used in the case where we need to execute the loop at least once.
• The syntax of the do-while loop is given below:
do{
//code to be executed
}while(condition);
Example: To print the table of 2.
#include<stdio.h>
void main()
{
int i=1, number;
printf("Enter a number: ");
scanf("%d",&number);
do
{
printf("%d \n",(number*i));
i++;
}while(i<=10);
}
while loop
• While loop is also known as a pre-tested loop. A while loop allows a part
of the code to be executed multiple times depending upon a given
condition.
• The syntax of while loop is given below:
while(condition)
{
//code to be executed
}
Example: To print the table of 2.
#include<stdio.h>
void main()
{
int i=1, number;
printf("Enter a number: ");
scanf("%d",&number);
while(i<=10)
{
printf("%d \n",(number*i));
i++; } }
for loop
• A for loop is a repetition control structure that allows you to efficiently write
a loop that needs to execute a specific number of times.
• The syntax of a for loop is given below:
for ( initialise; condition; increment )
{
//code to be executed
}
Example: To print the table of 2.
#include<stdio.h>
void main()
{
int i, number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=10;i++)
{
printf("%d \n",(number*i));
}
}
break statement