SlideShare une entreprise Scribd logo
1  sur  35
M.Florence Dayana
Head, Dept. of BCA
Bon Secours College for Women,
Thanjavur
1
 C was originally developed in the 1970s, by Dennis
Ritchie at Bell Telephone Laboratories, Inc.
 C is a High level , general –purpose structured
programming language.
 Instructions of C consists of terms that are very closely
same to algebraic expressions, consisting of certain
English keywords such as if, else, for ,do and while
 C contains certain additional features that allows it to be
used at a lower level , acting as bridge between machine
language and the high level languages.
 This allows C to be used for system programming as
well as for applications programming
2
• What is C?
• C is a structured, relatively low-level, portable
programming language.
• Why study C?
• Many popular software tools are written in C.
• Has strongly influenced many other languages.
• C-shell, java, C++, Perl, etc.
• Forces the user to understand fundamental
aspects of programming.
• Very concise language.
3
 C language consist of some characters set, numbers
and some special symbols. The character set of C
consist of all the alphabets of English language.
 C consist of
• Alphabets a to z, A to Z
• Numeric 0,1 to 9
• Special Symbols {,},[,],?,+,-,*,/,%,!,;,and more
 The words formed from the character set are building
blocks of C and are sometimes known as tokens.
 These tokens represent the individual entity of
