2. Introduction to C programming
• C is a programming language developed by AT & T Bell Laboratories of USA in
1972. It was designed and written by Dennis Ritchie. C is reliable and easy to
use. C language is a base to learn different programming language.
• If you want to learn C++ or JAVA, without the knowledge of C it becomes very
difficult to learn these programming languages. Many major components of
popular operating systems like Windows, UNIX, LINUX is still written in C.
Nothing beats C in terms of Speed of Execution.
3. Structure of C
A C program basically has the
following form:
• Pre-processor Commands
• Functions
• Variables
• Statements & Expressions
• Comments
Sample C program:
#include <stdio.h> // Pre-
processor Commands
int main() // Main Function
{
/* My first program */ //
Comments
printf("Hello, World! n"); //
Statements
return 0;
}
4. Tokens
Tokens are the basic building blocks in a C language which are
constructed together to write a C program.
Types:
• 1. Keywords (E.g. switch, int)
• 2. Identifiers (E.g. main, total)
• 3. Constants (E.g. 10,30)
• 4. Strings (E.g. “hello”, “welcome”)
• 5. Special symbols (E.g. (),{})
• 6. Operators (E.g. +,*,/,-)
5. Example
int main()
{
int x,y,sum;
x=5;
y=10;
sum = x+y;
printf(“sum= %d”, sum);
getch();
}
where,
main, sum – Identifier
int – Keyword
x, y, sum – Identifier
(,),{,} – Delimiter
sum, int, x, y, (, ), {, } – Tokens
The real time application
program where Token is used are Real
time calculator and Real time Bank
application programs.
6. Keywords
Keywords are the predefined words which is meant for
specific purpose in a C program. Since, keywords are the
referred names for complier, they can’t be used as variable
name.
32 keywords are there in C language.
8. Note:
• C is a case sensitive language.
• White spaces (tab space and space bar) are ignored.
• Each and every statement must be terminated with a semicolo
• Multiple statements can be on the same line.
• Statements can continue over multiple statements.
9. C Character set:
Any alphabet, digit or special
symbol can be termed as a character.
Below shows list of valid alphabets,
digits and symbols allowed in C.
Alphabets:
A, B, C, D, …, X, Y, Z
a, b, c, d, … ,x, y, z
Digits:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special Symbols:
~ ‘ ! @ # % ^ & * ( ) _ - +
= | { }
[ ] : ; " ' < > , . ? /
10. Data types
Primary data types
1. Character
2. Integer
3. Float
4. Double
Secondary data types
1. structure
2. Union
3. Pointer
4. enum
12. Secondary data types
1.Type Definition
Example:
typedef int age;
age male, female;
2.Enumerated Data type
Example :
enum
mon{jan,feb,mar,apr,may,jun,jul,
aug,sep,oct,nov,dec };
13. Input/Output Functions
Input functions:
scanf() function: This is the
function which can be used to to
read an input from the command
line.
Example:
int a;
printf(“Enter the number n");
scanf("%d",&a);
Output functions:
printf() function: This is one
of the most frequently used
functions in C for output.
Example:
printf("Enter an integer: ");
16. Decision Making Statements
• Programs should be able to make logical (true/false)
decisions based on the condition they are in; every program
has one or few problem/s to solve.
Types:
• if statement
• if-else statement
• Nested if statement
• else-if ladder statement
• Switch case statement
25. Call by value:
void add(int,int);
void main()
{
add(5,10);
add(10,20);
getch();
}
void add(int a,int b)
{
int c;
c=a+b;
printf(“%d”,c);
}
Call by reference:
void disp(int);
void main()
{
int i;
printf(“enter the no”);
scanf(“%d”,&i);
disp(&i);
printf(“%d”,i);
}
void disp(int *a)
{
*a=*a+10;
Printf(“%d”,*a);
}
26. Arrays
Collection of similar data
type
Syntax;
Datatype Arrayname[size];
Example: INTEGER
DATA TYPE
Int a[20];
for(i=0;i<=19;i++)
{
Scanf(“%d”,a[i]);
}
Example:char data type
Void main()
{
Char s[50];
Printf(“enter the name”);
Gets(s);
Puts(s);
}
27. Pointers
• Pointers may reference a function or an object.
• The value of a pointer is the address of the corresponding object or
function
• Examples: int *i; char *x;
Example:
Void main()
{
int a=10;
int *p;
p=&a;
printf(“the address is %u”,p);
printf(“the value is %d”,*p);
}
28. Structures
• A structure is a collection of one or more components (members),
which may be of different types.
Syntax:
Struct structname;
{
datatype variable;
} s1, s2 ……….;
29. Example:
Struct emp
{
int id;float sal;
}e;
Void main()
{
Printf(“enter the id”);
Scanf(“%d”,e.id);
Printf(“enter the sal”);
Scanf(“%d”,e.sal);
Printf(“your id is %d”,e.id);
printf(“your salary is %d”,e.salary);
}
30. Unions
Example:
union emp
{
int id;float sal;
}e;
Void main()
{
Printf(“enter the id”);
Scanf(“%d”,e.id);
Printf(“enter the sal”);
Scanf(“%d”,e.sal);
Printf(“your id is %d”,e.id);
printf(“your salary is %d”,e.salary);
}
31. File handling
File Operations
• Creating a new file
• Opening an existing file
• Reading from and writing information to a file
• Closing a file
Working with file
• While working with file, you need to declare a pointer of type file.
This declaration is needed for communication between file and
program.
FILE *ptr;
32. Opening a file:
• Opening a file is performed using library function fopen().
Syntax:
ptr=fopen("fileopen", mode")
For Example:
fopen("E:cprogramprogram.txt","w");
Closing a File:
• The file should be closed after reading/writing of a file.
Closing a file is performed using library function fclose().
fclose(ptr);
//ptr is the file pointer associated with file to be
closed