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

Unit 1 Notes-2

The document provides a history of the C programming language. It states that C was developed in 1972 by Dennis Ritchie at Bell Labs to be used for the UNIX operating system. It inherited features from earlier languages like B and BCPL but was designed to overcome limitations of those languages. C is a widely used, portable, mid-level language that provides features like memory management, pointers, and speed.

Uploaded by

madhan
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)
59 views

Unit 1 Notes-2

The document provides a history of the C programming language. It states that C was developed in 1972 by Dennis Ritchie at Bell Labs to be used for the UNIX operating system. It inherited features from earlier languages like B and BCPL but was designed to overcome limitations of those languages. C is a widely used, portable, mid-level language that provides features like memory management, pointers, and speed.

Uploaded by

madhan
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/ 30

History of C Language

History of C language is interesting to know. Here we are going to


discuss a brief history of the c language.

C programming language was developed in 1972 by Dennis Ritchie at


bell laboratories of AT&T (American Telephone & Telegraph), located in
the U.S.A.

Dennis Ritchie is known as the founder of the c language.

t was developed to overcome the problems of previous languages such as


B, BCPL, etc.

Initially, C language was developed to be used in UNIX operating


system. It inherits many features of previous languages such as B and
BCPL.

Let's see the programming languages that were developed before C


language.

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

Features of C Language

C is the widely used language. It provides many features that are given


below.

1. Simple

2. Machine Independent or Portable

3. Mid-level programming language

4. structured programming language

5. Rich Library

6. Memory Management

7. Fast Speed

8. Pointers
9. Recursion

10. Extensible

1) Simple

C is a simple language in the sense that it provides a structured


approach (to break the problem into parts), the rich set of library
functions, data types, etc.

2) Machine Independent or Portable

Unlike assembly language, c programs can be executed on different


machines with some machine specific changes. Therefore, C is a machine
independent language.

3) Mid-level programming language

Although, C is intended to do low-level programming. It is used to


develop system applications such as kernel, driver, etc. It also supports
the features of a high-level language. That is why it is known as mid-
level language.

4) Structured programming language

C is a structured programming language in the sense that we can break


the program into parts using functions. So, it is easy to understand
and modify. Functions also provide code reusability.

5) Rich Library

C provides a lot of inbuilt functions that make the development fast.


6) Memory Management

It supports the feature of dynamic memory allocation. In C language,


we can free the allocated memory at any time by calling
the free() function.

7) Speed

The compilation and execution time of C language is fast since there are
lesser inbuilt functions and hence the lesser overhead.

8) Pointer

C provides the feature of pointers. We can directly interact with the


memory by using the pointers. We can use pointers for memory,
structures, functions, array, etc.

9) Recursion

In C, we can call the function within the function. It provides code


reusability for every function. Recursion enables us to use the approach of
backtracking.

10) Extensible

C language is extensible because it can easily adopt new features.

********************************************************
How to install C

There are many compilers available for c and c++. You need to download
any one. Here, we are going to use Turbo C++. It will work for both C
and C++. To install the Turbo C software, you need to follow following
steps.

1. Download Turbo C++

2. Create turboc directory inside c drive and extract the tc3.zip inside
c:\turboc

3. Double click on install.exe file

4. Click on the tc application file located inside c:\TC\BIN to write the c


program

1) Download Turbo C++ software

You can download turbo c++ from many sites. download Turbo c++

2) Create turboc directory in c drive and extract the tc3.zip

Now, you need to create a new directory turboc inside the c: drive. Now
extract the tc3.zip file in c:\truboc directory.

3) Double click on the install.exe file and follow steps

Now, click on the install icon located inside the c:\turboc


It will ask you to install c or not, press enter to install.

Change your drive to c, press c.


Press enter, it will look inside the c:\turboc directory for the required files.

Select Start installation by the down arrow key then press enter.
Now C is installed, press enter to read documentation or close the
software. 
4) Click on the tc application located inside c:\TC\BIN

Now double click on the tc icon located in c:\TC\BIN directory to write the
c program.

In windows 7 or window 8, it will show a dialog block to ignore and close


the application because fullscreen mode is not supported. Click on Ignore
button.

