Module 1 (PART 2) - Introduction To C Language
Module 1 (PART 2) - Introduction To C Language
Module 1 (PART 2) - Introduction To C Language
FUNDAMENTALS AND
PROGRAMMING IN C
MODULE 1- INTRODUCTION TO C
Module 1- Introduction to C
INTRODUCTION TO C
Introduction
Uses of C
C language is primarily used for system programming.
The portability, efficiency, the ability to access specific hardware addresses and
low run time demand on system resources makes it a good choice for implementing
operating systems and embedded system applications.
C has been so widely accepted by professionals that compilers, libraries, and
interpreters of other programming languages are often implemented in C.
For portability and convenience reasons, C is sometimes used as an intermediate
language by implementations of other languages.
Example of compilers which use C this way are BitC, Gambit, the Glasgow
Haskell Compiler, Squeak, and Vala.
C is widely used to implement end-user applications.
Structure of A C Program
A C program is composed of preprocessor commands, a global declaration section
and one or more functions.
The statements in a C program are written in a logical sequence to perform a
specific task.
Execution of a C program begins at the main() function.
The preprocessor directives contain special instructions that indicate how to prepare
the program for compilation.
One of the most commonly used pre-processor commands is include which tells the
compiler that to execute the program, some information is needed from the specified
header file.
All functions (including main()) are divided into two parts:
Declaration section.
Statement section .
The declaration section precedes the statements and is used to describe the data that will
be used in the function.
Note that the data declared within a function are known as local declaration as that
data will be visible only within that function.
The statement section in a function contains the code that manipulates the data to
perform a specified task.
A C program can have any number of functions depending on the tasks that have to be
performed.
Each functions can have any number of statements arranged according to a specific
meaningful sequence.
#include<stdio.h>
int main()
{
printf(“Welcome to the world of C”);
return 0;
}
Where -o is the output filename. If you leave out the -o, then the file name
a.out is used.
Files Used in a C Program
- Every C program has four kinds of the files associated with it.
Object Files
Object files are generated by the compiler as a result of processing the source code
file.
Object files contain compact binary code of the function definitions.
Linker uses this object file to produce an executable file (.exe file) by combining the
object files together.
Object files have a “.o” extension, although some operating systems including Windows
and MS-DOS have a “.obj” extension for the object file.
Binary Executable File
The binary executable file is generated by the linker.
The linker links the various object files to produce a binary file that can be directly
executed.
On Windows operating system, the executable files have “.exe” extension.
Figure 8.6- Modular Programming - the complete compilation and execution process
Comments
● It is a good programming practice to place some comments in the code to help the reader
understand the code clearly.
● Comments are just a way of explaining what a program does.
● It is merely an internal program documentation.
● The compiler ignores the comments when forming the object file.
● This means that the comments are non-executable statements.
● C supports two types of commenting:
// is used to comment a single statement.
This is known as a line comment.
A line comment can be placed anywhere on the line.
It does not require to be specifically ended as the end of the line automatically
ends the line.
/* is used to comment multiple statements.
A /* is ended with */ and all statements that lie within these
characters are commented.
The following code shows the way in which we can make use of comments in our
first program.
/* Author : Reema Thareja
Description : To print „Welcome to the world of C‟ on the screen */
#include <stdio.h>
int main()
{
// prints message
printf (“\n Welcome to the world of C”);
return 0; //returns a value 0 to the
// operating system
}
Since comments are not executed by the compiler, they do not affect the
execution speed and the size of the compiled program.
Therefore, use comments liberally in your programs so that other users can
understand the operations of the program and will serve as an aid to
debugging and testing.
Keywords
C has a set of 32 reserved words often known as keywords.
All keywords are basically a sequence of characters that have a fixed meaning.
By convention all keywords must be written in lower case (small) letters.
Identifiers
Identifiers are names given to program elements such as variables, arrays and functions.
Rules for forming identifier name are listed below:
- It cannot include any special characters or punctuation marks (like #, $, ^, ?, ., etc) except
the underscore"_".
- There cannot be two successive underscores.
- Keywords cannot be used as identifiers.
- The names are case sensitive. So, example, “FIRST” is different from “first” and “First”.
- It must begin with an alphabet or an underscore.
- It can be of any reasonable length.
- It should not contain more than 31 characters.
Examples of valid identifiers include the following:
roll_number, marks, name, emp_number, basic_pay, HRA, DA, dept_code.
Example for invalid identifiers include the following:
23_student, #marks, @name, #emp_number, basic.pay, -HRA, (DA), &dept_code,
auto.
Basic Data Types in C
C language provides very few basic data types. The following table list the data
types used in C.
Variables
A variable is defined as a meaningful name given to the data storage location
in computer memory.
When using a variable, we actually refer to address of the memory where the
data is stored.
C language supports two basic kinds of variables:
Numeric variables can be used to store either integer values or floating point
values.
While an integer value is a whole numbers without a fraction part or decimal
point, a floating point number, can have a decimal point in them.
Numeric values may also be associated with modifiers like short, long, signed
and unsigned.
By default, C automatically a numeric variable signed.
Character variables can include any letter from the alphabet or from the ASCII
chart and numbers 0 – 9 that are put between single quotes.
Declaring Variables
To declare a variable specify data type of the variable followed by its
name.
Variable names should always be meaningful and must reflect the purpose
of their usage in the program.
Variable declaration always ends with a semicolon.
Example:
int emp_num;
float salary;
char grade;
double balance_amount;
unsigned short;
int acc_no;
C allows multiple variables of the same type to be declared in one
statement.
The following statement is absolutely legal in C:
Initializing Variables
While declaring the variables, we can also initialize with some value.
Example: int emp_num=7;
float salary=5000;
char grade=‟A‟;
double balance_amount=100000000;
The initializer applies only to the variable defined immediately before it.
Therefore, the statement:
int count, flag=1; initializes the variable flag and not count.
If you want both the variables to be declared in a single statement then
write,
int count=0, flag=1;
When variables are declared but not initialized they usually contains a
garbage values.
Constants
Constants are identifiers whose value does not change.
Constants are used to define fixed values like PI or the charge on an electron
so that their value does not get changed in the program even by mistake.
A constant is an explicit data value specified by the programmer.
The value of the constant is known to the compiler at the compile time.
C allows the programmer to specify constants of integer type, floating point
type, character type and string type.
Integer Constants
A constant of integer type consists of a set of digits.
For Example-1, 34, 567, 8907 are valid integer constants.
For a long integer constant, the literal is succeeded with either „L‟ or
„l‟(Example – 1234567L)
Integer literals can be expressed in decimal, octal, hexadecimal notation.
Decimal integers consists of a set of digits, 0 through 9, preceded by an
optional - or + sign.
Examples of decimal integer constants include 123, -123, +123 and 0.
While writing integer constants, embedded spaces, commas and non-
digit characters are not allowed.
Following integer constants are not valid in C:
123 456 12,34,567 $123
An integer constant preceded by 0 is an octal number.
Octal integer consists of set of digits 0 through 7.
Examples of Octal integers include- 012, 0, 01234.
An integer constant is expressed in hexadecimal notation if it is
preceded with 0x or 0X.
Hexadecimal numbers contain digits from 0-9 and alphabets A through
F.
The alphabets A through F represent numbers 10 through 15.
Example – decimal 72 is equivalent to 0110 in octal notation and 0x48
in hexadecimal notation.
Some examples of hexadecimal integers are 0X12 , 0x7F, 0xABCD.
String Constants
A string is a sequence of characters enclosed in double quotes.
So “ a” is not the same as „a‟.
The characters comprising the string constant are stored in successive
memory locations.
When a string constant is encountered in a C program, the compiler
records the address of the first character and appends a null
character(„\0‟) to the string to mark the end of the string.
Declaring Constants
To declare a constant, precede the normal variable declaration with const
keyword and assign it a value.
Example- const float pi=3.14;
The const keyword specifies that the value of pi cannot change.
Another way to designate a constant is to use the pre-processor command
define.
Like other pre-processor commands, it is preceded with a # symbol.
Although #define statements can be placed anywhere in a C program, it is
always recommended that these statements be placed at the beginning of the
program to make them easy to find and modify at a later stage.
Let us look at the following example which defines values using define.
#define PI 3.14159
#define service_tax 0.12
In these examples, the values of PI and service_tax may change.
Width- specifies the minimum number of characters to print after being padded with
zeros or blank spaces that is it specifies the minimum number of positions in the output.
Precision- specifies the maximum number of characters to print.
For integer specifiers (d, i, o, u, x, X): precision flag specifies the minimum number
of digits to be written. If the value to be written is shorter than this number,
the result is padded with leading zeros. Otherwise, if the value is longer, it is
not truncated.
For character strings, precision specifies the maximum number of characters to
be printed.
For floating point numbers, the precision flag specifies the number of decimal
places to be printed.
Specifiers- is used to define the type and the interpretations of the value of the
corresponding argument.
Note that if the user specifiers a wrong specifier, then some strange things will
be seen on the screen and the error might propagate to other values in the printf
list.
The control string specifies the type and format of the data that has to be obtained
from the keyboard and stored in the memory locations pointed by the arguments arg1,
arg2,…, argn.
Prototype of the control string:
[=%[*][width][modifiers]type=]
* is an optional argument that suppresses assignment of the input field.
That is, it indicates that data should be read from the stream but ignored (not stored in
the memory location).
width is an optional argument that specifies the maximum number of characters to be
read.
modifiers is an optional argument that can be h, l or L for the data pointed by the
corresponding additional arguments.
Modifier h is used for short int or unsigned short int, l is used for long int, unsigned
long int or double values.
Finally, L is used long double data values.
Type – type of data that has to be read. It also indicates how this data is expected to
be read from the user.
The type specifiers for scanf function are listed in the below table:
int num;
float fnum;
double dnum;
short snum;
scanf("%d %f %c %s %e %hd %ld", &num, &fnum, &ch, str, &dnum, &snum, &lnum);