0% found this document useful (0 votes)
2 views

Module 1 Part 1

The document provides an overview of C programming, detailing keywords, operators (arithmetic, relational, logical, assignment, increment/decrement, conditional, bitwise, and special), data types, constants, variables, and control flow statements (if, if-else, switch). It includes examples of using these concepts in code, such as the use of logical operators, conditional operators, and the structure of if statements. Additionally, it explains the getchar() and putchar() functions for input and output operations.

Uploaded by

vismayaarun2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Module 1 Part 1

The document provides an overview of C programming, detailing keywords, operators (arithmetic, relational, logical, assignment, increment/decrement, conditional, bitwise, and special), data types, constants, variables, and control flow statements (if, if-else, switch). It includes examples of using these concepts in code, such as the use of logical operators, conditional operators, and the structure of if statements. Additionally, it explains the getchar() and putchar() functions for input and output operations.

Uploaded by

vismayaarun2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

Part1

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:

• These are the operators used to Compare arithmetic, logical and


character expressions.
• The value of a relational expression is either one or zero.
• It is 1 if the specified relation is true and zero if it is false.
For eg:
a) 10<20 is true
b) 20<10 is false
Logical operator:
• Logical Operators are used when we want to test more than one condition and make
decisions.
• Here the operands can be constants, variables and expressions
• Logical operators are &&, ||, !

Logical AND (&&)


False && false : false Logical NOT(!)
False && true : false !false : true
True && false : false !true : false
True && true : true
LogicalOR(||)
False || false : false
False || true : true
true || false :t rue
true || true : true
WAP to illustrate the use of Logical Operators.
void main()
{
printf(”5>3&&5<10: %d \n”,5>3&&5<10);
printf(”8<5||5==5: %d \n”,8<5||5==5);
printf(”!(8==8): %d \n”,!(8==8));
}

OUTPUT
5>3&&5<10: 1
8<5||5==5: 1
!(8==8): 0
Assignment Operators in C

1. “=”:This is the simplest assignment operator. This operator


is used to assign the value on the right to the variable on the
left.
• Example: a=10;

2. “+=”: This operator is combination of ‘+’ and ‘=’ operators.


• Example: (a+=b) can be written as (a=a+b)

3. “-=” This operator is combination of ‘-‘ and ‘=’ operators.


• Example: (a-=b) can be written as (a=a-b)
Assignment Operators in C
4. “*=”
This operator is combination of ‘*’ and ‘=’ operators.
• Example: (a*=b) can be written as (a=a*b)

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

• Increment or decrement operators are used to change the value of a


variable by 1.
• General Syntax for Increment operator:
i++; and ++i; (Equivalent to i = i + 1)
• General Syntax of decrement operator :
i--; and --i; (Equivalent to i = i - 1)
#include <stdio.h>
Prefix Increment and decrement void main()
• The operator precedes the {
operand int a=10,b;
(e.g., ++a and --a). b= ++a;
• The value of the operand will be printf("a= %d b= %d",a,b);
altered before it is used. }

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.

• Bit-wise logical AND (&)


• It takes two numbers as operands and does AND on every bit of two numbers.
The result of AND is 1 only if both bits are 1.
• Bit-wise logical OR (|)
• It takes two numbers as operands and does OR on every bit of two numbers. The
result of OR is 1 if any of the two bits is 1.
• Bitwise NOT (~)
• It takes one number and inverts all bits of it
Special operators in C
Eg:
• Some of the special operators used in #include<stdio.h>
C are: Void main( )
Comma Operator {
• Used to link expressions int a;
together. float b;
Eg: int a , b=5 , 2;
printf(“Size of int %d”,sizeof(a));
Size of operator printf(“Size of float %d”,sizeof(b));
• Used to return the size of the }
declared datatype.
• It returns the size of a declared
variable or a datatype. Output
Size of int 2 bytes
• sizeof(int); is 2 bytes
Size of float 4 bytes
• sizeof(float); is 4 byte
Constants in C
• Constants in C are fixed values that do not change during the
execution of a program.
• They are also known as literals.
• Constants can be integers, floating-point numbers, characters, or
strings.
#include<stdio.h>
void main()
{
const int a = 25;
const char b = 'A';
const float c = 15.66;
printf("Value of Integer Constant:%d\n",a);
printf("Value of Character Constant:%c\n",b);
printf("Value of Float Constant: %f", c);
}

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.