Now it will showing following console.


************************************************************************************************

First C Program

Before starting the abcd of C language, you need to learn how to write,
compile and run the first c program.

To write the first c program, open the C console and write the following
code:

1. #include <stdio.h>    
2. int main(){    
3. printf("Hello C Language");    
4. return 0;   
5. }  

#include <stdio.h> includes the standard input output library


functions. The printf() function is defined in stdio.h .
int main() The main() function is the entry point of every
program in c language.

printf() The printf() function is used to print data on the console.

return 0 The return 0 statement, returns execution status to the OS. The
0 value is used for successful execution and 1 for unsuccessful execution.

How to compile and run the c program

There are 2 ways to compile and run the c program, by menu and by
shortcut.

By menu

Now click on the compile menu then compile sub menu to compile


the c program.

Then click on the run menu then run sub menu to run the c program.

By shortcut

Or, press ctrl+f9 keys compile and run the program directly.

You will see the following output on user screen.


You can view the user screen any time by pressing the alt+f5 keys.

Now press Esc to return to the turbo c++ console.

*****************************************************

Compilation process in c

What is a compilation?

The compilation is a process of converting the source code into object


code. It is done with the help of the compiler. The compiler checks the
source code for the syntactical or structural errors, and if the source code
is error-free, then it generates the object code.
The c compilation process converts the source code taken as input into
the object code or machine code. The compilation process can be divided
into four steps, i.e., Pre-processing, Compiling, Assembling, and Linking.

The preprocessor takes the source code as an input, and it removes all
the comments from the source code. The preprocessor takes the
preprocessor directive and interprets it. For example, if <stdio.h>, the
directive is available in the program, then the preprocessor interprets the
directive and replace this directive with the content of the 'stdio.h' file.

The following are the phases through which our program passes before
being transformed into an executable form:

o Preprocessor

o Compiler

o Assembler

o Linker
Preprocessor

The source code is the code which is written in a text editor and the
source code file is given an extension ".c". This source code is first passed
to the preprocessor, and then the preprocessor expands this code. After
expanding the code, the expanded code is passed to the compiler.

Compiler

The code which is expanded by the preprocessor is passed to the


compiler. The compiler converts this code into assembly code. Or we can
say that the C compiler converts the pre-processed code into assembly
code.

Assembler

The assembly code is converted into object code by using an assembler.


The name of the object file generated by the assembler is the same as
the source file. The extension of the object file in DOS is '.obj,' and in
UNIX, the extension is 'o'. If the name of the source file is 'hello.c', then
the name of the object file would be 'hello.obj'.

Linker

The main working of the linker is to combine the object code of library
files with the object code of our program

Let's understand through an example.

hello.c

1. #include <stdio.h>  
2. int main()  
3. {  
4.     printf("Hello javaTpoint");  
5.     return 0;  
6. }  

Now, we will create a flow diagram of the above program:


Character set of C
Character set:-

It denotes any alphabet, digit or special symbol used to represent


information.

Use:-      These characters can be combined to form variables. C uses


constants, variables, operators, keywords and expressions as building
blocks to form a basic C program.

Character set:-   
The character set is the fundamental raw material of any language
and they are used to represent information. Like natural languages,
computer                                            language will also have well
defined character set, which is useful to build the programs.

The characters in C are grouped into the following two categories:

1.      Sourcecharacterset
 a.     Alphabets
b.     Digits
c.     SpecialCharacters
d.     WhiteSpaces

2.      Executioncharacterset

a.    EscapeSequence

Sourcecharacterset

ALPHABETS
            1)Uppercaseletters                A-Z
            2)Lowercaseletters                a-z
DIGITS  -  0, 1, 2, 3, 4, 5, 6, 7, 8, 9

SPECIACHARACTERS

 ~   tilde        

  %  percent sign      

  |   vertical bar    

@   at symbol       

 +    plus sign         

   <   less than

_    underscore     

    -     minus sign    

   >    greater than   

    ^    caret          

   #    number sign            

  =   equal to

