C Module1
C Module1
Why use C?
Documentation section
It Consists of the description of the program, programmer's
name, and creation date. These are generally written in the
form of single line or multiple line comments.
//name of a program
Or
/*
* Author name
* File:name
* Description:
*/
Link Section
This part of the code is used to declare all the header files that
will be used in the program.
Eg:#include<stdio.h>
Definition Section
In this section, we define different constants. The keyword
define is used in
this part.
Eg: #define PI=3.14
Sample Programs.
/*
* File Name: areaofcircle.c
* Author: Manthan Naik
* date: 09/08/2019
* description: a program to calculate area of circle
*user enters the radius
*/
#include<stdio.h>//link section
#define PI 3.14;//defination section
float area(float r);//global declaration
int main()//main function
{
float r;
printf(" Enter the radius:n");
scanf("%f",&r);
printf("the area is: %f",area(r));
return 0;
}
float area(float r)
{
return pi * r * r;//sub program
}
Integrated Development Environment
An integrated development environment (IDE) is a software
application that helps programmers develop software code
efficiently. It increases developer productivity by combining
capabilities such as software editing, building, testing, and
packaging in an easy-to-use application.
Character Set
Characters are used in forming either words or numbers or
even expressions in C programming.
Characters in C are classified into 4 groups:
1.Letters
In C programming, we can use both uppercase and lowercase
letters of English language
• Uppercase Letters: A to Z
• Lowercase Letters: a to z
2.Digits
We can use decimal digits from 0 to 9.
3.Special Characters
C Programming allows programmer to use following special
characters:
4.White Spaces
In C Programming, white spaces contains:
• Blank Spaces
• Tab
• Carriage Return
• New line
C Token
C tokens are the basic buildings blocks in C language which are
constructed together to write a C program. Each and every
smallest individual units in a C program are known as C tokens.
Keywords
Keywords are special words in C programming which have their
own predefined meaning. The functions and meanings of these
words cannot be altered. Some keywords in C Programming are
Identifiers
Identifiers are user-defined names of variables, functions and
arrays. It comprises of
combination of letters and digits. In C Programming, while
declaring identifiers, certain rules
have to be followed viz.
• It must begin with an alphabet or an underscore and not
digits.
• It must contain only alphabets, digits or underscore.
• A keyword cannot be used as an identifier
• Must not contain white space.
• Only first 31 characters are significant.
Variables
variable is a name of the memory location. It is used to store
data. Its value can be changed,
and it can be reused many times
Syntax
type variable_name;
Eg: int a;
float b;
char c;
Rules for defining variables
o A variable can have alphabets, digits, and underscore.
o A variable name can start with the alphabet, and underscore
only. It can't start with a digit.
o No whitespace is allowed within the variable name.
o A variable name must not be any reserved word or keyword,
e.g. int, float, etc.
Data Types
A data type specifies the type of data that a variable can store
such as integer, floating, character,
etc.
Primitive data types or primary data type are categorized into
these parts
⚫ integer data types, such as short, int, long.
As the name suggests, an int variable is used to store an integer.
Eg:int a;
Size of int is 2 byte
⚫ floating-point data types, such as float, double.
It is used to store decimal numbers (numbers with floating
point value) with single
precision.
double: It is used to store decimal numbers (numbers with
floating point value)
with double precision.
Eg: Float a;
Size of floating point is 4 byte
Size of double is 8 byte
⚫ character data type, such as char.
The most basic data type in C. It stores a single character and
requires a single byte of
memory in almost all compilers.
Eg:Char a
Size of char is 1 byte
In C, the number of bytes used to store a data type depends on
the
Compiler(depending on the bit size of a compiler and also the
OS). But irrespective of the bit-size of the compiler and OS, the
following rules are followed, such as -
Declaration of Variable
Declaration of variable in c can be done using following syntax:
data_type variable_name;
or
data_type variable1, variable2,…,variablen;
For example,
Initialization of Variable
C variables declared can be initialized with the help of
assignment operator ‘=’.
Syntax
data_type variable_name=constant/literal/expression;
or
variable_name=constant/literal/expression;
Example:
int a=10;
int a=b+c;
a=10;
a=b+c;
Constants in C
Syntax
Const type constant-name;
I.Numeric Constants
1. Integer Constants
2. Real Constants
a) Decimal Integer
b) Octal Integer
c) Hexadecimal Integer
Example:
2.Real Constants
It simply contains a single character enclosed within ' and '.eg ‘x’
2.String Constants
Symbolic Constants
A symbolic constant is a name given to any constant. In C, the
preprocessor directive #define is used for defining symbolic
constants.#define instructions are usually placed at the
beginning of the program. By convention, the names of
symbolic constants are written in uppercase, but this is not
compulsory. The syntax for creating a symbolic constant is as
follows:
#define PI 3.14