[go: up one dir, main page]

0% found this document useful (0 votes)
134 views62 pages

C Language 1

The document provides an overview of the C programming language. It discusses the history and features of C, data types, variables, constants, operators, input/output functions, flow control structures like if/else statements and loops, functions, arrays, pointers, structures, and file I/O in C. The document is an introductory guide to learning C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views62 pages

C Language 1

The document provides an overview of the C programming language. It discusses the history and features of C, data types, variables, constants, operators, input/output functions, flow control structures like if/else statements and loops, functions, arrays, pointers, structures, and file I/O in C. The document is an introductory guide to learning C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 62

ud

PROGRAMMING LA

C LANGUAGE
BASIC OF C

ABHISHEK SHARMA
C PROGRAMMING

INDEX
INTRODUCTION
KEYWORDS & IDENTIFIER, VARIABLES &
CONSTANTS, DATA TYPES, I/O,
OPERATORS
FLOW CONTROL
IF/ELSE , FOR LOOP , WHILE LOOP , BREAK
& CONTINUE , SWITCH CASE , GOTO
FUNCTIONS
STANDAD LIBRARY , USER DEFINE , TYPES ,
RECURTION , STORAGE CLASS
ARRAYS
ONE-DIMENTIONAL , MULTI-
DIMENTIONAL , FUNCTIONS
POINTERS
C PROGRAMMING
ARRAYS , FUNCTION , MEMORY
ALLOCATION
STRINGS
DEFINATION , FUNCTION
STRUCTURE & UNION
STRUCT & POINTERS , STRUCT &
FUNCTIONS ,UNIONS
FILES
I/O FILES
C PROGRAMMING
1.INTRODUCTION
. C programming language developed
by
DENNIS RITCHIE in 1972 at the bell
telephone laboratory.
. C was invented to write an operating
system called UNIX.
. C is a successor of B language which was
introduced around the early 1970s.
. The language was formalized in 1989 by
the AMERICAN NATIONAL STANDARD
INSTITUTE.
. The UNIX OS was totally written in C.
FEATURES OF C
. It is a high level language.
. It is a small language and having only 32
keywords.
C PROGRAMMING
. It is a core language as many language
are depends on C.
. It is portable.
. It is having built-in functions and
operators.
. Compiler and execution is faster rather
than other language.
. It support feature of dynamic memory
allocation.
. It is a case sensitive language.
STRUCTURE OF C PROGRAM
. Document Section(optional)
. Link Section
#include<stdio.h> and many more.
. Definition section
. Global declaration section
C PROGRAMMING
. Main section
. Sub program section (user define
function). [optional]
CHARACTER SET
. A character set is a set of alphabets,
letters, and some special characters that
are valid in C language.
. ALPHABETS
. DIGITS
. SPECIAL CHARACTER
. WHITE SPACE CHARACTER
CONSTANT IN C
. Constants are having fix values.
. We have two types of constants:
.Numeric constants
. Integer constants
C PROGRAMMING
. decimal
. octal
. hexadecimal
. Floating/Real constants
.Character constants
. Single character constants
. String character constants
. Sky values
A to Z = 65-90
a to z = 97-122
0 to 9 = 48-57
Special character = 0-47,58-64,91-96
VARIABLES IN C
. Variable is a name that is used to store
the value of data.
. Declaration of variable
C PROGRAMMING
Datatype variablename;
int a;
. Initialization of variable
a =10;
. Rules for constructing a variable
. abc , average , sum12 ,sum_12 , are
valid in C.
. sum 12 , sum$12 , sum’12 , 123 are not
allowed in C.
. We can not use keyword as variable
name.
KEYWORDS
. Keywords are predefined reserved words
used in programming that have special
meaning to the compiler.
C PROGRAMMING
. Keywords are part of syntax and they can
not be used as identifier.
. For example: int money;
. As C is a case sensitive language, all
keyword must be written in lower case.
. In C language we have 32 keywords.
NOTE: \n is used for new line, \t is used
for take a 8 space gap.
IDENTIFIER
. Identifier refers to name given to entities
such as variables, functions, structures
etc.
. Identifier must be unique. They are
created to given a unique name to an
entity to identify it during the execution of
the program.
C PROGRAMMING
. For example: int money, double
actbalance.
. Here money & actbalance are
identifiers.
. Also remember, identifier name must be
different from keywords.
RULES OF NAMING IDENTIFIER
. A valid identifier can have letter (both
upper-case and lower-case), digits and
underscore.
. The first letter of an identifier should be
either a letter or an underscore.
. You can not use keywords like int, while
as identifiers.
. There is no rule of how long an identifier
can be. However, you may run into
problems in some compiler if the identifier
is longer then 31 characters.
C PROGRAMMING
. You can choose any name as an identifier
you follow the above rule. However, give
meaningful names to identifier that make
sense.
DATA TYPES
. In C programming data types are
declaration for variables. This determine
the type and size of data associated with
variables.
. For example: int myVAR
. Here, myVAR is variable of int type. The
size of int is 4 bytes.

