Programming With C
DATA: Data is a collection of numbers, alphabets or some facts and figures.
INFORMATION: The arrangement of data is called information.
SOFTWARE: Collection of instructions is called software.
PROGRAM: Write the step by step instruction is known as program.
Or,
Creating software is called program.
PROGRAMMING: Write a process of a program is known as programming.
PROGRAMMER: The person who creates software is called programmer.
HISTORY OF ‘C’:
Principal of modcles [Theory]
(1948) E.K.Eduger [Italian Mathematician]
?
B.C.P.L. [Cambridge University]
(1960) Deni Carnign [by the help of Principal of modcles]
?
‘C’ [Bell Laboratories, USA]
(1972) Dennis Ritchie [Father’s of ‘C’]
? there are following types of compiler available in market-
a) Turbo C b) Quick C c) Broadland C
SYMBOLS USED IN ‘C’:
Alphabets:
A, B, C……….Y, Z
A, b, c……….y, z
Digits:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special Symbols:
~ ` ! @ # % ^ & * ( ) _ - + = | \ { } [ ] : ; ” ’ < > , . ? /
!= symbol is used for ‘not equal to’.
‘C’ KEWORDS:
i> auto
ii> break
iii> case
iv> char
v> const
vi> continue
vii> default
viii> do
ix> double
x> else
xi> enum
xii> extern
xiii> float
xiv> for
xv> goto
xvi> if
xvii> int
xviii> long
xix> register
xx> return
xxi> short
xxii> signed
xxiii> sizeof
xxiv> static
xxv> struct
xxvi> switch
xxvii> typedef
xxviii> union
xxix> unsigned
xxx> void
xxxi> volatile
xxxii> while
FEATURES OF ‘C’:
By ‘c’ language we can create-
a. Application Program
b. System Program
c. Middle Level Language Programming
d. Memory & Processor Management Programming
HOW WE CAN OPEN ‘C’:
To open ‘c’ can be used following steps-
Open my computer ? Open c drive ? Open ‘Turbo C’ Folder ? Double click on ‘TC’
NOTES:
a) ‘C’ programming is a case sensitive programming language.
b) Extension should be .c.
c) Use the semicolon (;) for ending the line statement.
A SIMPLE ‘C’ PROGRAM:
#include
main()
{
printf(“I’m Subhash”);
}
Pre Processor #: It is a symbol which is used to add any different files.
stdio.h: It is a header file which is used to accept data from standard input device (key board) and display information on the standard output device (monitor).
NOTE: There are 103 header files used in ‘c’ programming language.
main(): main() is used to print the information and display on the standard output device.
printf: It is used to print the information and display on the standard output device.
? Printf contains two types of arrangements-
a) Actual Argument b) Virtual Argument
In the form of,
printf(“Actual Argument,” Virtual Argument);
NOTES:
a) Use F2 button, to save the program.
b) Every program must be saved by using menu or F2 button with file extension .c.
c) Every program must be compiled by using menu or pressing Ctrl+F9.
d) After compile, to execute be used either run menu or pressing Ctrl+F9.
e) Use Alt+ F5 to view the output.
Wap to print your name & address-
#include
main()
{
printf(“My name is Subhash\n”);
printf(“My address is Patna”);
}
DATATYPE:
It is used to assign different types of value in a program in c programming language.
? there are three types of data type-
1. Integer 2. Decimal 3. Character
1. Integer Data Type: The data type which is used to store nonfractional values then such type of data type is called integer data type.
? there are three types of Integer data type-
a) short b) int c) long
a) short: It is an integer data type which is used to store nonfractional values. It is used to occupy 1 byte of memory space and its range is -16,678 to +16,677.
Wap to input three different numbers and find there product-
#include
void main()
{
Short a,b,c,d;
printf(“Enter first value”);
scanf(“%d”&a);
printf(“Enter second value”);
scanf(“%d”&b);
printf(“Enter third value”);
scanf(“%d”&c);
d=a*b*c;
printf(“product=%d”);
}
scanf: It is a function which is used to accept data type from the standard input device and store at given variable rules to create variable.
THE RULES FOR NAMING VARIABLES:
Deceleration a) must not be begin with digits, b) must not be embedded space.
b) int: It is an integer data type which is used to accept nonfractional no. values. It is used to occupy 2 byte of memory space and its range is -32,678 to +32,677.
Wap to enter three different numbers and find their sum-
Method 1
#include
void main()
{
int a,b,c,d;
printf(“Enter the first value”);
scanf(“%d”,&a);
printf(“Enter the second value”);
scanf(“%d”,&b);
printf(“Enter the third value”);
scanf(“%d”,&c);
d=a+b+c;
printf(“sum=%d”,d);
}
Method 2
#include
void main()
{
int a,b,c,d;
printf(“Enter the 3 values”);
scanf(“%d%d%d”,&a,&b,&c);
d=a+b+c;
printf(“sum=%d”,d);
}
c) Long: It is an integer data type which is used to accept long integer value, which is used to occupy 4 byte of memory space and its range is -2,148,678,678 to +2,148,678,677. The common facts used with long data type is %ld.
Wap to enter seven different numbers and find their product-
#include
void main()
{
long a,b,c,d,e,f,g,h;
printf(“Enter the 7 values”);
scanf(“%d%d%d%d%d%d%d”,&a,&b,&c,&d,&e,&f,&g);
h=a*b*c*d*e*f*g;
printf(“product=%d”,h);
}
2. Decimal Data Type: The data type which is used to store fractional values then such type of data type is called decimal data type.
? there are three types of Decimal data type-
a) float b) double c) long double
a) float: It is a decimal data type which is used to accept the decimal value. It is used to occupy 4 byte of memory space.
Wap to enter the radius of a circle and find the area-
#include
void main()
{
float r,a;
float pie=3.14;
printf(“Enter the radius”);
scanf(“%f”,&r);
a=pie*r*r;
printf(“area=%f”,a);
}
b) Double: It is a data type which is used for advanced calculation. It is used to occupy 8 byte of memory space.
Wap to enter the length and breathe of rectangle and find the area-
#include
void main()
{
double l,b,a;
printf(“Enter the length”);
scanf(“%lf”&l);
printf(“Enter the breath”);
scanf(“%lf”&b);
a=l*b;
printf(“area=%f”,a);
}
c) long double: It is a data type which is used for advanced calculation. It is used to occupy 10 byte of memory space.
Wap to enter the length and breath of rectangle and find the area-
#include
void main()
{
long l,b,a;
printf(“Enter the length”);
scanf(“%Lf”&l);
printf(“Enter the breath”);
scanf(“%Lf”&b);
a=l*b;
printf(“area=%f”,a);
OPERATION RESULT OPERATION RESULT
5/2 2 2/5 0
5.0/2 2.500000 2.0/5 0.400000
5/2.0 2.500000 2/5.0 0.400000
5.0/2.0 2.500000 2.0/5.0 0.400000
3. Character Data Type: The data type which is used to store a character then such type of data type is called character data type.
Wap to enter two float number and find their sum-
#include
void main()
{
float a=1.5,b=2.5,c=0;
char x=’5’;
c=a+b;
printf(“%f%c”,c,x);
DATA TYPE RANGE BYTES FORMAT
Signed char -128 to +127 1 %c
Unsigned char 0 to 255 1 %c
Short signed int -128 to +127 1 %
Short unsigned int 0 to 255 1 %
Signed int -32768 to +32767 2 %d
Unsigned int 0 to 65535 2 %u
Long signed int -2147483648 to +2147483647 4 %ld
Long unsigned int 0 to 4294967295 4 %lu
Float -3.4e38 to +3.4e38 4 %f
Double -1.7e308 to +1.7e308 8 %lf
Long double -1.7e4932 to +1.7e4932 10 %Lf
CONSTANTS:
Constants in c are fixed values that do not change during the execution of a program.
? there are two types of constants in C programming language-
CONSTANTS
_____________________|_____________________
? ?
NUMBER CONSTANTS CHARACTER CONSTANTS
____________|____________ ____________|____________
? ? ? ?
INTEGER REAL SINGLE CHARACTER STRING
CONSTANT MEANING
‘\b’ back space
‘\f’ form feed
‘\n’ new line
‘\r’ carriage return
‘\t’ horizontal tab
‘\v’ vertical tab
‘\’’ single quote
‘\”’ double quote
‘\\’ backslash
‘\0’ null
OPERATORS:
An operator is symbols that tell the computer to perform mathematical or logical manipulations.
1. ARITHMETIC OPERATOR:
+ addition
- subtraction
* multiplication
/ division
% modulus
2. UNARY OPERATOR:
- negative
e.g.- -3
3. RELATIONAL OPERATOR:
< less then
<= less then or equal to
> greater then
=> equal to or greater then
= = equal to
!= not equal to
4. LOGICAL OPERATOR:
&& And
|| Or
! Not
5. CONDITIONAL OPERATOR:
Syntax: (condition)?statement
Ex- a>b?a=1000
6. BIT-BYTE OPERATOR:
>>
<<
7. INCREMENT OR DECREMENT OPERATOR:
a++ Post incremental
a-- Post decrement
++a Pre incremental
--a Pre decrement
CONTROL FLOW STATEMENT OR CONTROL STRUCTURE:
There are three types of control flow statement in c-
a. The if/else if/else statement
b. Switch/case statement
c. Break
d. Continue
e. default
f. Goto statement
g. The conditional operator statement
a. THE if STATEMENT:
Syntax:
if(condition)
statement1;
statement2;
…….
…….
statementn;
b. THE IF_ELSE STATEMENT:
Syntax:
if(condition)
statement1;
statement2;
…….
…….
statementn;
else
statement1;
statement2;
…….
…….
statementn;
Ex-
#include
void main()
{
int a=28;
if(a>=18)
printf(“eligible for voting”);
else
printf(“not eligible for voting”);
}
Write a program to check age>=18 & age<=99 then eligible for voting otherwise print you are younger or older-
#include
void main()
{
int a;
printf(“Enter age:”);
scanf(“%d”,a);
if(a>=18<=99)
printf(“eligible for voting”);
else if(age<18)
printf(“You are younger\n”);
else(age>99)
printf(“you are older\n”);
}
c. SWITCH STATEMENT:
• Faster
• Multi way branching concept
d. GOTO STATEMENT:
e. THE CONDITIONAL OPERATORS:
? there are three types of library function in c-
a. Math Function
b. Character Function
c. String Function
a) MATH FUNCTION:
Ex- abs, ceil, cos, exp, log, pow etc.
FUNCTION PURPOSE
abs(i) Return the absolute value of i(integer)
ceil(d) Round up to the next integer value
sin(d) Return the sine of d
cos(d) Return the cosine of d
tan(d) Return the tangent of d
exp(d) Raise e to the power d(e=Naperian constant)
fabs(d) Return the absolute value of d(d is double)
floor(d) Round down to the next integer value
getchar() Enter a character from the keyboard
log(d) Return the natural logarithm of d
pow(d) Return d1 raised to the power d2
putchar(c) Send a character to the standard output device
rand() Return a random positive integer
sqrt(d) Return the square root of d
toascii(c) Convert value of argument to ASCII
tolower(c) Convert letter to lowercase
toupper sqrt(c) Convert letter to uppercase
sizeof(a) To calculate the size of a
#include
void main()
{
int a;
float b;
long c;
printf(“%d\n”,sizeof(a));
printf(“%d\n”,sizeof(b));
printf(“%d\n”,sizeof(c));
}
Ex-
#include
#include
void main()
{
int a=-10,b=2,c=20;
printf(“%d\n”,abs(a));
printf(“%d\n”,exp(b));
printf(“%d\n”,sin(c));
printf(“%d\n”,cos(c));
printf(“%d\n”,log(c));
printf(“%d\n”,pow(5,2));
printf(“%d\n”,pow(c,b));
}
b) CHARACTER FUNCTION:
Ex- get char() – Echo on without carry return.
getche() – Echo on with carry return
getch() – Echo off with carry return
#include
#include
void main()
{
char a, b, c;
printf(“For using getchar function”);
a=getchar();
printf(“For using getche function”);
b=getche();
printf(“For using getche function”);
c=getch();
clrscr();
printf(“%c%c%c”,a,b,c);
printf(“%c\n”,b);
printf(“%c”,c);
}
c) STRING FUNCTION:
a. strlen() – To calculate length
b. strcat() – To concatenate
c. strcpy() – To copy
d. strncpy() – No. of char copy
e. strrev() – Reverse string
f. strcmp() – Compare between two string
g. strlen() – To calculate length
Syntax: int variable=strlen(string value)
Ex- strlen(“Ram”);
h. strcat() – Concatenate
Syntax: string variable=strcat(string value1,string value2)
Ex- strcat(“Ram”,”Kumar”);
i. strcpy() – To copy
Syntax: strcpy(target variable, sourse variable)
Ex1- strcpy(nn,”Ram”)
Ex2- int a=10;
Char aa[20]
Strcpy(aa,”Ravi”)
j. strncpy() –No. of char copy
k. strrev() –Reverse string
Syntax: string variable=strrve(string value)
Ex- nn=strrev(“Ram”);
Output- Mar
l. strcmp() – Compare between two string
LOOP:
There are three types of loo in c programming language-
i> for loop statement
ii> while loop statement
iii> do/while loop statement
i> for loop statement:
Syntax: for(integer variable=starting value;condition;increment/decrement)
{
Statment1
Statment2
Statment3
…….
…….
}
Wap to print natural no. up to 10 using loop-
#include
void main()
{
int i=0;
for(i=1;i<=10;i++)
printf(“\n%d”,i);
}
Wap to print odd no. up to 10 using loop-
#include
void main()
{
int i;
for(i=1;i<=10;i=i+2)
{
printf(“\n%d”,i);
}
}
Wap to print even no. up to 10 using loop-
#include
void main()
{
int i;
for(i=1;i<=10;i=i+2)
{
printf(“\n%d”,i);
}
}
ii> while loop statement:
iii> do/while loop statement:
FUNCTION:
• A function is a self content program segment that carries out some specific well defined task.
• Every c program content one or more function. One of these function must be called main().
Note: In a user define function in c, it has a name that you called by it and a list of 0 or more arguments or parameters.
SECTION OF FUNCTION:
1. Prototype: data type function name(arg1,arg2….)
2. Declaration: variable=fn(value)
3. Declination: function name()
{
…..
…..
…..
}
#include
int add(int a,int b); ? Prototype
void main()
{
int x=10,y=20,z=0;
z=add(x,y); ? Declaration
printf(“%d”,z);
}
int add(a,b) ? Defination
{
int c;
c=a+b;
return(c);
}
Line function:
#include
void line()
void main()
{
line();
printf(“Institute of Computer Technology”);
line();
printf(“I am a student of Admerit”);
lin();
{
void line()
{
int i=0;
for(i=0;i<=79;i++)
}
printf(“%s”,”_”);
}
}
RECURSION:
STORAGE CLASSES:
1. Auto or Local variable
2. Global variable
3. Static variable
4. Extern variable
1. Auto or Local variable:- A variable declaration within the bracket(braces,{}) of a function is visible only within that function, variable declares within function are called local or Auto variable.
Note:- The keyword auto to declare automatic variable it is optional.
2. Global variable:-A variable declares outside of any function is called global variable.
3. Static variable:- Static variable are defined within individual function and there for have the same scope as Auto or local variable.
4. Extern variable:-It is possible to split a function up into several source file for easier maintenance.
#include
void main()
{
int a=0
extern kk
Q. How to create a own header file?
#include
int a=10;
int b=20;
Note:-
a. Save with .h extention(e.g.-subhash.h)
b. compile only
c. New
#include
#include”subhash.h”
void main()
{
extern int a;
extern int b;
int c=0;
c=a+b;
printf( “%d”,c);
}
Q:- Write a ‘c’ program to reverse digits-
#include
void main()
{
int a,b,c=0;
printf(“enter integer value”);
scanf(“%d”,&a);
while(a>0)
{
b=a%10;
c=c*10+b;
a=a/10;
}
printf(“%d”,c);
}
ARRAY:
• Array is like a container which contains several values of similar data types.
• Elements of array are arranged in sequence in the memory.
• Elements of array are access though index/subscript.
• Index of array always starts from 0 and it is always positive integer value.
• The name of array represents the address of first pocket of array.
DECLARATION:
a. int A[5];
b. int A[]={10,2,7};
c. int A[5]={10,7,9};
Wrong Method:
a. int A[];
b. int A[3]={10,2,3,4};
Wap to accept 5 no. in an array & print the sum of array-
#include
#include
void main()
{
char arr[5],i,sum=0;
clrscr();
for(i=0;i<5;i++)
{
printf(“Enter a no.:”);
scanf(“%d”;&arr[i]);
}
for(i=0;i<5;i++)
sum=sum+arr[i];
printf(“sum=%d”,sum);
getch(); //get a character
}
DOUBLE DIMENSION ARRAY(MATRIX):
int mat[3][2];
int mat[][]={{10,23},{5,2},{2,3,45}};
int mat[3][2]={2,3,4,5,6};
int [3][3]={{7,8},{2,3,4}};
it accepts values rowwise(default).
Wap to accept no. in matrix of size 3*4 and printf it-
#include
void main()
{
Int mat[3][4],r,c;
clrscr();
for(r=0;r<3;r++)
for(c=0;c<4;c++)
{
printf(“Enter a no.:”);
scanf(“%d”,&mat[r][c]);
}
for(r=0;r<3;r++)
{
for(c=0;c<4;c++)
printf(“\t%d”,mat[r][c]);
printf(“\n”);
}
getch();
}
Wap to accept no. in matrix of size 3*4 and print sum of matrix-
#include
void main()
{
Int mat[3][4],r,c,sum=0;
clrscr();
for(r=0;r<3;r++)
for(c=0;c<4;c++)
{
printf(“Enter a no.:”);
scanf(“%d”,&mat[r][c]);
}
for(r=0;r<3;r++)
{
for(c=0;c<4;c++)
sum=sum+mat[r][c];
printf(“sum=%d”,sum);
getch();
}
Wap to accept no. in square matrix of size 3 and print left diagonal sum-
#include
void main()
{
Int mat[3][3],r,c,sum=0;
clrscr();
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
{
printf(“Enter a no.:”);
scanf(“%d”,&mat[r][c]);
}
}
for(r=0;r<3;r++)
{
sum=sum+mat[r][r];
printf(“sum=%d”,sum);
getch();
}
POINTER:
• A pointer is a special variable which is used to store the address of any other variable of pointer.
• To make the pointer variable we use strick(*) sign as prefix with the name of variable.
• Pointer variable increases or decreases by scaling factor.
• Pointer is always of two bytes.
• Pointer can be treated as array bub array can not be treated as pointer.
Syntax: datatype *variablename
Ex- int *p;
Wap a program to show address of the variable-
#include
void main()
{
Int a=10,*p;
P=&a;
printf(“%d”,a);
printf(“%d”,*p);
printf(“%u”,p);
}
? there are two types of pointer variables-
a) & b) *
a) & - Return address of any variables.
b) * - Return value of any address.
#include
void main()
{
int *a=5;
printf(“\n%u”,&a);
printf(“\n%d”,a);
printf(“\n%d”*(&a));
printf(“\n%d”,*a);
}
STRUCTURE:
• C support a constructed data type known as structure, which is a methods for packing data of different types.
• A structure is a convenient tool for handling a group of logically related data items.
#include
#include
struct subhash
{
int rr;
char nn[20];
};
void main()
{
struct subhashx[5];
int i=0,s=0;
for(i=0;i<5;i++)
{
printf(“Enter name”);
scanf(“%s”,x[i].nn);
printf(“Enter roll”);
scanf(“%d”,x[i].rr);
}
printf(“Admerit”);
printf(“\n__”);
printf(“SN. Name Roll”);
printf(“\n__”);
for(i=0;i<5;i++)
{
s++;
printf(“%d%s%d”,sx[i].nn,x[i].rr);
}
getch();
}
|
Reference
https://sites.google.com/site/education4bca/home/programming-with-c
No comments:
Post a Comment