KEMBAR78
Unit 1 Notes | PDF | Variable (Computer Science) | Integer (Computer Science)
0% found this document useful (0 votes)
35 views14 pages

Unit 1 Notes

Notes

Uploaded by

prasads4342
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)
35 views14 pages

Unit 1 Notes

Notes

Uploaded by

prasads4342
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/ 14

Unit 1

Introduction to Program Planning & C Programming

Overview of C :

1.1 History of C
C is a general-purpose programming language created by Dennis Ritchie at the AT&T
(American Telephone & Telegraph) Laboratories in 1972. It is a very popular language,
despite being old. The main reason for its popularity is because it is a fundamental language
in the field of computer science.
Dennis Ritchie is known as the founder of the c language.
It was developed to overcome the problems of previous languages such as B, BCPL, etc.

Language Year Developed By

Algol 1960 International Group

BCPL 1967 Martin Richard

B 1970 Ken Thompson

Traditional C 1972 Dennis Ritchie

K&RC 1978 Kernighan & Dennis Ritchie

ANSI C 1989 ANSI Committee

ANSI/ISO C 1990 ISO Committee

C99 1999 Standardization Committee

1.2 Importance of C

● It is one of the most popular programming languages in the world


● If you know C, you will have no problem learning other popular programming
languages such as Java, Python, C++, C#, etc, as the syntax is similar
● C is very fast, compared to other programming languages, like Java and Python due to
its
● C is very versatile; it can be used in both applications and technologies
● It is robust language having rich set of built in functions and operators to write any
complex program.
● C is highly portable. This means that C program written for one computer can be run
on another with little or no modifications.
● C is well suited for structured programming. A problem is divided into no of
functions and modules.
● C is having ability to extend itself. It is having large set of built in functions that are
supported by C library.

1.3 Character set of C


In C programming language, a character set is defined as a set of characters that can be used
to write the source code.
In C, a character set refers to a predefined collection of characters recognized by the
language. This set typically includes the alphabet (both lowercase and uppercase), digits,
special symbols, and control characters. The most commonly used character set in C is ASCII
(American Standard Code for Information Interchange), which assigns numeric codes to
characters. These codes are used to represent characters in memory and facilitate operations
involving characters, such as input/output operations, string manipulation, and comparison.
ASCII value Context

32 space

48-57 0-9

65-90 A-Z

97-122 a-z

C programming language supports four types of characters:


● Alphabets
● Digits
● Special characters
● White space
Alphabets
The C programming language allows for all upper- and lower-case alphabets in its programs.
Lower Case alphabets: a-z (26 characters)
Upper case alphabets: A-Z (26 characters)
Digits
The C programming language also supports digits for building numeric expressions.
Digits: 0-9 (10 characters)
Special Characters
Certain special characters are used in C programs in arithmetic expressions, punctuation
marks, conditional operators, etc. These characters can also be used in defining the attributes
of a program in a better way.
Special Characters: ` ~ @ ! $ # ^ * % & ( ) [ ] { } < > + = _ – | / \ ; : ‘ “ , . ?
White spaces
The white space characters in C refer to vertical and horizontal spaces in the source code.
White spaces: Blank Spaces, Tab, Carriage Return, New Line, horizontal tab, vertical tab,
null

In C programming, special characters are often used to represent control characters or to


format output. Here are some commonly used special characters:
1. \n: Represents a newline character.
2. \t: Represents a tab character.
3. \b: Represents a backspace character.
4. \r: Represents a carriage return character.
5. \: Represents a backslash character.
6. ': Represents a single quote character.
7. ": Represents a double quote character.

1.4 C Tokens:

Tokens in C is the most important element to be used in creating a program in C. We can


define the token as the smallest individual element in C. For `example, we cannot create a
sentence without using words; similarly, we cannot create a program in C without using
tokens in C. Therefore, we can say that tokens in C is the building block or the basic
component for creating a program in C language.

Classification of tokens in C

Tokens in C language can be divided into the following categories:


Keywords:float,while.
Identifier:amount,num.
Constants:100 -15.5
Strings:”ABC”

1.6 Keywords and Identifiers:


Keywords are specific reserved words in C.
Each of which has a specific feature associated with it.
Almost all of the words which help us use the functionality of the C language are included in
the list of keywords.
There are a total of 32 keywords in C:
auto break case char const continue
default do double else enum extern
float for goto if int long
register return short signed sizeof static
struct switch typedef union unsigned void
volatile while

int money;

Here, int is a keyword that indicates money is a variable of type int (integer).

