0% found this document useful (0 votes)
2 views23 pages

C Programming Operators and Functions Guide

Uploaded by

abidshahriar283
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views23 pages

C Programming Operators and Functions Guide

Uploaded by

abidshahriar283
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Structured Programming Language

CSE 1203

Introduction

Prepared by
Lt Col Gautom Kumar, psc, Sigs
Associate Professor, Dept. of CSE
Bangladesh University of Professionals
Arithmatic Operators
Operator Name Example
+ Addition 7 + 8, x + y
- Subtraction/unary minus 17 - 11, x - 6
* Multiplication 12 * 13, x * 3
/ Division 18 / 3, x / y
% Modulus 17 % 3, x % 2

2
Sample Program Arithmatic Operator

#include<stdio.h>
int main()
{
int var, var1=9, var2=7;
var = var1 + var1 * var2;
var = var2 – var1 / var2;
var = var1 % var2; //var  2
var1 = – var1; //var1 = var1 * -1;
}

3
Relational Operators
Operator Name Example
> Greater than x>y
< Less than 11 < 17, x < 6
>= Greater than or equal x >= y
<= Less than or equal x <= y
!= Not equal to x != y
== Equal to x == y

4
Sample Program
Relational Operator

#include<stdio.h>
int main()
{
int var, var1=9, var2=7;
var = var1 > var2; //var  1
var = var1 == var2; //var  0
var = var1 != var2 //var  1
var = var1 % 2 == 0 //var  0
}

5
Logical Operators
Operator Name Example
! NOT !(x > y)
&& AND x < 6 && x > 2
|| OR x >= y || a<=b

6
Sample Program
Logical Operator

#include<stdio.h>
int main()
{
int var, var1=9, var2=7;
var = 9 > 7 && var1 != var2; // var  1
var = (var1 == 7) && (var1 > var2); // var  0
var = var1==7 || var2==7; // var  1
var = !(var1 == var2) ; // var  1
}

7
Bitwise Operators
Operator Name Example
~ bitwise Complement ~var
& bitwise AND var1 & var2
| bitwise OR var1 | var2
^ bitwise XOR var1 ^ var2
<< Left shift var1 << 1
>> Right Shift var2 >> 2

8
Assignment Operators
Operator Name Example Meaning
= Assignment Var = 7; Var  7
++ Increment and Assignment var++; var = var + 1;
-- Decrement and Assignment Var--; var = var – 1;
+= Add and assign var += 7; var = var + 7;
–= Subtract and assign var –= var1; var = var – var1;
*= Multiply and assign var *= 7; var = var * 7;
/= Divide and assign var /= var1; var = var / var1;
%= Take modulus and assign var %= var1; var = var % var1;
|= bitwise OR and assign var |= var1; var = var | var1;
&= bitwise AND and assign var &= var1; var = var & var1;
^= bitwise XOR and assign var ^= var1; var = var ^ var1;
<<= Left shift and assign var <<= 2; var = var << 2;
>>= Right shift and assign var >>= var1; var = var >> var1;
9
Constant

➢Four types:
➢Integer constant: 10, 12, -7

➢Floating point constant: 0.12, 12E-3

➢Character constant: ‘a’ ‘A’ Single Quotation

➢String constant: “Hello” Double Quotation

13
Sample Program

#include<stdio.h>

int main()
{
char ch; Input
scanf(“ %c”, &ch);
R
printf(“%c”, ch);
printf(“%d”, ch); Output
R
} 82

14
Sample Program (cont.)

#include<stdio.h>

int main()
{
char ch;
ch = ‘R’;
printf(“%c”, ch);
printf(“%d”, ch); Output
R
} 82

15
Sample Program

#include<stdio.h>

int main()
{
char ch; Input
scanf(“ %d”, &ch);
65
printf(“%c”, ch);
printf(“%d”, ch); Output
A
} 65

16
Function
• Syntax:
• return_type function_name(datatype perameter_name, …){
• statements;
•}
Variable
• Names that point to some memory location.
• Variable must be declared before use.
• Declaration: announcing the properties of variables to the
compiler.
• Properties: 1. Size of the variable, 2. Name
• Int var;
Data type
• Data type defines the memory to be taken.
• It depends on the machine.
• For integers, it can be 2 to 8 bytes.
• We can assign a value while declaring, which is called
initialization.
• int var = 50;
• While updating the value of the variable, we do not need to add
the data type.
• We can assign the value of a variable with another variable.
• int b = a;
Variable naming
• Case sensitive
• No numbers at the front
• No special characters like @, #, %, ^
Output Function
• printf() is the output function.
• It can take at least one to many arguments.
• printf(“aaa”);
• printf(“%d %d”, a, b);
• %d is the placeholder for a variable. (d stands for decimal)
• %d is used for integer variables
• Most common placeholders used in C as follows.
• Specifiers
• %c - a character
• %s - a string
• %d - a decimal integer
• %o - an octal integer
• %x - a hexadecimal integer
• %p - an address (pointer)
• %f - for floats
• %lf – for double
• Filed width and precision
• %06d would print 456 like ;- 000456).
• %.6d would print 4.56 like ;- 4.560000)
• Escape Sequences.
• more often used /t, /n
• Large numbers
• Long long int - %lld
• Long double - %Lf
Input
• In C programming, scanf() is one of the commonly used
function to take input from the user. The scanf() function reads
formatted input from the standard input such as keyboards.
#include <stdio.h>
int main()
{
float num1;
double num2;
printf("Enter a number: ");
scanf("%f", &num1);
printf("Enter another number: ");
scanf("%lf", &num2);
printf("num1 = %f\n", num1);
printf("num2 = %lf", num2);
return 0;
}
Solve the following problems
1. Write a C program to display “This is my first C Program”.
2. Write a C Program to display your Id, Name, Dept. Name and University Name
followed by a newline.
3. Write a C program to add two numbers (2 and 6) and display its sum.
4. Write a C program to multiply two numbers (4 and 5) and display its product.
5. Write a C program to add two numbers (5 and 8) and display its sum like (5 + 8 =
13).
6. Write a C program to input two numbers and display those numbers.
7. Write a C Program to input two numbers as input and display its sum.
8. Write a C Program to input two numbers as input and display its product.
9. Write a C Program to input two float numbers as input and display its sum
[Follow the printing style of problem 5].

You might also like