Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views23 pages

What Is C Language

C Language is a general-purpose programming language developed by Dennis Ritchie in 1972, primarily for system software and operating systems. It is characterized by its simplicity, portability, and ability to perform both low-level and high-level programming. C is widely used in various applications, including operating systems, compilers, and database management software.

Uploaded by

Rehaan Zaina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views23 pages

What Is C Language

C Language is a general-purpose programming language developed by Dennis Ritchie in 1972, primarily for system software and operating systems. It is characterized by its simplicity, portability, and ability to perform both low-level and high-level programming. C is widely used in various applications, including operating systems, compilers, and database management software.

Uploaded by

Rehaan Zaina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

What is C language?

C Language is a general-purpose, programming language. Which was created by


Dennis Ritchie in 1972 at AT & T’S Bell Telephone Laboratories. Dennis Ritchie
wanted to create an operating system named Unix Operating System. to develop
it, the C language was developed.
The special thing is that we can do low-level programming with the help of C
language, due to this feature, C programming language is used to make system
software such as Operating system, Device Driver, Compiler, etc.

Features of C Language
1. C is a simple and easy programming language.
2. There are commands/instructions like English in C language which are very
easy for a programmer to read, understand, and code.
3. C is a Procedure Oriented Programming Language.
4. C is a very powerful and case sensitive programming language.
5. C Language is a Compiler based dynamic programming language.
6. C language is a middle level language due to which both low level and high
level programming can be done.
7. The C language is the most commonly used programming language in
developing operating systems and embedded systems.
8. C language is a very portable and powerful programming language.
9. C language is a Syntax Based Language.
10. C is a general purpose programming language that covers basic
features of all other languages.

1. Libraries with Rich Functions


DELL 1
Robust libraries and functions in C help even a beginner coder to code with
ease.
2. Middle-Level Language
As it is a middle-level language so it has the combined form of both capabilities
of assembly language and features of the high-level language.
3. Portability
C language is lavishly portable as programs that are written in C language can
run and compile on any system with either no or small changes.
4. Easy to Extend
Programs written in C language can be extended means when a program is
already written in it then some more features and operations can be added to it.
5.Rich set of built-in Operators
It is a diversified language with a rich set of built-in operators which are used in
writing complex or simplified C programs.

Why is C so important?
If C language is your first programming language, then believe me you will
understand programming quite well because C language Is very helpful in
building our programming skills, as well as C language teaches us to understand
the logic.

some applications made with C language -:


Oracle and MySql is a software for database management that is built-in C
Language.
Almost all the device drivers are made in C language. The device driver is a tool
through which the content of your pen drive is read.
Today’s Hot Technology Android’s core library is also written in C language.
All the operating systems of today are also made in C language like – Unix
Operating System
Many parts of Web Browser are also written in C Language.

Applications of C Language
1. You can make a good operating system using C Language like- Windows,
Linux, Mac. because the major part of this operating system is written in C
language.
2. With the help of C Language you can create a Compiler which converts
programming language code into machine language.
3. With this, you can create a Text Editor.
4. Utility Software can be created.
5. Can create software with database management like – Oracle, Mysql, etc.
DELL 2
Basic Structure of C Program
Whenever we create a program in C language, we can divide that program into
six different sections. This section is as follows:

Documentation (Documentation Section)


Pre-processor Statements (Link Section)
Definition Section
Global Declarations Section
Main functions section
User-Defined Functions or Sub Program
Section
1. Documentation (Documentation Section)
Programmers write comments in the Documentation section to describe the
program. The compiler ignores the comments and does not print them on the
screen. Comments are used only to describe that program. That is
/* Name of the program */ this is called comment line
/* */ comment lline

Preprocessor Statements (Link Section)


Within the Link Section, we declare all the Header Files that are used in our
program. From the link section, we instruct the compiler to link those header files
from the system libraries, which we have declared in the link section in our
program.These can be followe by #(hash) character
Example -:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <math.h>

Definition Section
The definition of Symbolic Constant is defined in this section, so this section is
called Definition Section. Macros are used in this section.
Example -:
#define PI 3.14

4. Global Declarations Section