1.7 C Identifiers
Identifier refers to name given to entities such as variables, functions, structures etc.

Identifiers must be unique.

They are created to give a unique name to an entity to identify it during the execution of the
program.

For example:

int money;

double accountBalance;

Here, money and accountBalance are identifiers.


Also remember, identifier names must be different from keywords.
You cannot use int as an identifier because int is a keyword.
Rules for naming identifiers
1. A valid identifier can have letters (both uppercase and lowercase letters), digits and
underscores.
2. The first letter of an identifier should be either a letter or an underscore.
3. You cannot use keywords like int, while etc. as identifiers.
4. There is no rule on how long an identifier can be. However, you may run into problems in
some compilers if the identifier is longer than 31 characters.

R.
NO. KEYWORD IDENTIFIER

Keywords are predefined word that Identifiers are used to define different
gets reserved for working program programming items such as variables,
1
that have special meaning and cannot integers, structures, unions and others and
get used anywhere else. mostly have an alphabetic character.

2 Specify the type/kind of entity. Identify the name of a particular entity.

An identifier can be in upper case or lower


3 A keyword should be in lower case.
case.

A keyword contains only An identifier can consist of alphabetical


4
alphabetical characters. characters, digits and underscores.

No special symbol, punctuation is No punctuation or special symbol except


5
used. ‘underscore’ is used.

Examples of keywords are: int, char, Examples of identifiers are: Test, count1,
6
if, while, do, class etc. high_speed, etc.
1.8 Constants:
The constants in C are the read-only variables whose values cannot be modified once they are
declared in the C program. The type of constant can be an integer constant, a floating pointer
constant, a string constant, or a character constant. In C language, the const keyword is used
to define the constants.
Syntax to Define Constant
const data_type var_name = value;

#include <stdio.h>
int main()
{
const int int_const = 25;
const char char_const = 'A';
const float float_const = 15.66;
printf("Printing value of Integer Constant: %d\n",int_const);
printf("Printing value of Character Constant: %c\n",char_const);
printf("Printing value of Float Constant: %f",float_const);
return 0;
}
Integer Constants:
It refers to a sequence of digit.
1.integer(123,-321,0)
2.decimal(037,0,0435)
3.hexadecimal(0X2 0x9F)
Real Constants:
mantissa e exponent
it refers to numbers having fractional part.
e.g 0.0083,435.56
character constants:
A single character constant contains a single character.
e.g ’5’ ‘x’ ‘;’
string constants:
A string constant is a sequence of characters enclosed in double quotes.
e.g.”Hello”,”1987”

1.9 Variable:

Variables are containers for storing data values, like numbers and characters.

In C, there are different types of variables (defined with different keywords), for example:

● int - stores integers (whole numbers), without decimals, such as 123 or -123
● float - stores floating point numbers, with decimals, such as 19.99 or -19.99
● char - stores single characters, such as 'a' or 'B'. Characters are surrounded by single
quotes

Syntax
type variableName = value;
int myNum = 15;
int myNum;
myNum = 15;
Example:
int myNum = 15;
printf(myNum);

1.10 Data Types:


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

Types Description

Primitive Data Primitive data types are the most basic data types that are used for
Types representing simple values such as integers, float, characters, etc.

User Defined
The user-defined data types are defined by the user himself.
Data Types

The data types that are derived from the primitive or built-in
Derived Types
datatypes are referred to as Derived Data Types.

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 of Integer: int var_name;
Types of integer Data type:
short int
int
long int
Floating Data Types:
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;

Void Data Type

The void data type in C is used to specify that no value is present. It does not provide a result
value to its caller. It has no values and no operations. It is used to represent nothing.
Character Data Type
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
Syntax of char
char var_name;

1.11 Declaration of variables:


C Variable Declaration
Variable declaration in C tells the compiler about the existence of the variable with the given
name and data type.
Syntax:
data_type variable_name; // defining single variable
or
data_type variable_name1,variable_name2;// defining multiple variable

1.12 Storage Classes in C


Storage classes in C are used to determine the lifetime, visibility, memory location, and initial
value of a variable. There are four types of storage classes in C
o Automatic
o External
o Static
o Register

Automatic
o Automatic variables are allocated memory automatically at runtime.
o The visibility of the automatic variables is limited to the block in which they are
defined.

The scope of the automatic variables is limited to the block in which they are defined.
o The automatic variables are initialized to garbage by default.
o The memory assigned to automatic variables gets freed upon exiting from the block.
o The keyword used for defining automatic variables is auto.
o Every local variable is automatic in C by default.

