Вы находитесь на странице: 1из 92

 The C programming language was developed

at Bell Laboratories in 1972 by Dennis Ritchie.


 C programming language features were
derived from an earlier language called “B”
(Basic Combined Programming Language –
BCPL)
 C language was invented for implementing
UNIX operating system
 In 1978, Dennis Ritchie and Brian Kernighan
published the first edition “The C
Programming Language” and commonly
known as K&R C
 In 1983, the American National Standards
Institute (ANSI) established a committee to
provide a modern, comprehensive definition
of C. The resulting definition, the ANSI
standard, or “ANSI C”, was completed late
1988.
C is often called a middle-level language
because it combines the best elements of
low-level or machine language with high-
level languages. Middle level languages don’t
provide all the built-in functions found in high
level languages, but provide all building
blocks that we need to produce the result we
want
C is a structured programming language, which
means that it allows you to develop programs
using well-defined control and provides
modularity (breaking the task into multiple
sub tasks that are simple enough to
understand and to reuse).
 C Programming provides low level features
that are generally provided by the Lower level
languages. C is Closely Related to Lower level
Language such as “Assembly Language“.

 It is easier to write assembly language code in


C programming.
It is more User friendly as compare to Previous
languages. Previous languages such as BCPL,
Pascal and other programming languages
never provide such great features to manage
data.
 C Programs are portable i.e they can be run
on any Compiler with Little or no
Modification
 Provides Wide verity of ‘Data Types‘
 Provides Wide verity of ‘Functions’
 Provides useful Control & Loop Control
Statements
C Programs can be manipulated using bits. We
can perform different operations at bit level.
We can manage memory representation at
bit level
Modular programming is a software design
technique that increases the extent to which
software is composed of separate parts,
called modules
C Program Consist of Different Modules that
are integrated together to form complete
program
 Pointers has direct access to memory.

 C Supports efficient use of pointer.


Various memory management in-built
functions are available in C language which
helps to save memory and hence improve
efficiency of the program. e.g. malloc(),alloc()
and calloc().
The syntax errors can be easily detected by the
C compiler. The error is displayed with line
number of the code and the error message.
C has a rich set of library functions. These are
the functions that provide readymade
functionality to the users. It also supports
graphic programming.
 C Programming is best known programming
language. C Programming can be used to do
verity of tasks such as networking related,OS
related.
 Additionally, even compilers and interpreters
for other languages such as FORTRAN,
Pascal, and BASIC are written in C.
The following is a partial list of areas where C
language is used:
Ø Embedded Systems
Ø Systems Programming
Ø Artificial Intelligence
Ø Industrial Automation
Ø Computer Graphics
Ø Space Research
Ø Image Processing
Ø Game Programming
 Let us look at a simple code that would print
the words "Hello World" −
#include <stdio.h>

void main()
{
printf("Hello, World! \n");
}
All C compilers support a variety of data types.
This enables the programmer to select the
appropriate data types as per the need of the
application. The type of a variable determines
how much space it occupies in storage.
C Data types can be classified as follows:

 Basic Data Types


 Derived Data Types
 User Defined Data Types
 Void Data Type
 Basic data types: integer (int), character
(char), floating point (float), double floating
point (double).

 Derived data types: pointers, functions and


arrays.
 User-defined data types: struct, union and
typedef.

 Void data type: no value


 The integer type has a value without any
decimal.

All C compilers offers different types:

 short integer and long integer.


 Signed integer and Unsigned integer
SHORT INTEGER LONG INTEGER

 Occupies 2 bytes  Occupies 4 bytes

 Range: -32768 to 32767  Range: -2147483648 to


2147483647
 Program runs faster
 Program runs slower
 Format specifier: %d or %i
 Format specifier: %ld
SIGNED INTEGER UNSIGNED INTEGER

 Occupies 2 bytes  Occupies 2 bytes

 Range: -32768 to 32767  Range: 0 to 65535

 Format specifier: %d or %i  Format specifier: %u

 Eg. long int b=2  Eg. unsigned long b=4562


 A character denotes any alphabet, digit or