language. The following different types of token are
used in C
1) Identifiers 2) Keywords 3)
Constants 4
• A 'C' program consist of two types of elements , user
defined and system defined.
• Identifiers is nothing but a name given to these
elements.
• An identifier is a word used by a programmer to name
a variable , function, or label.
• identifiers consist of letters and digits, in any order,
except that the first character.
• Identifiers consist of letters and digits if any order,
except that the first character must be letter.
• Both Upper and lowercase letters can be used
5
• Keywords are nothing but
system defined identifiers.
• Keywords are reserved
words of the language.
• They have specific
meaning in the language
and cannot be used by
the programmer as
variable or constant
names
• C is case sensitive, it
means these must be
used as it is
• 32 Keywords in C
Programming
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short
unsigne
d
continue for signed void
default goto sizeof volatile
do if static while
6
• A variable is nothing but a name given to a storage area
that our programs can manipulate.
• Each variable in C has a specific type, which determines
the size and layout of the variable's memory
• The name of a variable can be composed of letters,
digits, and the underscore character.
• Upper and lowercase letters are distinct because C is
case-sensitive. There are following basic variable types
−
Type Description
• char Typically a single octet(one byte). This is an
integer type.
• int The most natural size of integer for the machine.
• float A single-precision floating point value.
• Double A double-precision floating point value.
7
• A constant is a value or an identifier whose value cannot
be altered in a program. For example: 1, 2.5,
• As mentioned, an identifier also can be defined as a
constant. eg. const double PI = 3.14
• Here, PI is a constant. Basically what it means is that, PI
and 3.14 is same for this program.
Integer constants
• A integer constant is a numeric constant (associated with
number) without any fractional or exponential part.
• There are three types of integer constants in C
programming:
• decimal constant (base 10)
• octal constant (base 8)
• hexadecimal constant (base 16)
8
Floating-point constants
• A floating point constant is a numeric constant that
has either a fractional form or an exponent form.
For example: 2.0,0.0000234,-0.22E-5
Character constants
• A character constant is a constant which uses
single quotation around characters.
For example: 'a', 'l', 'm', 'F'
String constants
• String constants are the constants which are
enclosed in a pair of double-quote marks.
For example: "good" ,"x", "Earth is roundn"
9
Sometimes, it is necessary to use characters which
cannot be typed or has special meaning in C
programming.
For example: newline(enter), tab, question mark etc. In
order to use these characters, escape sequence is used.
• For example: n is used for newline.
• The backslash (  ) causes "escape" from the normal way
the characters are interpreted by the compiler.
Escape Sequences Character
• b Backspace f Form feed
n Newline r Return
• t Horizontal tab v Vertical tab
•  Backslash ' Single quotation mark
• " Double quotation mark ? Question mark
• 0 Null character 10
• An operator is a symbol which operates on a
value or a variable. For example: + is an
operator to perform addition.
C programming has wide range of operators to
perform various operations. For better
understanding of operators, these operators can
be classified as:
• Arithmetic Operators
• Increment and Decrement Operators
• Assignment Operators
• Relational Operators
• Logical Operators
• Conditional Operators
• Bitwise Operators 11
Operator Meaning of Operator
• + addition or unary plus
• - subtraction or unary minus
• * multiplication
• / division
• % remainder after division (
modulo division)
12
1. C programming has two operators
increment ++ and decrement -- to change
the value of an operand (constant or
variable) by 1.
2. Increment ++ increases the value by 1
whereas decrement -- decreases the value
by 1.
3. These two operators are unary operators,
meaning they only operate on a single
operand.
eg. int a=10, b=100 ++a = 11 13
• An assignment operator is used for assigning a
value to a variable. The most common
assignment operator is =
Operator Example Same as
• = a = b a = b
• += a += b a = a+b
• -= a -= b a = a-b
• *= a *= b a = a*b
• /= a /= b a = a/b
• %= a %= b a = a%b
14
• A relational operator checks the relationship between two
operands. If the relation is true, it returns 1; if the relation
is false, it returns value 0.
• Relational operators are used in decision making and
loops.
Operator Meaning of Operator Example
• == Equal to 5 == 3 returns 0
• > Greater than 5 > 3 returns 1
• < Less than 5 < 3 returns 0
• != Not equal to 5 != 3 returns 1
• >= Greater than or equal to 5 >= 3 returns 1
• <= Less than or equal to 5 <= 3
return 0
15
• Logical operators, like relational operators, are
typically used in conditional expressions
1. if ( (a == 1) && (b < 3) || (c == 1) ) etc.
• However, these can also be used in regular
expressions
int a = 1, b = 2, c = 3, d;
d = ( a > b ) || ( c == (b – 1) );
What is the value of d here?
16
17
• Comparision Operators: <, > , <=, >= , !=, ==,
!, &&, || .
•example:
• int a=4, b=5;
• a<b returns a true(non zero number) value.
• Bitwise Operators: <<, >>, ~, &, | ,^ .
•example
• int a=8;
• a= a>>1; // value of a becomes 4
18
• Meaning of a + b * c ?
is it a+(b*c) or (a+b)*c
• All operators have precedence over each
other
• *, / have more precedence over +, - .
• If both *, / are used, associativity comes into
picture. (more on this later)
• example :
• 5+4*3 = 5+12= 17.
19
Highest on top
++ -- (Postfix)
++ -- (Prefix)
* / %
+ -
<< >>
< >
&
|
&&
||
20
• Input operation
• an instruction that copies data from an input
device into memory
• Output operation
• an instruction that displays information stored
in memory to the output devices (such as the
monitor screen)
21
• A C function that performs an input or output
operation
• A few functions that are pre-defined in the
header file stdio.h such as :
•printf()
•scanf()
•getchar() & putchar()
22
• Used to send data to the standard output
(usually the monitor) to be printed according to
specific format.
• General format:
• printf(“string literal”);
• A sequence of any number of characters surrounded by
double quotation marks.
• printf(“format string”, variables);
• Format string is a combination of text, conversion
specifier and escape sequence.
23
• Example:
• printf(“Thank you”);
• printf (“Total sum is: %dn”, sum);
• %d is a placeholder (conversion specifier)
• marks the display position for a type integer
variable
• n is an escape sequence
• moves the cursor to the new line
Principles of Programming -
NI 2005
24
• Read data from the standard input device
(usually keyboard) and store it in a variable.
• General format:
• scanf(“Format string”, &variable);
• Notice ampersand (&) operator :
• C address of operator
• it passes the address of the variable instead of the
variable itself
• tells the scanf() where to find the variable to store the
new value
Principles of Programming -
NI 2005
25
• Example :
int age;
printf(“Enter your age: “);
scanf(“%d”, &age);
• Common Conversion Identifier used in printf
and scanf functions.
printf scanf
int %d %d
float %f %f
double %f %lf
char %c %c
string %s %s
Principles of Programming -
NI 2005
26
• If you want the user to enter more than one
value, you serialise the inputs.
• Example:
float height, weight;
printf(“Please enter your height and weight:”);
scanf(“%f%f”, &height, &weight);
Principles of Programming -
NI 2005
27
• getchar() - read a character from standard input
• putchar() - write a character to standard output
• Example: #include <stdio.h>
void main(void)
{
char my_char;
printf(“Please type a character: ”);
my_char = getchar();
printf(“nYou have typed this character: ”);
putchar(my_char);
}
Principles of Programming -
NI 2005
28
• Alternatively, you can write the previous code
using normal scanf and %c placeholder.
• Example
#include <stdio.h>
void main(void)
{
char my_char;
printf(“Please type a character: ”);
scanf(“%c”,&my_char);
printf(“nYou have typed this character: %c ”, my_char);
}
29
• The operands of a binary operator must have a the
same type and the result is also of the same type.
• Integer division:
c = (9 / 5)*(f - 32)
The operands of the division are both int and hence the
result also would be int. For correct results, one may
write
c = (9.0 / 5.0)*(f - 32)
• In case the two operands of a binary operator are
different, but compatible, then they are converted to the
same type by the compiler. The mechanism (set of
rules) is called Automatic Type Casting.
c = (9.0 / 5)*(f - 32)
30
1. char and short operands are converted
to int
2. Lower data types are converted to the
higher data types and result is of higher
type.
3. The conversions between unsigned and
signed types may not yield intuitive
results.
4. Example
float f; double d; long l;
int i; short s;
d + f f will be converted to double
i / s s will be converted to int
l / i i is converted to long; long
Hierarchy
Double
float
long
Int
Short and
char
31
• Identify the start of the program
• Every C program has a main ( )
• 'main' is a C keyword. We must not use it for
any other variable.
• 4 common ways of main declaration
int
main(void)
{
return 0;
}
void
main(void)
{
}
main(void)
{
}
main( )
{
}
• Note: all statements end with a semicolon;
• Statements can (with a few exceptions) be
broken across lines or ganged on a single line
• Commas separate multiple declarations
• Blank lines have no effect
• Extra spaces between tokens has no effect.
• Comments are ignored by the compiler
32
33
• A specification of an action to be taken by the
computer as the program executes.
• Each statement in C needs to be terminated with
semicolon (;)
• Example:
#include <stdio.h>
void main(void)
{
printf(“I love programmingn”);
printf(“You will love it too once ”);
printf(“you know the trickn”);
}
statement
statement
statement
• So far our C programs are as follows:
/* description of program */
#include <stdio.h>
/* any other includes go here */
int main(){
/* program body */
return 0;
}
• Let’s learn more about the structure of “program
body” 34
35