Within the Global Declarations Section section, we declare such variables which
we can use anywhere in our program, and that variable is called Global Variables,
we can use these variables in any function.
DELL 3
In the Global Declaration section, we also declare functions that we want to use
anywhere in our program, and such functions are called Global Function.
Example -:
int area (int x); //global function
int n; // global Variable

5. Main functions section


Whenever we create a program in C language, there is one main() function in that
program. The main () function starts with curly brackets and also ends with curly
brackets. In the main () function, we write our statements.
The code we write inside the main() function consists of two parts, one
Declaration Part and the other Execution Part. In the Declaration Part, we declare
the variables that we have to use in the Execution Part, let’s understand this with
an example.
Example -:
int main (void)
{
int n = 15; // Declaration Part
printf ("n = %d", n); // Execution Part
return (0);
}

6. User-Defined Functions or Sub Program Section


Declare all User-Defined Functions under this section.
Example -:
int sum (int x, int y)
{
return x + y;
}

/* Program to calculate Area of Circle */ /* comment line */


#include <stdio.h> /* pre-processor directives */
#define PI 3.14159 /* global declaration*/
void main() /* main */
{
float radius, area; /* declaration part */
printf("Enter the radius of the circle: "); /* execution part */
scanf("%f", &radius);
area = PI * radius * radius;
DELL 4
printf("The area of the circle is: %f", area);
return 0;
}

Creating, compilation and Executing C program


Open a text editor and type the above mentioned code
Save the files as circle
Open a command prompt and to the directory where you want to save the file.
Compile the program by using the keyboard. If there are errors, it displays lien
number and the type of error occurred.
If there are no errors in your code, then run the program to see the output on the
screen.

C Character set
As every language contains a set of characters used to construct words,
statements, etc., C language also has a set of characters which
include alphabets, digits, and special symbols. C language supports a total of
256 characters.

DELL 5
TOKENS
Every C program is a collection of instructions and every instruction is a collection
of some individual units. Every smallest individual unit of a c program is called
token. Every instruction in a c program is a collection of tokens. Tokens are used
to construct c programs and they are said to the basic building blocks of a c
program.

In a c program tokens may contain the following...


Keywords
Identifiers
Operators
Special Symbols
Constants
Strings
Data values
In a C program, a collection of all the keywords, identifiers, operators, special
symbols, constants, strings, and data values are called tokens.

DELL 6
Keywords are special words in C programming which have their own predefined
meaning.

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.
1. It must begin with an alphabet or an underscore and not digits.
2. It must contain only alphabets, digits or underscore.
3. A keyword cannot be used as an identifier
4. Must not contain white space.
5. Only first 31 characters are significant.
Let us again consider an example
int age1;
float height_in_feet; Here, age1 is an identifier of integer data type

DELL 7
Constant in C
Constant in C is defined as the value which cannot be modified during the
execution of the program. The constants in C are used for representing the fixed
values which do not change.

Integer Constants
An integer constant is a sequence of digits from 0 to 9 without decimal points
or fractional part or any other symbols. There are 3 types of integers namely
decimal integer, octal integers and hexadecimal integer.

Decimal Integers consists of a set of digits 0 to 9 preceded by an optional + or –


sign. Spaces, commas and non digit characters are not permitted between digits.
Example for valid decimal

Integer constants are


1 int y = 123; //here 123 is a decimal integer constant
Octal Integers constant consists of any combination of digits from 0 through 7
with a O at the beginning.
int x = 0123; // here 0123 is a octal integer constant .
Hexadecimal integer constant is preceded by OX or Ox, they may contain
alphabets from A to F or a to f. The alphabets A to F refers to 10 to 15 in decimal
digits. Example of valid hexadecimal integers are
int x = 0x12 // here Ox12 is a Hexa-Decimal integer constant
1

RealConstants
Real Constants consists of a fractional part in their representation. Integer
constants are inadequate to represent quantities that vary continuously. These
quantities are represented by numbers containing fractional parts like
26.082. Example of real constants are
float x = 6.3; //here 6.3 is a double constant.

DELL 8
float y = 6.3f

SingleCharacterConstants
A Single Character constant represent a single character which is enclosed in a
pair of quotation symbols.
Example for character constants are
char y ='u';

