0% found this document useful (0 votes)
0 views12 pages

Programming in C _unit1.Docx

C Program

Uploaded by

vigneshsalian2k7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views12 pages

Programming in C _unit1.Docx

C Program

Uploaded by

vigneshsalian2k7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Programming in C

‘C’ is high level language and is the upgraded version of another language
(Basic Combined Program Language). C language was designed at Bell
laboratories in the early 1970’s by Dennis Ritchie. C being popular in the
modern computer world can be used in Mathematical Scientific,
Engineering and Commercial applications.

The most popular Operating system UNIX is written in C language. This


language also has the features of low level languages and hence called as
“System Programming Language”

Structure of C Program:

The sections of a C program are listed below:

Documentation section
Preprocessor section (link section)
Definition section
Global declaration
Main() function section
{
Declaration part
Executable part
}
Sub program section Use defined functions

Function1()

Function2()

1.​ Documentation section


2.​ Preprocessor section
3.​ Definition section
4.​ Global declaration
5.​ Main function
6.​ User defined functions
1.Document section:It includes the statement specified at the beginning of
a program, such as a program's name, date, description, and title. It is
represented as:

//name of a program
Or
/*
Overview of the code
.
*/

2.The preprocessor section contains all the header files used in a program.
It informs the system to link the header files to the system libraries. It
includes

#include<stdio.h>
#include<conio.h>

The #include statement includes the specific file as a part of a function at


the time of the compilation. Thus, the contents of the included file are
compiled along with the function being compiled.
The #include<stdio.h> consists of the contents of the standard input output
files, which contains the definition of stdin, stdout, and stderr.

3.The definition section comprises of different constants declared using


the define keyword. It is given by:

4. Global declaration
The global section comprises of all the global declarations in the program.
5.Main function
main() is the first function to be executed by the computer. It is necessary
for a code to include the main(). It is like any other function available in the
C library. Parenthesis () are used for passing parameters (if any) to a
function.
The main function is declared as: int main() or void main()
Main function is further categorized into local declarations,
statements, and expressions.
The variable that is declared inside a given function or block refers to as
local declarations.
main()
{
int i = 2;
i++;
} // here i is the local variable

Statements

The statements refers to if, else, while, do, for, etc. used in a program
within the main function.

Expressions

An expression is a type of formula where operands are linked with each


other by the use of operators. It is given by:

a - b;
a +b;

Return function is generally the last section of a code. But, it is not


necessary to include. It is used when we want to return a value. The return
function returns a value when the return type other than the void is
specified with the function.
For example,
return 0;

Features of C language:

• Simple, versatile, general purpose language


• It has rich set of Operators
• Program execution are fast and efficient
• Can easily manipulates with bits, bytes and addresses
• Varieties of data types are available
• Separate compilation of functions is possible and such functions can be
called by any C program
• Block- structured language
• Can be applied in System programming areas like operating systems,
compilers & Interpreters, Assembles, Text Editors, Print Spoolers, Network
Drivers, Modern Programs, Data Bases, Language Interpreters, Utilities etc.

The character set

The character set is the fundamental raw-material for any language. Like
natural languages, computer languages will also have well defined
character-set, which is useful to build the programs.
The C language consists of

i. Alphabets : A to Z, a to z
ii. Decimal digits : 0 to 9
iii. Special symbols: + - * / ^ % = & ! ( ) { } [ ] “ etc
iv) White spaces : This set of characters are also called as non-graphic
characters because these are invisible and cannot be printed or displayed
directly. These characters will have effect only when the program being
executed. These characters are represented by a back slash (\) followed by a
character.

\n End of a line Transfers the active


position of cursor
to the initial position of
next line

\ 0 (zero) End of string Null

\t Horizontal Tab Transfers the active


position of cursor
to the next Horizontal
Tab.
\v Vertical Tab Transfers the active
position of cursor
to the next Vertical Tab

\f Form feed Transfers the active


position of cursor
to the next logical page

\r Carriage return Transfers the active


position of cursor
to the initial position of
current line

Tokens in C

Tokens in C is the most important element to be used in creating a program


