0% found this document useful (0 votes)
2 views

Unit-I _ C-Programming

The document provides a comprehensive introduction to C programming, covering the basic structure of a C program, features of the C language, character sets, tokens, keywords, identifiers, constants, and variables. It explains essential components such as comments, preprocessor directives, and data types, along with rules for naming variables and identifiers. The content serves as a foundational guide for understanding the syntax and functionality of C programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit-I _ C-Programming

The document provides a comprehensive introduction to C programming, covering the basic structure of a C program, features of the C language, character sets, tokens, keywords, identifiers, constants, and variables. It explains essential components such as comments, preprocessor directives, and data types, along with rules for naming variables and identifiers. The content serves as a foundational guide for understanding the syntax and functionality of C programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Allotment Mapped

of with
Hours CO
L T/A CO
Number
UNITDetails of Topic to C programming
1: Introduction
Basic structure of C program, Features of C language, Character set, C 7
tokens, Keywords and Identifiers, Constants, Variables, Datatypes 1

UNIT 1: Introduction to C programming


1.1. Basic structure of C program
Comments/Documentation Section
Preprocessor Directives
Global declaration section
void main( ) [Program Header]
{
Local Declaration part
Execution part
Statement 1
------------
------------
Statement n
}
User defined Functions
1.1.1. Comments/Documentation Section
 Comments are short explaining the purpose of the program or a statement.
 They are non executable statements which are ignored by the compiler.
 The comment begins with /* and ends with */.
 The symbols /* and */ are called comment line delimiters.
 We can put any message we want in the comments.
Example:
/* Program to compute Quadratic Equation */
Note: Comments are ignored by C compiler, i.e. everything within comment delimiters
1.1.2. Preprocessor Directives
 Preprocessor Directives begins with a # symbol
 Provides instructions to the compiler to include some of the files before compilation starts.
 Some examples of header files are
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <math.h>
#include<stdlib.h> Etc…

1.1.3. Global Declaration Section


 There will be some variable which has to be used in anywhere in program and used in
more than one function which will be declared as global.
 The Program Header
 Every program must contain a main () function.
 The execution always starts from main function.
 Every C program must have only one main function.
 Body of the program
 Series of statements that performs specific task will be enclosed within braces { and }
 It is present immediately after program header.
It consists of two parts
1. Local Declaration part: Describes variables of program
Ex: int sum = 0;
int a, b;
float b;
2. Executable part: Task done using statements.

All statements should end with semicolon (;)

