You are on page 1of 34

About C

 C is a general-purpose language which has been closely associated with


the UNIX operating system for which it was developed - since the system and most of
the programs that run it are written in C.
 C is a successor of B language which was introduced around the early 1970s.
 C programming language was developed in 1972 by Dennis Ritchie at bell
laboratories of AT&T (American Telephone & Telegraph), located in U.S.A.
 Dennis Ritchie is known as the founder of C language.
 C -language is Case-sensitive.

1
Character Set of C-Language

 Alphabets : A-Z and a-z

 Digits : 0-9

 Special Symbols : ~ ! @ # $ % ^ & ( ) _ - + = | \ { } [ ] : ; “ ‘ < > , . ? /

 White Spaces : space , Horizontal tab, Vertical tab, New Line

2
C Tokens
 In a passage of text, individual words and punctuation marks are called tokens.
Similarly, in C programming the smallest individual units are known as C tokens.

 Key words, Identifiers, Constants, Operators, Delimiters.

 Comment: are used for increasing readability of the program.


 // (Single line comment)
 /* */ (Multiple line comment)

3
C Key words
 Key words : have a predefined meaning and these meanings cannot be changed. All
keywords must be written in small letters (except additional c99 keywords).
 There are only 32 keywords available in C.

4
Identifiers

Identifiers : names of variables, functions, structures, unions, macros, labels, arrays etc.,

Rules for define identifiers :

a) First character must be alphabetic character or under score


b) Second character onwards alphabetic character of digit or under score.
c) First 31 characters of an identifier are significant.
d) Cannot duplicate a key word.
e) May not have a space or any other special symbol except under score.
Ex. si_int , m_hra , pop_e_89

Note: C – language is Case-sensitive.

5
Constants
 Integer Constants
 25 and 0

 Floating Constants
 3.14159 and 0.1

 Character Constants
 ‘a’ and ‘B’ and ‘+’ and ‘;’ but not “a” or “B”

 String Constants
 “Hello world” , “Have a nice day!”

 Complex Constants
 real part + imaginary part * I ex : 12.3 + 3.45 * I
6
Operators & Delimiters

 Operators : a symbol, which indicates an operation to be performed. Operators are


used to manipulate data in program. (+ , - , * , / % , etc)

 Delimiters : Language Pattern of c-language uses special kind of symbols

 : (colon, used for labels) ; (semicolon terminates statement ) ( ) parameter list

 [ ] ( array declaration and subscript ), { } ( block statement )

 # ( hash for preprocessor directive ) , (comma variable separator )

7
Data Types
Type Typical Size in Bytes Range Format Specifier

 signed char 1 –128 to 127 %c


 unsigned char 1 0 to 255 %c
 int or signed int 2 –32,768 to 32,767 %d
 unsigned int 2 0 to 65,535 %u
 long int or signed long int 4 –2,147,483,6478 to 2,147,483,647 %ld
 unsigned long int 4 0 to 4,294,967,295 %lu
 float 4 3.4e-38 to 3.4e+38 %f
 double 8 1.7e-308 to 1.7e+308 %lf
 long double 10 3.4e-4932 to 1.1e+4932 %Lf

8
Variables
 Variable is a name that can be used to store values, it can take different values but
one at a time. The rule for naming variables are same as for naming identifiers.

 Declaration and initialization of Variables


 int x; //Declaration
 int x=5; //Initialization
 float salary, y=5.9;
 char grade, ch=‘y’;
 long y;

9
Operators
 Operator is a special symbol that tells the compiler to perform specific
mathematical or logical Operation.
 Increment and Decrement Operator
 Arithmetic Operators
 Relational Operators
 Logical Operators
 Bitwise Operators
 Assignment Operators
 Ternary or Conditional Operators

10
Operators

11
Precedence and Associativity of Operators

12
Examples of Operators

13
Examples of Operators

14
Bitwise Operators

15
Relational Operators

16
Conditional or Ternary Operator
 A ternary or conditional operator has the following form:
 exp1 ? exp2 : exp3
 The expression exp1 will be evaluated always. Execution of exp2 and exp3 depends
on the outcome of exp1. If the outcome of exp1 is non zero exp2 will be evaluated,
otherwise exp3 will be evaluated.
 Example:
Output:
int x=1, y ; x=1
y=2
y = ( x ==1 ? 2 : 0 ) ;

17
sizeof() Operator

 Sizeof is a much used operator in the C programming language. It is a compile time


unary operator which can be used to compute the size of its operand.
 Example:

sizeof(char);

  sizeof(int); Output:
1
  sizeof(float); 2
4
  sizeof(double); 8

18
Input-Output in C
 C programming has several in-built library functions to perform input and output
tasks.
 Two commonly used functions for I/O (Input/Output) are printf() and scanf().
 The scanf() function reads formatted input from standard input (keyboard)
whereas the printf() function sends formatted output to the standard output
(screen).

19
Example 1: C Output

#include <stdio.h>
#include <conio.h>
void main()
{
/* my first program in C */
Output:
printf(“Hello World! \n”);
getch(); Hello World!
}

20
Example 2: C Integer Output

#include <stdio.h>
#include <conio.h>
void main()
{
int a=5; Output:
printf(“number=%d”, a);
getch(); number=5
}

21
Example 3: C Integer Input/ Output
#include <stdio.h>
#include <conio.h>
void main()
{
int a;
printf("Enter an integer: ");
Output:
scanf("%d",&a); Enter an integer: 4
printf(“\n number=%d”, a); number=4
getch();
}

