Module 1 Part 1
Module 1 Part 1
C TOKENS
KEYWORDS AND IDENTIFIERS
• Every C word is classified as either a key word or an identifier.
• All keywords have fixed meanings and these meanings cannot be
changed.
• Keywords serve as basic building blocks for program statements.
Operators in C
What is a C operator?
Symbol that helps us to perform some specific
mathematical, relational, bitwise, conditional, or logical
computations on values and variables.
Operatorsin C
It is a Symbol that tells the computer to perform mathematical or logical
manipulations .
1. Arithmetic
2. Relational
3. Logical
4. Assignment
5. Increment and Decrement
6. Conditional
7. Bitwise
8. Special
Arithmetic Operators:
• C provides all the basic arithmetic operators, they are +, -, *, /, %
• Integer division truncates any fractional part.
• The modulo division produces the remainder of an integer division.
• % cannot be used for floating point data.
• C does not have an operator for exponentiation.
For Eg. a=14 and b=4 (Here ”a” and ”b” are variables and are known as operands.)
a-b=10
a+b=18
a*b=56
a/b=3
a%b=2
Relational Operator:
OUTPUT
5>3&&5<10: 1
8<5||5==5: 1
!(8==8): 0
Assignment Operators in C
5. “/=”
This operator is combination of ‘/’ and ‘=’ operators.
• Example: (a/=b) can be written as (a=a/b)
Unary operators (Increment and Decrement )
There are two unary operators in C
Increment and Decrement operators
OUTPUT
a= 11 b= 11
Postfix Increment and decrement #include <stdio.h>
• The operator follows the operand void main()
{
(e.g., a++ and a--).
int a=10,b;
• The value of the operand will be
altered after it is used. b= a++;
printf("a= %d b= %d",a,b);
}
OUTPUT
a= 11 b= 10
Ternary Operator or Conditional Operator (?:)
• Conditional operators are also called ternary operators.
• These are used in decision making statements, return one value if condition is true and
returns another value if condition is false.
General syntax of ternary operator is
Test expression?statement1:statement2;
Eg:
#include <stdio.h>
void main()
{
int a=4,b=2,result=;
result=(a>b? a: b);
printf("%d",result);
}
----------------------
Output
4
Bitwise operators
• These operators are used for manipulation of data at bit level.
• These are used for bit-wise logical decision making.
Output:
Value of Integer Constant:25
Value of Character Constant:A
Value of Float Constant: 15.660000
Data Types in C
Types Description
Primitive data types are the most basic data types that are
Primary Data Types used for representing simple values such as integers, float,
characters, etc.
The data types that are derived from the primary datatypes
Derived Types
are referred to as Derived Data Types.
Primary Data Types in C
• C supports several different types of data, each of which may be represented
differently within the computer’s memory.
The basic data types are listed below:
Typical Memory
Data Type Description
Requirements
2 bytes or one word (varies
int integer quantity from one compiler to
another)
char single character 1 byte
floating-point number
float (i.e., a number containing a decimal point 1 word (4 bytes)
and/or an exponent)
double-precision floating-point number
(i.e., more significant figures, and an
double 2 words (8 bytes)
exponent which may be larger in
magnitude)
Character Set in C
Output: Output:
Enter a character: a Enter a character: a
You entered: a You entered: a
putchar() Function in C
Output: Output:
Enter a character: a Enter a character: a
a a
If Statement
Syntax:
if(expression)
{
//statements to execute if condition is true
}
#include<stdio.h>
main()
{
int x;
x=7;
if(x>3)
{
printf(“Number is greater than 3\n");
}
}
#include <stdio.h>
void main()
{
int x, y;
printf("enter the numbers:\n");
scanf("%d %d",&x,&y);
if (x < y)
{
printf("Variable x is less than y");
}
if (x > y)
{
printf("Variable x is greater than y");
}
if (x==y)
{
printf("x is equal to y\n");
}
}
Find the largest among 3 integer values
#include<stdio.h>
main()
{
int num1,num2,num3;
printf("Enter the number1=");
scanf("%d",&num1);
printf("Enter the number2=");
scanf("%d",&num2);
printf("Enter the number3=");
scanf("%d",&num3);
if(num1>=num2&&num1>=num3)
{
printf("\n %d is the largest number.\n",num1);
}
if(num2>=num1&&num2>=num3)
{
printf("\n %d is the largest number.\n",num2);
}
if(num3>=num1&&num3>=num2)
{
printf("\n %d is the largest number.\n",num3);
}
If–else Statement
Syntax:
if(expression)
{
//code to be executed if condition is true
}
else
{
// code to be executed if condition is false
}
#include <stdio.h>
void main()
{
int n;
printf("enter the number:");
scanf("%d",&n);
if (n>=0)
{printf("Number is positive\n");}
else
{printf("Number is negative\n");}
}
Q) Write a program to test whether number is even or odd.
#include<stdio.h>
void main()
{
int n;
printf("Enter the value of n");
scanf("%d",&n);
if(n%2==0)
{
printf("Even");
}
else
{
printf("Odd");
}
}
To check if a given year is leap year or not
Variable = Expression1?Expression2:Expression3
or
(condition)?(variable = Expression2):(variable=Expression3);
#include<stdio.h> #include<stdio.h>
main() main()
{ {
int waterlevel,alarm; int waterlevel,alarm;
scanf(“%d”,&waterlevel);
scanf(“%d”,&waterlevel);
if(waterlevel>50)
alarm=(waterlevel>50)?1:0;
{
alarm=1; }
}
else
{
alarm=0;
}
}
Largest of three numbers using conditional operator (ternary operator)
#include<stdio.h>
void main()
{
int a,b,c,large;
printf("Enter the value of a, b & c \n");
scanf("%d %d %d",&a,&b,&c);
large =(a>b && a>c)? a: ((b>a && b>c)? b:c);
printf("%d is the largest",large);
}
If else-if ladder Statement
Syntax:
if(condition1)
{
//code to be executed if condition1 is true
}
else if(condition2)
{
//code to be executed if condition2 is true
}
else if(condition3)
{
//code to be executed if condition3 is true
}
...
else
{
//code to be executed if all the conditions are false
}
Program to check whether a
number is positive, negative or if (n > 0)
zero. {
#include<stdio.h> printf("Positive.\n");
void main() }
{ else if (n < 0)
int n; {
printf("Enter the number: "); printf("Negative.\n");
scanf("%d",&n); }
else
{
printf("Zero.\n");
}
}
C Switch Statement
switch(expression)
{
case value1:statement_1;
break; ⟹ The value of the expressions in a
case value2:statement_2; switch-case statement must be
break; integer, char.
. ⟹ Float and double are not
.
. allowed.
case value_n:statement_n;
break;
default:default_statement;
}
Select from menu in restaurant
#include<stdio.h>
main()
{
char choice;
printf("Enter your choice: A.Pizza, B.Burger, C.Pasta\n");
scanf("%c",&choice);
switch(choice)
{
case'A':
printf("You chose Pizza");
break;
case'B':
printf("You chose Burger");
break;
case'C':
printf("You chose Pasta");
break;
default:
printf("Invalid choice");
}
}
C Program to check whether a character is vowel or consonant
#include<stdio.h>
void main()
{
case 'o':
char letter; case 'O':
printf("Enter a character:"); case 'u':
scanf("%c",&letter);
switch(letter)
case 'U':
{ printf("Vowel");
case 'a': break;
case 'A':
case 'e':
default:
case 'E': printf("Consonant");
case 'i': }
case 'I':
}
C Program to Make a Simple Calculator Using switch ... case
#include<stdio.h>
void main()
{
char operation;
float n1,n2,result;
printf("Enter an operator(+,-,*,/):");
scanf("%c",&operation);
printf("Enter two operands:\n");
scanf("%f%f",&n1,&n2);
switch(operation)
{case '+':
result=n1+n2;
break;
case '-':
result=n1-n2;
break;
case '*':
result=n1*n2;
break;
case '/':
result=n1/n2;
break;
default:
printf("Error! Operator is not correct");
}
printf(“The result is %f”,result);
}
C Program to Make a Simple Calculator Using switch ... case
#include<stdio.h>
void main()
{
char operation;
float n1,n2;
printf("Enter an operator(+,-,*,/):");
scanf("%c",&operation);
printf("Enter two operands:\n");
scanf("%f%f",&n1,&n2);
switch(operation)
{
case '+':
printf("%f+%f=%f",n1,n2,n1+n2);
break;
case '-':
printf("%f-%f=%f",n1,n2,n1-n2);
break;
case '*':
printf("%f*%f=%f",n1,n2,n1*n2);
break;
case '/':
printf("%f/%f=%f",n1,n2,n1/n2);
break;
default:
printf("Error! Operator is not correct");
}
}
Menu Driven Program in C
#include <stdio.h>
void main()
{
int x;
printf("Enter the number x: ");
scanf("%d",&x);
switch(x)
{
case 1 ... 30:
printf ("The number you entered is between 1 and 30\n");
break;
default:
printf("The number is geater than 30\n");
}}
Prepare a grade card
case 5:grade='E';
#include<stdio.h>
break;
main()
case 6:grade='D';
{
break;
int marks; case 7:grade='C';
printf("Enter the marks secured:"); break;
scanf("%d",&marks); case 8:grade='B';
char grade; break;
case 9:
switch(marks/10)
case 10:grade='A';
{
break;
case 1:
}
case 2:
printf("Your grade is %c",grade);
case 3:
}
case 4:grade='F';
break;
THE GOTO STATEMENT
Syntax:
To find square root
#include<stdio.h>
#include<math.h>
main()
{
float x,y;
read:
printf("Enter the number x:\n");
scanf("%f",&x);
if(x<0)
goto read;
y=sqrt(x);
printf(“Square root of %f is %f\n",x,y);
goto read;
}
To check whether odd or even.
#include <stdio.h>
void main()
{
int n;
printf("Enter the value of n: ");
scanf("%d",&n);
if(n<0)
goto END;
if(n % 2 ==0)
printf("Even number\n");
else
printf("Odd number\n");
END: printf("**End of program**");
}