cprogrammingNotes1
cprogrammingNotes1
3) Constants
Fixed values that do not change during the execution of a program.
Following chart shows types constants
Constants
4) Operator:
An operator defined as symbol that tells the computer to perform certain
mathematical or logical manipulation. The C programming has 8 type of
operator are as below
1) Arithmetic Operators
2) Relational Operators
3) Logical Operators
4) Assignment Operators
5) Unary Operators
6) Conditional Operators
7) Bitwise Operators
8) Special Operators
1) Arithmetic Operators:- Symbols which used to perform Arithmetical
operation likes as addition, subtraction , division ,multiplication there +,-,*
,/,% are known as arithmetic operator. These operators are used to
perform following three type of Arithmetic
i) Integer arithmetic:- operation performs on two whole number or
integers number is called as integer Arithmetic .it gives always
integer result. in integer division the fractional part is truncated .
int X=23 ,Y=5;
X + Y=28
X - Y=18
X*Y=115
X/Y= 4
X%Y=3
ii) Floating point arithmetic: Operation performs on two real number
or fractional numbers is called floating point arithmetic. it gives
always floating point result.
float X=14.0,Y=4.0
X+Y=18.0
CProgramming Prof.Rajendra N.Motinge
X+Y=10.0
X*Y=56.0
X/Y=3.50
iii) Mixed Mode Arithmetic: Operation performs on real number and
integer number is called Mixed Mode Arithmetic. it gives always
floating result.
int X=15; float Y=10.0;
X+Y=25.0
X-Y=5.0
X*Y=150.0
X/Y=1.5
2) Relational Operator:- the operator which use to compare two quantities
is called Relational operator or comparison operator. Its result always 0 or
1 ,if expression is true then result is 1 otherwise result is 0.there are six
kinds of relational operator
i) < less than
ii) > greater than
iii) <= less than equal to
iv) >= greater than equal to
v) == equal to
vi) != not equal to
Relational operator in C used in decision making statement and
looping statement.
3) Logical Operator:- when expression has more than one condition and we
want makes certain decision then we use operator is called Logical
Operator. C has 3 kinds of logical operators are as below
i) && (Logical And)
A B A&&B
0 0 0
0 1 0
1 0 0
1 1 1
A has first relational expression and B has second relational expression. When we
used AND operator between this two expression then it gives the result 1 or 0 if both
expression has result 1 then output is 1 otherwise any one expression has 0 then
result is 0.
A has first relational expression and B has second relational expression. When we
used OR operator between this two expression then it gives the result 1 or 0 if any
one expression has result 1 then output is 1 otherwise both expression has 0 then
result is 0.
iii) ! (logical NOT)
A Y=!A
0 1
1 0
When we used not operator then if expression has result 0 then it shows 1 ,or
expression has result 1 then it shows 0.
4) Assignment Operators: it is the combination of Arithmetic operator and
equal sign. In case of assignment operator the left hand side must be a
variable but the right hand side can be an expression or any constant value
it also called short hand assignment operator following table describe it
Assignment operator Statement with short Statement with
hand assignment simple
assignment
+= a+=1 a=a+1
-= a-=1 a=a-1
*= a*=1 a=a*1
/= a/=1 a=a/1
%= a%=1 a=a%1
5) Unary Operator:-
C++ has two unary operator ++(increment) and –(decrement).both this operator can
be used as postfix and prefix. Postfix means it use after operant .prefix means it use
before operant .when we use increment operator then it add one in operant. we use
decrement operator it subtract one from operant.
Fundamental Data type Derived Data Type User Defined Data Type
C) Void Type:
t is Specify the type of function. when function does not return any values
then we specify the return as void Moreover if there is empty parameter list for
function then it is specified as void.
2) Derived Data Types:-
The data type which has some characteristics of primary data type and some of
its own. so that it is also known as secondary data type.Secondary data types
are arrays, function, pointer etc.
3) User Defined data types:
This data type are completely defined by User .its characteristic defined by
user and it as that so that is known as user defined data type .user defined data
type are typedef , enum , structure, union etc
a) Typedef: the typedef feature of C Language allows the user to define an
identifier that would represent an existing data type. This user defined
identifier can be used to declare variable that type
Unformatted formatted
1.getchar() 1.scanf()
2.putchar() 2.printf()
3.getch()
4.getche()
5.gets()
6.puts()
A) unformatted function:
1. getchar()
This is simplest unformatted input function.it used to reading the single character
from standard input unit or device.this function does not require any argument.
Syntax
Character_variable =getchar();
Example
main()
{
Char ch;
ch=getchar();
printf(“%c”,ch);
}
char ch;
ch = getch();
printf("Input Char Is :%c",ch);
}
Here,declare the variable ch as char data type, and then get a value
through getch() library function and store it in the variable ch.And then,print the
value of variable ch. During the program execution, a single character is get or read
through the getch(). The given value is not displayed on the screen and the compiler
does not wait for another character to be typed.And then,the given character is
printed through the printf function.
char ch;
ch = getche();
printf("Input Char Is :%c",ch);
}
Here,declare the variable ch as char data type, and then get a value
through getche()library function and store it in the variable ch.And then,print the
value of variable ch.During the program execution, a single character is get or read
through the getche(). The given value is displayed on the screen and the compiler
does not wait for another character to be typed. Then,after wards the character is
printed through the printf function.
5.gets()
The gets() is the function used to read sequence of character. It terminate only when
Enter is occurred .its syntax is as below
gets(variable_name);
where variable_name is any identifier that used to storing the string here no need to
format specifiers and address operator.
It illustrate by following example
main()
{
char name[10];
printf(“Enter the name”);
gets(name);
printf(“\nname=%s”,name);
CProgramming Prof.Rajendra N.Motinge
}
6.puts()
This is function also used to output the value of the string display on screen.this
function require one argument as variable_name which value we want to display on
screen.
Its syntax is as below
puts( variable_name);
it can be illustrate by following example
main()
{
Char name[10];
printf(“\nEnter the name”);
gets(name);
puts(name);
}
B) formatted function
1) printf()
This is the standard formatted output function.it is defined in the standard
input/output library.it is used to display or output the string that is given in double
quotes.
Syntax
printf(“string”);
or
printf(“control string”,arg1,arg2,….arg n);
where control string consists of following
a) sequence of characters to display
b) type specifier
c) Escape sequence characters
arg1,arg2…….arg n are arguments or variable whose value can be formatted.so as to
format the output
Eg.printf(“%9.2f”,x);
1 2 . 3 4
Eg.printf(“%-9.2f”,x);
1 2 . 3 4
iii)formatting character
%wc
Where w specifies minimum field width
Eg name=’x’;
printf(“%c”,name);
x
Eg name=’x’;
printf(“%4c”,name);
x
Eg name=’x’;
printf(“%-4c”,name);
x
iv)formatting string
the string can be formatted as the same way as character
%ws
Where w specify the width of string
Eg. name= “new delhi”
Printf(“%s”,name)
n e w d e l h i
Eg. name= “new delhi”
Printf(“%6s”,name)
Structures of C program
The C language is known as the procedural language and hence it must be written
according to its structure and procedure. the structure of c program divide into 6
section it shown as below
Documentation section
Linking section
Global Declaration section
Function prototype section
main()
{
Declaration statement;
Executable section;
return statement;
}
Function definition section
Function header
{
Declaration statement;
Executable section;
return statement;
}
Each and every statement of c program terminated by semi colon(;) and is known as
delimiter. opening curly brace indicate start of body and Ending curly brace indicate
end of the body.
i)Documentation section:
this section contain un-executable statement. This section consist of statement that
never executed by compiler. It shows extra information about program like as author
of program. date of creation, title of program and other information about the code.
This comment can be write anywhere in the program .this section is not compulsory
.it can be write by two ways they are as below
1)Single line comments
It is start with //(double back slash) it makes that whole line as comment
Eg. //program for addition of two number
CProgramming Prof.Rajendra N.Motinge
2)Multi line comments
It start with /*(back slash and asterisk mark) and terminated by */(asterisk mark and
back slash) it makes many line between those as comments
Eg.
/* program for addition of two number
Date:-11/11/2008
Author:R.N.Motinge */
ii)Linking section
This section used to include header file which provide the instruction to compiler to
link the function used in program link with system library.#-pre-processor directive
sign use to write this section
Syntax is as below
#include<headerfile.h>
Where headerfile is name of library file that contain the required function definition.
this must be placed before the main section.
Most commonly used linking section
#include<stdio.h>
#include<conio.h>
Stdio.h contain the definition of standard input and output functionConio.h contain
the definition of console input and output function
iii)Global declaration section
This section is used to define variable which include symbolic constants which are
known as pre-processor directives. it does not terminated with semicolon.
Syntax
#define variable_name value
iv) Function prototype section
This section contain the format of user define function. it define the model of
function which later we define in program.it terminated with semicolon
Syntax is as below
Return_type function_name(list of argument);
v) main() function section
CProgramming Prof.Rajendra N.Motinge
this is compulsory section for all type of program.every program contain only one
main section.program execution start with section and finally return again main so
that main section contain three type of statement
1)declaration statement
2)executable statement
3)return statement
Every statement terminated by semicolon, declaration statement contain declaration
of data member with data type. executable statement contain input and output
statement, calculation part, function call etc. every function should return a value so
main section finally contain return statement .if main has no return then we use void
with main.
vi) Subprogram or function definition section
This is user define function. that can call in main section. This section contain
statement same as main section.When the program written by same as its structure
of program and it is ready we must compile and run c program. following diagram
shows process of compilation and running of C program
System Ready
syntax
NO Object code
Error
No error
Correct output
stop