22
Example 4: C Character Input /Output
#include <stdio.h>
#include <conio.h>
void main()
{
char ch;
printf("Enter a character: "); Output:
scanf("%c",&ch); Enter a character: g
printf(“\n character=%c” ,ch); character=g
getch();
}

23
if Statement
Syntax: /* check a citizen is eligible for voting */
#include<stdio.h>
if (testExpression)
#include<conio.h>
{ void main()
{
// statements int age;
printf(“Enter the age : ”);
} scanf(“%d”, &age);
if(age >= 18)
{
printf(“Eligible for voting…”);
}
getch(); Output:
} Enter the age: 19
Eligible for voting…
24
if else Statement
/* print a number is even or odd */
Syntax: #include<stdio.h>
#include<conio.h>
if (testExpression) void main()
{
{ int number;
// codes inside the body of if printf(“Enter a number : ”);
scanf(“%d”, &number);
} if((number %2) == 0)
printf(“%d is even number.”,
else number);
else
{
printf(“%d is odd number.”, number);
// codes inside the body of else getch(); Output:
} Enter a number: 19
} 19 is odd number.

25
if else if Statement
/* program to print the grade of student */

Syntax: #include<stdio.h>
#include<conio.h>
if (testExpression) void main() {
int marks;
{// codes inside the body of if
printf("Enter marks : ");
} scanf("%d", &marks);
if(marks >= 75)
else if((testExpression) printf("Distinction");
else if(marks >= 60)
{ // codes inside the body of else
printf("First class");
} else if(marks >= 50)
printf("Second class"); Output:
else else if(marks >= 35) Enter marks: 65
printf("Third class");
{ // codes inside the body of else First class
else
} printf("Failed");
getch();
} 26
Switch Statement
#include<stdio.h>
Syntax: #include<conio.h>
switch(expression)
void main() Output:
{ Choice is 2
{ case constant-expression : statement(s);    int x = 2;
   switch (x)
break; /* optional */
   {
case constant-expression : statement(s);        case 1: printf("Choice is 1");
               break;
break; /* optional */
       case 2: printf("Choice is 2");
/* you can have any number of case statements                break;
*/
       case 3: printf("Choice is 3");
default : /* Optional */
               break;
statement(s);        default: printf("Choice other than 1, 2
and 3");
}
                break;   }
   getch();
} 27
Switch Statement (Example 2)
#include<stdio.h>
#include<conio.h>
void main() {
char grade = 'B';
switch(grade)
{
case 'A' : printf("Excellent!\n" );
break;
case 'B' :
case 'C' : printf("Well done\n" ); Output:
break; Well done
case 'D' : printf("You passed\n" ); break; Your grade is B
case 'F' : printf("Better try again\n" ); break;
default : printf("Invalid grade\n" );
}
printf("Your grade is %c\n", grade );
   getch();
} 28
Loop Statement (for Loop)
/* Print 1 to 5 numbers */
Syntax: #include<stdio.h>
for (initialization; condition test; increment or decrement) #include<conio.h>
{ void main() {
//Statements to be executed repeatedly int i;
} for (i=1; i<=5; i++)
{ Output:
1
printf("%d\n", i);
2
} 3
4
getch();
5
}
29
Various forms of for loop in C
 Here instead of num++, I’m using num=num+1 which is same as num++.

Example: for (num=10; num<20; num=num+1)


 Initialization part can be skipped from loop as shown below, the counter variable is
declared before the loop.

Example: int num=10;

for (;num<20;num++)
 Like initialization, you can also skip the increment part as we did below. In this case
semicolon (;) is must after condition logic. In this case the increment or decrement
part is done inside the loop.

Example: for (num=10; num<20; ) { //Statements

num++; } 30
Contd…
 This is also possible. The counter variable is initialized before the loop and
incremented inside the loop.

Example: int num=10; for (;num<20;) { num++; }


 The counter variable can be decremented as well. In the below example the variable
gets decremented each time the loop runs until the condition num>10 returns false.

Example: for(num=20; num>10; num--)


 Infinite Loop: An infinite loop (or endless loop) is a sequence of instructions in
a computer program which loops endlessly, either due to the loop having no
terminating condition, having one that can never be met.

Ex: for(i = 0; i < 100; i--) Ex: for(; ;) // or equivalently, while (1)

{ printf("%d\n", i); } 31
Loop Statement
while (Entry controlled )
/* sum of 1 to 10 numbers */
Syntax:
#include<stdio.h>
while (condition test)
#include<conio.h>
{
void main() {
//Statements to be executed repeatedly
int i = 1,sum = 0;
// Increment (++) or Decrement (--) Operation Output:
while(i<=10){
} Total: 55
sum = sum + i;
i = i + 1; }
printf(“Total : %d “,sum);
getch();
}
32
Loop Statement
do-while (Exit controlled )
/* print 0 to 3 numbers */
Syntax: #include<stdio.h>
do #include<conio.h>
{ void main() {
//Statements int j=0;
// Increment (++) or Decrement (--) Operation do {
}while(condition test); printf("Value of variable j is: %d\n", j);
j++; Output:
Value of variable j is: 0
}while (j<=3);
Value of variable j is: 1
getch(); Value of variable j is: 2
Value of variable j is: 3
}
33
THANK YOU!!!

34

You might also like