C language
- Priya Patel
Index
2
Page----4
1. Introduction of c
Page----9
2. Datatype, constant
and variable
Page----14
3. Operator
Page----18
4. Control structure
Page----26
5. Looping
Page----39
8. String
Page----35
7. Array
Page----31
6. Looping with
pattern
Page----44
9. User defined
function
1.Introduction of c
A. History and importance of c
B. Difference between compiler and interpreter
C. Simple printf() function
D. Escape sequence character
3
A. History and importance of c
C is popular high level programming language. It was developed by Dennis Ritchie at
AT&T Bell Laboratories in 1972. It was derived from earlier programming language
called B. It was originally designed to write system programs under UNIX operating
system.
4
B. Difference between compiler and
interpreter
5
Compiler Interpreter
C, C++, Go, Fortran Python, PHP, Ruby
Language Language
Machine code Ready to run!
Ready to run! Virtual Machine
Machine code
C. Simple printf() function
6
 printf() is pre-defined function from the header stdio.h
 printf() is used to write formatted output to console
 Example -
printf(“hello!!”); output : hello!!
printf(“My age is %d”,23); output : my age is 23
where %d is format specifier for integer.
D. Escape sequence character
 It is composed of two or more character starting with backslash.
 Escape means skip.
 Sequence character means always together.
 Example -
printf(“”); output - 
printf(“n”); output - n
printf(“%%”); output - %
printf(“%%d”); output - %d
7
2. Data type, constant & variable
A. Datatype
B. Constant variable
C. Variable
8
A. Data type in c
9
Pre-defined
i.Int
ii. Float
iii. Char
iv. Void
Derived
i. Array
ii. Pointer
User-defined
i.Structure
ii. Union
iii. Enum
1 2 3
B. Constant
 Definition –
This is a variable which is only
initialized once and it’s value cannot be
modifiable.
 Note –
A constant variable name should
be in capitalized latter.
 How to create a constant variable -
1] Using a const variable.
2] By defining a marco.
3] Initialization must be done
while declaring a constant
variable.
10
 Example –
1] const float PI = 3.14;
printf(“%f”,PI);
2] #define Min 1
# define Max 9
main()
{
printf(“%d%d”,Min,Max);
}
11
C. Variable
 Definition –
It is a container which is used to store some data.
 Rules – A variable name
1] must be started with alphabet or underscore(_).
2] cannot be starts with numeric digit or any special symbol.
3] cannot contain any whitespace.
4] cannot be same as any keyword.
12
3. Operator & expression
A. Types of operator
B. Operator precedence
C. Type Conversation
13
A. Types of operator
14
Arithmetic
+
-
*
/
%
++
--
Relation
==
!=
>
<
> =
< =
Logical
&&
||
!
Bitwise
&
|
^
<<
>>
Assignment
=
+=
-=
*=
/=
%=
1 2 3 4 5 6
Miscellaneous
Size()
&
*
?:
B. Operator precedence
15
No. Priority Order
1 ( ) Left to right
2 % / * Left to right
3 + - Left to right
4 = Right to left
 Definition :
In a given expression which operator runs first and in which order.
C. Type conversation(Type casting)
16
 Definition :
Convert one data type into another data type for temporary time.
 Example :
