0% found this document useful (0 votes)
13 views

C - New Notes

C is a general purpose programming language developed in the 1970s at Bell Labs. It has features like control structures, looping statements, and arrays that make it well-suited for business and scientific applications. The basic structure of a C program includes a main function containing declarations and executable statements, as well as other functions. C supports a variety of data types including primary types like int and float, and secondary types like arrays and structures. Operators in C are used to perform operations on variables and include assignment, arithmetic, logical, relational, and shorthand operators.

Uploaded by

JaganMohanGuduru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

C - New Notes

C is a general purpose programming language developed in the 1970s at Bell Labs. It has features like control structures, looping statements, and arrays that make it well-suited for business and scientific applications. The basic structure of a C program includes a main function containing declarations and executable statements, as well as other functions. C supports a variety of data types including primary types like int and float, and secondary types like arrays and structures. Operators in C are used to perform operations on variables and include assignment, arithmetic, logical, relational, and shorthand operators.

Uploaded by

JaganMohanGuduru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

WHAT IS C?

C language is a general purpose and structured programming language developed by 'Dennis


Ritchie' at AT &T's Bell Laboratories in the 1972s in USA.
It is also called as 'Procedure oriented programming language.'
C is not specially designed for specific applications areas like COBOL (Common Business-
Oriented Language) or FORTRAN (Formula Translation). It is well suited for business and
scientific applications. It has some various features like control structures, looping statements,
arrays, macros required for these applications.
The C language has following humorous features as:
 Portability
 Flexibility
 Effectiveness and efficiency
 Reliability
 Interactivity
Execution of C Program:
C program executes in following 4 (four steps).

1. Creating a program :
An editor like notepad or wordpad is used to create a C program. This file contains a
source code which consists of executable code. The file should be saved as '*.c'
extension only.
2. Compiling the program :
The next step is to compile the program. The code is compiled by using compiler.
Compiler converts executable code to binary code i.e. object code.
3. Linking a program to library :
The object code of a program is linked with libraries that are needed for execution of a
program. The linker is used to link the program with libraries. It creates a file with '*.exe'
extension.
4. Execution of program :
The final executable file is then run by dos command prompt or by any other software.
History of C:

Year of Language Name Developed By


Establishment
1960 ALGOL-60 Cambridge University
1963 CPL (Combined Programming Cambridge University
Language)
1967 BCPL (Basic Combined Martin Richard at Cambridge
Programming Language) University
1970 B Ken Thompson at AT & T's Bell
Laboratories.
1972 C Dennis Ritchie at AT & T' Bell
Laboratory.

The development of C was a cause of evolution of programming languages like Algol 60, CPL
(Combined Programming Language), BCPL (Basic Combined Programming Language) and B.

 Algol-60 : (1963) :
ALGOL is an acronym for Algorithmic Language. It was the first structured procedural
programming language, developed in the late 1950s and once widely used in Europe. But
it was too abstract and too general structured language.
 CPL : (1963) :
CPL is an acronym for Combined Programming Language. It was developed at
Cambridge University.
 BCPL : (1967) :
BCPL is an acronym for Basic Combined Programming Language. It was developed by
Martin Richards at Cambridge University in 1967. BCPL was not so powerful. So, it was
failed.
 B : (1970) :
B language was developed by Ken Thompson at AT & T Bell Laboratories in 1970. It
was machine dependent. So, it leads to specific problems.
 C : (1972) :
'C' Programming Language was developed by Dennis Ritchie at AT & T Bell
Laboratories in 1972. This is general purpose, compiled, structured programming
language. Dennis Ritchie studied the BCPL, then improved and named it as 'C' which is
the second letter of BCPL
Structure of C Program
The basic structure of C program is as follow:
Document Section
Links Section (File)
Definition Section
Global variable declaration Section
void main()
{
Variable declaration section
Function declaration section
executable statements;
}
Function definition 1
---------------------
---------------------
Function definition n
where,
Document Section : It consists of set of comment lines which include name of a program, author
name, creation date and other information.
Ex:-/*--------*/
Links Section (File) : It is used to link the required system libraries or header files to excute a
program.
Ex:- #include<stdio.h>
#include<math.h>
Definition Section : It is used to define or set values to variables.
Ex:#define pi=3.14;
Global variable declaration Section : It is used to declare global or public variable.
Int a;
void main() : Used to start of actual C program. It includes two parts as declaration part and
executable part.
Ex: main()
{
}
Variable declaration section : Used to declare private variable.
Ex: int a=10;
Function declaration section : Used to declare functions of program from which we get required
output.
Ex: add()
{
}
Then, executable statements are placed for execution.
Function definition section : Used to define functions which are to be called from main().
Character Set:
A character refers to the digit, alphabet or special symbol used to data representation.
1. Alphabets : A-Z, a-z
2. Digits : 0-9
3. Special Characters: ~! @ # $ % ^ & * ( ) _ + { } [ ] - < > , . / ? \ | : ; " '
4. White Spaces : Horizontal tab, Carriage return, New line, form feed
Identifier:
Identifier is the name of a variable that is made up from combination of alphabets, digits and
underscore.
Variable:
It is a data name which is used to store data and may change during program execution. It is
opposite to constant. Variable name is a name given to memory cells location of a computer
where data is stored.
* Rules for variables:
 First character should be letter or alphabet.
 Keywords are not allowed to use as a variable name.
 White space is not allowed.
 C is case sensitive i.e. UPPER and lower case are significant.
 Only underscore, special symbol is allowed between two characters.
 The length of identifier may be up to 31 characters but only only the first 8 characters are