in C. We can define the token as the smallest individual element in C.
For `example, we cannot create a sentence without using words; similarly,
we cannot create a program in C without using tokens in C.

Classification of tokens in C

Tokens in C language can be divided into the following categories:


1.​ Keywords
2.​ Identifiers
3.​ Strings
4.​ Operators
5.​ Constant
6.​ Special Characters
1.Keywords

Keywords in C can be defined as the pre-defined or the reserved


words having its own importance, and each keyword has its own
functionality.
Since keywords are the pre-defined words used by the compiler, so they
cannot be used as the variable names. If the keywords are used as the
variable names, it means that we are assigning a different meaning to the
keyword, which is not allowed. C language supports 32 keywords given
below:

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

2. Identifiers

Identifiers in C are used for naming variables, functions, arrays, structures,


etc. Identifiers in C are the user-defined words. It can be composed of
uppercase letters, lowercase letters, underscore, or digits, but the starting
letter should be either an underscore or an alphabet. Identifiers cannot be
used as keywords. Rules for constructing identifiers in C are given below:
o​ The first character of an identifier should be either an alphabet or an
underscore, and then it can be followed by any of the character, digit, or
underscore.
o​ It should not begin with any numerical digit.
o​ In identifiers, both uppercase and lowercase letters are distinct. Therefore,
we can say that identifiers are case sensitive.
o​ Commas or blank spaces cannot be specified within an identifier.
o​ Keywords cannot be represented as an identifier.
o​ The length of the identifiers should not be more than 31 characters.
o​ Identifiers should be written in such a way that it is meaningful, short.

3. Strings

Strings in C are group of of characters. Strings in C are enclosed within


double quotes, while characters are enclosed within single quotess. The
size of a string is a number of characters that the string contains.
Strings in C are always represented as an array of characters having null
character '\0' at the end of the string. This null character denotes the end
of the string.

4. Operators
Operators in C is a special symbol used to perform the functions. The data
items on which the operators are applied are known as operands. Operators
are applied between the operands. + , -, *, / are the examples.

5. Constants
A constant is a value assigned to the variable which will remain the same
throughout the program, i.e., the constant value cannot be changed.
Constants are classified into
1.Numeric Constant
2. Character constant.
1. Numeric constants are – Integer constants and Real constants
Integer constant is a sequence of digits. Real constants are numbers
containing fractional parts.
​ 2. Character constant are – single character and string constants.
​ A single character constant contains a single character enclosed
within single quote marks. Like ‘x’, ‘5’.
A string constant is a sequence of characters enclosed in double
quotes. Eg: “hello”, “xyz”, “1989” etc

Constant Example

Integer constant 10, 11, 34, etc.

Floating-point constant 45.6, 67.8, 11.2, etc.


(Real)
Octal constant 011, 088, 022, etc.

Hexadecimal constant 0x1a, 0x4b, 0x6b, etc.

Character constant 'a', 'b', 'c', etc.

String constant "java", "c++", ".net", etc


6.Special symbols:
Some special characters are used in C, and they have a special meaning
which cannot be used for another purpose.
Square brackets [ ]
Simple brackets ( )
Curly braces { }
Comma (,)
Asterisk (*)
Period (.)

******************

Variables:

Variables are containers for storing data values, like numbers and
characters.
A variable is a data name that may be used to store a data value. A variable
may take different values at different times during execution.
A variable name may consist of letters, digits anthe underscore(_)
character.
o​ The first character of a variable should be either an alphabet or an
underscore, and then it can be followed by any of the character, digit, or
underscore.
o​ It should not begin with any numerical digit.
o​ both uppercase and lowercase letters are distinct. Therefore, we can say
that variable names are case sensitive.
o​ Commas or blank spaces cannot be specified
o​ Keywords cannot be used
o​ The length of the variable name should not be more than 31 characters.
o​ should be written in such a way that it is meaningful, short.

Data Types:

Each variable in C has an associated data type. It specifies the type of data
that the variable can store like integer, character, floating, double, etc. Each
data type requires different amounts of memory and has some specific
operations which can be performed over it.

The data types in C can be classified as follows:

Types Description

Primitive data types are the most basic data


1.​ Primitive Data
types that are used for representing simple
Types
values such as integers, float, characters, etc.

2.​ User Defined The user-defined data types are defined by


Data Types the user himself.

The data types that are derived from the


3.​ Derived Types primitive or built-in data types are referred to
as Derived Data Types.

Primary data types (Fundamental) are : Integer (int) , Character


(char), Floating point(float), double precision floating point (double)
and void.
Integer type is classified into- Signed integer and unsigned integer
Signed integer is divided into – int, short int, long int.
Unsigned – unsigned int, unsigned short int, unsigned long int
Character is divided into – char, signed char,unsigned char.
Floating point type – float, double, long double.

Char- range of values: -128 to 127


Int – range of values : -32768 to 32,767
Float – rangle of values : 3.4e-38 to 3.4e+e38
Double range of values: 1.7e-308 to 1.7e+308

Data Size Description


Type

int 2 or 4 bytes Stores whole numbers, without decimals

float 4 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing 6-7
decimal digits

double 8 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing

15 decimal digits

char 1 byte Stores a single character/letter/number, or ASCII values

Void - void type has no value. It is used to specify


the type of functions. When a function does not return a value to calling
function, the type of function is said to be void.

Declaration of variables:

A variable declaration (definition) tells the compiler where and how much
storage to create for the variable.
A variable declaration provides assurance to the compiler that there exists a
variable with the given type and name so that the compiler can proceed
with further compilation without requiring complete detail about the
variable. A variable definition has its meaning at the time of compilation
only, the compiler needs actual variable definition at the time of linking the
program.

A variable declaration specifies a data type and contains a list of one or


more variables of that type as follows.

int a;
int a,b;
char c;
float avg;

The general form or syntax to declare a variable is –

data type variablename1, variablename 2, ……variablename n;

example: int a;
​ int m1,m2,m3;

Variables can be initialized (assigned an initial value) in their declaration.


The initializer consists of an equal sign followed by a constant expression
as follows –

​ datatype variable name = value;

example: ​
// declaration with initialization
int x = 10;
// the variable x has the value 'x'
char x = 'x';
// define and initialize two variables
int d = 3, f = 5;

User Defined type declaration


C supports “type definition” that allows users to define an identifier that
would represent an existing data type. The user defined data type identifier
can later be used to declare variables. The general form is -

typedef type identifier;

example : typedef int units;


here units symbolizes int and this can be used to declare variables as
follows-
units batch1, batch2; // batch1,batch2 are declared as
int

Another user defined data type is enumerated data type which can
be used to declare variables that can have one of the values enclosed
within the braces.
The general form is
enum identifier (value1, value2,….vn);
enum identifier v1,v2…vn;
enumerated variables v1, v2… can have one of the values value1,
value2…

Defining symbolic constants

Identifiers used to represent fixed values in programs


using symbolic constants in the C programming language. These
parameters are frequently used to increase
the readability, maintainability, and modifiability of code, which may be
numerical or not.

The C language's "#define" command is used to declare symbolic


constants.
Symbolic constants are nothing more than a label or name that is used to
represent some fixed value that never changes throughout the course of a
program. For example, one might define PI as a constant to represent the
value 3.14159.

Syntax:

Syntax for defining a symbolic constant in C:

#define MAX 100

You might also like