TYPE SIZE (BYTES) FORMAT SPECIFIER


int at least 2,usally 4 %d
char 1 %c
float 4 %f
double 8 %lf
short int 2 usually %hd
unsigned int at least 2, usually 4 %u
long int at least4, usually 8 %ld,%li
long long int at least 8 %lld,%lli
unsigned long int at least 4 %lu
C PROGRAMMING
unsigned long long int at least 8 %llu
signed char 1 %c
unsigned char 1 %c
long double at least 10, usually 12 or 16 %Lf

SIGNED & UNSIGNED


. Signed and Unsigned are type modifiers.
You can alter the data type by using them.
. For example: unsigned int x, int y;
. Here, the variable x can hold only zero
and positive value because we have used
the unsigned variable.
DERIVED DATA TYPES
. Data types that are derived from
fundamental data types are called derived
data types.
. For example: arrays, pointers, functions
types, structures etc.
USER DEFINE DATATYPE
C PROGRAMMING
. typedef data type
. enum (enumerated) data type
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
short b;
long c;
double d;
clrscr();
printf(“The size of int=%d\n”, sizeof(a));
printf(“The size of short=%d\n”, sizeof(b));
printf(“The size of long=%d\n”,sizeof(c));
printf(“The size of double=%d\n”,sizeof(d));
getch();
C PROGRAMMING
}
sizeof() function tells us the size of the
keyword.
NOTE: to run a program press ctrl+f9 ,
to compile a program press alt+f9 , to
save a program press f2.
INPUT/OUTPUT(I/O)
. printf() is one of the main output
function. he function send formatted
output to the screen .
. All valid C programs must contains the
main() function. The code execution
begins from the start of the main()
function.
. To use printf() in our program, we need
to include stdio.h header file using the
#include<stdio.h> statement.
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 keyboard.
NOTE: to exit from turbo C compiler
press alt+x
OPERATORS
. An operator is a symbol that operates on
a value or a variable. For example: + is an
operator to perform addition.
. C has a wide range of operators to
perform various operations.
. Arithmetic Operators
. Increment and Decrement Operators
. Assignment Operators
. Relational Operators
. Logical Operators
C PROGRAMMING
. Bitwise Operators
NOTE: to copy something press
ctrl+insert, to paste something press
shift+insert .
Arithmatic Operators
(+) for addition
(-) for substraction
(*) for multiply
(/) for division
(%) for remainder

Increment & Decrement Operators


(++) for increase the no by 1
(--) for decrease the no by 1

PROGRAM FOR ARITHMATIC OPERATORS


#include<stdio.h>
#include<conio.h>
void main()
C PROGRAMMING
{
int a=10,b=5,c;
clrscr();
c=a+b;
printf(“Addition of a & b=%d\n”,c);
c=a-b;
printf(“Subtraction of a & b=%d\n”,c);
c=a*b;
printf(“Multiply of a & b=%d\n”,c);
c=a/b;
printf(“Division of a & b=%d\n”,c);
c=a%b;
printf(“Remainder of a & b=%d\n”,c);
getch();
}
Output
Addition of a & b=15
C PROGRAMMING
Subtraction of a & b=5
Multiply of a & b=50
Division of a & b =2
Remainder of a & b=0

