Important C Language Notes

Language

A language is the main medium of communicating between the Computer systems and the most common are the programming languages.

Computer Language

A Computer language includes various languages that are used to communicate with a Computer machine. Some of the languages like programming language which is a set of codes or instructions used for communicating the machine.

Introduction to Programming Languages

A computer is a computational device which is used to process the data under the control of a computer program. Program is a sequence of instruction along with data. While executing the program, raw data is processed into a desired output format. These computer programs are written in a programming language which are high level languages. High level languages are nearly human languages which are more complex than the computer understandable language which are called machine language, or low level language.

Between high-level language and machine language there are assembly language also called symbolic machine code. Assembly language are particularly computer architecture specific. Utility program (Assembler) is used to convert assembly code into executable machine code. High Level Programming Language are portable but require Interpretation or compiling to convert it into a machine language which is computer understood.

Characteristics of a programming Language

  • A programming language must be simple, easy to learn and use, have good readability and human recognizable.
  • Abstraction is a must-have Characteristics for a programming language in which ability to define the complex structure and then its degree of usability comes.
  • A portable programming language is always preferred.
  • Programming language’s efficiency must be high so that it can be easily converted into a machine code and executed consumes little space in memory.
  • A programming language should be well structured and documented so that it is suitable for application development.
  • Necessary tools for development, debugging, testing, maintenance of a program must be provided by a programming language.
  • A programming language should provide single environment known as Integrated Development Environment(IDE).
  • A programming language must be consistent in terms of syntax and semantics


Classification of Computer Programming Languages

  1. Low Level Language
    • Machine/ Binary Language
    • Assembly Language
  2. High Level Language


Machine/ Binary Language-:-(First Generation) Machine Language is one of the low-level programming languages which is the first generation language developed for communicating with a Computer. It is written in machine code which represents 0 and 1 binary digits inside the Computer string which makes it easy to understand and perform the operations. As we know a Computer system can recognize electric signals so here 0 stands for turning off electric pulse and 1 stands for turning on electric pulse. It is very easy to understand by the Computer and also increases the processing speed.

Assembly Language-: (Second Generation) Assembly Language is the second generation programming language that has almost similar structure and set of commands as Machine language. Instead of using numbers like in Machine languages here we use words or names in English forms and also symbols. The programs that have been written using words, names and symbols in assembly language are converted to machine language using an Assembler.

High Level Language-: (Third Generation) The high level languages are the most used and also more considered programming languages that helps a programmer to read, write and maintain. It is also the third generation language that is used and also running till now by many programmers. They are less independent to a particular type of Computer and also require a translator that can convert the high level language to machine language. The translator may be an interpreter and Compiler that helps to convert into binary code for a Computer to understand. There are various high level programming languages like C, FORTRAN or Pascal that are less independent and also enables the programmer to write a program

Compiler

A compiler is a software program that transforms high-level source code that is written by a developer in a high-level programming language into a low level object code (binary code) in machine language, which can be understood by the processor. The process of converting high-level programming into machine language is known as compilation
A compiler that converts machine language into high-level natural language is called a decompiler. Compilers that produce the object code meant to run on a system are called cross-compilers. Finally, a compiler that converts one programming language into another is called a language translator.

FORTRAN

In 1957, the first of the major languages appeared in the form of FORTRAN. Its name stands for FORmula TRANslating system. The language was designed at IBM (International Business Machine) for scientific computing.

Algol (1950-1960)



C Programming Language

C is a high-level and general-purpose programming language that is ideal for developing firmware (chipset level / low level software) or portable applications. Originally intended for writing system software, C was developed at Bell Labs(USA) by Dennis Ritchie for the Unix Operating System in the early 1970s (1971-73).

Variable

When we want to use some data value in our program, we can store it in a memory space and name the memory space so that it becomes easier to access it.
The naming of an address is known as variable. Variable is the name of memory location.

Rules to name a Variable-

  1. Variable name must not start with a digit.
  2. Variable name can consist of alphabets, digits and special symbols like underscore _ .
  3. Blank or spaces are not allowed in variable name.
  4. Upper and lower case names are treated as different, as C is case-sensitive, so it is suggested to keep the variable names in lower case.


