4. Importance of C
• C is the base language of any other programming language.
• To be a Good Programmer one must know fundamentals of C
programming language.
• It is a robust language.
• Whose rich set of built in functions and operators can be used to write any complex
program.
• Programs written in ‘C’ are efficient and fast.
• Because of variety of data types and powerful operators
• Highly Portable. (related to OS)
– C program written for one computer can be run on another with little or no
modification
• Well suited for structured programming
• Ability to extend itself
• Well suited for writing system software
– UNIX, MS-DOS operating system,
5. Program & Programming Language
• Program:- A Set of instructions which carried out
by processor for some Specific input, generates
specific output.
• Programming language:- A specific manner of
writing a program with some Predefined rules,
symbols & their use as a part of language.
– i.e. Pascal, C, C++, VC++, JAVA, VB.
6. Basic structure of ‘C’
1) Documentation Section :-
It has set of comment lines(name of program, author
details).
What is Comment line??
To guide a programmer. To write a note for
function,operation,logic in between a program.
Non-executable statement.
Can’t be nested.
e.g:- /* Hello /* abc */ Hi */
ERROR.
7. 2)Link Section :-
It provides instructions to the compiler to
link
function from the system library.
# include Directive:-
To access the functions which are stored in
the
library, it is necessary to tell the compiler ,
about the file to be accessed.
Syntax:-
#include<stdio.h>
stdio.h is header file.
8. 3) Definition Section
It defines all symbolic constants.
#define instruction defines value to a symbolic
constant.
#define:-
It is a preprocessor compiler directive, not a
statement.
Therefore it should not end with a semicolon.
Generally written in uppercase.
9. 4)Global Declaration Section
Some variables that are used in more than
one function, such variables (global variables)
declared in the global declaration section.
It also declares all the user-defined function.
10. 5) Main() function section:
Every ‘C’ program must have one main() function
section.
It contains two parts
1) Declaration part:
It declares all variables used in the executable part.
2) Executable part:
It has atleast one statement.
Ex: main( ) = main (void)
No arguments Explicitly indicate no arguments
11. 5) Basic structure of C Programs:
Documentation section
Link Section
Definition Section
Global Declaration Section
main() function section
{
Declaration part
Executable part
}
Subprogram section
Function1
Function2
…
user defined function
12. How to run a program?
• There are two ways to run programs written
in a high-level language.
– The most common is to compile the program
– The other method is to pass the program through
an interpreter.
13. Why Compiler
• As machine (a processor) can operate on binary
code instruction only…..
• If we use higher level language then …
– For execution of the program we must convert it to
lower level / machine level code.
• Compiler is a program that translates source code
into object code.
• The compiler derives its name from the way it
works, looking at the entire piece of source code
and collecting and reorganizing the instructions.
14. Interpreter
• Analyzes and executes each line of source code
without looking at the entire program.
Advantage of interpreter:
• It can execute a program immediately.
– Compilers require some time before an executable
program emerges.
• But,
– However, programs produced by compilers run much
faster than the same programs executed by an
interpreter.
15. C Compiler
- checks for syntax errors if any
- on success coverts ‘C source code
into object code form –
which is nearer to machine…
16. Linker:
A linker is a program that combines object
modules to form an executable program.
17.
18. Types of languages
(I)Lower level languages:-
Languages which are very near to
machine…. I.e. machine language,
Assembly language.
(II) Higher level languages:-
Languages which are very near to
programmer rather than to machine….
I.e. C++,Visual C++,Visual basic,Java.
19.
20. BlockDiagram:
Represents only
-> what should be the input?
-> what will be the output?
- One need not to worry about how
the result generated….
- or what will be the logic for the
program?
22. Algorithm
Specifies steps of doing the things
Irrespective of any programming language
Example: Addition of two numbers
Step 1: [Read two numbers]
Read(no1)
Read(no2)
Step 2:[add two numbers into sum]
summation = no1 + no2
Step 3:[Display the result]
write (summation)
23. Flowchart
Represents the flow of program
Symbols:-
Start-Begin/End
Input/Output Rectangle
Process box/operation box
Decision Box
Connector
24. Flowchart-example
Example: To add two numbers and display the
result.
Start
SUMMATION = NO1 + NO2
End
Read No1 and No2 from user
Write Summation on monitor
25. Files ,directory, file extension
Files:- storage of logically related data
Directory:- Placeholder which can store files
and subdirectories within them.
File Extension:- special postfix attached to
each file which indicates type
of the file.
26. Sample C program:-
/* print hello*/
#include<stdio.h> /* link section*/
void main() /* execution of program*/
{
print(“hello world”); /*executable
statement*/
}
28. C AS A LANGUAGE
• Interface between computer & human
being.
ALPHABETS WORDS SENTENCES PARAGRAPH
Alphabets, digits
special symbol
Constants,
Variables,
Keywords
Instructions Program
29. C CHARACTER SET
ALPHABETS A,B…………Z
a,b………….z
DIGITS 0,1,2,3,4,5,6,7,8,9
SPECIAL SYMBOLS ? ! @ # $ % & * ()+ - =
_ | : ;etc..
WHITE SPACES Blank space,Tab,New
line
30. TRIGRAPH
CHARACTERS
Many non-English keyboards do not support all
the characters.
For that C introduces the concept of “trigraph”
sequences to provide a way to enter certain
characters that are not available on some
keyboards.
Each trigraph sequence consists of three
characters(two questionmark followed by another
character). Ex. keyword not support square
brackets,
Ex. Trigraph Sequence Translation
??=, ??/ #,
??( , ??) [, ]
32. KEYWORDS AND IDENTIFIERS
All keyword have fixed meanings and meanings cannot be changed.
Ex. break, else, do, while….
Identifiers refer to the names of variable, functions and arrays.
These are user defined names and consist of sequence of letters
and digits
Rules:-
First char must be alphabet.
Contain letters, digits, underscore not white space and keyword.
33. DIFFERENCE BETWEEN
CONSTANTS & VARIABLES
• CONSTANT is a quantity that doesn’t change.
• VARIABLE is a quantity that value can be changed respectively.
For example :
3X + Y = 20
Since 3 and 20 can not change, they are called constants, where as
the quantities X & Y can vary or change hence are called
variables.
34. TYPES OF C CONSTANTS
•
C Constants
Primary Constants Secondary Constants
Integer Constants
Real Constants
Character Constants
String constants
Array, Pointer ,
Structure , Union
Enum etc.
35. INTEGER CONSTANTS
- An integer constants refers to a sequence of digits
Three types
Decimal: Set of digits,0 through 9
123, -87, 0, +56 <- valid $1000, 15 750, (20,000) <-Invalid
Octal: Combination of 0 to 7 with leading zero. i.e. 075, 051, 0435 are
octal
Hexadecimal: Sequence of digits 0-9,A..F/a..f …..
36. REAL CONSTANTS(FLOATING POINT)
- to represent varying entity like prices. it contain fractional parts.
in decimal notation 34.32, -0.56, .4555 , 0.00054 etc.
In exponential notation(or scientific) 215.65 may be written as
2.1565e2 also valid
More 0.65e4 ,12e-2, 3.18E3.
37. SINGLE CHARACTER CONSTANTS
-a single character enclosed within a pair of single quote marks i.e
‘y’ , ‘4’, ‘;’ , ‘ ‘, etc.
- character constants have integer values known as ASCII (American
Standard Code for Information Interchange) values.
i.e. printf(“%d”,’a’); will print no. 97, the ascii value of the letter a.
While printf(“%c”,97); will print --- a
*error in book statement instead of ’97’ it should be only 97
38. STRING CONSTANTS
-sequence of characters enclosed in double quotes like
“hello!” , “hi 18”, “well done”, “a”
Remember that “x” and ‘x’ is different because ‘x’ has
equivalent integer value in ASCII.
String constants are normally used like displaying
menu/messages.
39. BACKSLASH CHARACTER CONSTANTS
- used in output functions…
- ‘n’ stands for new line character
- ‘b’ for back space
- ‘t’ for making horizontal tab
all above represents one character, although they consist of two
chars.
These char combination are known as “escape sequences”
40. VARIABLES
A specific place where specific data is stored
variable name : to access/use the data stored at the specific
place.
A variable is a data name that may be used to store a data value
i.e. you want to store two numbers 423 and 54.
No1 No2
423 54
41. RULES FOR DEFINING A VARIABLE
1. They must begin with a letter & no special symbols are used
without underscore.( _ )
i.e. total, mark1, ave_mark (Valid)
3mark, 345 (Invalid)
2. The length should not be normally more than 8 characters,
since only the first 8 characters are treated as significant by
the compiler.
i.e. average_weight & average_height
will be treated same.
42. RULES FOR DEFINING A VARIABLE
3. Uppercase and lowercase are significant.
i.e. the variable Total is not the same as total or TOTAL.
4. The variable name should not be a keyword.
5. White space is not allowed.
i.e. variable name ‘my total’ is invalid.
43. DATA TYPES
Classes of data types :
Primary data types
User defined data types
Derived data types
Basically C supports four Primary data types :-
integer (int) , character (char), floating point (float) , double
precision floating point (double).
44. PRIMARY DATA TYPES
TYPE SIZE RANGE
Char 1 byte -128 to 127
Int 2 byte -32,768 to 32,767
Float 4 byte 3.4e-38 to 3.4e+38
Double 8 byte 1.7e-308 to 1.7e+308
Long int 4 byte -232
to 232
Long double 10 byte +/- 3.4*104932
45. QUALIFIER
Signed : Indicates that the data can be positive or negative
Unsigned : Indicates that data to store is positive only…
Short : Data to store will require less memory
Long : Data to store will require more memory…
46. RANGE OF DATA TYPE
• –32768 to 32767. ( from -215
to 215)
• So, here first bit is used for sign. Means if it is 0 then
+ve no and 1 then –ve no. so, only other 15 bits are
used for magnitude. And so that range is –32768 to
32767.
1
SIGN BIT
47. TYPE SHORT FORM SYMBOL
character char %c
Signed integer int %d
Unsigned int Same %u
real float %f
double double %lf
Long double Same %Lf
Signed long int Same %ld
Unsigned long int Same %lu
48. DECLARATION OF VARIABLES
data_type v1,v2,….,vn;
Where v1,v2, … vn are variable names.
i.e. int maximum;
will have two bytes memory allocated and the name ‘maximum’
associated with it.
(by default the int is considered as signed, so range -32768 to
32767)
i.e. unsigned int age;
then 2 bytes allocated but the range will be 0 to 65535.
49. ASSIGNING VALUES TO VARIABLE
assignment operator
int num1,num2,sum;
sum = num1 + num2;
No1=no2=no3; also possible…
int no1=10; also possible …. Initialization at the time of declaration.
char choice;
choice = ‘y’;
or scanf(“%c”,&choice);
50. USER DEFINED TYPE DECLARATION
Type definition that allows programmers to define an
identifier/name that would represent an existing data type….
general form: typedef type identifier;
i.e. typedef int marks_inc
So, now onward when we require to store marks of c subject we will
create variable of type marks_inc like,
marks_inc m_stu1;
typedef just increase readability via creating meaningful data type
names, it can’t create a new datatype.
51. ENUMERATED DATA TYPE
Suppose u want to store week days like
Sunday, Monday,…,
So, u know the possible values in advance …. Can be done using
enumerated data type
enum identifier {value1,value2,..};
i.e enum day {Mon,Tue,Wed,Thur,Fri,Sat,Sun};
enum day week_st;
So, here week_st can have any possible value from the list
52. CONTINUING ENUM
Here by default
Enumeration constant Mon is assigned 0,
Tue assigned 1, Wed is assigned 2, and so on.
However automatic assignments can be overridden by …..
enum day {Mon=10,Tue,Wed,…..};
Now As Mon is having 10, Tue will have one more 11, Wed will have
12, and so on.
Q. int x; x = Wed – Mon; x = ?
53. VARIABLE’S STORAGE CLASS
Provides information about the location and visibility/scope of a
variable.
There are four storage class specifiers auto,register,static,extern.
auto variables
……. defined within a function
…. gets memory when the function is executing
……destroys when the function is over
…..local to that function so can’t be used in outside function
…. Gets garbage initialized..
54. …GLOABAL
the variable declare outside any function before main
known as global variable.
It can be used in all the functions after the declaration
of it.
Note ::
Global is External variable…
External and static variable are initialized to zero by
default… .while auto variable contains garbage .. Until
they are initialized.
55. DECLARING A IDENTIFIER AS
CONSTANT
Suppose u want to make some identifier whose value need not to be
changed during the execution …
const int class_size = 40;
So, you can use const qualifier.
This ensures that
x= class_size; valid
class_size = 10; invalid because as class_size is constant and
can’t change it’s value.
56. OVERFLOW OF DATA
- when the value of a variable is either too big or too small for the
data type to hold…
- ‘C compiler does not provide any warning or indication of integer
overflow.
- Programmer must take care for the type and range of input
possible
57. THE GENERAL FORMAT OF #DEFINE
A constant can be defined as follows:
#define symbolic-name value of constant
Valid examples are:
#define PI 3.14159
#define MAX 100
58. RULES APPLY TO #DEFINE
1.Symbolic name have the same form as variable names. But usually we
are using CAPITALS for symbolic names. But that is not a rule.
#define MAX 200
2.No blank space between the pound sign’#’ and the word define is
permitted.
# define MAX 200 <- Not permitted
3.’#’ must be the first character in the line.
4.A blank space is required between #define and symbolic name and
between the symbolic name and value.
59. CONTINUE…CONTINUE…
5.#define statements must not end with a semicolon.
#define PI 3.1415; <- Not valid
6.After definition, the symbolic name should not be assigned any
other value within the program using an assignment statement.
i.e. #define STRENGTH 100
main()
{
STRENGTH = 200;
}
Is illegal.
60. List of Number System Conversion Programs in C
1.C program to convert number from Decimal to Binary.
2.C program to convert number from Decimal to Octal.
3.C program to convert number from Decimal to Hexadecimal.
4.C program to convert number from Binary to Decimal.
5.C program to convert number from Octal to Decimal.
6.C program to convert number from Hexadecimal to Decimal.
7.Check EVEN or ODD program using switch
8.Calculator program with Basic operations using switch
9.Check VOWEL or CONSONANT program using switch
10.Print all PRIME numbers from 1 to N using C program
11.Print all Leap Years from 1 to N using C program
12.Print Square, Cube and Square Root using C program
13.Print number from 1 to N with EVEN and ODD message using C
program
14.Sum of first N natural numbers using C program
Trigraph sequences allow C programs to be written using only the ISO (International Standards Organization) Invariant Code Set. Trigraphs are sequences of three characters (introduced by two consecutive question marks) that the compiler replaces with their corresponding punctuation characters.