PROGRAM FOR INCREMENT &


DECREMENT OPERATORS
#include<stdio.h>
#include<cono.h>
void main()
{
int a=10,b=20;
clrscr();
printf(“Increment of a=%d”,++a);
printf(“\nDecrement of b=%d”,--b);
getch();
}
C PROGRAMMING
OUTPUT
Increment of a=11
Decrement of b=19

ASSIGNMENT OPERATORS

Assignment Operators
Operators Example Same as
= a=b a=b
+= a+=b a=a+b
-= a-=b a=a-b
*= a*=b a=a*b
/= a/=b a=a/b
%= a%=b a=a%b

PROGRAM FOR ASSIGNMENT


OPERATORS
#include<stdio.h>
C PROGRAMMING
#include<conio.h>
void main()
{
Int a=9,b=5;
clrscr();
printf(“%d+%d=%d”,a,b,a+b);
a+=b;
printf(“\na+b=%d”,a);
a-=b;
printf(“\na-b=%d”,a);
a*=b;
printf(“\na*b=%d”,a);
a/=b;
printf(“\na/b=%d”,a);
getch();
}
OUTPUT
C PROGRAMMING
a+b=14
a+b=14
a-b=4
a*b=20
a/b=4
RELATIONAL OPERATOR

Relational Operators
== equal to
> greater than
>= greater then or equal to
< less than
<= less than or equal to
!= not equal to

PROGRAM FOR RELATIONAL OPERATORS


#include<stdio.h>
#include<conio.h>
void main()
C PROGRAMMING
{
int a=2,b=2;
clrscr();
printf(“a is equal to b=%d”,a==b);
printf(“a is greater then or equal to b=
%d”,a>=b);
printf(“a is greater then b=%d”,a>b);
printf(“a is less then or equal to b=
%d”,a<=b);
printf(“a is less then b=%d”,a<b);
printf(“a is not equal to b=%d”,a!=b);
getch();
}
OUTPUT
a is equal to b=1
a is greater then or equal to b=1
a is greater then b=0
C PROGRAMMING
a is less then or equal to b=1
a is less then b=0
a is not equal to b=0
LOGICAL OPERATORS

Logical Operators
&& logical AND, true only if all conditions are true
|| logical OR, true if either one condition is true
logical NOT, change the result if the condition is true than it change
! into false and if the condition is false than it change into true

PROGRAM FOR LOGICAL OPERATORS


#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=10;
clrscr();
printf(“a==b &&a>=b =%d”,(a==b)&&(
C PROGRAMMING
a>=b));
printf(“\na==b::a>b=%d”,(a==b)::(a>b));
printf(“\n!(a==b)=%d”,!(a==b));
getch();
}
OUTPUT
a==b && a>=b =1
a==b::a>b =1
!(a==b) =0

BITWISE OPERATORS
Bitwise Operators
input input output
A B C
AND 0 0 0
0 1 0
1 0 0
1 1 1
OR 0 0 0
0 1 1
1 0 1
C PROGRAMMING
1 1 1
XOR 0 0 0
0 1 1
1 0 1
1 1 0

Operation Decimal Binary


2 10
3 11
AND 2 10
OR 3 11
XOR 2 01
Complement 0 00

PROGRAM FOR BITWISE OPERATORS


#include<stdio.h>
#include<conio.h>
void main()
{
int a=2,b=3;
clrscr();
printf(“a&b=%d\n”,a&b);
C PROGRAMMING
printf(“a:b=%d\n”,a:b);
printf(“a^b=%d\n”,a^b);
printf(“a~b=%d\n”,~b);
getch();
}
OUTPUT
a&b=2
a:b=3
a^b=1
~a=-4
2.FLOW CONTROL
if…else statement
. if..else statement is a conditional
statement.
. It uses relational operators for
comparison.
C PROGRAMMING
. In C programming we have following
if..else statement
. if
. if else
. if else ladder
. nested if