Contenu connexe

Tendances

Javascript by Yahoo
Javascript by YahooJavascript by Yahoo
Javascript by Yahoo
birbal
 
C programming | Class 8 | III Term
C programming  | Class 8  | III TermC programming  | Class 8  | III Term
C programming | Class 8 | III Term
Andrew Raj
 
C language (Collected By Dushmanta)
C language  (Collected By Dushmanta)C language  (Collected By Dushmanta)
C language (Collected By Dushmanta)
Dushmanta Nath
 
Basic of the C language
Basic of the C languageBasic of the C language
Basic of the C language
Sachin Verma
 

Tendances (20)

C language basics
C language basicsC language basics
C language basics
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginner
 
Basic C Programming language
Basic C Programming languageBasic C Programming language
Basic C Programming language
 
c-programming
c-programmingc-programming
c-programming
 
Javascript by Yahoo
Javascript by YahooJavascript by Yahoo
Javascript by Yahoo
 
C programming | Class 8 | III Term
C programming  | Class 8  | III TermC programming  | Class 8  | III Term
C programming | Class 8 | III Term
 
#Code2 create c++ for beginners
#Code2 create  c++ for beginners #Code2 create  c++ for beginners
#Code2 create c++ for beginners
 
C language
C languageC language
C language
 
Constant and variacles in c
Constant   and variacles in cConstant   and variacles in c
Constant and variacles in c
 
Introduction to c language
Introduction to c languageIntroduction to c language
Introduction to c language
 
C Token’s
C Token’sC Token’s
C Token’s
 