User Defined Data


The user-defined data types are defined by the user himself.
Types

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

• The character set in C is a collection of characters that are valid in


the C programming language.
• It includes a set of valid characters we can use in our program in
different environments.
Variables in C
• Variable is a name given to the memory location that helps us to store
some form of data and retrieves it when required.
• It allows us to refer to memory location without having to memorize the
memory address.
• A variable name can be used in expressions as a substitute in place of the
value it stores.
Rules for Naming Variables in C
• A variable name must only contain alphabets, digits, and underscore.
• A variable name must start with an alphabet or an underscore only. It cannot
start with a digit.
• No whitespace is allowed within the variable name.
• A variable name must not be any reserved word or keyword.
getchar() Function in C
• getchar() function reads the next character from the
standard input (keyboard).
• This function is commonly used for simple input tasks,
such as reading characters one by one.
• It is defined inside the <stdio.h> header file.
#include <stdio.h> #include <stdio.h>
void main() void main()
{ {
char ch; char ch;
printf("Enter a character: "); printf("Enter a character: ");
scanf("%c",&ch); ch = getchar();
printf("You entered: %c\n",ch); printf("You entered:%c\n",ch);
} }

Output: Output:
Enter a character: a Enter a character: a
You entered: a You entered: a
putchar() Function in C

• It is used to write a single character to the standard output, which


is typically the console or terminal.
• The C library putchar() function is a part of the standard C
library
• It is a simple yet essential function for character output in C
programming.
#include <stdio.h> #include <stdio.h>
void main() void main()
{ {
char ch; char ch;
printf("Enter a character: "); printf("Enter a character: ");
scanf("%c",&ch); ch = getchar();
printf("%c", ch); putchar(ch);
} }

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

1 Year = 365.242375 days

• A leap year is a year that contains an additional day, February 29th,


making it 366 days long instead of the usual 365 days.
• Note: A year is a leap year if “any one of ” the following conditions are
satisfied:
1. The year is multiple of 400.
2. The year is a multiple of 4 and not a multiple of 100.
c program to check if a given year is leap year or not.
#include<stdio.h>
main()
{
int year;
printf("Enter a year:");
scanf("%d",&year);
if((year%4==0)&&(!(year%100==0))||(year%400==0))
{
printf("%d is a leap year",year);
}
else
{
printf("%d is not a leap year",year);
}
}
The Ternary Operator or Conditional Operator (?:)

• The Ternary operator in C is kind of similar to the if-else statement as it


follows the same algorithm as of if-else statement
• But the conditional operator takes less space and helps to write the if-
else statements in the shortest way possible.
• It is also known as the Conditional operator in C as it operates on three
operands.
Syntax:

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

Menu-Driven program using a switch case to calculate:


a) Area of a circle
b) Area of square
c) Area of sphere
#include <stdio.h>
void main()
{
float result;
int choice, num;
do{
printf("Press 1 to calculate area of circle\n");
printf("Press 2 to calculate area of square\n");
printf("Press 3 to calculate area of sphere\n");
printf("Press 4 to exit\n");
printf("Enter your choice:\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
printf("Enter radius:\n");
scanf("%d", &num);
result = 3.14 * num * num;
printf("Area of sphere=");
printf("%f", result);
break;
}
case 2:
{
printf("Enter side of square:\n");
scanf("%d", &num);
result = num * num;
printf("Area of square=");
printf("%f", result);
break;
}
case 3:
{
printf("Enter radius:\n");
scanf("%d", &num);
result = 4 * (3.14 * num * num);
printf("Area of sphere=");
printf("%f", result);
break;
}
case 4:
printf("Thank you");
}}
while(choice<4);
}
C Program to check whether a number is in a range Using switch ... case

#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**");
}

You might also like