SlideShare ist ein Scribd-Unternehmen logo
1 von 33
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);

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

C tokens
C tokensC tokens
C tokens
 
Introduction to c++ ppt
Introduction to c++ pptIntroduction to c++ ppt
Introduction to c++ ppt
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ Language
 
History of c++
History of c++ History of c++
History of c++
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Introduction to c++ ppt 1
Introduction to c++ ppt 1Introduction to c++ ppt 1
Introduction to c++ ppt 1
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 
C functions
C functionsC functions
C functions
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Enums in c
Enums in cEnums in c
Enums in c
 
Introduction to C Programming - I
Introduction to C Programming - I Introduction to C Programming - I
Introduction to C Programming - I
 
Function in C
Function in CFunction in C
Function in C
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
Intro to c++
Intro to c++Intro to c++
Intro to c++
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
Type casting in c programming
Type casting in c programmingType casting in c programming
Type casting in c programming
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
Basics of c++
Basics of c++Basics of c++
Basics of c++
 
C++ ppt
C++ pptC++ ppt
C++ ppt
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 

Ähnlich wie C introduction by thooyavan

C prog ppt
C prog pptC prog ppt
C prog pptxinoe
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C ProgrammingMOHAMAD NOH AHMAD
 
Unit 4 Foc
Unit 4 FocUnit 4 Foc
Unit 4 FocJAYA
 
c_pro_introduction.pptx
c_pro_introduction.pptxc_pro_introduction.pptx
c_pro_introduction.pptxRohitRaj744272
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginnersophoeutsen2
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programmingAlpana Gupta
 
C PROGRAMMING LANGUAGE.pptx
 C PROGRAMMING LANGUAGE.pptx C PROGRAMMING LANGUAGE.pptx
C PROGRAMMING LANGUAGE.pptxAnshSrivastava48
 
C programing Tutorial
C programing TutorialC programing Tutorial
C programing TutorialMahira Banu
 
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit DubeyMCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubeykiranrajat
 
C presentation book
C presentation bookC presentation book
C presentation bookkrunal1210
 
M.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageM.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageDr.Florence Dayana
 

Ähnlich wie C introduction by thooyavan (20)

C prog ppt
C prog pptC prog ppt
C prog ppt
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
Unit 4 Foc
Unit 4 FocUnit 4 Foc
Unit 4 Foc
 
Introduction to C
Introduction to CIntroduction to C
Introduction to C
 
c_pro_introduction.pptx
c_pro_introduction.pptxc_pro_introduction.pptx
c_pro_introduction.pptx
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginner
 
c programming session 1.pptx
c programming session 1.pptxc programming session 1.pptx
c programming session 1.pptx
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
C programming
C programmingC programming
C programming
 
C PROGRAMMING LANGUAGE.pptx
 C PROGRAMMING LANGUAGE.pptx C PROGRAMMING LANGUAGE.pptx
C PROGRAMMING LANGUAGE.pptx
 
C programing Tutorial
C programing TutorialC programing Tutorial
C programing Tutorial
 
C intro
C introC intro
C intro
 
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit DubeyMCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
 
C programming day#1
C programming day#1C programming day#1
C programming day#1
 
Basic of C Programming | 2022 Updated | By Shamsul H. Ansari
Basic of C Programming | 2022 Updated | By Shamsul H. AnsariBasic of C Programming | 2022 Updated | By Shamsul H. Ansari
Basic of C Programming | 2022 Updated | By Shamsul H. Ansari
 
C introduction
C introductionC introduction
C introduction
 
C presentation book
C presentation bookC presentation book
C presentation book
 
M.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageM.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C Language
 
C programming
C programmingC programming
C programming
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 

Mehr von Thooyavan Venkatachalam (7)

Import data MySQL to Excel & Excel to MySQL
Import data MySQL to Excel & Excel to MySQLImport data MySQL to Excel & Excel to MySQL
Import data MySQL to Excel & Excel to MySQL
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 
Career After B.A in Eng(lit)
Career After B.A in Eng(lit)Career After B.A in Eng(lit)
Career After B.A in Eng(lit)
 
Groupdiscussion
GroupdiscussionGroupdiscussion
Groupdiscussion
 
Bodylanguage
BodylanguageBodylanguage
Bodylanguage
 
advanced security system for women
advanced security system for womenadvanced security system for women
advanced security system for women
 

Kürzlich hochgeladen

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 

Kürzlich hochgeladen (20)

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 

C introduction by thooyavan

  • 1. Introduction to C programming
  • 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: ~ ‘ ! @ # % ^ &amp; * ( ) _ - + = | { } [ ] : ; " ' < > , . ? /
  • 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: ");
  • 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){ code when true } else { code when not true } Example: if(A>B) { printf(“A is greater"); } else { printf(“B is greater"); }
  • 19. 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”); }
  • 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 Loop Syntax 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 (test expression) { 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.User defined functions 2.1 without parameters 2.2 with parameters Passing parameters: • call by value • Call by reference
  • 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
  • 33. 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);