String Constants
A string constant is a set of characters enclosed in double quotation marks. The
characters in a string constant sequence may be a alphabet, number, special
character and blank space. Example of string constants are
1 "VISHAL" "1234" "God Bless""!.....?"

DECLARING A CONSTANT
there are two ways to declare this constant
1.By using the const keyword in a variable declaration which will reserve a
storage memory
#include <stdio.h>
void main()
{
const double PI = 3.14;
printf("%f", PI);
/*PI++; This will generate an error as constants
cannot be changed */
}

2. By using the #define pre-processor directive which doesn't use memory for
storage and without putting a semicolon character at the end of that statement
#include <stdio.h>
#define PI 3.14
void main()
{
printf("%f", PI);
}

DELL 9
C Variables

Declaration and Initialization?


Declaration of a variable in a computer programming language is a statement
used to specify the variable name and its data type. Declaration tells the compiler
about the existence of an entity in the program and its location. When you declare
a variable, you should also initialize it.
1. Initialization is the process of assigning a value to the Variable.
Every programming language has its own method of initializing the variable.
2. If the value is not assigned to the Variable, then the process is only called a
Declaration.
Basic Syntax
The basic form of declaring a variable is:
type identifier [= value] [, identifier [= value]]…];

DELL 10
OR
data_type variable_name = value;
where,
type = Data type of the variable
identifier = Variable name
value = Data to be stored in the variable (Optional field)
int a, b, c; // declare 3 variables.

int z = 35; // declare and initialize variable z with value 35.

Data Type is the classification of the data that is taken as input, processed,
and results in an output
Each variable in C has an associated data type. It specifies the type of data that
the variable can store like integer, character, floating, double, etc

The following are some main primitive data types in C:


1. Integer Data Type
The integer datatype in C is used to store the integer numbers(any number
including positive, negative and zero without decimal part). Octal values,
hexadecimal values, and decimal values can be stored in int data type in C.
Range: -2,147,483,648 to 2,147,483,647
Size: 4 bytes, Format Specifier: %d
Syntax is int var_name; Ex.int a = 9;

Character Data Type

DELL 11
Character data type allows its variable to store only a single character. The size
of the character is 1 byte. It is the most basic data type in C. It stores a single
character and requires a single byte of memory in almost all compilers.
Range: (-128 to 127) or (0 to 255)
Size: 1 byte, Format Specifier: %c
char a = 'a';
Syntax of char char
var_name;
The char keyword is used to declare the variable of character type:

Float Data Type


In C programming float data type is used to store floating-point values. Float in C
is used to store decimal and exponential values. It is used to store decimal
numbers (numbers with floating point values) with single precision.
Range: 1.2E-38 to 3.4E+38
Size: 4 bytes
Format Specifier: %f
Syntax of float float var_name; Eg: float a = 9.0f; float b = 2.5f;
The float keyword is used to declare the variable as a floating point:

Input and Output Functions in C


Input and output functions are available in the C language to perform the most
common tasks. In every C program, three basic functions take place – accepting
of data as input, the processing of data, and the generation of output.
Input means to provide the program with some data to be used in it.
Output means to display data on the screen or write the data to a printer or a file.

The acceptance of data refers to input


The presentation of data refers to the output

DELL 12
1. The printf() function
The printf() function is the most used function in the C language.
This function is defined in the stdio.h header file and is used to show output on
the console (standard output).
Following is how the printf() function is defined in the C stdio.h library.
These functions are called formatted I/O functions because we can use format
specifiers in these functions and hence, we can format these functions according
to our needs
Formatted I/O functions are used to take various inputs from the user and
display multiple outputs to the user

Format Specifiers
To print values of different data types using the printf() statement and while
taking input using the scanf() function, it is mandatory to use format specifiers.
It is a way to tell the compiler what type of data is in a variable.
Some examples are %c, %d, %f, etc.
Example
printf("%d", a + b);
Rules for declaring formatted input function
2. Control string should be enclosed within double quotation marks
3. Every input variable should have individual character group

DELL 13
4. Every character group should begin with a % symbol
5. More than one character group can be enclosed within a single control
string
6. Every input variable should being with a & symbol
7. Arguments are separated by commas.
8. Comma operator separates the control string and the arguments