Subroutine Section: All user defined functions that are called in main function should be
defined.
1.2. Features of C language
Features of C Language */ Advantages /*
C language is very popular language because of the following features:
1. C is structured Programming Language
2. It is considered a high-level language because it allows the programmer to solve a
problem without worrying about machine details.
3. It has wide variety of operators using which a program can be written easily to solve a
given problem.
4. C is more efficient which increases the speed of execution and management of
memory compared to low level languages.
5. C is machine independent. The program written on one machine will work on another
machine.
6. C can be executed on many different hardware platforms.
1.3. Character set
1.3 Character set of C language
Definition: A symbol that is used while writing a program is called a character.
A character can be: .
 Alphabets/Letters (Lowercase a-z, Uppercase A-Z)
 Digits (0-9)
 Special Symbols ( ~ ‘ ! @ # % & * () - + / $ = \ { } [ ] : ; “ “ ? etc)

Symbol Name Symbol Name Symbol Name


~ Tilde | Vertical bar [ Left bracket
# hash ( Left parenthesis ] Right bracket
$ Dollar sign ) Right parenthesis : Colon
% Percent sign _ Underscore ” Quotation mark
^ Caret + Plus sign ; Semicolon
& Ampersand { Left brace < Less than
* Asterisk } Right brace > Greater than
‘ Single quote . dot = Assignment
, comma \ backslash / Division
1.4. C tokens
 Tokens are the smallest or basic units of C program.
 One or more characters are grouped in sequence to form meaningful words. These meaningful
words are called as tokens.
 A token is collection of characters.
 Tokens are classified in to 5 types as below:

Tokens

Special
Keywords Identifiers Constants Operators
Symbols
1.5. Keywords

Words.
totally 32 keywords supported in C they are:

Rules for keywords


1. Keywords should not be used as variables ,function names, array names etc.
2. All keywords should be written in lowercase letters.
3. Keywords meaning cannot be changed by the users.
1.6. Identifiers

Definition:

names, array names etc

Rules for identifiers


1. The First character should be an alphabet or an underscore _
Then First character is followed by any number of letters or digits.

2. No extra symbols are allowed other than letters ,digits and Underscore
3. Keywords cannot be used as an identifier
4. The length can be 31 characters for external, 63 for internal.
5. Identifiers are case sensitive.
Example: Area, Sum_
1.7. Constants,
Definition:

1. Integer constant
2. Real constant/Floating Pointing constant
3. Enumeration constant
4. Character constant
5. String constant

1. Integer constant

are allowed other than + or _ .

-
Ex: 123 , -345, 0 , 5436 , +79
but it has a
prefix of 0
Ex: 027 , 0657 , 0777645

with 0X or 0x
Ex: 0X2 0x56 0X5fd 0xbdae

2. Real constants/Floating point:


re base 10 numbers that are represented by fractional
parts, such as 10.5.They can be positive or negative.

i. Fractional form:

followed by dot and a fractional part.

-71.
ii. Exponential form:

ger.

-3 => 9.86*10
3

3. Enumeration constant

enumeration constants.

enum identifier{enum list};


-3

NO is assigned with 0
YES is assigned with value 1

mon is assigned with 0


tue is assigned with 1
wed is assigned with 2
4.Character constant
pair of single quotes is called character constant.

d is followed
by one character.
5.String constant
 A sequence of characters enclosed within pair of double quotes is called string constant.
 The string always ends with a NULL character.
 Ex: “9” , “SVIT”
Variables,

 A variable is a name given to memory location where data can be stored.


 Using variable name data can be stored in a memory location and can be accessed or
manipulated very easily.
 Rules for variables
 The First character should be an alphabet or an underscore _
 Then First character is followed by any number of letters or digits.
 No extra symbols are allowed other than letters ,digits and Underscore
 Keywords cannot be used as an identifier
Example:
Sum – valid
For1- valid
for -invalid (it is a keyword)

1.4.4.1Declaration of variables:
 Giving a name to memory location is called declaring a variable.
 Reserving the required memory space to store the data is called defining a variable.
 General Syntax:
datatype variable; (or)
datatype variable1, variable2,……..variablen;
example: int a;
float x, y;
double sum;

 From the examples we will come to know that “a” is a variable of type integer and
allocates 2 bytes of memory. “x” and “y” are two variable of type float which will be
allocated 4 bytes of memory for each variable. “sum” is a double type variables
which will be allocated with 8 bytes of memory for each.

1.5 Variable Initialization


 Variables are not initialized when they are declared and defined, they contain garbage
values(meaningless values)
 The method of giving initial values for variables before they are processed is called
variable initialization.
 General Syntax:
Var_name = expr;
Where,
Var_name is the name of the variable ,
expr is the value of the expression.
 The “expr” on the right hand side is evaluated and stored in the variable name
(Var_name) on left hand side.
 The expression on the right hand side may be a constant, variable or a larger formula
built from simple expressions by arithmetic operators.
Examples:
 int a=10;
// assigns the value 10 to the integer variable a
 float x;
x=20;
// creates a variable y of float type and assigns value 20 to it.
 int a=10,b,b=a;
// creates two variables a and b. “a” is assigned with value 10, the value of “a” is
assigned to variable “b”. Now the value of b will be 10.
 price = cost*3;
//assigns the product of cost and 3 to price.
 Square = num*num;
// assigns the product of num with num to square.
Datatypes

1.4.4 Data types and sizes


 Definition: The data type defines the type of data stored in memory location or the type
of data the variable can hold.
 The classification of data types are:

 There are few basic data types supported in C.


1. int
2. float
3. double
4. char
5. void
1.Integer data type (int):
 It stores an integer value or integer constant.
 An int is a keyword using which the programmer can inform the compiler that the
data associated with this keyword should be treated as integer.
 C supports 3 different types of integer:
o short int,
 int,
 o long int
2. Floating data type (float):
 An float is a keyword using which the programmer can inform the compiler that the
data associated with this keyword should be treated as floating point number.
 The size is 4 bytes.
 The range is -3.4e38 to +3.4e38.
 Float can hold the real constant accuracy up to 6 digits after the decimal point.

3.Double data type (double):


 An double is a keyword using which the programmer can inform the compiler that the
data associated with this keyword should be treated as long floating point number.
 The size is 8 bytes.
 The range is from 1.7e-308 to 1.7 e+308.
 Double can hold real constant up to 16 digits after the decimal point.
4.Character data type (char):
 char is a keyword which is used to define single character or a sequence of character
in c language capable of holding one character from the local character set.
 The size of the character variable is 1 byte.
 The range is from -128 to +127.
 Each character stored in the memory is associated with a unique value termed as
ASCII (American Standard Code for Information Interchange).
 It is used to represent character and strings.

5. void data type (void):


 It is an empty data type.
 No memory is allocated.
 Mainly used when there are no return values.

You might also like