SlideShare a Scribd company logo
1 of 82
Beginners of c
CHAPTER 1-
OvERviEw
About C
• C was found by Dennis Ritchie in the year
1970.
• C is a structured programming language.This
means that is a proper collection of modules
would make a complete program.
Module 1
Module 2
Module n
A Program
ExampleBook
Name
Author
Name
Publications
name
Content,Page
no etc.,
Book
To Add Two Numbers
Get B
Get A
C=a+b
Addition
About C…
• C is a high level language.
• There are only 32 keywords in ANSI C.
• C is highly portable. This means that C
programs written for one computer can run
on another with little or no modification.
Complete Structure of c program
Document Section
Link Section
Global Declaration Section
Main()Function
{
}
Sub Program Section
Declaration Part
Execution Part
Function 1
Function 2
Function n
Simple Structure of C
header Files
void main()
{
variable declaration;
program statements;
}
Header Files
• It is called as preprocessor(predefined or in built or
default)
• It is one of the important function or one of the main
feature of ’c’.
• Header files are included in hash(#).
• Syntax:
#<directive name> <files (or) variables>
• Example
#include<stdio.h>
Predefined header Files
#include<conio.h>
• Stdio.h-Standard Input Output.header file
• The stdio.h header file should be used in a
program when printf and scanf statements are
used.
• conio.h-console Input Output.header file
• The conio.h header file should be used for
storing purpose and retriving it back.It is used
when clrscr() and getch() functions are used.
void main() function
• void-returns nothing
• It is a special function in ‘c’ which informs the
computer about the starting position of the
program.It is a combination of variable
declaration and program statements.
• Every program should have one main() function
void main()
{
}
Start of main() function
End of the main()
function
Program Statements
• Print function
• Scan function(We shall see this in the next
section)
Print function
• It is used to print the expected output in the user screen.
• Syntax:
printf(“message (or) string”);
printf(“control string”,variable 1,variable2,variable n);
• Now,We shall deal about the 1st
syntax
• Example
printf(“hai doakians”);
• Every Statement in C should end with SEMICOLON(;).
• There should be no space between ‘print’ and ‘f’.It is a single
word printf.
Write a program to print a
message
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“Hai doakians”);
getch();
}
Output:-
Hai doakians
• clrscr();-used to clear the output screen.
• getch();-used to run the program by getting
any character as input.
Comment
• Comment is a note that can be put into the source code.
• It is ignored by the compiler.
• Single line comment-Starts with //
• Example:
//Print a message
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“Hai doakians”);
getch();
}
• The commented line would be ignored during execution.
• Multi line comment-starts with/*…….*/
• Example:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
/*printf(“welcome”);
printf(“good morning”);*/
printf(“Hai doakians”);
getch();
}
Output:
How to run a program?
• The blue screen appears.
Step 1:-
-Select File
-Then select new
-Then select file again and click save
-Then save it with a file name
suffixed with the extension .c
-eg:-printhai.c
Step 2:-
Then type the coding line by line
Step 3:-
Again save the program with the keyboard
shortcut f2
Step 4:-
Compile the program with the
keyboard shortcut alt+f9.
Step 5:-
Correct the errors if any-then repeat
step2 to step4.
Step 6:-
Run the program with the keyboard
shortcut ctrl+f9
How to open and access the saved
file?
Step 1:-
Select File and then Open
Step 2:-
Then type your saved file name with
the extension.c
Important points to be noted….
• Every program should have a header file
• Every statement in the program should end with the
semicolon(;) except header files, main()function or
any other functions.
• A program can also be run without getch() function by
using the shortcut alt+f5 instead of ctrl+f9
• C is case sensitive i.e., lower case(small letters) or
upper case(capital letters) can be used. In order to
avoid errors its better to use lower case(small letters).
• There should be no space between ‘print’ and ‘f’.It is a
single word printf.
Exercise
• Write a c program to display a set of
statements related to your college.
Chapter 2
Character constants
• It is a combination of letters(A to Z ,a to z),
digits, special characters and white spaces.
• Digits may be of 3 types
1.Octal (0 to 7)(base=8)
2.Decimal(0 to 9)(base=10)
3.Hexadecimal (0 to 9,A,B,C,D,E,F)(base=16)
Special characters
Special
characters
Description
, Comma
. Period
; Semicolon
: colon
? Question
mark
‘ Apostrophe
“ Quotation
mark
! Exclamatio
n mark
~ Tilde
/ Slash
 Backslash