Here's a look at the format specifiers used by functions in a program:


Format Specifier Datatype
%d, %i int
%c char
%lf double
%f float
%hd short int
%li long int
%u unsigned int
%lu unsigned long int
%lli long long int
%llu unsigned long long int
%Lf long double
%c signed char
%c unsigned char

2. The scanf() function


When we want to take input from the user, we use the scanf() function and store
the input value into a variable.
Following is how the scanf() function is defined in the C stdio.h library.
Here is the syntax for scanf():
scanf("%x", &variable);
Copy
where, %x is the format specifier.
Using the format specifier, we tell the compiler what type of data to expect from
the user.
The & is the address operator which tells the compiler the address of the variable
so that the compiler can store the user input value at that address.

Unformatted Input/Output functions

DELL 14
Unformatted I/O functions are used only for character data type or character
array/string and cannot be used for any other datatype. These functions are
used to read single input from the user at the console and it allows to display
the value at the console.

3. getchar() & putchar() functions


The getchar and putchar functions are used for taking character input from the
user and printing the character as output.
The getchar() function
The getchar() function reads a character from the terminal and returns it as
an integer.
This function reads only a single character at a time.
Here is the syntax for the getchar() function:
int getchar(void);
Copy
You can use this method in a loop if you want to read more than one character.
The putchar() function
The putchar() function displays the character passed to it on the screen and
returns the same character.
This function too displays only a single character at a time.
Here is the syntax for the putchar() function:
4. gets() & puts() functions
The gets and puts functions are used for taking string input and giving string
output.
The gets() function
The gets() function reads a line of text from stdin(standard input) into the buffer
pointed to by str pointer, until either a terminating newline or EOF (end of file)
occurs.
Here is the syntax for the gets() function:
char* gets(char* str);
Copy
The puts() function
The puts() function writes the string str with a newline character ('\n') at the end
to stdout. On success, a non-negative value is returned.
Here is the syntax for the gets() function:
int puts(const char* str);

DELL 15
Operators in C
C OPERATORS AND EXPRESSION
The operators are types of symbols that inform a compiler for performing some
specific logical or mathematical functions

Expressions perform specific actions, based on an operator, with one or two


operands. An operand can be a constant, a variable or a function result.

Unary arithmetic operators


Unary operators are arithmetic operators that perform an action on a single
operand. The script language recognizes the unary operator negative (-).
EX: A=-10;

Unary operators perform an action with a single operand.


Increment and Decrement Operators in C

Increment Operator in C
The increment operator ( ++ ) is used to increment the value of a variable in an
expression by 1. It can be used on variables of the numeric type such as integer,
float, character, pointers, etc.

DELL 16
Syntax of Increment Operator
Increment Operator can be used in two ways which are as follows:
// AS PREFIX
++m
// AS POSTFIX
m++
1. Pre-Increment
In pre-increment, the increment operator is used as the prefix. Also known as
prefix increment, the value is incremented first according to the precedence and
then the less priority operations are done.
Example
result = ++var1;
The above expression can be expanded as
var = var + 1;
result = var;

2. Post-Increment
In post-increment, the increment operator is used as the suffix of the operand.
The increment operation is performed after all the other operations are done. It
is also known as postfix increment.
Example
result = var1++;
The above expression is equivalent
result = var;
var = var + 1;

Decrement Operator in C
The decrement operator is used to decrement the value of a variable in an
expression. In the Pre-Decrement, the value is first decremented and then used
inside the expression. Whereas in the Post-Decrement, the value is first used
inside the expression and then decremented.
Syntax
Just like the increment operator, the decrement operator can also be used in two
ways:
// AS PREFIX
--m
// AS POSTFIX
m--

where m is variable.
1. Pre-Decrement Operator
DELL 17
The pre-decrement operator decreases the value of the variable immediately
when encountered. It is also known as prefix decrement as the decrement
operator is used as the prefix of the operand.
Example
result = --m;

which can be expanded to


m = m - 1;
result = m;

2. Post-Decrement Operator
The post-decrement happens when the decrement operator is used as the suffix
of the variable. In this case, the decrement operation is performed after all the
other operators are evaluated.
Example
result = m--;

The above expression can be expanded as


