Professional Documents
Culture Documents
1
Character Set of C-Language
Digits : 0-9
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.
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.,
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
7
Data Types
Type Typical Size in Bytes Range Format Specifier
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.
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(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++.
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.
num++; } 30
Contd…
This is also possible. The counter variable is initialized before the loop and
incremented inside the loop.
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