special symbol used to represent
information. Like:

 Alphabets: A  Z Or a  z
 Digits: 0,1,2…..9
 Special symbols: ~ ‘ @ & % _ { etc…
SIGNED CHARACTER UNSIGNED CHARACTER

 Occupies 1 byte in memory  Occupies 1 byte in memory

 Range: -128 to 127  Range: 0 to 255

 Format specifier: %c  Format specifier: %c

 Eg. char ch=‘B’  Eg. Unsigned char=‘b’


 When variable is declared without short or
long keyword, the default is short signed int.
 A floating-point literal has an integer part, a
decimal point, a fractional part, and an exponent
part. You can represent floating point literals
either in decimal form or exponential form.
 While representing decimal form, you must
include the decimal point, the exponent, or
both; and while representing exponential form,
you must include the integer part, the fractional
part, or both. The signed exponent is introduced
by e or E.
FLOAT DOUBLE

 Occupies 4 bytes  Occupies 8 bytes

 Range: 3.4e-38 to 3.4e+38  Range: 1.7 e-308 to +308

 Format specifier: %f  Format string: %lf

 Eg. Float y  Eg. Double y


 %d integer variable
 %c character variable
 %f float variable
 % lf double variable
 %c character variable
 %s string variable
 %o octal value
 %x hexadecimal value
Data Type Size Range Format
String
char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c
short or int 2 -32768 to 32767 % i or % d
unsigned int 2 0 to 65535 %u
long 4 -2147483648 to 2147483647 % ld
unsigned long 4 0 to 4294967295 % lu
float 4 3.4 e-38 to 3.4 e+38 %f
double 8 1.7 e-308 to 1.7 e+308 % lf
long double 10 3.4 e-4932 to 1.1 e +4932 % lf
The smallest unit in a program/statement is called
as Token. A C program consists of various
tokens.
A token can be classified as:

 Keyword
 Variable
 Constant
 Operators
 Special Characters
 Strings
The following C statement consists of five tokens −
printf("Hello, World! \n");

The individual tokens are −

printf
(
"Hello, World! \n"
)
;
 The words which are reserved by C compiler
for some specific purpose are called
Keywords.
 Each keyword has its own significance and is
used for some predefined purpose. These
reserved words may not be used as constants
or variables or any other identifier names.
 There are 32 keywords in total.
 C variable is a named location in a memory
where a program can manipulate the data.
The data is stored in the memory, and at the
time of execution it is fetched for carrying out
different operations on it.
 The variable value keeps on changing at
different times during program execution.

 C variable might be belonging to any of the


data type like int, float, char etc. Variables are
case sensitive.
void main()
{
int roll_no=10;
float marks=75.5;
printf(“%d%f”,roll_no,marks);
}

Here, roll_no and marks are variables.


 Variable name must begin with letter or
underscore.
 They can be constructed with digits, letters.
 It can be a combination of lower case and
upper case letters.
 No special symbols are allowed other than
underscore.
 The variable should not be a keyword.
 int a;
 int roll_no;
 int su+m;
 int t@tal;
 int p%age;
 int a123;
 int 123a;
 int AbCd;
 int a; Valid
 int roll_no; Valid
 int su+m; Invalid
 int t@tal; Invalid
 int p%age; Invalid
 int a123; Valid
 int 123a; Invalid
 int AbCd; Valid
 A symbolic name used to refer to a variable,
constant, function, structure etc. Identifiers
are user defined names.

 int age;
 const pi;
 float marks;

 Here age, pi and marks are identifiers.


 Variables should be declared in the C
program before use. Memory space is not
allocated for a variable while declaration.

 It happens only on variable definition.

 Variable initialization means assigning a value


to the variable.
1. void main()
2. {
3. int a,b;
4. a=10,b=20;
5. }

 Here,
Line 3 is declaration.
Line 4 is initialization.
The initialization of variable at run time is called
dynamic initialization. Dynamic refers to the process
during execution.
Eg.
Void main()
{
int a=10;
int b;
b=a*10;
}
Here b is initialized dynamically.
In a C program, the semicolon is a statement
terminator. That is, each individual statement
must be ended with a semicolon. It indicates
the end of one logical entity.
Eg.
printf(“Hello”);
Comments are like helping text in your C
program and they are ignored by the
compiler
There are two types of comments:
// single line
/* double line */

Example:
/* my first program in C */
You cannot have comments within comments
 Whitespace is the term used in C to describe
blanks, tabs, newline characters and
comments.
 Whitespace separates one part of a
statement from another and enables the
compiler to identify where one element in a
statement, such as int, ends and the next
element begins.
int age;
there must be at least one whitespace
character (usually a space) between int and
age for the compiler to be able to distinguish
them. On the other hand, in the following
statement −
fruit = apples + oranges; // get the total fruit
no whitespace characters are necessary
between fruit and =, or between = and apples,
 Constants refer to fixed values that the
program may not alter during its execution.
These fixed values are also called literals.

 Eg. const int width = 5;


Constant

Numeric Character
Constant Constant

Integer Real Character String


constant constant constant constant
 Integer Constant: these constants are
represented with whole numbers.
Eg. 10,20, -50

 Real Constant/Floating Constant: these are


represented in exponential or fraction form.
Eg. 2.5, 5.521, 2.4561 X e+3
 Character Constant: It contains a single
character. It is enclosed in a single quote.
Eg. ‘a’, ‘8’, ‘-’

 String Constant: It contains sequence of


characters enclosed in double quotes.
Eg. “Hello”, “365”, “a”
 lvalue − Expressions that refer to a memory
location are called "lvalue" expressions. An
lvalue may appear as either the left-hand or
right-hand side of an assignment.

 rvalue − The term rvalue refers to a data


value that is stored at some address in
memory. rvalue appear on the right-hand
side but not on the left-hand side of an
assignment.
Error is a mistake or bug, which can be
associated with programmer in program,
common programming errors in C can be
classified into 3 types:

 1. compilation errors.(syntax errors)


 2. linker errors.
 3. logical errors.
These are raised when we compile the program
and can be located and corrected easily. Most
common compilation or syntax errors are:

 Undefined variable
 Missing semicolon
 Function call missing
 Function should have prototype
The program must be linked to the ‘C’ library. If
it fails in such case these errors are raised .
most common errors are:

 Unable to link cos.obj


 Undefined symbol
These are raised because of the due to logical
inefficiency. We can know them when program
gets executed. It is very difficult to locate
them, most common logical errors are:

 Divide by zero
 Null value
 Garbage result in printing.
Compiling a C program is a multi-stage process.
At an overview level, the process can be split
into four separate stages:

 Creation,
 Preprocessing,
 Compilation and linking,
 Executing the program.
This will be used to type your program like
Windows Notepad. The files you create with
your editor are called the source files and
they contain the program source codes. The
source files for C programs are typically
named with the extension ".c".
The first stage of compilation is called
preprocessing. In this stage, lines starting
with a # character are interpreted by the
preprocessor as preprocessor commands.
The preprocessor program executes first
automatically before the compilation of the
program.
The source program contains statements that
are to be translated into object codes. These
object codes are suitable for execution by the
computer. If there is no error in the program,
compilation proceeds and translated
program stored with the .obj extension.
Linking puts together all other program files
and functions that are required by the
program. Eg. If a program is using pow()
function, then the object code for this
function should be brought from math.h
library.
Therefore, to produce an executable program,
the existing pieces have to be rearranged and
the missing ones filled in. This process is
called linking.
After compilation the executable code will be
loaded in the computer’s main memory is
executed. The loader performs this function.
This executes your program, printing any
results to the screen. At this stage there may
be run-time errors.
If so, you must return to edit your program
source, and recompile it, and run it again.
An operator is a symbol that tells the compiler to
perform specific mathematical or logical
functions. C language is rich in built-in operators
and provides the following types of operators −

 Arithmetic Operators
 Relational Operators
 Logical Operators
 Bitwise Operators
 Assignment Operators
 Misc Operators
Arithmetic operators are used to perform
numeric calculations like addition,
subtraction, multiplication, division etc.
Unary operators: (+ - ++ --)
The operators which work upon single operand
are called unary operator.

Binary operators: (+ - * / %)
 The operators which work upon more than
one operand are called binary operator.
Increment ++: is used to increment value of
variable by one.
Eg.
 a=++b;
 a=b++;
Decrement --: is used to decrement value of
variable by one.
Eg.
 a=--b;
 a=b--;
Relational operators are used to provide the
relationship between two expressions.
If the relation is true then it returns a value 1
otherwise 0 for false.
The operators which are used to test relation
between two or more variables or
expressions. It provides logical true 1, if the
condition is true and 0 otherwise.
Bitwise operator works on bits and perform bit-
by-bit operation. These operators can
operate only on integer operands such as int,
char, short, long.
The assignment operator is used to assign value to a
variable.

Eg. int x=5;


Apart from above mentioned operators there
are other operators also:

 Comma operator
 Conditional operator
 sizeof operator
 The comma operator is used to separate two
or more expressions. It has lowest priority.
Eg.
 a=10,b=20,c=30;
 (a=10,b=20,c=a+b)
The conditional operator contains condition
followed by two statements or values. If the
condition is true, the first statement is
executed, otherwise the second statement is
executed.
Syntax:
Condition ? Expression 1 : Expression 2
Eg. printf(2==3?4:5);
This operator is used to find out the size
occupied by a variable.
Eg.
int a;
Printf(“%d”,sizeof(a));

Ans. 2
 Type casting is a way to convert a variable
from one data type to another data type. For
example, if you want to store a 'long' value
into a simple integer then you can type cast
'long' to 'int'. You can convert the values from
one type to another explicitly using the cast
operator as follows −
 (type_name) expression
 #include <stdio.h>
 main()
 {
 int sum = 17, count = 5; double mean;
 mean = (double) sum / count;
 printf("Value of mean : %f\n", mean );
 }
 Type casting is of two types:

 Implicit type casting


 Explicit type casting
 Implicit conversions do not required any
operator for converted . They are
automatically performed when a value is
copied to a compatible type in program .
 Here, the value of a has been promoted from
int to double and we have not had to specify
any type-casting operator. This is known as a
standard conversion.
void main()
{
int i,j;
float f;
double d;
i=d*f+f*j;
}
In c language , Many conversions, specially
those that imply a different interpretation of
the value, require an explicit conversion. We
have already seen two notations for explicit
type conversion.
They are not automatically performed when a
value is copied to a compatible type in
program.
void main()
{
int m1=70,m2=70,m3=100,total;
float per;
total=m1+m2+m3;
per=(float)total/300*100;
printf("%f",per);
}

output:- 80.000000

Вам также может понравиться