| Vertical
bar
_ Underscor
e
$ Dollar sign
% Percent
sign
Contd..
Special
characters
Description
& Ampersand
^ Caret
* Asterisk
- Minus sign
+ Plus sign
< Less than
sign/opening
angle bracket
> greater than
sign/closing
angle bracket
) Right
parenthesis
( Left
parenthesis
[ Left bracket
] Right
bracket
{ Left brace
} Right brace
# Number sign
White spaces
• Blank space
• Horizontal tab
• Carriage return
• Newline
• Form feed
Trigraph characters
• It is used to enter certain characters that are
not available on some keyboard.
• Each trigraph sequence consists of 3
characters(two question marks followed by
another character).
• Eg:
??= #number sign
Tokens
• It is the smallest individual units
Tokens
Keywords Identifiers Constants Strings Special
symbols
Operators
Keywords
• All keywords have fixed meanings and these
meanings cannot be changed.
• They serve as a basic building blocks for
program statements.
• They should be written in lower case.
C keywords
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
Identifiers
• It refers to the name of variables,functions
and arrays.
• These are user defined names and consist of a
sequence of letters and digits,with a letter as
first character.
Rules for identifier
1. First character must be an alphabet(or
underscore).
2. Must consist of only letters, digits or
underscore.
3. Only first 31 characters are significant.
4. Cannot use a keyword
5. Must not contain white space
6. It is case sensitive
Difference between keywords and
identifiers
Keywords Identifiers
All keywords have fixed meanings and
these meanings cannot be changed.
It refers to the name of
variables,functions and arrays.
It is predefined. These are user defined names
They should be written in lower case. Both lower and upper case letters are
permitted.
It consist of letters, digits or underscore. It consist of only letters
Example: int Example: int a;
Constants
• Constants are fixed values that do not change
during the execution of program.
Backslash character constants
• C supports some backslash character
constants that are used in output functions.
• These character combinations are known as
escape sequence.
Backslash character constants
Constant Meaning
a Audible alert
b backspace
f’ Form feed
n newline
r Carriage return
’ Single quote
” Double quote
? Question mark
 Backslash
0 Null
Example:- printf(“Hai”);
printf(“n Doakians”);
Write a program to display some
statements in the following way using
backslash character constants.
• The following output should be displayed:
Hi
1 2
hello
welcome
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
Contd..
clrscr();
printf(“Hi n”);
Printf(“1 t 2 n”);
Printf(“Hello”);
Printf(“n welcome);
getch();
}
Output:-
Hi
1 2
hello
welcome
Try and look some difference
• Try these statements
printf(“ hai /n”);
printf(“welcome”);
printf(“n hai”);
• output:-
hai/nwelcome
hai
• New line will not be created as the back
slash() is not used. So, it would print as it is
just as a part of the message. The compiler
does not generates any errors for wrong
usage of slash.
Exercise
• Write a c program to display the numbers in a
matrix form
Data Type
Data type
Primary(or
fundamental)
data types
User defined
data type
Derived
data types
Primary data types
• Data type specifies the type of the data that
the variable hold.
Data type Description
int Whole numbers
float Decimal numbers
char Characters
User defined and derived data types will be discussed later.
Variables
• A variable is a data name that may be used to
store data value.
• A variable may take different values at
different times during execution.
• A variable name can be chosen by the
programmer in a meaningful way so as to
reflect its function or nature in the program.
Rules of variables
Variable names may consist of letters, digits and
underscore(_) characters. The following are rules:
1.They must begin with a letter. Some systems
permit underscore as the first character.
2.Only first 31 characters are recognized. However,
length should not normally more than 8
characters are treated significant by many
compilers.
3.Uppercase and lower case are significant. That is,
the variable Total is not the same as total or
TOTAL
Rules of variables
4. It should not be a keyword.
5. White space is not allowed.
In short,
1. First character must be an alphabet(or
underscore).
2. Must consist of only letters, digits or
underscore.
3. Only first 31 characters are significant.
4. Cannot use a keyword
5. Must not contain white space
6. It is case sensitive
• Some valid variable names
average
height
ph_value
x1
sum1
john
delhi
• Some invalid variable names
123
%
(area)
25th
Variable declaration
• The declaration of variables are done before
they are used in the program.
• Declaration does two things:
1.It tells the compiler what the variable name is.
2.It specifies what type of data the variable will
hold.
Syntax:
data-type variable1,variable2,variable n;
• Example for variable declaration:-
int a,b,c;
float a1;
char d;
Assigning values to variables
Assignment
Statement
(ie.,=)
Reading
data from
keyboard
(i.e., scanf)
Assignment statement
• Values are assigned to the variables using the assignment operators
• Syntax
variable_name=constant;
(or)
data-type variable_name=constant;
Example:
int a;
a=10;
• This statement assigns the number 10 to the variable a.
Examples:
int p,q,r;
int b=20;
p=q=r=100.45;
scanf
• It is used to gives values to variables through keyboard
• In simple term, it is used to get the values from the
user at the run time.
• Syntax
scanf(“control
string”,&variable1,&variable2….);
• The ampersand symbol & before each variable is an
operator that specifies the variable name’s address .
• NOTE: We must always use the ampersand symbol
&(Except for character data type),otherwise
unexpected results may occur.
Control string or format string
• It contains the format of the data being
received.
Control string Data type/data given by user
%d int(Integer)
%ld long int(Long integer)
%f float(Float)
%c char(Character)
%e double(Double)
%s char(String)
• Example for scanf:
int a;
float b;
char c;
printf(“enter 2 numbers and a
character”);
scanf(“%d%f%c”,&a,&b,c);
printf(“the 2 numbers are %d %f”,a,b);
printf(“the character is %c”c);
Write a c program to perform simple
interest
#include<stdio.h>
#include<conio.h>
void main()
{
int p,n,r,si;
clrscr();
printf(“enter the principal,number of years and rate
of interest”);
scanf(“%d%d%d”&p,&n,&r);
si=p*n*r/100;
printf(“nthe simple interest is %d”,si);
getch();
}
Output:
enter the principal,number of years and rate of interest
10000
1
10
the simple interest is1000
Exercise
• Write a c program to perform arithematic
operation(like +,-,*,/)
• Write a c program to convert centigrade to
Fahrenheit
• Write a c program to convert Fahrenheit to
centigrade
• Write a c program to find average of numbers
• Write a c program to find simple interest using
assignment statements
General debugging techniques
• Errors would be specified along with the line numbers
• Some of the common errors:
1. Error:
Unable to open header files
(or)
Fatal Error
Solution:
First select option menu and then
select directories and then delete the path given in
output directories.
2. Error:
Multiple choice questions
1. Almost every c program begins with the
statement
a) main()
b) printf()
c) #include<stdio.h>
d) scanf()
e) None of the above
2. The function scanf() reads
a) A single character
b) Characters and strings
c) Any possible number
d) Any possible variable type
e) None of the above
3. Which of the following is an invalid variable
name?
a) first_tag
b) group one
c) int_type
d) value
4. Which of the following is a valid variable
name?
a) price$
b) char
c) j5*1
d) %$
5. # directives must be present
a) Before the main() function
b) After the main() function
c) At the end of the program
d) Anywhere in the function body
e) None of the above
6. Which of the following are valid characters
constants?
a) ‘n’
b) ‘’
c) ‘0’
d) All of the above
e) None of the above
7. Which of the following scanf statements is
correct
a) scanf(“%f”,float-var-name);
b) scanf(“%d”,&number);
c) scanf(“%d &sum”);
d) scanf(“%d”,&int-var-name);
e) None of the above
8. The preprocessor directive always starts with
the symbol
a) %
b) &
c) #
d) “”
e) None of the above
9. The conversion specification for the string
type of data is
a) %d
b) %s
c) %f
d) %c
10.The statement
int i=0123;
a) Assigns the number 123 to i
b) Assigns the number 0123 to i
c) Assigns the number i to 0123
d) Assigns the number i to 123
e) None of the above
11.Which of the following pairs of identifier(s)
are(is) considered to be identical?
a) name,names
b) Smith,johnsmith
c) Identifier_1,identifier_2
d) Char1,char_1
e) None of the above
12.A C program contains the following
statements;
#include<stdio.h>
int I,j,k;
Assume all variables represent decimal integers,
what will be the printf function for the group
of variables:
i,j and k;
a) printf(“%d%d%d”,i,j,k);
b) printf(“%d%d”,(i+j),(i-k));
c) printf(“%f%d”,sqrt(i+j),abs(i-k));
d) printf(“%5d%5d%5d”,i,j,k);
e) None of the above
13.The comments in a program in the c
language can extend over
a) Only one line
b) Several lines
c) Two adjacent lines
d) All of the above
e) None of the above
14.The comments in a c language program are
placed between
a) * and*
b) */and*
c) /*and*/
d) #and #
e) None of the above
Multiple choice questions -Answers
Fill up
1. scanf() uses Of variables rather
than variable names.
2. statement is used in ‘c’ to gives
values to variables.
3. Escape sequences start with a
4. ‘c’ is a level language.
5. The C language was developed by
Fill up answers
1. Address
2. Assignment
3. Back slash
4. High
5. Dennis ritchie