PROGRAM FOR IF STATEMENT


#include<stdio.h>
#include<conio.h>
void main()
{
int a=3,b=2;
clrscr();
if(a>b)
{
C PROGRAMMING
printf(“a is greater than b”);
}
getch();
}
OUTPUT
A is greater than b

PROGRAM FOR IF ELSE STATEMENT


#include<stdio.h>
#include<conio.h>
void main()
{
int a=1,b=2;
clrscr();
if(a>b)
{
printf(“a is greater than b”);
C PROGRAMMING
}
else
{
printf(“b is greater than a”);
}
getch();
}
OUTPUT
B is greater than a

PROGRAM FOR IF ELSE IF LADDER


STATEMENT
#include<stdio.h>
#include<conio.h>
void main()
{
int a=2,b=4,c=5;
C PROGRAMMING
clrscr();
if(a>b)
{
printf(“a is greater than b”);
}
else
if(a>c)
{
printf(“a is greater than c”);
}
else
{
printf(“a is lesser than b and c”);
}
getch();
}
OUTPUT
C PROGRAMMING
a is lesser than b and c

PROGRAM FOR NESTED IF ELSE


STATEMENT
#include<stdio.h>
#include<conio.h>
void main()
{
int a=8,b=10,c=9;
clrscr();
if(a>b)
{
if(a>c)
{
printf(“a is greatest\n”);
}
else
C PROGRAMMING
{
printf(“a is lesser than c\n”);
}
}
else
{
printf(“a is smallest\n”);
if(b>c)
{
printf(“b is greater than c\n”);
}
else
{
printf(“b is lesser than c\n”);
}
}
getch();
C PROGRAMMING
}
OUTPUT
a is smallest
b is greater than c
for loop
. The syntax of the for loop is:
. for(initialization; condition; updation )
{
//statement inside the loop
}

PROGRAM FOR for LOOP


#include<stdio.h>
#include<conio.h>
void main()
{
int a, mul, i ;
C PROGRAMMING
clrscr();
printf(“which no table do you want to
print=”);
scanf(“%d”,&a);
for(i=1;i<=10;i++)
{
mul=a*I;
printf(“%d*%d=%d\n”,a,i,mul);
}
getch();
}
OUTPUT
which no table do you want to print=2
2*1=2
2*2=4
2*3=6
2*4=8
C PROGRAMMING
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20
while loop
. The syntax of the while loop is:
. while(test expression)
{
//statement inside the body of the loop
//update expression
}

PROGRAM FOR while LOOP


#include<stdio.h>
#include<conio.h>
C PROGRAMMING
void main()
{
int i=1;
clrscr();
while(i<5)
{
printf(“i=%d\n”,i);
i++;
}
getch();
}
OUTPUT
i=1
i=2
i=3
i=4
C PROGRAMMING
do-while loop
. The syntax of do-while loop is:
. do
{
//statement inside the body of the loop
}
while(test expression);
BREAK & CONTINUE STATEMENTS
BREAK
. The break statement ends the loop
immediately when it is encountered.
. It’s syntax is : break;
. break statement is almost always
used with if.. else statement inside the
loop.
for(initialization; test expression; update)
C PROGRAMMING
{
//codes
if(condition to break)
{
break;
}
//codes
}

PROGRAM FOR break STATEMENT


#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
C PROGRAMMING
for(i=1;i<=10;i++)
{
if(i<1)
break;
printf(“i=%d\n”,i);

}
getch();
}
OUTPUT
i=1
i=2
i=3
i=4
i=5
i=6
i=7
C PROGRAMMING
i=8
i=9
i=10
CONTINUE
. The continue statement skips the current
iteration of the loop and continues with
the next iteration .
. it’s syntax is : continue;
. The continue statement is almost always
used with if..else statement.
for(initialization; test expression; update)
{
//codes
if(test expression)
{
continue;
C PROGRAMMING
}
}
PROGRAM FOR continue STATEMENT
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
if(i==5)
continue;
printf(“%d\n”,i);
}
getch();
}
C PROGRAMMING
OUTPUT
1
2
3
4
6
7
8
9
10