float a;
a = 5.9;
printf(“%d”, (int)a);
4. Control structure
A. If statement
B. If else
C. Ladder
D. Nested if
E. Ternary operator
F. Switch case
17
18
18
Control structure
If
if(condition)
{
//true
}
If else Switch case
If-else If-else if Nested if
if (condition)
{
//true
}
else
{
//false
}
if(condition 1)
{
// true
}
else if(condition
2)
{
//true
}
else
{
}
if (condition 1)
{
if (condition)
{
}
else
{
}
}
else
{
if (condition)
{
}
else
{
}
}
switch (variable)
{
case 1:
break;
Case 2:
break;
default:
}
condition
?
// true
:
//false;
Ternary operate
A. If statement
19
Variable x is
less than y
Example : Find which value is less?
Output
#include<stdio.h>
Int main()
{
int x = 20;
int y = 22;
if (x<y)
{
printf(“Variable x is less than y”);
}
}
B. If else
20
The value is
greater than
10
Output
#include<stdio.h>
int main()
{ int num=19;
if (num<10)
{ printf("The value is less than 10"); }
else
{ printf("The value is greater than 10"); }
}
Example : The value is less than ten or greater than ten?
C. Ladder(steps)
21
First class
Example : Prints the grade as per the marks scored in a test.
Output
#include<stdio.h>
main()
{ int marks=83;
If(marks>75)
{ printf("First class"); }
else if(marks>65)
{ printf("Second class"); }
else if(marks>55)
{ printf("Third class"); }
else
{ printf("Fourth class"); }
}
D. Nested if
22
The value is
greater than
10
Output
Example : Checks if a number is less or greater than 10
#include<stdio.h>
main()
{ int num=100;
If(num<10)
{
if(num==1)
{ printf("The value is:%dn",num); }
else
{ printf("The value is greater than 1"); }
}
else
{ printf("The value is greater than 10"); }
}
E. Ternary operator
23
C is max
Example : find maximum number from 3 number.
Output
#include<stdio.h>
int main()
{
int a=10,b=100,c=200;
(a==b && b==c) ?
Printf(“all numbers are same”):
a>b ?
a>c ? printf(“a is max”)
: printf(“a is max”)
: b>c ? printf(“b is max”)
: printf(“c is max”);
}
F. Switch case (Menu driven)
24
The value is 8
Output
Example : Check what number is ?
#include <stdio.h>
main()
{
int num = 8;
switch (num)
{
case 7: printf("Value is 7");
break;
case 8: printf("Value is 8");
break;
case 9: printf("Value is 9");
break;
default: printf("Out of range");
}
}
5. Looping
A. While loop
B. Do while loop
C. For loop
25
26
Looping
While For
Do while
Initialization
While(condition)
{
statement
increment or
decrement
}
Initialization
do
{
Statement
increment or
decrement
}While(condition)
For
(
initial value;
condition;
increment or
decrement
)
{
statements;
}
A. While loop
27
1
2
3
4
5
6
7
8
9
10
Example : Print first 10 natural number. Output
#include <stdio.h>
void main()
{
int i=1;
while (i<=10)
{
printf("%d ",i);
i++;
}
printf("n");
}
B. Do while loop
28
1
2
3
4
5
6
7
8
9
10
Example : Print first 10 natural number.
Output
#include <stdio.h>
void main()
{
int i=1;
do
{
printf("%d ",i);
i++;
} while (i<=10);
printf("n");
}
C. For loop
29
1
2
3
4
5
6
7
8
9
10
Example : Print first 10 natural number. Output
#include <stdio.h>
void main()
{
int i;
for (i=1;i<=10;i++)
{
printf("%d ",i);
}
printf("n");
}
6. Looping with patterns
A. Without spacing
B. With spacing
C. custom
30
A. Without spacing pattern
31
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
Example : Output
#include <stdio.h>
void main()
{
int i,j;
for(i=1;i<6;i++)
{
For(j=1;j<6;j++)
{ printf(“%d ”,j); }
printf(“n”);
}
}
B. With spacing pattern
32
Example :
Output
#include <stdio.h>
void main()
{
int I , j , k;
For(i=1 ; i<=5 ; i++)
{
For(k=5;k>i ; k--)
{ printf (“ ”); }
For(j=1;j<=i ; j++)
{ printf (“%d ”,j); }
printf (“n”);
}
}
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
C. Custom
33
Example : Output
#include <stdio.h>
int main()
{ int i, j, N=5;
for(i=1; i<=N; i++)
{ for(j=1; j<=i; j++)
{ printf("%d", j); }
printf("n"); }
for(i=N-1; i>=1; i--)
{ for(j=1; j<=i; j++)
{ printf("%d", j); }
printf("n"); }
}
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
7. Array
A. 1D array
B. 2D array
34
Array
 1D array : Collection of multiple values or elements which have same data
type.
 2D array : Collection of multiple 1D array.
35
A. 1D array
36
Example : Print elements in an array
1
2
3
4
5
6
7
8
9
10
Output
#include <stdio.h>
int main()
{
int a[100];
int i, N;
printf("Enter size of array: ");
scanf("%d", &N);
printf("Enter %d elements in the array : ", N);
for(i=0; i<N; i++) { scanf("%d", &arr[i]); }
printf("nElements in array are: ");
for(i=0; i<N; i++) { printf("%d, ", arr[i]); }
}
Input
Input size: 10
Input elements:
1
2
3
4
5
6
7
8
9
10
B. 2D array
37
Example : Enter element in matrix and print.
#include<stdio.h>
void main ()
{ int arr[3][3],i,j;
Printf(“input element:n”);
for (i=0;i<3;i++)
{ for (j=0;j<3;j++)
{ scanf("%d",&arr[i][j]); } }
printf("n printing the elements ....n");
for(i=0;i<3;i++)
{ printf("n");
for (j=0;j<3;j++)
{ printf("%dt",arr[i][j]); } }
}
1 2 3
4 5 6
7 8 9
Output
Input
Input elements:
1
2
3
4
5
6
7
8
9
8. String
A. String concept
B. gets(), puts()
C. String function
38
A. String concept
The string can be defined as the one-dimensional array of characters
terminated by a null ('0'). The character array or the string is used to
manipulate text such as word or sentences.
39
B. gets() and puts()
40
gets() Puts()
 The gets() function is similar to scanf()
