C Fundamentals
C Fundamentals
Lecture:
What is C ?
Program Structure in C
To accomplish the given task programs are written and it is executed in Turbo C/C++
compiler. The structure of the program is given below.
Comments / /* Sample C Program */
main()
{ int a,b;
clrscr();
scanf(“%d%d”, &a,&b);
} sum = a + b;
Executing a C program
Executing a C program involves a series of following steps:
1. Creating a program
2. Compiling the program
3. Linking the program with functions that are needed from the C library.
4. Executing the program
Keywords
Key words or Reserve words of the C language are the words whose meaning is already
defined and explained to the C language compiler. Therefore, reserve words cannot be
used as identifiers or variable names. They should only be used to carry the pre-defined
meaning. For example, int is a reserve word. It indicates the data type of the variable as
integer. Therefore, it is reserved to carry the specific meaning. Any attempt to use it other
than the intended purpose will generate a compile time error. The following list shows the
reserved words in C.
double
Basic Data Types
C supports five fundamental data types: Character, Integer, Floating-Point, Double
floating-Point, and valueless. These are denoted as char, int, float double, void,
respectively, ’void’ is typically used to declare as function as retuning null value.
Constants
It refers to fixed values that the program may not alter during its execution. These
fixed values are also called literals. Constants can be of any of the basic data types like
an integer constant, a floating constant, a character constant, or a string literal. There are
enumeration constants as well. Constants are treated just like regular variables except
that their values cannot be modified after their definition.
Variables
A variable is an identifier that may be used to store data value. A value or a quantity
which may vary during the program execution can be called as a variable. Each variable
has a specific memory location in memory unit, where numerical values or characters can
be stored. A variable is represented by a symbolic name. Thus variable name refers to
the location of the memory in which a particular data can be stored. Variables names are
also called as identifiers since they identify the varying quantities.
For Ex : sum = a+b. In this equation sum, a and b are the identifiers or variable names
representing the numbers stored in the memory locations.
Rules to be followed for constructing the Variable names(identifiers):
1. They must begin with a letter and underscore is considered as a letter.
2. It must consist of single letter or sequence of letters, digits or underscore character.
3. Uppercase and lowercase are significant. For ex: Sum, SUM and sum are three
distinct variables.
4. Keywords are not allowed in variable names.
5. Special characters except the underscore are not allowed.
6. White space is also not allowed.
Variable Declarations
Assigning an identifier to data type is called type declaration. In other words a declaration
associates a group of variables with a specific data types. All variables must be declared
before they appear in executable statements. The syntax of variable declaration and
some examples are given below. The syntax of variable declaration and some examples
are given below.
Syntax :
Data type ‘variable’
For example:
int a;
float c;
char name;
double count;
If two are more variables of the same data type has to be declared they can be clubbed
as shown in example given below.
int a; intb; intc;
this is same as
int a, b, c;
short int a, b, c ;
long int r, s, t ;
The above declarations cab also be written as :
short a, b, c;
long r, s, r;
Syntax:
Variable name = value;
For Example:
a = 10; int lvalue = 0;
It is also possible to assign a value to a variable at the time the variable is declared. The
process of giving initial value to the variable is called initialization of the variable.
For Example:
int a=10, b=5; float x=10.5, y=1.2e-9;
The data item can be accessed in the program simply by referring to the variable name.
Data type associated with the variable cannot be changed. However, variables hold the
most recently assigned data.
Declaring a variable as constant
The value of the certain variable to remain constant during the execution of the program.
It can be achieved by declaring the variable with const qualifier at the time of initialization.
For example:
const int tax_rate = 0.30;
The above statement tells the compiler that value of variable must not be modified during
the execution of the program. Any attempt change the value will generate a compile time
error.