KEMBAR78
C introduction by thooyavan | PPTX
Introduction to C programming
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.
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;
}
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. +,*,/,-)
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.
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.
Keywords types
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.
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:
~ ‘ ! @ # % ^ &amp; * ( ) _ - +
= |  { }
[ ] : ; " ' < > , . ? /
Data types
Primary data types
1. Character
2. Integer
3. Float
4. Double
Secondary data types
1. structure
2. Union
3. Pointer
4. enum
Primary data types
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 };
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: ");
Format Specifier
• %d (%i) - integer
• %f - float
• %lf - double
• %c - character
• %s – String
• %u - address(pointer)
OPERATORS
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Misc Operators
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
if statement
Definition:
if (condition) { code here if “true”}
Example:
if(rate==1)
{
printf("nit is poor");
}
if-else statement
Definition:
if(condition){ code when true } else { code when
not true }
Example:
if(A>B)
{ printf(“A is greater"); }
else
{ printf(“B is greater"); }
else if ladder statement
Definition:
if(condition a)
{ statements }
else if (condition b)
{ Statement b }
else
{ statement }
Example:
if(c<a>b)
{ printf(“a is greater”);}
else if(a<b>c)
{ printf(“b is greater”);
else if(b<c>a)
{ printf(“c is greater);}
else
{ printf(“all are equal”); }
Switch case statement
Definition:
Switch (variable)
{
Case 1: statement1 break;
Case 2: statement 2 break; 40
Case 3:statement 3 break;
…..
Default: default statement
break;
}
Example:
Switch(week)
{
Case 1:printf(“Sunday”);break;
Case 2:printf(“Monday”); break;
Case 3:printf(“Tuesday”); break;
Case 4:printf(“Wednesday”);
break;
Case 5:printf(“Thursday”); break;
Default:printf(“---------”); break;
}
Looping statements
For Loop Syntax
for(initialization statement; test
expression; update statement)
{
code/s to be executed;
}
Example:
for(i=1;i<=n;i++)
{
printf(“%d”,i)
}
While loop:
while (test expression)
{
statement/s to be executed.
}
Example:
while(i<=n)
{
printf(“%d”,i);
i++;
}
do...while loop:
do
{
some codes;
}
while (test expression);
Example :
do
{ printf(“%d“,i);
i++;
} while(i<=n);
Functions
Two types:
1.built-in functions
2.User defined functions
2.1 without parameters
2.2 with parameters
Passing parameters:
• call by value
• Call by reference
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);
}
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);
}
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);
}
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 ……….;
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);
}
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);
}
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;
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
example:
void main()
{
file *ptr;
int a;
ptr=fopen(“d:pg.txt”,”w”);
if(ptr==null)
{
printf(“error”);
exit(1);
}
else
{
scanf(“%d”,&a);
fprintf(ptr,”%d”,a);
printf(“successfully writed”);
}
fclose(ptr);

C introduction by thooyavan

  • 1.
    Introduction to Cprogramming
  • 2.
    Introduction to Cprogramming • 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 AC 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 thebasic 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 thepredefined 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.
  • 7.
  • 8.
    Note: • C isa 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: Anyalphabet, 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: ~ ‘ ! @ # % ^ &amp; * ( ) _ - + = | { } [ ] : ; " ' < > , . ? /
  • 10.
    Data types Primary datatypes 1. Character 2. Integer 3. Float 4. Double Secondary data types 1. structure 2. Union 3. Pointer 4. enum
  • 11.
  • 12.
    Secondary data types 1.TypeDefinition 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: ");
  • 14.
    Format Specifier • %d(%i) - integer • %f - float • %lf - double • %c - character • %s – String • %u - address(pointer)
  • 15.
    OPERATORS • Arithmetic Operators •Relational Operators • Logical Operators • Bitwise Operators • Assignment Operators • Misc Operators
  • 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
  • 17.
    if statement Definition: if (condition){ code here if “true”} Example: if(rate==1) { printf("nit is poor"); }
  • 18.
    if-else statement Definition: if(condition){ codewhen true } else { code when not true } Example: if(A>B) { printf(“A is greater"); } else { printf(“B is greater"); }
  • 19.
    else if ladderstatement Definition: if(condition a) { statements } else if (condition b) { Statement b } else { statement } Example: if(c<a>b) { printf(“a is greater”);} else if(a<b>c) { printf(“b is greater”); else if(b<c>a) { printf(“c is greater);} else { printf(“all are equal”); }
  • 20.
    Switch case statement Definition: Switch(variable) { Case 1: statement1 break; Case 2: statement 2 break; 40 Case 3:statement 3 break; ….. Default: default statement break; } Example: Switch(week) { Case 1:printf(“Sunday”);break; Case 2:printf(“Monday”); break; Case 3:printf(“Tuesday”); break; Case 4:printf(“Wednesday”); break; Case 5:printf(“Thursday”); break; Default:printf(“---------”); break; }
  • 21.
    Looping statements For LoopSyntax for(initialization statement; test expression; update statement) { code/s to be executed; } Example: for(i=1;i<=n;i++) { printf(“%d”,i) }
  • 22.
    While loop: while (testexpression) { statement/s to be executed. } Example: while(i<=n) { printf(“%d”,i); i++; }
  • 23.
    do...while loop: do { some codes; } while(test expression); Example : do { printf(“%d“,i); i++; } while(i<=n);
  • 24.
    Functions Two types: 1.built-in functions 2.Userdefined functions 2.1 without parameters 2.2 with parameters Passing parameters: • call by value • Call by reference
  • 25.
    Call by value: voidadd(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 similardata 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 mayreference 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 structureis 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;floatsal; }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;floatsal; }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
  • 33.
    example: void main() { file *ptr; inta; ptr=fopen(“d:pg.txt”,”w”); if(ptr==null) { printf(“error”); exit(1); } else { scanf(“%d”,&a); fprintf(ptr,”%d”,a); printf(“successfully writed”); } fclose(ptr);