More Related Content

What's hot

Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniquesvalarpink
 
Introduction of c_language
Introduction of c_languageIntroduction of c_language
Introduction of c_languageSINGH PROJECTS
 
Unit 4 Foc
Unit 4 FocUnit 4 Foc
Unit 4 FocJAYA
 
Basic C Programming language
Basic C Programming languageBasic C Programming language
Basic C Programming languageAbhishek Soni
 
INTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGINTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGAbhishek Dwivedi
 
Chapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingChapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingEric Chou
 
Basic of the C language
Basic of the C languageBasic of the C language
Basic of the C languageSachin Verma
 
Chapter1 c programming data types, variables and constants
Chapter1 c programming   data types, variables and constantsChapter1 c programming   data types, variables and constants
Chapter1 c programming data types, variables and constantsvinay arora
 
1. introduction to computer
1. introduction to computer1. introduction to computer
1. introduction to computerShankar Gangaju
 
What is identifier c programming
What is identifier c programmingWhat is identifier c programming
What is identifier c programmingRumman Ansari
 

What's hot (20)

C tokens
C tokensC tokens
C tokens
 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniques
 
Introduction of c_language
Introduction of c_languageIntroduction of c_language
Introduction of c_language
 
Unit 4 Foc
Unit 4 FocUnit 4 Foc
Unit 4 Foc
 