C language (Collected By Dushmanta)
C language  (Collected By Dushmanta)C language  (Collected By Dushmanta)
C language (Collected By Dushmanta)
 
C Tokens
C TokensC Tokens
C Tokens
 
Unit 2. Elements of C
Unit 2. Elements of CUnit 2. Elements of C
Unit 2. Elements of C
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 
Fundamentals of c language
Fundamentals of c languageFundamentals of c language
Fundamentals of c language
 
C tokens
C tokensC tokens
C tokens
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ Language
 
Basic of the C language
Basic of the C languageBasic of the C language
Basic of the C language
 
Learning c - An extensive guide to learn the C Language
Learning c - An extensive guide to learn the C LanguageLearning c - An extensive guide to learn the C Language
Learning c - An extensive guide to learn the C Language
 

Similaire à M.Florence Dayana / Basics of C Language

Similaire à M.Florence Dayana / Basics of C Language (20)

Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
Funa-C.ppt
Funa-C.pptFuna-C.ppt
Funa-C.ppt
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
Basics of C (1).ppt
Basics of C (1).pptBasics of C (1).ppt
Basics of C (1).ppt
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
Basics of C (1).ppt
Basics of C (1).pptBasics of C (1).ppt
Basics of C (1).ppt
 
C programming basic pdf.ppt
C programming basic pdf.pptC programming basic pdf.ppt
C programming basic pdf.ppt
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
Basics of C--C Basics------------------.ppt
Basics of C--C Basics------------------.pptBasics of C--C Basics------------------.ppt
Basics of C--C Basics------------------.ppt
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
Basics of C
Basics of CBasics of C
Basics of C
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
Basic of c language
Basic of c languageBasic of c language
Basic of c language
 

Plus de Dr.Florence Dayana

Plus de Dr.Florence Dayana (20)

Dr.M.Florence Dayana-Cloud Computing-unit - 4.pdf
Dr.M.Florence Dayana-Cloud Computing-unit - 4.pdfDr.M.Florence Dayana-Cloud Computing-unit - 4.pdf
Dr.M.Florence Dayana-Cloud Computing-unit - 4.pdf
 
Dr.M.Florence Dayana-Cloud Computing-Unit - 1.pdf
Dr.M.Florence Dayana-Cloud Computing-Unit - 1.pdfDr.M.Florence Dayana-Cloud Computing-Unit - 1.pdf
Dr.M.Florence Dayana-Cloud Computing-Unit - 1.pdf
 
M. Florence Dayana - Hadoop Foundation for Analytics.pptx
M. Florence Dayana - Hadoop Foundation for Analytics.pptxM. Florence Dayana - Hadoop Foundation for Analytics.pptx
M. Florence Dayana - Hadoop Foundation for Analytics.pptx
 
M. FLORENCE DAYANA/unit - II logic gates and circuits.pdf
M. FLORENCE DAYANA/unit - II logic gates and circuits.pdfM. FLORENCE DAYANA/unit - II logic gates and circuits.pdf
M. FLORENCE DAYANA/unit - II logic gates and circuits.pdf
 
M.FLORENCE DAYANA/electronic mail security.pdf
M.FLORENCE DAYANA/electronic mail security.pdfM.FLORENCE DAYANA/electronic mail security.pdf
M.FLORENCE DAYANA/electronic mail security.pdf
 
M. FLORENCE DAYANA - INPUT & OUTPUT DEVICES.pdf
M. FLORENCE DAYANA - INPUT & OUTPUT DEVICES.pdfM. FLORENCE DAYANA - INPUT & OUTPUT DEVICES.pdf
M. FLORENCE DAYANA - INPUT & OUTPUT DEVICES.pdf
 
Professional English - Reading
Professional English - ReadingProfessional English - Reading
Professional English - Reading
 
Professional English - Speaking
Professional English - SpeakingProfessional English - Speaking
Professional English - Speaking
 
Professional English - Listening
Professional English - ListeningProfessional English - Listening
Professional English - Listening
 
INPUT AND OUTPUT DEVICES.pdf
INPUT  AND OUTPUT DEVICES.pdfINPUT  AND OUTPUT DEVICES.pdf
INPUT AND OUTPUT DEVICES.pdf
 