function but it allows entering some
characters by the user in string
 Syntax :
char[ ]
gets( char [ ])
 The puts() function is similar
to printf() function. but puts() function
is used to display only the string after
reading by gets() function entered by
user(gets similar to scanf() function)
 Syntax :
puts(char[ ])
C. String function
 strlwr() :
char a[100];
puts(“enter aby string:”);
gets(a); // WELCOME
printf(“%s”,strlwr(a)); // welcome
 strupr() :
char a[100];
puts(“enter aby string:”);
gets(a); // welcome
printf(“%s”,strupr(a)); // WELCOME
 strrev() :
char a[100];
puts(“enter aby string:”);
gets(a); // priya
printf(“%s”,strrev(a)); // ayirp
 strlen() :
char a[100];
puts(“enter aby string:”);
gets(a); // WELCOME
printf(“%s”,strlen(a)); // 7
 strcpy() :
char a[100];
strcpy(a,”home”);
puts(a); // home
9. User defined function
A. Concept of UDF
B. Recursion
42
A. Concept of UDF
43
Library function
i. printf()
ii. scanf()
iii. gets()
iv. puts()
User defined function
i. Take nothing, return nothing
ii. Take something, return nothing
iii. Take nothing, return something
iv. Take something, return something
1 2
 Definition –