significant by compiler.
 (Note: Some compilers allow variable names whose length may be upto 247 characters.
But, it is recommended to use maximum 31 characters in variable name. Large variable
name leads to occur errors.)
Keywords:
Keywords are the system defined identifiers.
All keywords have fixed meanings that do not change.
White spaces are not allowed in keywords.
Keyword may not be used as an identifier.
It is strongly recommended that keywords should be in lower case letters.
There are totally 32(Thirty Two) keywords used in a C programming.
int float double long
short signed unsigned const
if else switch break
default do while for
register extern static struct
typedef enum return sizeof
goto union auto case
void char continue volatile
Escape Sequence Characters (Backslash Character Constants) in C:
C supports some special escape sequence characters that are used to do special tasks.
These are also called as 'Backslash characters'.
Some of the escape sequence characters are as follow:
Character Constant Meaning
\n New line (Line break)
\b Backspace
\t Horizontal Tab
\f Form feed
\a Alert (alerts a bell)
\r Carriage Return
\v Vertical Tab
\? Question Mark
\' Single Quote
\'' Double Quote
\\ Backslash
\0 Null

C Tutorial - Data Types

Data types in any of the language means that what are the various type of data the variables can
have in that particular language. Whenever a variable is declared it becomes necessary to define
data type that what will be the type of data that variable can hold.
Mainly there are two types of data types:
1. Primary Data Type
2. Secondary Data Type

Primary Data Type : character, integer, float, double, void


Secondary Data Type : arrays, pointer, structures, union, enum
C language also supports the wide range of data type like :
Type Storage size Value range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295

Operators in C:
"Operator is a symbol that is used to perform mathematical operations."
When we use a variable in a program then we have to mention the type of data. This can be
handled using data type in C.
Followings are the most commonly used data types in C.

Operator Name Operators


Assignment =
Arithmetic +, -, *, /, %
Logical &&, ||, !
Relational <, >, <=, >=, ==, !=
Shorthand +=, -=, *=, /=, %=
Unary ++, --
Conditional ()?:;
Bitwise &, |, ^, <<, >>, ~

1. Assignment Operator

It is used to assign a value to variable.

/* Program to demonstrate assignment operator .


Creation Date : 10:05 AM 08/11/2010
Author : www.technoexam.com [ Technowell Web Solutions, Sangli ] */

#include <stdio.h>
#include <conio.h>
void main()
{
int a,b;
clrscr();
a = 53;
printf("\n\t Value of A : %d",a); // 53
b = a; // Interchange of value using assignment
printf("\n\n\t Value of B : %d",b); // 53
getch();
}
2. Arithmetic Operators

It is also called as 'Binary operators'. It is used to perform arithmetical operations. These


operators operate on two operands.

/* Program to demonstrate arithmetic operators .


Creation Date : 10:03 AM 08/11/2010
Author : www.technoexam.com [ Technowell Web Solutions, Sangli ] */

#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c,d,e,f,g;
clrscr();
printf("\n\t Enter First Number :"); // 5
scanf("%d",&a);
printf("\n\t Enter Second Number :"); // 2
scanf("%d",&b);
c = a + b;
printf("\n\n\t Addition is : %d",c); // 7
d = a - b;
printf("\n\n\t Subtraction is : %d",d); // 3
e = a * b;
printf("\n\n\t Multiplication is : %d",e); // 10
f = a / b;
printf("\n\n\t Division is : %d",f); // 2
g = a % b;
printf("\n\n\t Modulus is : %d",g); // 1
getch();
}

3. Logical Operators

