Uploaded by Yash Dave

Fundamentals of C Programming Presentation

advertisement
Unit 1.2 –
Fundamentals of C
Programming
Module 2: Fundamentals of
C-Programming
Keywords, Identifiers, Constants and
Variables
Data types in C
Operators in C
Basic Input and Output Operations
Expressions and Precedence of Operators
In-built Functions
Steps in learning English language:
Alphabets
Words
Sentence
Paragraphs
Constants
Variables
Keywords
Instructions
Program
Steps in learning C:
Alphabets
Digits
Special
symbols
The First C Program
/* Program for Addition of two numbers*/
#include<stdio.h>
void main( )
{
int num1, num2,sum ;
// variable declaration
num1=5;
//variable initialization
num2=3;
//variable initialization
printf(“Addition of two numbers is: ”);
sum=num1+num2;
printf ( “sum=%d" , sum ) ;
}
Preprocessor Directive
C preprocessor is a collection of special
statements called the preprocessor directives.
It is executed before the C program is
compiled.
It is useful for including file, substitution of
values etc.
e.g. #include, #define, #undef
Header Files
Header Files
Used for
stdio.h
Input/output functions
math.h
Mathematical functions
string.h
String manipulation functions
malloc.h
Memory allocation/deallocation function
stdlib.h
Some standard library functions
ctype.h
Character manipulation functions
time.h
Time computing functions
graphic.h
Graphical functions
dos.h
Functions linking DOS routines
Library Functions
C Standard library functions or simply C
Library functions are inbuilt functions in C
programming.
The prototype and data definitions of the
functions are present in their respective
header files, and must be included in your
program to access them.
E.g printf(), scanf()
C Libraries and library
functions
Commonly used functions of math.h
Function
Action
double acos(double x)
Returns the arc cosine of x in radians.
double asin(double x)
Returns the arc sine of x in radians.
double atan(double x)
Returns the arc tangent of x in radians.
double atan2(doubly y,
double x)
Returns the arc tangent in radians of y/x
based on the signs of both values to
determine the correct quadrant.
double cos(double x)
Returns the cosine of a radian angle x.
double cosh(double x)
Returns the hyperbolic cosine of x.
double sin(double x)
Returns the sine of a radian angle x.
double sinh(double x)
Returns the hyperbolic sine of x.
double tanh(double x)
Returns the hyperbolic tangent of x.
double exp(double x)
Returns the value of e raised to the xth
power.
Contd…
double pow(double x, double y) Returns x raised to the power of y.
double sqrt(double x)
Returns the square root of x.
double ceil(double x)
Returns the smallest integer value
greater than or equal to x.
double fabs(double x)
Returns the absolute value of x.
double floor(double x)
Returns the largest integer value less
than or equal to x.
double fmod(double x, double
y)
Returns the remainder of x divided
by y.
double log(double x)
Returns the natural logarithm (base-e
logarithm) of x.
double log10(double x)
Returns the common logarithm
(base-10 logarithm) of x.
Double trunc(double x)
truncates the decimal value from
floating point value and returns
integer value
The C Character Set
Tokens
C tokens are the
basic buildings
blocks in C language
which are
constructed together
to write a C program.
Each and
every smallest
individual units in a C
program are known
as C tokens.
Identifiers
Identifier refers to name given to entities
such as variables, functions, structures etc.
Identifier must be unique.
They are created to give unique name to a
entity to identify it during the execution of
the program.
e.g. int money;
double accountBalance;
Rules for writing an identifier
A valid identifier can have letters (both
uppercase and lowercase letters), digits and
underscores.
The first letter of an identifier should be
either a letter or an underscore. However, it is
discouraged to start an identifier name with
an underscore.
There is no rule on length of an identifier.
However, the first 31 characters of identifiers
are discriminated by the compiler.
Constants, Variables and
Keywords
A variable can be defined as a quantity
that varies during program execution.
A constant can be defined as a
quantity that doesn’t change during
the execution of a program.
Keywords are reserved words.
Types of C Constants
Rules for Constructing Integer
Constants
Rules for Constructing Real
Constants
Rules for Constructing
Character Constants
Types of C Variables
The types of variables that language can
support depend on the types of constants
that it can handle.
A particular type of variable can hold only the
same type of constant.
e.g. An integer variable can hold only an
integer constant, a real variable can hold only
a real constant and a character variable can
hold only a character constant.
Rules for Constructing Variable Names
A variable name is any combination of alphabets,
digits or underscores.
The first character in the variable name must be an
alphabet or underscore.
No commas or blanks are allowed within a variable
name.
No special symbol other than an underscore can be
used in a variable name.
Ex.: si_int
m_hra
pop_e_89
C Keywords
Keywords are the words whose meaning has
already been explained to the C compiler.
The keywords cannot be used as variable names
because if we do so we are trying to assign a new
meaning to the keyword, which is not allowed by
the computer.
The keywords are also called ‘Reserved words’.
32 keywords available in C.
Constants, Variables and
Keywords
Data Types in C
Data Types in C
Description
Range
Memory
Requireme
nt
Format
specifier
int
Integer quantity
-32768 to
32767
2 bytes
%d
float
Floating point
number
3.4*10-38 to
3.4*1038
4 bytes
%f
double
Exponential
floating point
1.7*10-308 to
1.7*10308
8 bytes
%lf, %e
char
Single character
-128 to +127
1 byte
%c
Data-typ
e
Formatted & Unformatted
I/O
Formatted Input / Output
scanf( )
Used to read the values for the variables in a C program
from the keyboard.
Syntax:
scanf(“control string”, address list);
e.g. scanf(“%d”,&num1);
printf( )
Used to display some text as output on the monitor.
Syntax:
printf(“control string”);
e.g. printf(“Enter number 1”);
Format Specifiers/Control strings
in C
Unformatted Input functions
These functions are primarily concerned with
reading the character type data from the
keyboard.
getchar( )
reads a single character from the standard input
device.
Syntax : ch=getchar();
gets( )
reads a string from standard input device.
Syntax : gets(string);
getch()
getch() are used to read a character from screen.
Unformatted Output functions
These functions are mainly concerned with
displaying or printing the character type data on
the monitor.
putchar( )
prints a single character on the screen.
Syntax : putchar(ch);
puts( )
prints a string of characters on the screen.
Syntax : puts(string);
putch()
putch() are used to write a character to screen.
Accepting Input from user
#include<stdio.h>
void main( )
{
int num1, num2,sum ;
printf(“Program for Addition of two numbers”);
printf(“Enter number 1”);
scanf(“%d”,&num1);
printf(“Enter number 2”);
scanf(“%d”,&num2);
sum=num1+num2;
printf ( "%d",sum ) ;
}
Operators in C
Sr. No.
Type of operator
Operators
1
Arithmetic
+,-,*,/,%
2
Assignment / Compound
Assignment
=,+=,-=,*=,/=,%=
3
Relational
<,>,<=,>=,
4
Logical
!,&&,||
5
Bitwise
&,|,^,~,<<,>>
6
Conditional (ternary)
?:
7
Increment and Decrement
++,--
8
Special
sizeof()
9
Equality comparison
==, !=
10
Comma operator
,
Conditional operator
This is used to test the relationship between two
variables.
Syntax:
expression ? value1: value2;
Where,
Expression= relational expression
Value1= value to be assigned when the result of
expression is true
Value2=value to be assigned when the result of the
expression is false
Ex:
k=(a>b)?10:20;
Logical operators
Example
Bitwise Operators
Truth Table
Example
a =6;
b=4;
The binary representation:
a = 0110
b = 0100
bitwise AND operation a&b = 0100
Left-shift operator
It is an operator that shifts the number of bits to the
left-side.
Syntax : Operand << n;
Where,
Operand is an integer expression on which the
left-shift operation is to be applied.
n is the number of bits to be shifted.
e.g.
int a = 5;
The binary representation of 'a' = 0101
a << 2;
0101<<2 = 00010100
Right-shift operator
It is an operator that shifts the number of bits to the
right side.
Syntax : Operand >> n;
Where,
Operand is an integer expression on which the
right-shift operation is to be applied.
n is the number of bits to be shifted.
e.g.
int a = 7;
The binary representation of a = 0111
a>>2;
0000 0111 >> 2 = 0000 0001
Increment and Decrement
Operator
Increment and Decrement
Operator
Increment Operators(++) are used to
increased the value of the variable by one.
Decrement Operators(--) are used to decrease
the value of the variable by one.
Type of Increment Operator
pre-increment
In pre-increment first increment the value of
variable and then used inside the expression
Syntax:
++variable
post-increment
In post-increment first value of variable is used in
the expression and then increment the value of
variable
Syntax:
variable++
Type of Decrement Operator
pre-decrement
In pre-decrement first decrement the value of
variable and then used inside the expression
Syntax:
--variable
post-decrement
In Post-decrement first value of variable is used
in the expression and then decrement the value
of variable.
Syntax:
variable--
Solved Examples
Example 1
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b,c;
clrscr();
c=b=a;
b-=a--;
c+=++a;
printf("a=%d\nb=%d\nc=%d\n",a,b,c);
getch();
}
Example 1
#include<stdio.h>
#include<conio.h>
Output:
void main()
{
a=10 b=0 c=20
int a=10,b,c;
clrscr();
c=b=a;
b-=a--;
c+=++a;
printf("a=%d\nb=%d\nc=%d\n",a,b,c);
getch();
}
Example 2
void main()
{
int x=4, y, z;
z= y = x;
y= -- x;
z= x--;
x= --x - z;
printf("y=%d z=%d x=%d", y,z,x);
}
Example 2
void main()
Output:
{
int x=4, y, z;
y=3 z=3 x= -2
z= y = x;
y= -- x;
z= x--;
x= --x - z;
printf("y=%d z=%d x=%d", y,z,x);
}
Unary operators
These are the type of operators that act upon
just a single operand for producing a new
value.
unary minus (-)
unary plus (+)
decrement (- -)
increment (++)
NOT (!)
sizeof ()
Address of operator (&)
Unary Minus
This operator changes the sign of any given
argument. It means that a positive number will
become negative, and a negative number will
become positive.
e.g.
int p = 20;
int q = -p; // q = -20
Unary Plus
This operator changes the sign of any negative
argument. It means that a negative number will
become positive, and a positive number will also
become positive.
e.g
int p = -20;
int q = +p; // q = 20
NOT (!)
This operator is used for reversing the logical
state of the available operand. It means that the
logical NOT operator will make an available
condition to be false if it is already true.
e.g
If p is true, then !p will be false.
If p is false, then !p will be true.
Address of Operator (&)
This type of operator provides the user with the
address of any variable. The address of operator
is used to return the address (memory address)
of any variable.
E.g.
&a
sizeof( ) operator
The sizeof operator is used to calculate the
size of data type or variables.
This operator returns the size of its variable
in bytes.
Ex.
int a,x;
x=sizeof(a);
Then x will be 2
Comma Token
Comma As Separator
Comma act as separator in variables
declarations, function calls, function
definitions.
Comma operator separates the Expressions.
the separated expressions are evaluated from
Left to right.
E.g.
int a=1, b=2, c=3, d=4;
Comma As Operator
The Comma operator Separates the Expressions.
the separated expressions are evaluated from left
to right and value of rightmost expression is the
type and value of the compound expression.
If multiple expressions are separated by Comma
in a group (group means covered by parenthesis),
then all the expressions are evaluated from left to
right and group value is the Right most
expression value.
Example 1
#include<stdio.h>
int main()
{
int a=1, b=2, c ; // here comma is Separator
c=a,b;
// here comma is Operator
printf("a = %d b = %d c = %d",a,b,c);
return 0;
}
Output:
a=1b=2c=1
Example 2
#include<stdio.h>
int main()
{
int a=1, b=2, c ;
// here comma is Separator
c = ( a , b );
// here comma is Operator
printf("a = %d b = %d c = %d",a,b,c);
return 0;
}
Output:
a=1b=2c=2
Expressions & Precedence of
Operators
Operator precedence determines which operator
is performed first in an expression with more than
one operators with different precedence.
Certain operators have higher precedence than
others
E.g the multiplication operator has a higher
precedence than the addition operator.
Within an expression, higher precedence operators
will be evaluated first.
Operators with the highest precedence appear at the top
of the table, those with the lowest appear at the bottom.
In-built Functions
Built-in functions are
also called as library
functions or predefined
functions.
E.g printf()
scanf()
Type Casting
Type casting is process to convert a variable
from one data type to another data type.
For example if we want to store a integer
value in a float variable then we need to
typecast integer into float.
Automatic Conversion
When the type conversion is performed
automatically by the compiler without
programmers intervention, such type of
conversion is known as Automatic conversion
or implicit type conversion or type
promotion.
Char ->int ->long -> float -> double
E.g
int i = 17;
char c = 'c';
int sum;
sum = i + c;
/* ascii value is 99 */
Explicit casting
The value of an expression can be converted to a
different data types if desired.
Explicit type conversion is a type conversion which
is explicitly defined within a program (instead of
being done by a compiler )
Syntax:
(target data type) expression;
Ex.
int k=2;
float term;
term=1/(float)k;
Download