Basic C Programming language
Basic C Programming languageBasic C Programming language
Basic C Programming language
 
CProgrammingTutorial
CProgrammingTutorialCProgrammingTutorial
CProgrammingTutorial
 
INTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGINTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMING
 
Chapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingChapter 2: Elementary Programming
Chapter 2: Elementary Programming
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
 
Unit 2. Elements of C
Unit 2. Elements of CUnit 2. Elements of C
Unit 2. Elements of C
 
C
CC
C
 
C fundamental
C fundamentalC fundamental
C fundamental
 
Basic of the C language
Basic of the C languageBasic of the C language
Basic of the C language
 
Chapter1 c programming data types, variables and constants
Chapter1 c programming   data types, variables and constantsChapter1 c programming   data types, variables and constants
Chapter1 c programming data types, variables and constants
 
Basics of c
Basics of cBasics of c
Basics of c
 
1. introduction to computer
1. introduction to computer1. introduction to computer
1. introduction to computer
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
 
What is identifier c programming
What is identifier c programmingWhat is identifier c programming
What is identifier c programming
 
Cnotes
CnotesCnotes
Cnotes
 
Structure
StructureStructure
Structure
 

Viewers also liked

Viewers also liked (10)

Pełnotekstowe bazy wiedzy humanistycznej
Pełnotekstowe bazy wiedzy humanistycznejPełnotekstowe bazy wiedzy humanistycznej
Pełnotekstowe bazy wiedzy humanistycznej
 