&    ampersand            

     $    dollar sign       

     /    slash           

     (     left parenthesis     

*     asterisk                     

\      back slash

)      right parenthesis 

    ′     apostrophe      


   :       olon   

   [     left bracket   

    "   quotation mark   

    ;   semicolon

]    right bracket      

    !    exclamation mark  

   ,    comma               

{ left flower brace             

  ?    Question mark      

.    dot operator 

 }     Right flower brace   

WHITESPACECHARACTERS

\b         blank space             

  \t          horizontal tab                  

  \v         vertical tab        

   \r         carriage return         

\f         form feed          

   \n      new line

       
\\     Back slash    

     \’      Single quote           


       \"      Double quote    

   \?    Question mark   

       \0      Null                         

   \a     Alarm (bell)

ExecutionCharacterSet

Certain ASCII characters are unprintable, which means they are not
displayed on the screen or printer. Those characters perform other
functions aside from displaying text. Examples are backspacing, moving
to a newline, or ringing a bell.

They are used in output statements. Escape sequence usually consists of


a backslash and a letter or a combination of digits. An escape sequence is
considered as a single character but a valid character constant.

These are employed at the time of execution of the program. Execution


characters set are always represented by a backslash (\) followed by a
character. Note that each one of character constants represents one
character, although they consist of two characters. These characters
combinations are called as escape sequence.

                                    Backslash character constants

Character                      ASCII  value    Escape Sequence       


Result

Null                                          000                   \0                                


Null
Alarm (bell)                            007                  \a                                   
Beep Sound

Back space                            008                  \b                                  


Moves previous position

Horizontal tab                        009                  \t                                   
Moves next horizontal tab

New line                                 010                  \n                                  
Moves next Line

Vertical tab                             011                  \v                                  
Moves next vertical tab

Form feed                              012                  \f                                   
Moves initial position of next page

Carriage return                     013                  \r                                   
Moves beginning of the line

Double quote                       034                   \"                                   
Present Double quotes

Single quote                         039                   \'                                   
Present Apostrophe

Question mark                     063                   \?                                 
Present Question Mark

Back slash                            092                   \\                                  
Present back slash

Octal number                       \000                           

Hexadecimal number        \x

Tokens in C

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:


o

Keywords in C

o Identifiers in C

o Strings in C

o Operators in C

o Constant in C

o Special Characters in C

Let's understand each token one by one.

Keywords in C

Keywords in C can be defined as the pre-defined or the reserved


words having its own importance, and each keyword has its own
functionality. Since keywords are the pre-defined words used by the
compiler, so they cannot be used as the variable names. If the keywords
are used as the variable names, it means that we are assigning a different
meaning to the keyword, which is not allowed. C language supports 32
keywords given below:

auto double int struct

break else long switch

case enum register typedef

char extern return union

const float short unsigned

continue for signed void

default goto sizeof volatile

do if static while

Identifiers in C

Identifiers in C are used for naming variables, functions, arrays,


structures, etc. Identifiers in C are the user-defined words. It can be
composed of uppercase letters, lowercase letters, underscore, or digits,
but the starting letter should be either an underscore or an alphabet.
Identifiers cannot be used as keywords. Rules for constructing identifiers
in C are given below:

o The first character of an identifier should be either an alphabet or


an underscore, and then it can be followed by any of the character,
digit, or underscore.

o It should not begin with any numerical digit.

o In identifiers, both uppercase and lowercase letters are distinct.


Therefore, we can say that identifiers are case sensitive.

o Commas or blank spaces cannot be specified within an identifier.


o Keywords cannot be represented as an identifier.

o The length of the identifiers should not be more than 31 characters.

o Identifiers should be written in such a way that it is meaningful,


short, and easy to read.

Strings in C

Strings in C are always represented as an array of characters having null


character '\0' at the end of the string. This null character denotes the end
of the string. Strings in C are enclosed within double quotes, while
characters are enclosed within single characters. The size of a string is a
number of characters that the string contains.

Now, we describe the strings in different ways:

char a[10] = "javatpoint"; // The compiler allocates the 10 bytes to the 'a'
array.

char a[] = "javatpoint"; // The compiler allocates the memory at the run
time.

char a[10] = {'j','a','v','a','t','p','o','i','n','t','\0'}; // String is represented


in the form of characters.

