1.2 Introduction Part-2 Unit-1
1.2 Introduction Part-2 Unit-1
1.2 Introduction Part-2 Unit-1
C PROGRAMMING
(21CS107)
IMPORTANCE OF C
• C programming language is most popular programming language.
• C was created in 1972 by Dennis Ritchie at the Bell Labs in USA as a
part of UNIX operating system.
• C was also used to develop some parts of this operating system.
FEATURES OF C
• Programs Written in C are efficient and fast.
• C is highly portable this means that programs once written can be run on
another machines.
• Another important feature of C program is its ability to extend itself.
• A C program is basically a collection of functions that are supported by C
library. We can also create our own function and add it to C library.
• C language is the most widely used language.
STRUCTURE OF C PROGRAM
• A C program involves the following sections:
• Documentations (Documentation Section)
• Pre-processor Statements (Link Section)
• Global Declarations (Definition Section)
• The main() function
• Local Declarations
• Program Statements & Expressions
• User Defined Functions
DOCUMENTATION SECTION / COMMENTS –
• They are two types of comments:
• Single – line comment.
• Multi – line comment.
}
Output:
Hello World!
COMPILE AND EXECUTE A C PROGRAM
• Open a text editor and add the above mentioned code.
• Save the file as hello.c.
• Open command prompt and go to the directory where we have saved the file.
• Type cc file_name.c and press enter to compile our code.
• If there are no errors in our code, the command prompt will take you to the next line and
would generate a.out executable file.
• Now type ./a.out to execute our program.
• Finally we will get output.
• $cc hello.c
• $./a.out
• Hello world!
C TOKENS
• C CHARACTER SET
• IDENTIFIERS
• KEYWORDS,
• TYPE QUALIFIERS
• TYPE MODIFIERS
• VARIABLES
• CONSTANTS
• PUNCTUATIONS AND
• OPERATORS
C CHARACTER SET
• A character set defines the valid characters that can be
used in a source program (or) interpreted when a
program is running.
SOURCE CHARACTER SET
This character set is used to construct
statements in a source program.
Alphabets A to Z and a to z
Digits 0 to 9
Type Modifiers -
A type modifier alter the meaning of the base data type to
yield a new type. There are four types of modifiers in C they are
• signed
• unsigned
• short
• long
VARIABLES IN C
✓ Variable is defined as the reserved memory space which stores a
value of a definite datatype.
✓ The value of the Variable is not constant, instead, it allows changes.
✓ There are mainly five types of variables supported in C.
• Local variable
• Global variable
• Static variable
• Automatic variable
• External variable 99
Local Variables
A variable that is declared at the inside a code block or a function and has the scope
confined to that particular block of code or function is called to be a local variable.
void vignan()
{
int Local_variable=10;
}
Global Variables
A variable that is declared at the outside a code block or a function and has the scope
across the entire program and allows any function to change it’s value is known as
Global Variable.
int Global_variable=10;
void vignan()
{
int Local_variable=20; 100
}
Static Variables
✓ Any variable that is declared using the keyword static is known as a Static Variable.
✓ Static variables retain the declared value throughout the entire execution of the
program and will not be changed between multiple function calls.
#include<stdio.h> #include<stdio.h>
int fun() int fun()
{ {
static int count = 0; int count = 0;
count++; 12 count++; 11
return count; return count;
} }
int main() int main()
{ {
printf("%d ", fun()); printf("%d ", fun());
printf("%d ", fun()); printf("%d ", fun()); 101
return 0; return 0;
} }
Auto Variables
Automatic Variables can be declared by using the keyword auto.
void main()
{
int Local_variable=10; //(automatic default)
auto int auto=20; //(automatic variable)
};
External Variables
External Variables are declared by using the extern keyword. External variable can be
shared among multiple C source files.
extern external=10;
102
CONSTANTS
• A Constant is an entity whose value remains the same throughout the
execution of the program. These fixed values are also called as Literals.
They are classified into three types:
1. Literal Constants.
2. Qualified Constants.
3. Symbolic Constants.
1. LITERAL CONSTANTS
They are just called as Literals denotes a fixed value, which may be an
integer, floating point number, character or a string.
• An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the
base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.
INTEGER LITERAL CONSTANT
• Following are other examples of various types of integer literals −
• 85 /* decimal */
• 0213 /* octal */
• 0x4b /* hexadecimal */
• 30 /* int */
• 30l /* long */
It can have one or at most two characters enclosed within single quotes e.g. ‘A’,
‘a’, ‘\n’ etc. These are classified into two types:
Non – Printable Character Literal Constant: These are represented with the
help of ‘Escape Sequences’. An escape sequence consists of backward slash (\)
followed by a character and both enclosed within a single quote. Ex: ‘\n’, ‘\t’..
LIST OF ESCAPE SEQUENCES
S.NO Escape Character Value Action on Output Device
Sequence
1 \’ Single quotation Prints ‘
mark
2 \” Double quotation Prints “
mark
3 \? Question Mark Prints ?
4 \\ Backslash(\) Prints \
5 \a Alert Generates beep sound
6 \b Back space Moves the cursor one position to the left of its current
position.
7 \f Form Feed Moves the cursor to the beginning of the next page
8 \n New line Moves the cursor to the beginning of the next line.
9 \r Carriage Return Moves the cursor to the beginning of the current line.
10 \t Horizontal Tab Moves the cursor to the next horizontal tab stop
STRING LITERAL CONSTANT
Constants are not able to modified, but they are able to access.
Symbolic Constants: These are created with the help of define pre-
processor directive.
Syntax
( ) [ ] { } * , : = ; ... #
These characters have special meanings in C. # occurs only in pre-
processor directives.
OPERATORS
An operator is a symbol that tells the compiler to perform specific
mathematical or logical functions. C language is rich in built-in operators
and provides the following types of operators −
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
OPERATORS
5. Conditional Operators
6. Special Operators
7. Bitwise operators
9. Unary operators