0% found this document useful (0 votes)
33 views16 pages

C Module1

C was originally developed in 1972 and has become widely used. It produces efficient, portable programs and can be compiled on many computers. C programs are first compiled to generate object code, then the object code is run. The main components of a C program include header files, global declarations, the main function, and user-defined functions. Variables are declared with a data type and can be initialized. Common data types include integers, floating-point numbers, and characters. Constants represent fixed values that don't change during program execution. Symbolic constants can be defined using the #define preprocessor directive.

Uploaded by

Muhammad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
33 views16 pages

C Module1

C was originally developed in 1972 and has become widely used. It produces efficient, portable programs and can be compiled on many computers. C programs are first compiled to generate object code, then the object code is run. The main components of a C program include header files, global declarations, the main function, and user-defined functions. Variables are declared with a data type and can be initialized. Common data types include integers, floating-point numbers, and characters. Constants represent fixed values that don't change during program execution. Symbolic constants can be defined using the #define preprocessor directive.

Uploaded by

Muhammad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 16

Module I

introduction to C Programming-Overview and Importance of C


C is a general-purpose high level language that was
originally developed by Dennis Ritchie for the Unix operating
system in 1972.
C has now become a widely used professional language for
various reasons.
• Easy to learn
• Structured language
• It produces efficient programs.
• It can be compiled on a variety of computers.
History of C language
'ALGOL.' – 1960
'BCPL’ - 1967
B Programming language – 1970
C Programming language – 1972
To assure that 'C' language will remain standard, American
National Standards Institute (ANSI) defined a commercial
standard for 'C' language in 1989. Later, it was approved by the
International Standards Organization (ISO) in 1990. 'C'
programming language is also called as 'ANSI C'.

Why use C?

C was initially used for system development work, particularly


the programs that make-up the operating system. C was
adopted as a system development language because it
produces code that runs nearly as fast as the code written in
assembly language. Some examples of the use of C might be −
 Operating Systems
 Language Compilers
 Assemblers
 Text Editors
 Print Spoolers
 Network Drivers
 Modern Programs
 Databases
 Language Interpreters
 Utilities

How C Programming Language Works?


C is a compiled language. A compiler is a special tool that
compiles the program and converts it into the object file which
is machine readable. After the compilation process, the linker
will combine different object files and creates a single
executable file to run the program. The following diagram
shows the execution of a 'C' program.
C Program Structure,

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

Global Declaration Section


This part of the code is the part where the global variables are
declared. All the global variable used are declared in this part.
Eg:float area(float r); int a=7;

Main Function Section


Every C-programs needs to have the main function. Each main
function contains 2 parts. A declaration part and an Execution
part. The declaration part is the part where all the variables are
declared. The execution part begins with the curly brackets and
ends with the curly close bracket. Both the declaration and
execution part are inside the curly braces.
int main()
{
int a=10;
printf(" %d", a);
return 0;
}
Sub Program Section
All the user-defined functions are defined in this section of the
program.
int add(int a, int b)
{
return a+b;
}

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.

Compile and Run C Program

To compile and run a C language program, you need a C


compiler. A compiler is a software that is used to compile and
execute programs. To set up a C language compiler in your
Computer/laptop, there are two ways:

1. Download a full-fledged IDE like Turbo C++ or Microsoft


Visual C++ or DevC++, which comes along with a C language
compiler.
2. you can use any text editor to edit the program files and
download the C compiler separately and then run the C
program using the command line.

Using an IDE - Turbo C or C++

Step 1: Open turbo C IDE(Integrated Development


Environment), click on File and then click on New
Step 2: Write a Hello World program that we created in the
previous article - C Hello World program.

Step 3: Click on Compile menu and then on Compile option, or


press the keys press Alt + F9 to compile the code.

Step 4: Click on Run or press Ctrl + F9 to run the code. Yes, C


programs are first compiled to generate the object code and
then that object code is Run.
Step 5: Output is here.
Elements of C Language and Program Construct:

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,

int a;float variable;float a, b;

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

Constants in C are the fixed values used in a program, and their


value remains the same during the entire program execution.

 Constants are also called literals.


 Constants can be any of the data types.

Syntax
Const type constant-name;

Example : const float pi=3.14;

Constants are categorized into two basic types,

I.Numeric Constants

1. Integer Constants
2. Real Constants

II. Character Constants

1. Single Character Constants


2. String Constants
3. Backslash Character Constants
I.Numeric Constants
1. Integer constants are of three types viz:

a) Decimal Integer
b) Octal Integer
c) Hexadecimal Integer

Example:

15, -265, 0, 99818, +25, 045, 0X6

2.Real Constants

The numbers containing fractional parts like 99.25 are called


real or floating points constant.

II. Character Constants

1.Single Character Constants

It simply contains a single character enclosed within ' and '.eg ‘x’

2.String Constants

It simply contains a sequence of character enclosed within


double quotes.eg “computer”

3.Backslash Character Constants

C supports some character constants having a backslash in


front of it. The lists of backslash characters have a specific
meaning known to the compiler. They are also termed "Escape
Sequences".
Example:

\t is used to give a tab \n is used to give a new line

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 constant_name value


For example:

#define PI 3.14

You might also like