Data types in C Language

Data types specify how we enter data into our programs and what type of data we enter. C language has some predefined set of data types to handle various kinds of data that we can use in our program. These datatypes have different storage capacities.

C language supports 2 different type of data types:

  1. Primary data types:
  2. These are fundamental data types in C namely integer(int), floating point(float), character(char) and void.
  3. Derived data types:
  4. Derived data types are nothing but primary datatypes but a little twisted or grouped together like array, structure, union and pointer.


Data type determines the type of data a variable will hold. If a variable x is declared as int. it means x can hold only integer values. Every variable which is used in the program must be declared as what data-type it is.

Primary Data types

  1. Integer type
  2. Integers are used to store whole numbers
  3. Floating point type
  4. Floating types are used to store real numbers(decimal).
  5. Character type
  6. Character types are used to store characters’ value (alphabets and symbol).
  7. void type
  8. void type means no value. This is usually used to specify the type of functions which returns nothing. We will get acquainted to this datatype as we start learning more advanced topics in C language, like functions, pointers etc.


Derived data types

  1. Array
  2. An array is a collection of data items, all of the same type, accessed using a common name.
  3. Structure
  4. a struct (or structure) is a collection of variables (can be of different types) under a single name.
  5. Union
  6. union is a user defined data type. In union, all members share the same memory location.
  7. Pointer
  8. A pointer is a variable that stores the address of another variable


Signed and Unsigned Keywords



Signed Keyword -Keyword signed is used for those variables which can take all positive, negative and zero values.
Unsigned Keyword -Keyword unsigned is used for those variables which can take only values which are zero or positive.

Various Data Types Size

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 bytes -32,768 to 32,767
long int 4 bytes -2,147,483,648 to 2,147,483,647
unsigned int 2 bytes 0 to 65,535
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 8 bytes -9223372036854775808 to 9223372036854775807
unsigned long 8 bytes 0 to 18446744073709551615
float 4 bytes 7 decimal digits
double 8 bytes 16 decimal digits


Operator

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/conditional/compression
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators


Decision Making Statement

Decision making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
  • if statement
  • An if statement consists of a boolean expression followed by one or more statementst
  • if...else statement
  • An if statement can be followed by an optional else statement, which executes when the Boolean expression is false
  • nested if statements
  • You can use one if or else if statement inside another if or else if statement(s).
  • switch statement
  • A switch statement allows a variable to be tested for equality against a list of values.
  • nested switch statements
  • You can use one switch statement inside another switch statement(s).

Loops

Loops in programming come into use when we need to repeatedly execute a block of statements. For example: Suppose we want to print “Hello World” 10 times.
This can be done in two ways as shown below:
  1. while loop
  2. do while loop
  3. for loop


Arrays in C Programming

An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number.

Advantage of Array

  1. Code Optimization:
  2. Less code to the access the data.
  3. Ease of traversing:
  4. By using the for loop, we can retrieve the elements of an array easily.
  5. Ease of sorting:
  6. To sort the elements of the array, we need a few lines of code only.
  7. Random Access:
  8. We can access any element randomly using the array.

Disadvantage of Array

Fixed Size: Whatever size, we define at the time of declaration of the array, we can't exceed the limit. So, it doesn't grow the size dynamically like LinkedList which we will learn later

Tyeps of Array

  1. One dimensional array:
    • Single or One Dimensional array is used to represent and store data in a linear form.
    • Array having only one subscript variable is called One-Dimensional array
    • It is also called as Single Dimensional Array or Linear Array
  2. Two dimensional array:
    • Array having more than one subscript variable is called Multi-dimensional array.
    • Multi Dimensional Array is also called as Matrix.


Pointer

A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable addres

How to Use Pointers ?

There are a few important operations, which we will do with the help of pointers very frequently.
(a) We define a pointer variable,
(b) assign the address of a variable to a pointer and
(c) finally access the value at the address available in the pointer variable.
This is done by using unary operator * that returns the value of the variable located at the address specified by its operand