Constants in C

A constant is a value assigned to the variable which will remain the same
throughout the program, i.e., the constant value cannot be changed.

There are two ways of declaring constant:

o Using const keyword

o Using #define pre-processor

Types of constants in C
Constant Example

Integer constant 10, 11, 34, etc.

Floating-point constant 45.6, 67.8, 11.2, etc.

Octal constant 011, 088, 022, etc.

Hexadecimal constant 0x1a, 0x4b, 0x6b, etc.

Character constant 'a', 'b', 'c', etc.

String constant "java", "c++", ".net", etc.

Special characters in C

Some special characters are used in C, and they have a special meaning
which cannot be used for another purpose.

o Square brackets [ ]: The opening and closing brackets represent


the single and multidimensional subscripts.

o Simple brackets ( ): It is used in function declaration and function


calling. For example, printf() is a pre-defined function.

o Curly braces { }: It is used in the opening and closing of the code.
It is used in the opening and closing of the loops.

o Comma (,): It is used for separating for more than one statement
and for example, separating function parameters in a function call,
separating the variable when printing the value of more than one
variable using a single printf statement.

o Hash/pre-processor (#): It is used for pre-processor directive. It


basically denotes that we are using the header file.

o Asterisk (*): This symbol is used to represent pointers and also


used as an operator for multiplication.

o Tilde (~): It is used as a destructor to free memory.

Period (.): It is used to access a member of a structure or a union.


****************************************************************************************

Variables in C

A variable is a name of the memory location. It is used to store data. Its


value can be changed, and it can be reused many times.

It is a way to represent memory location through symbol so that it can be


easily identified.

Let's see the syntax to declare a variable:

1. type variable_list;  

The example of declaring the variable is given below:

1. int a;  
2. float b;  
3. char c;  

Here, a, b, c are variables. The int, float, char are the data types.

We can also provide values while declaring the variables as given below:

1. int a=10,b=20;//declaring 2 variable of integer type  
2. float f=20.8;  
3. char c='A';  
Rules for defining variables

o A variable can have alphabets, digits, and underscore.

o A variable name can start with the alphabet, and underscore only. It
can't start with a digit.

o No whitespace is allowed within the variable name.

o A variable name must not be any reserved word or keyword, e.g.


int, float, etc.
Valid variable names:

1. int a;  
2. int _ab;  
3. int a30;  

Invalid variable names:

1. int 2;  
2. int a b;  
3. int long;  
Types of Variables in C

There are many types of variables in c:

1. local variable

2. global variable

3. static variable

4. automatic variable

5. external variable

Local Variable

A variable that is declared inside the function or block is called a local


variable.

It must be declared at the start of the block.

1. void function1(){  
2. int x=10;//local variable  
3. }  

You must have to initialize the local variable before it is used.


Global Variable

A variable that is declared outside the function or block is called a global


variable. Any function can change the value of the global variable. It is
available to all the functions.

It must be declared at the start of the block.

1. int value=20;//global variable  
2. void function1(){  
3. int x=10;//local variable  
4. }  
Static Variable

A variable that is declared with the static keyword is called static variable.

It retains its value between multiple function calls.

1. void function1(){  
2. int x=10;//local variable  
3. static int y=10;//static variable  
4. x=x+1;  
5. y=y+1;  
6. printf("%d,%d",x,y);  
7. }  

If you call this function many times, the local variable will print the
same value for each function call, e.g, 11,11,11 and so on. But
the static variable will print the incremented value in each function
call, e.g. 11, 12, 13 and so on.
Automatic Variable

All variables in C that are declared inside the block, are automatic
variables by default. We can explicitly declare an automatic variable
using auto keyword.

1. void main(){  
2. int x=10;//local variable (also automatic)  
3. auto int y=20;//automatic variable  
4. }  
External Variable

We can share a variable in multiple C source files by using an external


variable. To declare an external variable, you need to use extern
keyword.

myfile.h

1. extern int x=10;//external variable (also global)  
program1.c

1. #include "myfile.h"  
2. #include <stdio.h>  
3. void printValue(){  
4.     printf("Global variable: %d", global_variable);  
5. }  

You might also like