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.
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
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.
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;
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()
{
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.
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
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
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;
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