Instagr.am ipad
Instagr.am ipadInstagr.am ipad
Instagr.am ipad
 
Bruxelles
BruxellesBruxelles
Bruxelles
 
Music based programming evaulation
Music based programming evaulationMusic based programming evaulation
Music based programming evaulation
 
Increase your followers on keek free
Increase your followers on keek freeIncrease your followers on keek free
Increase your followers on keek free
 
Refki & Nanda
Refki & NandaRefki & Nanda
Refki & Nanda
 
Yayat & Fika
Yayat & FikaYayat & Fika
Yayat & Fika
 
пасха близко
пасха близкопасха близко
пасха близко
 
66245374 leadership
66245374 leadership66245374 leadership
66245374 leadership
 
Top 10 Themed Retail of 2010
Top 10 Themed Retail of 2010Top 10 Themed Retail of 2010
Top 10 Themed Retail of 2010
 

Similar to Chap 1 and 2

Similar to Chap 1 and 2 (20)

C PADHLO FRANDS.pdf
C PADHLO FRANDS.pdfC PADHLO FRANDS.pdf
C PADHLO FRANDS.pdf
 
Introduction to C
Introduction to CIntroduction to C
Introduction to C
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
 
c programming session 1.pptx
c programming session 1.pptxc programming session 1.pptx
c programming session 1.pptx
 
C PROGRAMMING LANGUAGE.pptx
 C PROGRAMMING LANGUAGE.pptx C PROGRAMMING LANGUAGE.pptx
C PROGRAMMING LANGUAGE.pptx
 
Aniket tore
Aniket toreAniket tore
Aniket tore
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
SPC Unit 2
SPC Unit 2SPC Unit 2
SPC Unit 2
 
All C ppt.ppt
All C ppt.pptAll C ppt.ppt
All C ppt.ppt
 
unit 1 cpds.pptx
unit 1 cpds.pptxunit 1 cpds.pptx
unit 1 cpds.pptx
 
unit2.pptx
unit2.pptxunit2.pptx
unit2.pptx
 
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
 
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
 
C programming basic pdf.ppt
C programming basic pdf.pptC programming basic pdf.ppt
C programming basic pdf.ppt
 