NETWORK SECURITY-SET.pptx
NETWORK SECURITY-SET.pptxNETWORK SECURITY-SET.pptx
NETWORK SECURITY-SET.pptx
 
Network Security- Secure Socket Layer
Network Security- Secure Socket LayerNetwork Security- Secure Socket Layer
Network Security- Secure Socket Layer
 
M.florence dayana dream weaver
M.florence dayana   dream weaverM.florence dayana   dream weaver
M.florence dayana dream weaver
 
M.florence dayana computer networks transport layer
M.florence dayana   computer networks transport layerM.florence dayana   computer networks transport layer
M.florence dayana computer networks transport layer
 
M.Florence Dayana Computer Networks Types
M.Florence Dayana  Computer Networks TypesM.Florence Dayana  Computer Networks Types
M.Florence Dayana Computer Networks Types
 
M.Florence Dayana Computer Networks Introduction
M.Florence Dayana   Computer Networks IntroductionM.Florence Dayana   Computer Networks Introduction
M.Florence Dayana Computer Networks Introduction
 
M. FLORENCE DAYANA/DATABASE MANAGEMENT SYSYTEM
M. FLORENCE DAYANA/DATABASE MANAGEMENT SYSYTEMM. FLORENCE DAYANA/DATABASE MANAGEMENT SYSYTEM
M. FLORENCE DAYANA/DATABASE MANAGEMENT SYSYTEM
 
M.Florence Dayana
M.Florence DayanaM.Florence Dayana
M.Florence Dayana
 
M.Florence Dayana/Cryptography and Network security
M.Florence Dayana/Cryptography and Network securityM.Florence Dayana/Cryptography and Network security
M.Florence Dayana/Cryptography and Network security
 
M.FLORENCE DAYANA WEB DESIGN -Unit 5 XML
M.FLORENCE DAYANA WEB DESIGN -Unit 5   XMLM.FLORENCE DAYANA WEB DESIGN -Unit 5   XML
M.FLORENCE DAYANA WEB DESIGN -Unit 5 XML
 

Dernier

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Dernier (20)

Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 