SWITCH AND GOTO STATEMENT


switch statement
. The switch statement allow us to execute
one code block among many alternatives.
. Syntax of switch case:
C PROGRAMMING
switch(expression)
{
case constant:
//statement
break;

case constant:
//statement
break;
.
.
.
Default:
//default statement
}
C PROGRAMMING
PROGRAM FOR switch STATEMENT
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf(“Enter the number b/w 1 to 4=”);
scanf(“%d”,&n);
switch(n)
{
case 1:
printf(“you enter 1\n”);
break;
case 2:
printf(“you enter 2\n”);
C PROGRAMMING
break;
case 3:
printf(“you enter 3\n”);
break;
case 4:
printf(“you enter 4\n”);
break;
default:
printf(“you enter wrong choice\n”);
}
getch();
}
OUTPUT
Entre the number b/w 1 to 4=1
you enter 1
goto STATEMENT
C PROGRAMMING
. The goto statement allows us to transfer
control to the specified label.
. Syntax of goto statement
goto label;
….. goto label;
….. ………
….. ………
label: label:
statement; ………
. The label is an identifier. When the goto
statement is encountered , the control of
the program jump to label: and start
executing the code.
PROGRAM FOR goto STATEMENT
#include<stdio.h>
#include<conio.h>
C PROGRAMMING
void main()
{
int n=2;
if(n>1)
{
goto num;
}
else
printf(“Welcome to home”);
num:
printf(“Welcome to a arena”);
getch();
}

OUTPUT
Welcome to home
C PROGRAMMING
3.FUNCTION
. A function is a block of code that
perform a specific task.
. There are two type of function in C
programming.
1.standard library function
2. user define function
. The standard library function are built in
function in C programming. These
functions are defined in header file.
. C allows to define functions according to
your need, these functions are known as
user define functions.
# include<stdio.h>
C PROGRAMMING
void functionname();
void functionname()
{
…..
……
…..
}
void main()
{
…..
…..
functionname();
……
…….
}
PROGRAM FOR function
C PROGRAMMING
#include<stdio.h>
#include<conio.h>
void sum();
void main()
{
clrscr();
sum();
getch();
}
void sum()
{
int a=10,b=20,c;
c=a+b;
printf(“sum=%d”,c);
}
OUTPUT
sum=30
C PROGRAMMING
4.ARRAY
. An array is a variable that can store
multiple values.
. Syntax : data type array name[array size];
. Array have 0 as the first index, not 1.
. If the size of an array is n, to access the
last element, the n-1 index is used.
. float x[3][4]
. Here, x is two dimensional (2d) array.
The
array can hold 12 elements. You can think
the array as a table with 3 rows and each
row has 4 columns.
PROGRAM FOR ARRAY
#include<stdio.h>
C PROGRAMMING
#include<conio.h>
void main()
{
int a[5],i;
clrscr();
printf(“enter the value in array=”);
for(i=0;i<=4;i++)
scanf(“%d”,&a[i]);
printf(“you entered these values in array\
n”);
for(i=0;i<=4;i++)
printf(“%d ”,a[i]);
getch();
}
OUTPUT
Enter the values in array
1
C PROGRAMMING
2
3
4
5
You entered these values in array
12345
5.POINTER
. If you have a variable var in your
program
, and var will give you its address in the
memory.
. We have use address numerous time
while using the scanf() function.
. scanf(“%d”,&var);
. Pointer (pointer variables ) are special
variables that are used to store addresses
C PROGRAMMING
rather than values.
. syntax : int *p;
PROGRAM FOR POINTER
#include<stdio.h>
#include<conio.h>
void main()
{
int *p, n=10;
clrscr();
printf(“The value of n=%d\n”,n);
printf(“The address of n=%p”,p);
getch();
}
OUTPUT
The value of n=10
The address of n=….
C PROGRAMMING
6.STRING
. String are one dimensional array of
character terminated by null character ‘\
0’.
Thus a null-terminated string contains the
character that comprise the string
followed by null.
. Syntax : char greeting[]=”Hello”;
. char greeting[6]={‘H’,’e’,’l’,’l’,’o’,’\0’};
. Actually , you do not place the null
character at the end of a string constant.
The C compiler automatically places the ‘\
0’ at the end of the string when it
initializes the array.
. Format specifier of string is %s.