Sometimes, we have to check more than one condition at a time then it is operator which is
primarily used to check more than two conditions. This operator returns 1 if condition is true
otherwise 0.

/* Program to demonstrate logical operators .


Creation Date : 10:31 AM 08/11/2010
Author : www.technoexam.com [ Technowell Web Solutions, Sangli ] */

#include <stdio.h>
#include <conio.h>

void main()
{
int no1=2, no2=5;
clrscr();
printf("\n\n %d",(no1 && no2)); // returns 1
printf("\n\n %d",(no1 || no2)); // returns 1
getch();
}
4. Relational Operators

It is also used to check conditions. These operators return 1 if condition is true otherwise 0.

/* Program to demonstrate Relational operators.


Creation Date : 10:09 AM 08/11/2010
Author : www.technoexam.com [ Technowell Web Solutions, Sangli ] */

#include <stdio.h>
#include <conio.h>

void main()
{
int a=6, b=2;
clrscr();
printf("\n\n A<=B : %d",(a<=b)); // 0 - False
printf("\n\n A>B : %d",(a>b)); // 1 - True
printf("\n\n A!=B : %d",(a!=b)); // 1 - True
getch();
}

5. Shorthand Operators

It is used to perform mathematical operations at which the result or output can affect on
operands.

/* Program to demonstrate shorthand operators .


Creation Date : 10:42 AM 08/11/2010
Author : www.technoexam.com [ Technowell Web Solutions, Sangli ] */

#include <stdio.h>
#include <conio.h>
void main()
{
int a,b;
clrscr();
a = 18;
b = 4;
printf("\n\t Value of A : %d",a); // 18
printf("\n\t Using of B : %d",b); // 4
b += a ; // b = b + a
printf("\n\n\t Using += (i.e b=b+a): %d",b); // 22
// Change the operator as -=, *=, /=, %=
getch();
}
6. Unary Operators

It operates on a single operand. Therefore, this operator is called as 'unary operator.' It is used to
increase or decrease the value of variable by 1.

/* Program to demonstrate Unary / Ternary operators .


Creation Date : 10:57 AM 08/11/2010
Author : www.technoexam.com [ Technowell Web Solutions, Sangli ] */

#include <stdio.h>
#include <conio.h>

void main()
{
int a=4, b;
clrscr();
printf("\n\n Value of A : %d",a); // 4
a++; // Post
printf("\n\n Value of A : %d",a); // 5
++a; // Pre
printf("\n\n Value of A : %d",a); // 6
b=--a;
printf("\n\n Value of A : %d",a); // 5
printf("\n\n Value of B : %d",b); // 5
b=a++;
printf("\n\n Value of A : %d",a); // 6
printf("\n\n Value of B : %d",b); // 5
b++;
printf("\n\n Value of B : %d",b); // 6
getch();
}
7. Conditional Operator

Conditional operator is also called as 'ternary operator.' It is widely used to execute condition in
true part or in false part. It operates on three operands. The logical or relational operator can be
used to check conditions.

/* Program to demonstrate conditional operator .


Creation Date : 10:14 AM 08/11/2010
Author : www.technoexam.com [ Technowell Web Solutions, Sangli ] */

#include <stdio.h>
#include <conio.h>
void main()
{
int a, b=3;
clrscr();
a = 5;
printf("\n\n A is less than B ? ");
// No
getch();
}
Operators Precedence and Associatively:
In C, each and every operator has a special precedence which is associated with it. There are
various levels of precedence. This precedence is especially used to determine to evaluation of
expression which has more than one operator in it. The operators which have higher precedence
are executed first and vice-versa. Operators which have same precedence level are evaluated
from left to right. It is dependent on its level. This feature is well known as 'Associatively of an
operator.'

Associatively Operator Description


() Function
[] Array
Left to Right
--> Pointer to member
. Structure

- Unary Minus
+ Unary Plus
++ / -- Increment/Decrement
~ One's Complement
Right to left & Address of
(type) Type casting
sizeof Size (in bytes)
! Logical Not
* Pointer reference

* Multiplication
Left to Right / Division
% Modulus

+ Addition
Left to Right
- Subtraction

<< Left Shift


Left to Right
>> Right Shift

Left to Right < Less than


<= Less than or equal to
> Greater than
>= Greater than or equal to

== Equality
Left to Right
!= Not Equal to

Left to Right & Bitwise AND

Left to Right ^ Bitwise XOR

Left to Right | Bitwise OR

Left to Right && Logical AND

Left to Right || Logical OR

Left to Right ?: Conditional Operator

Right to Left = *= += Assignment

Left to Right , Comma

Fig.: Precedence and associatively of operators

You might also like