M.Florence Dayana / Basics of C Language

  • 1. M.Florence Dayana Head, Dept. of BCA Bon Secours College for Women, Thanjavur 1
  • 2.  C was originally developed in the 1970s, by Dennis Ritchie at Bell Telephone Laboratories, Inc.  C is a High level , general –purpose structured programming language.  Instructions of C consists of terms that are very closely same to algebraic expressions, consisting of certain English keywords such as if, else, for ,do and while  C contains certain additional features that allows it to be used at a lower level , acting as bridge between machine language and the high level languages.  This allows C to be used for system programming as well as for applications programming 2
  • 3. • What is C? • C is a structured, relatively low-level, portable programming language. • Why study C? • Many popular software tools are written in C. • Has strongly influenced many other languages. • C-shell, java, C++, Perl, etc. • Forces the user to understand fundamental aspects of programming. • Very concise language. 3
  • 4.  C language consist of some characters set, numbers and some special symbols. The character set of C consist of all the alphabets of English language.  C consist of • Alphabets a to z, A to Z • Numeric 0,1 to 9 • Special Symbols {,},[,],?,+,-,*,/,%,!,;,and more  The words formed from the character set are building blocks of C and are sometimes known as tokens.  These tokens represent the individual entity of language. The following different types of token are used in C 1) Identifiers 2) Keywords 3) Constants 4
  • 5. • A 'C' program consist of two types of elements , user defined and system defined. • Identifiers is nothing but a name given to these elements. • An identifier is a word used by a programmer to name a variable , function, or label. • identifiers consist of letters and digits, in any order, except that the first character. • Identifiers consist of letters and digits if any order, except that the first character must be letter. • Both Upper and lowercase letters can be used 5
  • 6. • Keywords are nothing but system defined identifiers. • Keywords are reserved words of the language. • They have specific meaning in the language and cannot be used by the programmer as variable or constant names • C is case sensitive, it means these must be used as it is • 32 Keywords in C Programming auto double int struct break else long switch case enum register typedef char extern return union const float short unsigne d continue for signed void default goto sizeof volatile do if static while 6
  • 7. • A variable is nothing but a name given to a storage area that our programs can manipulate. • Each variable in C has a specific type, which determines the size and layout of the variable's memory • The name of a variable can be composed of letters, digits, and the underscore character. • Upper and lowercase letters are distinct because C is case-sensitive. There are following basic variable types − Type Description • char Typically a single octet(one byte). This is an integer type. • int The most natural size of integer for the machine. • float A single-precision floating point value. • Double A double-precision floating point value. 7
  • 8. • A constant is a value or an identifier whose value cannot be altered in a program. For example: 1, 2.5, • As mentioned, an identifier also can be defined as a constant. eg. const double PI = 3.14 • Here, PI is a constant. Basically what it means is that, PI and 3.14 is same for this program. Integer constants • A integer constant is a numeric constant (associated with number) without any fractional or exponential part. • There are three types of integer constants in C programming: • decimal constant (base 10) • octal constant (base 8) • hexadecimal constant (base 16) 8
  • 9. Floating-point constants • A floating point constant is a numeric constant that has either a fractional form or an exponent form. For example: 2.0,0.0000234,-0.22E-5 Character constants • A character constant is a constant which uses single quotation around characters. For example: 'a', 'l', 'm', 'F' String constants • String constants are the constants which are enclosed in a pair of double-quote marks. For example: "good" ,"x", "Earth is roundn" 9
  • 10. Sometimes, it is necessary to use characters which cannot be typed or has special meaning in C programming. For example: newline(enter), tab, question mark etc. In order to use these characters, escape sequence is used. • For example: n is used for newline. • The backslash ( ) causes "escape" from the normal way the characters are interpreted by the compiler. Escape Sequences Character • b Backspace f Form feed n Newline r Return • t Horizontal tab v Vertical tab • Backslash ' Single quotation mark • " Double quotation mark ? Question mark • 0 Null character 10
  • 11. • An operator is a symbol which operates on a value or a variable. For example: + is an operator to perform addition. C programming has wide range of operators to perform various operations. For better understanding of operators, these operators can be classified as: • Arithmetic Operators • Increment and Decrement Operators • Assignment Operators • Relational Operators • Logical Operators • Conditional Operators • Bitwise Operators 11
  • 12. Operator Meaning of Operator • + addition or unary plus • - subtraction or unary minus • * multiplication • / division • % remainder after division ( modulo division) 12
  • 13. 1. C programming has two operators increment ++ and decrement -- to change the value of an operand (constant or variable) by 1. 2. Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. 3. These two operators are unary operators, meaning they only operate on a single operand. eg. int a=10, b=100 ++a = 11 13
  • 14. • An assignment operator is used for assigning a value to a variable. The most common assignment operator is = Operator Example Same as • = a = b a = b • += a += b a = a+b • -= a -= b a = a-b • *= a *= b a = a*b • /= a /= b a = a/b • %= a %= b a = a%b 14
  • 15. • A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0. • Relational operators are used in decision making and loops. Operator Meaning of Operator Example • == Equal to 5 == 3 returns 0 • > Greater than 5 > 3 returns 1 • < Less than 5 < 3 returns 0 • != Not equal to 5 != 3 returns 1 • >= Greater than or equal to 5 >= 3 returns 1 • <= Less than or equal to 5 <= 3 return 0 15
  • 16. • Logical operators, like relational operators, are typically used in conditional expressions 1. if ( (a == 1) && (b < 3) || (c == 1) ) etc. • However, these can also be used in regular expressions int a = 1, b = 2, c = 3, d; d = ( a > b ) || ( c == (b – 1) ); What is the value of d here? 16
  • 17. 17 • Comparision Operators: <, > , <=, >= , !=, ==, !, &&, || . •example: • int a=4, b=5; • a<b returns a true(non zero number) value. • Bitwise Operators: <<, >>, ~, &, | ,^ . •example • int a=8; • a= a>>1; // value of a becomes 4
  • 18. 18 • Meaning of a + b * c ? is it a+(b*c) or (a+b)*c • All operators have precedence over each other • *, / have more precedence over +, - . • If both *, / are used, associativity comes into picture. (more on this later) • example : • 5+4*3 = 5+12= 17.
  • 19. 19 Highest on top ++ -- (Postfix) ++ -- (Prefix) * / % + - << >> < > & | && ||
  • 20. 20 • Input operation • an instruction that copies data from an input device into memory • Output operation • an instruction that displays information stored in memory to the output devices (such as the monitor screen)
  • 21. 21 • A C function that performs an input or output operation • A few functions that are pre-defined in the header file stdio.h such as : •printf() •scanf() •getchar() & putchar()
  • 22. 22 • Used to send data to the standard output (usually the monitor) to be printed according to specific format. • General format: • printf(“string literal”); • A sequence of any number of characters surrounded by double quotation marks. • printf(“format string”, variables); • Format string is a combination of text, conversion specifier and escape sequence.
  • 23. 23 • Example: • printf(“Thank you”); • printf (“Total sum is: %dn”, sum); • %d is a placeholder (conversion specifier) • marks the display position for a type integer variable • n is an escape sequence • moves the cursor to the new line
  • 24. Principles of Programming - NI 2005 24 • Read data from the standard input device (usually keyboard) and store it in a variable. • General format: • scanf(“Format string”, &variable); • Notice ampersand (&) operator : • C address of operator • it passes the address of the variable instead of the variable itself • tells the scanf() where to find the variable to store the new value
  • 25. Principles of Programming - NI 2005 25 • Example : int age; printf(“Enter your age: “); scanf(“%d”, &age); • Common Conversion Identifier used in printf and scanf functions. printf scanf int %d %d float %f %f double %f %lf char %c %c string %s %s
  • 26. Principles of Programming - NI 2005 26 • If you want the user to enter more than one value, you serialise the inputs. • Example: float height, weight; printf(“Please enter your height and weight:”); scanf(“%f%f”, &height, &weight);
  • 27. Principles of Programming - NI 2005 27 • getchar() - read a character from standard input • putchar() - write a character to standard output • Example: #include <stdio.h> void main(void) { char my_char; printf(“Please type a character: ”); my_char = getchar(); printf(“nYou have typed this character: ”); putchar(my_char); }
  • 28. Principles of Programming - NI 2005 28 • Alternatively, you can write the previous code using normal scanf and %c placeholder. • Example #include <stdio.h> void main(void) { char my_char; printf(“Please type a character: ”); scanf(“%c”,&my_char); printf(“nYou have typed this character: %c ”, my_char); }
  • 29. 29 • The operands of a binary operator must have a the same type and the result is also of the same type. • Integer division: c = (9 / 5)*(f - 32) The operands of the division are both int and hence the result also would be int. For correct results, one may write c = (9.0 / 5.0)*(f - 32) • In case the two operands of a binary operator are different, but compatible, then they are converted to the same type by the compiler. The mechanism (set of rules) is called Automatic Type Casting. c = (9.0 / 5)*(f - 32)
  • 30. 30 1. char and short operands are converted to int 2. Lower data types are converted to the higher data types and result is of higher type. 3. The conversions between unsigned and signed types may not yield intuitive results. 4. Example float f; double d; long l; int i; short s; d + f f will be converted to double i / s s will be converted to int l / i i is converted to long; long Hierarchy Double float long Int Short and char
  • 31. 31 • Identify the start of the program • Every C program has a main ( ) • 'main' is a C keyword. We must not use it for any other variable. • 4 common ways of main declaration int main(void) { return 0; } void main(void) { } main(void) { } main( ) { }
  • 32. • Note: all statements end with a semicolon; • Statements can (with a few exceptions) be broken across lines or ganged on a single line • Commas separate multiple declarations • Blank lines have no effect • Extra spaces between tokens has no effect. • Comments are ignored by the compiler 32
  • 33. 33 • A specification of an action to be taken by the computer as the program executes. • Each statement in C needs to be terminated with semicolon (;) • Example: #include <stdio.h> void main(void) { printf(“I love programmingn”); printf(“You will love it too once ”); printf(“you know the trickn”); } statement statement statement
  • 34. • So far our C programs are as follows: /* description of program */ #include <stdio.h> /* any other includes go here */ int main(){ /* program body */ return 0; } • Let’s learn more about the structure of “program body” 34
  • 35. 35