Chap 1 and 2

  • 3. About C • C was found by Dennis Ritchie in the year 1970. • C is a structured programming language.This means that is a proper collection of modules would make a complete program.
  • 6. To Add Two Numbers Get B Get A C=a+b Addition
  • 7. About C… • C is a high level language. • There are only 32 keywords in ANSI C. • C is highly portable. This means that C programs written for one computer can run on another with little or no modification.
  • 8. Complete Structure of c program Document Section Link Section Global Declaration Section Main()Function { } Sub Program Section Declaration Part Execution Part Function 1 Function 2 Function n
  • 9. Simple Structure of C header Files void main() { variable declaration; program statements; }
  • 10. Header Files • It is called as preprocessor(predefined or in built or default) • It is one of the important function or one of the main feature of ’c’. • Header files are included in hash(#). • Syntax: #<directive name> <files (or) variables> • Example #include<stdio.h> Predefined header Files #include<conio.h>
  • 11. • Stdio.h-Standard Input Output.header file • The stdio.h header file should be used in a program when printf and scanf statements are used. • conio.h-console Input Output.header file • The conio.h header file should be used for storing purpose and retriving it back.It is used when clrscr() and getch() functions are used.
  • 12. void main() function • void-returns nothing • It is a special function in ‘c’ which informs the computer about the starting position of the program.It is a combination of variable declaration and program statements. • Every program should have one main() function void main() { } Start of main() function End of the main() function
  • 13. Program Statements • Print function • Scan function(We shall see this in the next section)
  • 14. Print function • It is used to print the expected output in the user screen. • Syntax: printf(“message (or) string”); printf(“control string”,variable 1,variable2,variable n); • Now,We shall deal about the 1st syntax • Example printf(“hai doakians”); • Every Statement in C should end with SEMICOLON(;). • There should be no space between ‘print’ and ‘f’.It is a single word printf.
  • 15. Write a program to print a message #include<stdio.h> #include<conio.h> void main() { clrscr(); printf(“Hai doakians”); getch(); } Output:- Hai doakians
  • 16. • clrscr();-used to clear the output screen. • getch();-used to run the program by getting any character as input.
  • 17. Comment • Comment is a note that can be put into the source code. • It is ignored by the compiler. • Single line comment-Starts with // • Example: //Print a message #include<stdio.h> #include<conio.h> void main() { clrscr(); printf(“Hai doakians”); getch(); }
  • 18. • The commented line would be ignored during execution. • Multi line comment-starts with/*…….*/ • Example: #include<stdio.h> #include<conio.h> void main() { clrscr(); /*printf(“welcome”); printf(“good morning”);*/ printf(“Hai doakians”); getch(); } Output:
  • 19. How to run a program? • The blue screen appears. Step 1:- -Select File -Then select new -Then select file again and click save -Then save it with a file name suffixed with the extension .c -eg:-printhai.c
  • 20. Step 2:- Then type the coding line by line Step 3:- Again save the program with the keyboard shortcut f2 Step 4:- Compile the program with the keyboard shortcut alt+f9. Step 5:- Correct the errors if any-then repeat step2 to step4.
  • 21. Step 6:- Run the program with the keyboard shortcut ctrl+f9
  • 22. How to open and access the saved file? Step 1:- Select File and then Open Step 2:- Then type your saved file name with the extension.c
  • 23. Important points to be noted…. • Every program should have a header file • Every statement in the program should end with the semicolon(;) except header files, main()function or any other functions. • A program can also be run without getch() function by using the shortcut alt+f5 instead of ctrl+f9 • C is case sensitive i.e., lower case(small letters) or upper case(capital letters) can be used. In order to avoid errors its better to use lower case(small letters). • There should be no space between ‘print’ and ‘f’.It is a single word printf.
  • 24. Exercise • Write a c program to display a set of statements related to your college.
  • 26. Character constants • It is a combination of letters(A to Z ,a to z), digits, special characters and white spaces. • Digits may be of 3 types 1.Octal (0 to 7)(base=8) 2.Decimal(0 to 9)(base=10) 3.Hexadecimal (0 to 9,A,B,C,D,E,F)(base=16)
  • 27. Special characters Special characters Description , Comma . Period ; Semicolon : colon ? Question mark ‘ Apostrophe “ Quotation mark ! Exclamatio n mark ~ Tilde / Slash Backslash | Vertical bar _ Underscor e $ Dollar sign % Percent sign
  • 28. Contd.. Special characters Description & Ampersand ^ Caret * Asterisk - Minus sign + Plus sign < Less than sign/opening angle bracket > greater than sign/closing angle bracket ) Right parenthesis ( Left parenthesis [ Left bracket ] Right bracket { Left brace } Right brace # Number sign
  • 29. White spaces • Blank space • Horizontal tab • Carriage return • Newline • Form feed
  • 30. Trigraph characters • It is used to enter certain characters that are not available on some keyboard. • Each trigraph sequence consists of 3 characters(two question marks followed by another character). • Eg: ??= #number sign
  • 31. Tokens • It is the smallest individual units Tokens Keywords Identifiers Constants Strings Special symbols Operators
  • 32. Keywords • All keywords have fixed meanings and these meanings cannot be changed. • They serve as a basic building blocks for program statements. • They should be written in lower case.
  • 33. C keywords 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
  • 34. Identifiers • It refers to the name of variables,functions and arrays. • These are user defined names and consist of a sequence of letters and digits,with a letter as first character.
  • 35. Rules for identifier 1. First character must be an alphabet(or underscore). 2. Must consist of only letters, digits or underscore. 3. Only first 31 characters are significant. 4. Cannot use a keyword 5. Must not contain white space 6. It is case sensitive
  • 36. Difference between keywords and identifiers Keywords Identifiers All keywords have fixed meanings and these meanings cannot be changed. It refers to the name of variables,functions and arrays. It is predefined. These are user defined names They should be written in lower case. Both lower and upper case letters are permitted. It consist of letters, digits or underscore. It consist of only letters Example: int Example: int a;
  • 37. Constants • Constants are fixed values that do not change during the execution of program.
  • 38. Backslash character constants • C supports some backslash character constants that are used in output functions. • These character combinations are known as escape sequence.
  • 39. Backslash character constants Constant Meaning a Audible alert b backspace f’ Form feed n newline r Carriage return ’ Single quote ” Double quote ? Question mark Backslash 0 Null Example:- printf(“Hai”); printf(“n Doakians”);
  • 40. Write a program to display some statements in the following way using backslash character constants. • The following output should be displayed: Hi 1 2 hello welcome Program: #include<stdio.h> #include<conio.h> void main() {
  • 41. Contd.. clrscr(); printf(“Hi n”); Printf(“1 t 2 n”); Printf(“Hello”); Printf(“n welcome); getch(); } Output:- Hi 1 2 hello welcome
  • 42. Try and look some difference • Try these statements printf(“ hai /n”); printf(“welcome”); printf(“n hai”);
  • 43. • output:- hai/nwelcome hai • New line will not be created as the back slash() is not used. So, it would print as it is just as a part of the message. The compiler does not generates any errors for wrong usage of slash.
  • 44. Exercise • Write a c program to display the numbers in a matrix form
  • 45. Data Type Data type Primary(or fundamental) data types User defined data type Derived data types
  • 46. Primary data types • Data type specifies the type of the data that the variable hold. Data type Description int Whole numbers float Decimal numbers char Characters User defined and derived data types will be discussed later.
  • 47. Variables • A variable is a data name that may be used to store data value. • A variable may take different values at different times during execution. • A variable name can be chosen by the programmer in a meaningful way so as to reflect its function or nature in the program.
  • 48. Rules of variables Variable names may consist of letters, digits and underscore(_) characters. The following are rules: 1.They must begin with a letter. Some systems permit underscore as the first character. 2.Only first 31 characters are recognized. However, length should not normally more than 8 characters are treated significant by many compilers. 3.Uppercase and lower case are significant. That is, the variable Total is not the same as total or TOTAL
  • 49. Rules of variables 4. It should not be a keyword. 5. White space is not allowed.
  • 50. In short, 1. First character must be an alphabet(or underscore). 2. Must consist of only letters, digits or underscore. 3. Only first 31 characters are significant. 4. Cannot use a keyword 5. Must not contain white space 6. It is case sensitive
  • 51. • Some valid variable names average height ph_value x1 sum1 john delhi
  • 52. • Some invalid variable names 123 % (area) 25th
  • 53. Variable declaration • The declaration of variables are done before they are used in the program. • Declaration does two things: 1.It tells the compiler what the variable name is. 2.It specifies what type of data the variable will hold. Syntax: data-type variable1,variable2,variable n;
  • 54. • Example for variable declaration:- int a,b,c; float a1; char d;
  • 55. Assigning values to variables Assignment Statement (ie.,=) Reading data from keyboard (i.e., scanf)
  • 56. Assignment statement • Values are assigned to the variables using the assignment operators • Syntax variable_name=constant; (or) data-type variable_name=constant; Example: int a; a=10; • This statement assigns the number 10 to the variable a. Examples: int p,q,r; int b=20; p=q=r=100.45;
  • 57. scanf • It is used to gives values to variables through keyboard • In simple term, it is used to get the values from the user at the run time. • Syntax scanf(“control string”,&variable1,&variable2….); • The ampersand symbol & before each variable is an operator that specifies the variable name’s address . • NOTE: We must always use the ampersand symbol &(Except for character data type),otherwise unexpected results may occur.
  • 58. Control string or format string • It contains the format of the data being received. Control string Data type/data given by user %d int(Integer) %ld long int(Long integer) %f float(Float) %c char(Character) %e double(Double) %s char(String)
  • 59. • Example for scanf: int a; float b; char c; printf(“enter 2 numbers and a character”); scanf(“%d%f%c”,&a,&b,c); printf(“the 2 numbers are %d %f”,a,b); printf(“the character is %c”c);
  • 60. Write a c program to perform simple interest #include<stdio.h> #include<conio.h> void main() { int p,n,r,si; clrscr(); printf(“enter the principal,number of years and rate of interest”); scanf(“%d%d%d”&p,&n,&r);
  • 61. si=p*n*r/100; printf(“nthe simple interest is %d”,si); getch(); } Output: enter the principal,number of years and rate of interest 10000 1 10 the simple interest is1000
  • 62. Exercise • Write a c program to perform arithematic operation(like +,-,*,/) • Write a c program to convert centigrade to Fahrenheit • Write a c program to convert Fahrenheit to centigrade • Write a c program to find average of numbers • Write a c program to find simple interest using assignment statements
  • 63. General debugging techniques • Errors would be specified along with the line numbers • Some of the common errors: 1. Error: Unable to open header files (or) Fatal Error Solution: First select option menu and then select directories and then delete the path given in output directories.
  • 65. Multiple choice questions 1. Almost every c program begins with the statement a) main() b) printf() c) #include<stdio.h> d) scanf() e) None of the above
  • 66. 2. The function scanf() reads a) A single character b) Characters and strings c) Any possible number d) Any possible variable type e) None of the above
  • 67. 3. Which of the following is an invalid variable name? a) first_tag b) group one c) int_type d) value
  • 68. 4. Which of the following is a valid variable name? a) price$ b) char c) j5*1 d) %$
  • 69. 5. # directives must be present a) Before the main() function b) After the main() function c) At the end of the program d) Anywhere in the function body e) None of the above
  • 70. 6. Which of the following are valid characters constants? a) ‘n’ b) ‘’ c) ‘0’ d) All of the above e) None of the above
  • 71. 7. Which of the following scanf statements is correct a) scanf(“%f”,float-var-name); b) scanf(“%d”,&number); c) scanf(“%d &sum”); d) scanf(“%d”,&int-var-name); e) None of the above
  • 72. 8. The preprocessor directive always starts with the symbol a) % b) & c) # d) “” e) None of the above
  • 73. 9. The conversion specification for the string type of data is a) %d b) %s c) %f d) %c
  • 74. 10.The statement int i=0123; a) Assigns the number 123 to i b) Assigns the number 0123 to i c) Assigns the number i to 0123 d) Assigns the number i to 123 e) None of the above
  • 75. 11.Which of the following pairs of identifier(s) are(is) considered to be identical? a) name,names b) Smith,johnsmith c) Identifier_1,identifier_2 d) Char1,char_1 e) None of the above
  • 76. 12.A C program contains the following statements; #include<stdio.h> int I,j,k; Assume all variables represent decimal integers, what will be the printf function for the group of variables: i,j and k;
  • 77. a) printf(“%d%d%d”,i,j,k); b) printf(“%d%d”,(i+j),(i-k)); c) printf(“%f%d”,sqrt(i+j),abs(i-k)); d) printf(“%5d%5d%5d”,i,j,k); e) None of the above
  • 78. 13.The comments in a program in the c language can extend over a) Only one line b) Several lines c) Two adjacent lines d) All of the above e) None of the above
  • 79. 14.The comments in a c language program are placed between a) * and* b) */and* c) /*and*/ d) #and # e) None of the above
  • 81. Fill up 1. scanf() uses Of variables rather than variable names. 2. statement is used in ‘c’ to gives values to variables. 3. Escape sequences start with a 4. ‘c’ is a level language. 5. The C language was developed by
  • 82. Fill up answers 1. Address 2. Assignment 3. Back slash 4. High 5. Dennis ritchie