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

ME 172 Introduction To Programming Language: Class 3

This document provides an overview of the ME 172 Introduction to C Programming Language class. It includes examples of arithmetic operators in C, functions for getting single characters as input like getch() and getche(), math functions in the math.h header file, using character arrays to store strings, and functions for reading and writing lines of text and characters like scanf, printf, gets, puts, getchar, and putchar. The document concludes by thanking participants and sharing a quote about beginnings.

Uploaded by

Shaumik Rahman
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)
43 views

ME 172 Introduction To Programming Language: Class 3

This document provides an overview of the ME 172 Introduction to C Programming Language class. It includes examples of arithmetic operators in C, functions for getting single characters as input like getch() and getche(), math functions in the math.h header file, using character arrays to store strings, and functions for reading and writing lines of text and characters like scanf, printf, gets, puts, getchar, and putchar. The document concludes by thanking participants and sharing a quote about beginnings.

Uploaded by

Shaumik Rahman
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
You are on page 1/ 18

ME 172

Introduction to C Programming
Language
Class 3
Cyrus Ashok Arupratan Atis
Lecturer,Dept. of ME,BUET

M Aminul Hoque
Lecturer,Dept. of ME,BUET

Courtesy: Dr. Noor Al-Quddus


Dr. Monjur Morshed
M Jamil Hossain

10/3/2015

ME 172 : C Programming Sessional

Performance Test

Write a C program that will divide 29765


apples to 51 buyers. Display how many apples
each buyer will get and how many apples will
be left (Use of arithmetic operator is a must,
do not do the calculations and then print the
desired output).

The getch() function


The getch( ) function reads a single character the instant its typed
without waiting for ENTER.
get
means it gets something i.e. its an input function
ch
means it gets a character

The getche() function


The getche( ) function also reads a single character the instant
its typed without waiting for ENTER and also echoes it.
get
means it gets something i.e. its an input function
ch
means it gets a character
e
means it echoes the character to the screen when
you type it.

Example
#include <stdio.h>
#include <conio.h>
void main(void)
{
char test;
printf(Type any character: );
test = getch();
printf(\nThe character you typed was:
%c,test);
}
Replace getch() with getche()

Arithmetic operators
Operator

Name

Example

Addition

a+b

Subtraction

ab

Multiplication

a*b

Division

a/b

Remainder

a%b

++

Increment

++a/a++

--

Decrement

--a/a--

Arithmetic operator(Contd...)
++a/a++ is equivalent to a=a+1
--a/a-- is equivalent to a=a-1
++m and m++
Prefix operator

Postfix operator

Whats the difference????

Arithmetic operator(Contd...)
x=x*a++

is equivalent to x=x*a ; a=a+1


x=x*++a is equivalent to a=a+1 ; x=x*a
y=y*b-- is equivalent to y=y*b ; b=b-1
y=y*--b is equivalent to b=b-1 ; y=y*b

Arithmetic Operators(Contd...)
Write the following program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=20,x;
x=a*++b;
printf("\n The value of x is:
%d,x);
getch();
}
Replace the line x=a*++b with x=a*b++

Arithmetic Operator(Contd...)
Shorthand Notation
Operator
+=

Description
Add AND assignment operator

Example
C += A is equivalent to
C= C +A

-=

Subtract AND assignment operator

C -= A is equivalent to
C= C -A

*=

Multiply AND assignment operator C *= A is equivalent to


C= C *A

/=

Divide AND assignment operator

C /= A is equivalent to
C= C /A

Practice Example
#include<stdio.h>
void main ()
{
int x=10;
x+=3;
printf(%d,x);
}

Math.h header file


Most of the mathematical functions are placed in math.h header
Some are specified in the stdlib.h header
Some common mathematical functions:
Function Name

Description

exp

returns e raised to the given power (ex)

sqrt

computes square root (x)

cos

computes cosine (cos(x))

pow

raises a number to the given power (xy)

sinh

computes hyperbolic sine (sh(x))

erf

error function

And so on.

The outputs of the functions are of the double format.

Math.h header file


Math Constants:
Constant Name

Description

M_E

The base of natural logarithms (e).

M_LOG2E

The base-2 logarithm of e.

M_PI

3.141593

M_SQRT2

The positive square root of 2.

M_SQRT1_2

The positive square root of 1/2.

And so on.

Practice Problem
Write a program that takes two numbers as input.
Find the square root of the first number and the resulting
output will be the radius of a cylinder.
Raise the second input number to a power of 5. The
resulting output will be the height of the cylinder.
Find the volume of the cylinder by using the saved value of
pi in the header file.
Remember to use the math.h file.

10/3/2015

ME 172 : C Programming Sessional

13

Character type arrays


Declaration :
char variable_name[size of array]
Example :
char name[50]
Size must be no. of characters needed + 1. In C, a string of characters is
stored in successive elements of a character array and terminated by
the NULL character. The first array element is name[0], then name[1]
and so on.
Do not declare the size smaller than the desired input.
10/3/2015

ME 172 : C Programming Sessional

14

Reading and Writing a Line of Text


(using scanf and printf)
#include<stdio.h>
void main()
{
char line[80];
scanf("%[^\n]",line);
printf("%s",line);
}

gets() and puts() function


General format
gets(string name);
puts(string name);
#include<stdio.h>
void main()
{
char dept[50];
printf(Enter your Department Name:\n");
gets(dept);
puts(dept);
}

getchar() and putchar() function


#include <stdio.h>
void main()
{
char a;
printf(Enter your Department Name:\n");
a=getchar();
putchar(a);
}

Thank you
Everything has its beginning. But it doesn't start at "one."
-Big Boss (Metal Gear Solid 4)

10/3/2015

ME 172 : C Programming Sessional

18

You might also like