A block of code that is reusable its called function.
There is two type of function :-
B. Recursion
44
24
Output
Example : Find factorial of number?
#include <stdio.h>
Int fact(int n)
{ if(n==1)
{ return 1;}
else
{ return n * fact(n-1) ;}
Void main()
{
printf(“%d ”,fact(4));
}
 Definition –
A function call it’s self that means recursion.
Thank you

C language

  • 1.
  • 2.
    Index 2 Page----4 1. Introduction ofc Page----9 2. Datatype, constant and variable Page----14 3. Operator Page----18 4. Control structure Page----26 5. Looping Page----39 8. String Page----35 7. Array Page----31 6. Looping with pattern Page----44 9. User defined function
  • 3.
    1.Introduction of c A.History and importance of c B. Difference between compiler and interpreter C. Simple printf() function D. Escape sequence character 3
  • 4.
    A. History andimportance of c C is popular high level programming language. It was developed by Dennis Ritchie at AT&T Bell Laboratories in 1972. It was derived from earlier programming language called B. It was originally designed to write system programs under UNIX operating system. 4
  • 5.
    B. Difference betweencompiler and interpreter 5 Compiler Interpreter C, C++, Go, Fortran Python, PHP, Ruby Language Language Machine code Ready to run! Ready to run! Virtual Machine Machine code
  • 6.
    C. Simple printf()function 6  printf() is pre-defined function from the header stdio.h  printf() is used to write formatted output to console  Example - printf(“hello!!”); output : hello!! printf(“My age is %d”,23); output : my age is 23 where %d is format specifier for integer.
  • 7.
    D. Escape sequencecharacter  It is composed of two or more character starting with backslash.  Escape means skip.  Sequence character means always together.  Example - printf(“”); output - printf(“n”); output - n printf(“%%”); output - % printf(“%%d”); output - %d 7
  • 8.
    2. Data type,constant & variable A. Datatype B. Constant variable C. Variable 8
  • 9.
    A. Data typein c 9 Pre-defined i.Int ii. Float iii. Char iv. Void Derived i. Array ii. Pointer User-defined i.Structure ii. Union iii. Enum 1 2 3
  • 10.
    B. Constant  Definition– This is a variable which is only initialized once and it’s value cannot be modifiable.  Note – A constant variable name should be in capitalized latter.  How to create a constant variable - 1] Using a const variable. 2] By defining a marco. 3] Initialization must be done while declaring a constant variable. 10  Example – 1] const float PI = 3.14; printf(“%f”,PI); 2] #define Min 1 # define Max 9 main() { printf(“%d%d”,Min,Max); }
  • 11.
  • 12.
    C. Variable  Definition– It is a container which is used to store some data.  Rules – A variable name 1] must be started with alphabet or underscore(_). 2] cannot be starts with numeric digit or any special symbol. 3] cannot contain any whitespace. 4] cannot be same as any keyword. 12
  • 13.
    3. Operator &expression A. Types of operator B. Operator precedence C. Type Conversation 13
  • 14.
    A. Types ofoperator 14 Arithmetic + - * / % ++ -- Relation == != > < > = < = Logical && || ! Bitwise & | ^ << >> Assignment = += -= *= /= %= 1 2 3 4 5 6 Miscellaneous Size() & * ?:
  • 15.
    B. Operator precedence 15 No.Priority Order 1 ( ) Left to right 2 % / * Left to right 3 + - Left to right 4 = Right to left  Definition : In a given expression which operator runs first and in which order.
  • 16.
    C. Type conversation(Typecasting) 16  Definition : Convert one data type into another data type for temporary time.  Example : float a; a = 5.9; printf(“%d”, (int)a);
  • 17.
    4. Control structure A.If statement B. If else C. Ladder D. Nested if E. Ternary operator F. Switch case 17
  • 18.
    18 18 Control structure If if(condition) { //true } If elseSwitch case If-else If-else if Nested if if (condition) { //true } else { //false } if(condition 1) { // true } else if(condition 2) { //true } else { } if (condition 1) { if (condition) { } else { } } else { if (condition) { } else { } } switch (variable) { case 1: break; Case 2: break; default: } condition ? // true : //false; Ternary operate
  • 19.
    A. If statement 19 Variablex is less than y Example : Find which value is less? Output #include<stdio.h> Int main() { int x = 20; int y = 22; if (x<y) { printf(“Variable x is less than y”); } }
  • 20.
    B. If else 20 Thevalue is greater than 10 Output #include<stdio.h> int main() { int num=19; if (num<10) { printf("The value is less than 10"); } else { printf("The value is greater than 10"); } } Example : The value is less than ten or greater than ten?
  • 21.
    C. Ladder(steps) 21 First class Example: Prints the grade as per the marks scored in a test. Output #include<stdio.h> main() { int marks=83; If(marks>75) { printf("First class"); } else if(marks>65) { printf("Second class"); } else if(marks>55) { printf("Third class"); } else { printf("Fourth class"); } }
  • 22.
    D. Nested if 22 Thevalue is greater than 10 Output Example : Checks if a number is less or greater than 10 #include<stdio.h> main() { int num=100; If(num<10) { if(num==1) { printf("The value is:%dn",num); } else { printf("The value is greater than 1"); } } else { printf("The value is greater than 10"); } }
  • 23.
    E. Ternary operator 23 Cis max Example : find maximum number from 3 number. Output #include<stdio.h> int main() { int a=10,b=100,c=200; (a==b && b==c) ? Printf(“all numbers are same”): a>b ? a>c ? printf(“a is max”) : printf(“a is max”) : b>c ? printf(“b is max”) : printf(“c is max”); }
  • 24.
    F. Switch case(Menu driven) 24 The value is 8 Output Example : Check what number is ? #include <stdio.h> main() { int num = 8; switch (num) { case 7: printf("Value is 7"); break; case 8: printf("Value is 8"); break; case 9: printf("Value is 9"); break; default: printf("Out of range"); } }
  • 25.
    5. Looping A. Whileloop B. Do while loop C. For loop 25
  • 26.
    26 Looping While For Do while Initialization While(condition) { statement incrementor decrement } Initialization do { Statement increment or decrement }While(condition) For ( initial value; condition; increment or decrement ) { statements; }
  • 27.
    A. While loop 27 1 2 3 4 5 6 7 8 9 10 Example: Print first 10 natural number. Output #include <stdio.h> void main() { int i=1; while (i<=10) { printf("%d ",i); i++; } printf("n"); }
  • 28.
    B. Do whileloop 28 1 2 3 4 5 6 7 8 9 10 Example : Print first 10 natural number. Output #include <stdio.h> void main() { int i=1; do { printf("%d ",i); i++; } while (i<=10); printf("n"); }
  • 29.
    C. For loop 29 1 2 3 4 5 6 7 8 9 10 Example: Print first 10 natural number. Output #include <stdio.h> void main() { int i; for (i=1;i<=10;i++) { printf("%d ",i); } printf("n"); }
  • 30.
    6. Looping withpatterns A. Without spacing B. With spacing C. custom 30
  • 31.
    A. Without spacingpattern 31 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 Example : Output #include <stdio.h> void main() { int i,j; for(i=1;i<6;i++) { For(j=1;j<6;j++) { printf(“%d ”,j); } printf(“n”); } }
  • 32.
    B. With spacingpattern 32 Example : Output #include <stdio.h> void main() { int I , j , k; For(i=1 ; i<=5 ; i++) { For(k=5;k>i ; k--) { printf (“ ”); } For(j=1;j<=i ; j++) { printf (“%d ”,j); } printf (“n”); } } 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
  • 33.
    C. Custom 33 Example :Output #include <stdio.h> int main() { int i, j, N=5; for(i=1; i<=N; i++) { for(j=1; j<=i; j++) { printf("%d", j); } printf("n"); } for(i=N-1; i>=1; i--) { for(j=1; j<=i; j++) { printf("%d", j); } printf("n"); } } 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
  • 34.
    7. Array A. 1Darray B. 2D array 34
  • 35.
    Array  1D array: Collection of multiple values or elements which have same data type.  2D array : Collection of multiple 1D array. 35
  • 36.
    A. 1D array 36 Example: Print elements in an array 1 2 3 4 5 6 7 8 9 10 Output #include <stdio.h> int main() { int a[100]; int i, N; printf("Enter size of array: "); scanf("%d", &N); printf("Enter %d elements in the array : ", N); for(i=0; i<N; i++) { scanf("%d", &arr[i]); } printf("nElements in array are: "); for(i=0; i<N; i++) { printf("%d, ", arr[i]); } } Input Input size: 10 Input elements: 1 2 3 4 5 6 7 8 9 10
  • 37.
    B. 2D array 37 Example: Enter element in matrix and print. #include<stdio.h> void main () { int arr[3][3],i,j; Printf(“input element:n”); for (i=0;i<3;i++) { for (j=0;j<3;j++) { scanf("%d",&arr[i][j]); } } printf("n printing the elements ....n"); for(i=0;i<3;i++) { printf("n"); for (j=0;j<3;j++) { printf("%dt",arr[i][j]); } } } 1 2 3 4 5 6 7 8 9 Output Input Input elements: 1 2 3 4 5 6 7 8 9
  • 38.
    8. String A. Stringconcept B. gets(), puts() C. String function 38
  • 39.
    A. String concept Thestring can be defined as the one-dimensional array of characters terminated by a null ('0'). The character array or the string is used to manipulate text such as word or sentences. 39
  • 40.
    B. gets() andputs() 40 gets() Puts()  The gets() function is similar to scanf() function but it allows entering some characters by the user in string  Syntax : char[ ] gets( char [ ])  The puts() function is similar to printf() function. but puts() function is used to display only the string after reading by gets() function entered by user(gets similar to scanf() function)  Syntax : puts(char[ ])
  • 41.
    C. String function strlwr() : char a[100]; puts(“enter aby string:”); gets(a); // WELCOME printf(“%s”,strlwr(a)); // welcome  strupr() : char a[100]; puts(“enter aby string:”); gets(a); // welcome printf(“%s”,strupr(a)); // WELCOME  strrev() : char a[100]; puts(“enter aby string:”); gets(a); // priya printf(“%s”,strrev(a)); // ayirp  strlen() : char a[100]; puts(“enter aby string:”); gets(a); // WELCOME printf(“%s”,strlen(a)); // 7  strcpy() : char a[100]; strcpy(a,”home”); puts(a); // home
  • 42.
    9. User definedfunction A. Concept of UDF B. Recursion 42
  • 43.
    A. Concept ofUDF 43 Library function i. printf() ii. scanf() iii. gets() iv. puts() User defined function i. Take nothing, return nothing ii. Take something, return nothing iii. Take nothing, return something iv. Take something, return something 1 2  Definition – A block of code that is reusable its called function. There is two type of function :-
  • 44.
    B. Recursion 44 24 Output Example :Find factorial of number? #include <stdio.h> Int fact(int n) { if(n==1) { return 1;} else { return n * fact(n-1) ;} Void main() { printf(“%d ”,fact(4)); }  Definition – A function call it’s self that means recursion.
  • 45.