result = m;
m = m-1;

EG :int a = 5; int b = 5;
int prefix = --a; int postfix = b--;
Ans: Prefix = 4 Postfix = 5

Binary operators perform actions with two operands


Arithmetic operators ( +, -, *, /, **, % )
Arithmetic operators perform mathematical operations such as addition and
subtraction with operands
Operator precedence
Expressions are normally evaluated left to right. Complex expressions are
evaluated one at a time. The order in which the expressions are evaluated is
determined by the precedence of the operators used
The following tables list the C operator precedence from highest to lowest and
the associativity for each of the operators:

DELL 18
Operator

Precedence Description Associativity

() Parentheses (function call)

[] Array Subscript (Square Brackets)

1 . Dot Operator Left-to-Right

-> Structure Pointer Operator

++ , — Postfix increment, decrement

++ / — Prefix increment, decrement

+/– Unary plus, minus

!,~ Logical NOT, Bitwise complement

2 (type) Cast Operator Right-to-Left

* Dereference Operator

& Addressof Operator

sizeof Determine size in bytes

3 *,/,% Multiplication, division, modulus Left-to-Right

4 +/- Addition, subtraction Left-to-Right

5 << , >> Bitwise shift left, Bitwise shift right Left-to-Right

DELL 19
Operator

Precedence Description Associativity

Relational less than, less than or


< , <=
equal to
6 Left-to-Right
Relational greater than, greater
> , >=
than or equal to

Relational is equal to, is not equal


7 == , != Left-to-Right
to

8 & Bitwise AND Left-to-Right

9 ^ Bitwise exclusive OR Left-to-Right

10 | Bitwise inclusive OR Left-to-Right

11 && Logical AND Left-to-Right

12 || Logical OR Left-to-Right

13 ?: Ternary conditional Right-to-Left

= Assignment
14

Example of Operator Precedence and Associativity


exp = 100 + 200 / 10 - 3 * 10
Here, we have four operators, in which the / and * operators have the same
precedence but have higher precedence than the + and – operators. So,
according to the Left-to-Right associativity of / and *, / will be evaluated first.
exp = 100 + (200 / 10) - 3 * 10
= 100 + 20 - 3 * 10
DELL 20
After that, * will be evaluated,
exp = 100 + 20 - (3 * 10)
= 100 + 20 - 30
Now, between + and –, + will be evaluated due to Left-to-Right associativity.
exp = (100 + 20) - 30
= 120 - 30
At last, – will be evaluated.
exp = 120 - 30
= 90

Assignment Operator (= )
Use the assignment operator (=) to copy a constant, literal, variable expression
result, or function result to a variable. The script language does not support
multiple assignments in a single statement (such as a=b=c=0). String lengths
are defined based on the size of the string assigned to the variable and can
change dynamically at runtime.

Logical Operators (AND, OR)


Logical operators allow the combining of more than one relational test in one
comparison. Logical operators return a TRUE (1) or FALSE (0) value. Logical
operators have a lower precedence than arithmetic operators.

Symb
Operation Example Description
ol
AND True if both expr1 and expr2 are
AND Expr1 $$ expr2
&& true.
OR || OR Expr1 OR expr2 True if either expr1 or expr2 are true.
Table 2. Logical Operators

Relational Operators
Relational operators are as follows:
SymbolOperation Example Description
< Less than a<b True if a is less than b.
> Greater than a GT b True if a is greater than b.
== Equal a == b True if a is equal to b.
!= Not equal a NE b True if a is not equal to b.
Less than or
<= a <= b True if a is less than or equal to b.
equal
>= Greater than ora GE b True if a is greater than or equal to b.

DELL 21
SymbolOperation Example Description
equal
Table 3. Relational Operators

The conditional operator in C


The ternary operator in C is a conditional operator that works on three operands.
It works similarly to the if-else statement and executes the code based on the
specified condition. It is also called conditional Operator. It is also known as
the ternary operator in C as it operates on three operands.
Syntax of Conditional/Ternary Operator in C
The conditional operator can be in the form
variable = Expression1 ? Expression2 : Expression3;
Or the syntax can also be in this form
variable = (condition) ? Expression2 : Expression3;

DELL 22
DELL 23

You might also like