Sr.no Function and


C PROGRAMMING
Purpose
1 strcpy(s1,s2);
2 strcat(s1,s2)
3 strlen(s1);
4 strcmp(s1,s2);
5 strchr(s1,ch);
6 strstr(s1,s2);
. strcpy(s1,s2) is used to copy s2 string to
s1 string.
. strcat(s1,s2) is used to add s2 string to s1
string.
. strlen(s1) is used to calculate a length of
a string.
. strcmp(s1,s2) is used to compare two
string.
. strchr(s1,ch) and strstr(s1,s2) is used for
finding a value between two string.
C PROGRAMMING
PROGRAM FOR STRING
#include<stdio.h>
#include<conio.h>
void main()
{
char a[]=”abhishek”;
clrscr();
printf(“The value of a=%s”,a);
getch();
}
OUTPUT
The value of a =abhishek
7.STRUCTURE & UNION

STRUCTURE
C PROGRAMMING
. Structure is another user define data
type available in C that allows to combine
data items to different kinds.
. To define a structure , you must use the
struct statement . That struct statement
defines a new data type m with more than
one member.
. To access any member of a structure m
we use the member access operator(.).
. The format of the struct statement is as
follows:
struct[structure tag]
{
Member definition;
Member definition;
…..
Member definition;
C PROGRAMMING
}
[one or more structure variable];
UNION
. A union is a special data type available in
C that allows to store different data ypes
in the same memory location . You can
define a union with many members , but
only one member can contain a value at
any given time , Unions provide an
efficient way of using the same memory
location foe multiple purpose.
. The union tag is optional and each
member definition is a normal variable
definition . At the end of the union’s
definition , before the final semicolon ,
you can specify one or more union
variables , but it is optional . Here is the
C PROGRAMMING
way you would define a union type named
data having three members I, f and str-
union data
{
int a;
float b;
char str[20];
}data;
. To define a union , you must use the
format of the union statement is as
follows-
union[union tag]
{
Member definition;
Member definition;
Member definition;
C PROGRAMMING
……..
Member definition;
}[one or more union variables];

8.FILE HANDLING
. A file is a container in computer storage
devices used for storing data.
. Types of files
. Text file
. Binary file
. File operations
. Creating a new file
. Opening a new file
. Closing a file
. Reading from and writing information
to
C PROGRAMMING
a file.
. When working with files you need to
declare a pointer of type file. This
declaration is needed for communication
between the file and the program.
.Syntax :
FILE *fptr;

Opening Modes in Standard I/O


Mode Meaning of mode During Inexistence of file
r open for reading if the file does not exist , fopen() return NULL
rb open for reading in binary mode if the file does not exist , fopen() return NULL
w open for writing if the file exist , its content are overtwritten. If the file does not exist , it will be created
wb open for writing in binary mode if the file exist , its content are oberwritten. If the file does not exist , it will be created
a open for append. Data is added to the end of the file If the file does not exist , it will be created
ab open for append in binary . Data is added to the end of the file. If the file does not exist , it will be created
r+ open for both reading and writing if the file does not exist , fopen() return NULL
rb+ open for both reading and writing in binary mode if the file does not exist , fopen() return NULL
w+ open for both reading and writing if the file exist , its content are oberwritten. If the file does not exist , it will be created
wb+ open for both reading and writing in binary mode if the file exist , its content are oberwritten. If the file does not exist , it will be created
a+ open for both reading and appending If the file does not exist , it will be created
ab+ open for both reading and appending in binary mode If the file does not exist , it will be created

You might also like