e.g
#include <stdio.h>
int main()
{
auto int a; //or int a
char b;
float c;
printf("%d %c %f",a,b,c);
return 0;
}
Static
o The variables defined as static specifier can hold their value between the multiple
function calls.
o Static local variables are visible only to the function or the block in which they are
defined.
o A same static variable can be declared many times but can be assigned at only one
time.
o Default initial value of the static integral variable is 0 otherwise null.
o The visibility of the static global variable is limited to the file in which it has declared.
o The keyword used to define static variable is static.

#include<stdio.h>
int main()
{
static int a;
static int b = 24;
printf("%d \n %d \n",a,b);
return 0;
}
Register
o The variables defined as the register is allocated the memory into the CPU registers
depending upon the size of the memory remaining in the CPU.
o We can not dereference the register variables, i.e., we can not use &operator for the
register variable.
o The access time of the register variables is faster than the automatic variables.
o The initial default value of the register local variables is 0.
o The register keyword is used for the variable which should be stored in the CPU
register. However, it is compiler?s choice whether or not; the variables can be stored
in the register.
o We can store pointers into the register, i.e., a register can store the address of a
variable.
o Static variables can not be stored into the register since we can not use more than one
storage specifier for the same variable.

1. #include <stdio.h>
2. int main()
3. {
4. register int a; // variable a is allocated memory in the CPU register. The initial default value
of a is 0.
5. printf("a=%d",a);
6. }

Output:a=0
External
o The external storage class is used to tell the compiler that the variable defined as
extern is declared with an external linkage elsewhere in the program.
o The variables declared as extern are not allocated any memory. It is only declaration
and intended to specify that the variable is declared elsewhere in the program.
o The default initial value of external integral type is 0 otherwise null.
o We can only initialize the extern variable globally, i.e., we can not initialize the
external variable within any block or method.
o An external variable can be declared many times but can be initialized at only once.
o If a variable is declared as external then the compiler searches for that variable to be
initialized somewhere in the program which may be extern or static. If it is not, then
the compiler will show an error.

Example 1
1. #include <stdio.h>
2. int main()
3. {
4. extern int a;
5. printf("%d",a);
6. }

1.13 Assigning values to Variables

To create a variable, specify the type and assign it a value:

Syntax
type variableName = value;

Where type is one of C types (such as int), and variableName is the name of the variable
(such as x or myName). The equal sign is used to assign a value to the variable.

So, to create a variable that should store a number, look at the following example:

Example

Create a variable called myNum of type int and assign the value 15 to it:

int myNum = 15;

You can also declare a variable without assigning the value, and assign the value later:

Example
/int myNum;
myNum = 15;
1.14 Symbolic Constants in C
Identifiers are used to represent fixed values in programs using symbolic constants in the C
programming language.
These parameters are frequently used to increase the readability, maintainability,
and modifiability of code, which may be numerical or not.
The C language's "#define" command is used to declare symbolic constants.
Syntax:
Syntax for defining a symbolic constant in C
#define MAX_V 100
In this example, the symbolic constant "MAX_V" is specified to have a value of 100. It
indicates that the number 100 will be substituted for "MAX_V" wherever it appears in the
code at compilation time.

Variables can be declared as constant using the const keyword or the #define preprocessor
directive. Details about these are given as follows.

The const keyword

Variables can be declared as constants by using the “const” keyword before the datatype of
the variable. The constant variables can be initialized once only. The default value of constant
variables are zero.

A program that demonstrates the declaration of constant variables in C using const keyword
is given as follows.

#include<stdio.h>
int main()
{
const int a;
const int b = 12;
printf("The default value of variable a : %d",a);
printf("The value of variable b : %d",b);
return 0;
}
The #define preprocessor directive

Variables can be declared as constants by using the #define preprocessor directive as it


declares an alias for any value.

A program that demonstrates the declaration of constant variables in C using #define


preprocessor directive is given as follows.
#include<stdio.h>
#define num 25
int main()
{
printf("The value of num is: %d",num);
return 0;
}
1.15 Volatile Keyword in C
In C, the volatile keyword is used to indicate to the compiler that a variable's value may
change unexpectedly, so it should not rely on the value being cached in a register or
optimized away.
When a variable is declared as volatile, the compiler must generate code to read and write the
variable's value from memory each time it is used, rather than relying on a cached value in a
register. It is because the value of a volatile variable may be changed by external factors, such
as hardware or other threads running concurrently.
Example: volatile int x = 0; // declare a volatile integer variable

You might also like