C Programming Notes
C Programming Notes
C Programming Notes
Updated on:
03 August 2015
C Programming
DISCLAIMER
The data in the tutorials is supposed to be one for reference.
We have made sure that maximum errors have been
rectified. Inspite of that, we (ECTI and the authors) take no
responsibility in any errors in the data.
The programs given in the tutorials have been prepared on,
and for the IDE Microsoft Visual Studio 2013.
To use the programs on any other IDE, some changes might
be required.
The student must take note of that.
www.ecti.co.in
C Programming
History of C Programming
C Programming Language was introduced by Mr. Dennis
Ritchie in 1972. He invented this language for the internal use
of AT & T Bell Labs. But due to the features of C Programming
Language it became very popular.
Problems with languages existing before C
They were categorized in Lower Level Languages and Higher Level
Languages. The Lower Level Languages were designed to give better
machine efficiency, i.e. faster program execution. The Higher Level
Languages were designed to give better programming efficiency i.e.
faster program development.
The languages before C were Application Specific e.g. FORTRAN
(FORmula TRANslation) was built for Engineering Applications
Development, COBOL (COmmon Business Oriented Language) was
developed for Commercial Application Development.
www.ecti.co.in
C Programming
History of C Programming
Dennis Ritchie thought about giving features of both Lower
Level Languages and Higher Level Languages. So he
introduced the concept of Compilers in C. The function of
compiler is to "Convert the code from Human Readable
Language to Machine Readable Language and Vice-a-Versa".
www.ecti.co.in
C Programming
Basics of C Programming
Steps in Learning English Language:
Alphabets
Words
Sentences
Paragraph
Instructions
C Program
Constants
Numbers
Variables
Special Symbols
Keywords
www.ecti.co.in
C Programming
Basics of C Programming
'C' Alphabets:
A, B, , Y, Z
Alphabets
a, b, ..., y, z
Numbers
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special Symbols
www.ecti.co.in
C Programming
Basics of C Programming
'C' Constants:
A constant is an entity that never changes. There are two type of C
Constants Primary Constants, Secondary Constants.
C Constants
Primary
Constants
Integer
Real
Character
Secondary
Constants
Array
Pointer
Structure
etc.
www.ecti.co.in
C Programming
Basics of C Programming
Rules for Constructing Integer Constants:
www.ecti.co.in
C Programming
Basics of C Programming
Rules for Constructing Character Constants:
A character constant is a single alphabet, a single digit or a
single special symbol enclosed within single inverted
commas. Both the inverted commas should point to the
left.
'C' Variables:
An entity that may vary during the program execution is
called as Variable.
Variable names are names given to the memory locations.
These locations can hold integer, real or character constants.
The integer variable needs 4 bytes, float variable needs
4 bytes, double variable needs 8 bytes and char variable
needs 1 byte memory space.
www.ecti.co.in
C Programming
Basics of C Programming
Rules for Constructing a Variable Name:
The variable name should not exceed 30 characters. Some
compilers allow variable names upto 247 characters. But
restrict variable name, as it adds to the typing effort.
The variable name can be Alpha-numeric e.g. no1
But it should start with an alphabet.
It should not contain any spaces in between.
It should not contain any special symbols except
underscore (_) in between e.g. area_circle
It should not contain any Keyword.
www.ecti.co.in
C Programming
Basics of C Programming
Keywords:
The words which are assigned with special meaning to them
in C Compiler are called as keywords. As they are assigned
with special meaning, they can not be used as variable names.
There are only 32 keywords available in C.
auto
double
int
struct
break
else
long
switch
case
enum
register
typedef
char
extern
return
union
const
float
short
unsigned
continue
for
signed
void
default
goto
sizeof
volatile
do
if
static
while
www.ecti.co.in
C Programming
Basics of C Programming
'C' Instructions:
Type Declaration Instruction: These instructions are used to
declare the type of variables used in a C Program.
int no1, no2, sum;
float radius, area;
char ch;
Arithmetic Instructions:
Arithmetic Operators
Addition
5+2=7
Subtraction
52=3
Multiplication
5 * 2 = 10
Division
5/2=2
Modulus
www.ecti.co.in
5%2=1
C Programming
Basics of C Programming
Priority of operators
Priority
Operators
Description
1st
()
Brackets
2nd
* / %
3rd
+ -
Addition, Subtraction
4th
Assignment
www.ecti.co.in
C Programming
Basics of C Programming
Basic Structure of a C Program:
#include<stdio.h>
#include<conio.h>
void main()
{
.;
.;
.;
}
# preprocessor
instruction
include<x> To include a
file x to the C Program
stdio.h / conio.h Header
Files
void main() Start of a
program
{ } Scope of a Program
www.ecti.co.in
C Programming
C Programming
First C Program
#include<stdio.h>
void main()
{
printf("This is my first C Program"); /*to print the statement*/
}
The program should be properly documented. The
statements enclosed in /* */ are called as comments.
Single line comments can also be given using //.
The program should be properly indented.
printf() is a function which prints the string embedded in
between " " on the console.
www.ecti.co.in
C Programming
void main()
{
int no1;
float no2;
char ch;
no1 = 10;
no2 = 25.75;
ch = 'A';
printf("Integer = %d, Real = %f, Character = %c", no1, no2, ch);
getch();
/*waits for user to press any key on console*/
}
www.ecti.co.in
C Programming
scanf() Function
C Programming
<
x<y
greater than
>
x>y
<=
x <= y
>=
x >= y
equal to
==
x == y
not equal to
!=
x != y
www.ecti.co.in
C Programming
if(condition)
..;
if(condition)
{
..;
..;
}
if(condition)
..;
else
..;
if(condition)
{
..;
..;
}
else
..;
www.ecti.co.in
if(condition)
..;
else
{
..;
..;
}
if(condition)
{
..;
..;
}
else
{
..;
..;
}
C Programming
www.ecti.co.in
else
{
if(condition)
{
..;
..;
}
else
{
..;
..;
}
}
C Programming
Logical Operators
C contains Logical
Operators && (AND), ||
(OR) and ! (NOT).
&& and || allow to join
multiple conditions.
! Operator reverses the
result of the expression it
operates on.
Examples
C Programming
void main()
{
int no1=15, no2=10, no3=8;
if (no1 >no2 && no1 > no3)
printf("The no1 is maximum");
else if(no2 > no1 && no2 > no3)
printf("The no2 is maximum");
else
printf("The no3 is maximum");
getch();
}
www.ecti.co.in
C Programming
int x, y;
scanf("%d", &x);
y = (x > 5 ? 3 : 4)
This expression will store 3 in y if x is
greater than 5 otherwise it will store 4
in y.
int i;
scanf("%d",&i);
If(i==1 ? printf("Amit") : printf("Rohit);
char a = 'z';
printf("%c", (a >= 'a' ? a : '!'));
int big a, b, c;
big=(a>b ? (a>c ? 3 : 4) : (b>c ? 6 : 8))
www.ecti.co.in
C Programming
C Programming
More Operators
++ and -- operators:
www.ecti.co.in
int i = 10, j;
j = ++i;
printf(" %d %d", i, j); /* 11 11*/
In the first example the value of i is
first assigned to j and then the
value of i gets incremented.
In the second example the value of i
is first incremented and then it is
assigned to j.
i=i+5
i+=5
i=i5
i-=5
i=i*5
i*=5
i=i/5
i/=5
i=i%5
i%=5
C Programming
*
**
***
****
In the above example, you
have to print multiple stars in
columns for each row. Such
times you need nesting of
loops.
www.ecti.co.in
void main()
{
int r = 1, c;
while(r <= 4)
{
c = 1;
while (c <= r)
{
printf("*");
c++;
}
printf("\n");
r++;
}
getch();
}
C Programming
do while Loop
#include<stdio.h>
#include<conio.h>
void main()
{
int no1, no2, sum;
char ch;
do
{
printf("Enter 2 nos: ");
scanf("%d%d", &no1, &no2);
sum = no1 + no2;
printf("Sum = %d", sum);
printf("Want to continue <y/n>: ");
flushall();
scanf("%c", &ch);
} while(ch == 'y');
}
C Programming
for Loop
for loop specify three things
about the loop in a single line.
The general form of for
statement is as follows
for(initialize; condition; increment /
decrement)
{
..;
..;
}
e.g.
for(i = 0; i <= 10; i++)
{
printf("%d", i);
}
www.ecti.co.in
C Programming
01234
continue statement
takes the control of the
program to the
beginning of loop.
e.g.
for(i = 0; i <= 10; i++)
{
if(i == 5)
continue;
printf("%d ", i);
}
The output of the above program
is:
0 1 2 3 4 6 7 8 9 10
www.ecti.co.in
C Programming
www.ecti.co.in
void main()
{
int i;
for(i = 0; i < 10; i++)
{
if(i == 5)
goto xyz;
else
printf(" %d", i);
}
xyz:
printf("\nI am out of main");
}
The output is:
01234
I am out of main
C Programming
C Programming
Functions
#include<stdio.h>
void pune();
void mumbai();
void main()
{
printf("\nI am in main");
pune();
Call to a
function
mumbai();
printf("\nI am back in main");
getch();
}
void pune()
{
printf("\nI am in Pune");
}
void mumbai()
{
printf("\nI am in Mumbai");
}
Function
Definitions
C Programming
#include<stdio.h>
#include<conio.h>
#include<stdio.h>
#include<conio.h>
void calsum();
void main()
{
calsum();
getch();
}
void calsum()
{
int no1, no2, add;
printf("\nEnter two numbers: ");
scanf("%d%d", &no1, &no2);
add = no1 + no2;
printf("\nThe sum is: %d", add);
}
void main()
{
int no1, no2;
printf("\nEnter two numbers: ");
scanf("%d%d", &no1, &no2);
calsum(no1, no2);
getch();
Actual Arguments
}
void calsum(int a, int b)
{
Formal Arguments
int add;
add = a + b;
printf("\nThe sum is: %d", add);
}
www.ecti.co.in
C Programming
www.ecti.co.in
#include<stdio.h>
#include<conio.h>
void main()
{
int no1, no2, sum;
printf("\nEnter two numbers: ");
scanf("%d%d", &no1, &no2);
sum = calsum(no1, no2);
printf("\nThe sum is: %d", sum);
getch();
}
int calsum(int a, int b)
{
int add;
add = a + b;
return add;
}
C Programming
Recursive Functions
#include<stdio.h>
#include<conio.h>
int factorial(int );
void main()
{
int no, fact;
printf("\nEnter any number: ");
scanf("%d", &no);
fact = factorial(no);
printf("\nThe factorial is: %d",
fact);
getch();
}
www.ecti.co.in
C Programming
Recursive Function
from main()
factorial (int no)
{
int f;
if(no == 1)
return(1);
else
f=
no*factorial(no-1);
return(f);
}
return(f);
}
to main()
www.ecti.co.in
C Programming
Range
Bytes
Format
Specifiers
char
-128 to 127
%c
unsigned char
0 to 255
%c
-32,768 to 32,767
%d
0 to 65,535
%u
signed int
-32768 to 32767
%d
unsigned int
0 to 65535
%u
-2147483648 to 2147483647
%ld
0 to 4294967295
%lu
float
-3.4e38 to 3.4e38
%f
double
-1.7e308 to 1.7e308
%lf
long double
-1.7e4932 to 1.7e4932
%Lf
C Programming
C Preprocessor
File Inclusion
Conditional Compilation
Miscellaneous Directives
C Programming
C Preprocessor
Macro Expansion
Some examples -
#define PI 3.142
C Programming
Multiline Macros
C Programming
File Inclusion
C Programming
Expanded Source
Code Prog1.i
Assembly Code
Prog1.asm
Relocatable
Object Code
Object Code of
Library Functions
Linker
Executable Code
Prog1.exe
www.ecti.co.in
C Programming
Arrays
arr
10
20
30
40
50
C Programming
Arrays
Accept and Print Array:
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5], i;
printf("\nEnter five numbers: ");
for(i = 0; i < 5; i++)
scanf("%d", &arr[i]);
printf("\nYou have entered: ");
for(i = 0; i < 5; i++)
printf(" %d", arr[i]);
getch();
}
www.ecti.co.in
C Programming
www.ecti.co.in
C Programming
Declare 2D-Array:
int a[2][2];
int a[ ][2] = {10,20,30,40};
int a[ ][2] = {{10,20},{30,40}};
a[0][0]
a[0][1]
a[1][0]
a[1][1]
10
20
30
40
6231
6235
6239
6243
www.ecti.co.in
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[3][3], i, j;
printf("\nEnter nine numbers: ");
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
scanf("%d", &arr[i][j]);
printf("\nYou have entered: ");
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
printf(" %d", arr[i][j]);
getch();
}
C Programming
www.ecti.co.in
C Programming
Strings
#include<stdio.h>
#include<conio.h>
\0
6231
6232
6233
6234
6235
6236
void main()
{
char name[50];
int i;
printf("\nEnter your name: ");
//scanf("%s", name);
gets(name);
printf("\nYour name is: %s", name);
printf("\nYour name is: ");
for(i = 0; name[i] != '\0'; i++)
printf("%c", name[i]);
getch();
}
C Programming
Strings
www.ecti.co.in
Use
strlen
strlwr
Converts string to
lowercase
strupr
Converts string to
uppercase
strcpy
strcat
strcmp
strrev
C Programming
www.ecti.co.in
C Programming
char s[2][10];
char s[ ][10] = {"Parag",
"Pooja",
"Sarang"};
\0
6241
\0
6251
www.ecti.co.in
\0
#include<stdio.h>
#include<conio.h>
void main()
{
char name[3][50];
int i;
printf("\nEnter three names: ");
for(i = 0; i < 3; i++)
gets(name[i]);
printf("\nThe names are: ");
for(i = 0; i < 3; i++)
puts(name[i]);
getch();
}
C Programming
www.ecti.co.in
C Programming
Pointers
printf("\nAddress of i = %u", j);
printf("\nAddress of j = %u", &j);
printf("\nValue of j = %u", j);
printf("\nValue of i = %d", i);
printf("\nValue of i = %d", *(&i));
printf("\nValue of i = %d", *j);
getch();
Variable name
10
Variable value
6231
Address of i =
6231
Value of i =
10
6231
Variable address
5785
Address of i= 6231
Value of i =
10
Address of j =
6233
Value of i =
10
#include<stdio.h>
void main()
{
int i = 10, *j;
j = &i;
printf("\nAddress of i = %u", &i);
www.ecti.co.in
Value of j = 6231
C Programming
Pointers
#include<stdio.h>
void main()
{
int i = 10, *j, **k;
j = &i;
k = &j;
printf("\nAddress of i = %u", &i);
printf("\nAddress of i = %u", j);
printf("\nAddress of i = %u", *k);
printf("\nAddress of j = %u", &j);
printf("\nAddress of j = %u", k);
printf("\nAddress of k = %u", &k);
printf("\nValue of j = %u", j);
printf("\nValue of k = %u, k);
printf("\nValue of i = %d", i);
printf("\nValue of i = %d", *(&i));
printf("\nValue of i = %d", *j);
printf("\nValue of i = %d", **k);
getch();
}
www.ecti.co.in
10
6231
5827
6231
5827
8989
i
i
i
j
j
k
=
=
=
=
=
=
= 6231
= 6231
= 6231
= 6233
= 6233
=6235
6231
6233
10
10
10
10
C Programming
#include<stdio.h>
#include<conio.h>
void calsum(int, int, int *);
void main()
{
int no1, no2, sum;
printf("\nEnter two numbers:");
scanf("%d%d", &no1, &no2);
calsum(no1, no2, &sum);
printf("\nSum = %d");
getch( );
}
void calsum(int x, int y, int *add)
{
*add = x + y;
}
C Programming
arr
10
20
30
40
50
#include<stdio.h>
void display (int *, int);
void main()
{
int arr[5], i;
printf("\nEnter five numbers: ");
for(i = 0; i < 5; i++)
scanf("%d", &arr[i]);
display(arr, 5);
getch();
}
void display (int *b, int s)
{
int i;
printf("\nYou have entered: ");
for(i = 0; i < s; i++)
printf(" %d", *(b+i));
}
C Programming
str
\0
6231
6232
6233
6234
6235
6236
#include<stdio.h>
#include<conio.h>
void display (char *);
void main()
{
char str[50];
printf("\nEnter your name: ");
gets(str);
display(str);
getch();
}
void display (char *s)
{
printf("\nYou have entered: %s", s);
printf("\nYou have entered: ");
for( ; *s != '\0'; s++)
printf("%c", *s);
}
C Programming
Structures
If we see real world data it is a
combination of integers,
characters, floats, arrays,
strings etc. So we can call it as
a heterogeneous data.
If we want to store data of a
student then we need to store
her/ his Roll Number, Name,
Address, Age, Gender, Phone
No., E-mail etc. So to store this
kind of data we can use
structures.
Structure is also called as
User Defined Datatype.
www.ecti.co.in
struct stud
{
int roll_no;
char name[20];
char gender;
};
Above syntax is used to define
a structure.
Memory Map of Structure:
stud
roll_no
name
gender
Parag Patki
50
6231
6235
6255
C Programming
Structures
#include<stdio.h>
#include<conio.h>
struct stud
{
int roll_no;
char name[50], address[100];
char gender;
};
void main()
{
struct stud s;
printf("\nEnter roll number:");
scanf("%d", &s.roll_no);
printf("\nEnter name: ");
flushall( );
gets(s.name);
www.ecti.co.in
C Programming
Nested Structures
#include<stdio.h>
#include<conio.h>
scanf("%d", &e.p.roll_no);
printf("\nEnter Name: ");
flushall( );
gets(e.p.name);
printf("\nEnter Gender <y/n>: ");
flushall( );
scanf("%c", &e.p.gender);
printf("\nEnter E-mail: ");
flushall( );
gets(e.p.email);
printf("Enter marks of three subjects:");
scanf("%d%d", &e.m1, &e.m2);
printf("\nName = %s", e.p.name);
printf("\nRoll No. = %d", e.p.roll_no);
printf("\nGender = %c", e.p.gender);
printf("\nEmail = %s", e.p.email);
printf("Marks = %d %d", e.m1, 2.m2);
getch( );
struct p_details
{
int roll_no;
char name[50];
char gender;
char email[50];
};
struct e_student
{
int m1, m2;
struct p_details p;
};
void main()
{
struct e_student e;
printf("\nEnter roll number:");
}
www.ecti.co.in
C Programming
Array of Structures
#include<stdio.h>
#include<conio.h>
struct student
{
int roll_no;
char name[50];
int m1, m2;
};
typedef struct student STUD;
void main()
{
STUD s[50];
int i, size;
printf("\nEnter no. of records: ");
scanf("%d", &size);
printf("\nEnter %d records:", size);
}
www.ecti.co.in
C Programming
printf("\nEnter Marks:");
scanf("%d%d", &s.m1, &s.m2);
display(s);
getch( );
}
void display(STUD s1)
{
printf("\nRoll No.: %d", s1.roll_no);
printf("\nName: %s", s1.name);
printf("\nMarks: %d %d", s1.m1, s1.m2);
}
C Programming
C Programming
Structure to Pointer
#include<stdio.h>
#include<conio.h>
struct stud
{
int roll_no;
char name[50];
int m1, m2;
};
void display(struct stud *);
void main( )
{
struct stud s;
printf("\nEnter Roll Number: ");
scanf("%d", &s.roll_no);
printf("\nEnter Name: ");
flushall( );
gets(s.name);
www.ecti.co.in
printf("\nEnter Marks:");
scanf("%d%d", &s.m1, &s.m2);
display(&s);
getch( );
}
void display(struct stud *s1)
{
printf("\nRoll No.: %d", s1->roll_no);
printf("\nName: %s", s1->name);
printf("\nMarks: %d %d", s1->m1, s1->m2);
}
C Programming
display(s,3);
getch();
}
void display(struct stud *s1, int n)
{
int i;
printf("\nYou have entered: \n\n");
for(i=0; i<n; i++, s1++)
{
printf("\nEnter roll no.: %d", s1->roll_no);
printf("\nEnter Name: %s", s1->name);
}
}
C Programming
Input / Output in C
There are two types of I/O.
Console I/O
File I/O
C Programming
C Programming
while(1)
{
ch = fgetc(fs);
if(ch == EOF)
break;
else
fputc(ch, ft);
}
fclose(fs);
fclose(ft);
printf("File copied successfully");
getch();
C Programming
fputs("\n", fp);
}
fclose(fp);
printf("Data writing successful");
getch();
}
C Programming
www.ecti.co.in
C Programming
do
{
C Programming
C Programming
do
{
printf("\nEnter name, age, sal:");
scanf("%s%d%f", e.name, &e.age, &e.sal);
fwrite(&e, sizeof(e), 1, fs);
printf("\nAdd another record <y/n>?: ");
fflush(stdin);
another = getche();
}while(another == 'y');
fclose(fs);
printf("\nData written successfully to file");
getch();
}
fs = fopen("Source.txt", "wb");
if(fs == NULL)
{
puts("Cannot open source file");
exit(1);
}
www.ecti.co.in
C Programming
C Programming
www.ecti.co.in
C Programming
ft = fopen(argv[2], "w");
if(ft == NULL)
{
puts("Cannot open target file");
fclose(fs);
getch();
exit(2);
}
while(!feof(fs))
{
ch = fgetc(fs);
fputc(ch, ft);
}
fclose(fs);
fclose(ft);
printf("File copied successfully");
getch();
}
www.ecti.co.in
C Programming
C Programming
Step 2
www.ecti.co.in
C Programming
Step 3
www.ecti.co.in
C Programming
Continued..
www.ecti.co.in
C Programming
END OF BASICS IN C
Thus, the basics of C programming ends here.
We hope you are satisfied with the theory provided.
Feel free to share, distribute or use it in any form you wish to.
IT IS FOR YOU.
www.ecti.co.in
C Programming
END OF BASICS IN C
For advance C programming course or for any doubts in this
tutorial, please contact us on any of the following details:
Call us on: 02065000419 / 02065111419
E-mail us at: prog@ecti.co.in
Web URL: www.ecti.co.in
www.ecti.co.in