C is a high-level structured oriented programming language, used in general purpose programming, developed
by Dennis Ritchie at AT&T Bell labs, USA between 1969 and 1973.
Some Facts About C Programming Language
In 1988, the American National Standards Institute (ANSI) has formalized the C language.
C was invented to write UNIX operating system.
C is a successor of ‘Basic Combined Programming Language’ (BCPL) called B language.
Linux OS, PHP and MySQL is written in C.
C has been written in assembly language.
Uses of C Programming Language
In the beginning C was used for developing system applications e.g. :
Database Systems
Language Interpreters
Compilers and Assemblers
Operating Systems
Network Drivers
Word Processors
C has Become Very Popular for Various Reasons
One of the early programming languages.
Still the best programming language to learn quickly.
C language is reliable, simple and easy to use.
C language is a structured language.
Modern programming concepts are based on C.
It can be compiled on a variety of computer platforms.
Universities preferred to add C programming in their courseware.
Features of C Programming Language
C is a robust language with rich set of built-in functions and operators.
Programs written in C are efficient and fast.
C is highly portable, programs once written in C can be run on another machines with minor or no modification.
C is basically a collection of C library functions, we can also create our own function and add it to the C library.
C is easily extensible.
Advantages of C
C is the building block for many other programming languages.
Programs written in C are highly portable.
Several standard functions are there (like in-built) that can be used to develop programs.
C programs are basically collections of C library functions, and it’s also easy to add own functions in to the C library.
The modular structure makes code debugging, maintenance and testing easier.
Disadvantages of C
C does not provide Object Oriented Programming (OOP) concepts.
There is no concepts of Namespace in C.
C does not provide binding or wrapping up of data in a single unit.
C does not provide Constructor and Destructor.
It is good to learn history of C programming. C was developed and written by Dennis M. Ritchie in the year 1972 and hence he is known
as the founder of C .
C programming language was developed to overcome the difficulties found in older programming languages such as BCPL, BASIC, B
etc.
History of Programming Languages and C
Here is the lists of programming languages that were developed with or before C:
Programming Language Development Year
Regional Assembly Language 1951
Autocode 1952
IPL (forerunner to LISP) 1954
FLOW-MATIC (led to COBOL) 1955
FORTRAN (First compiler) 1957
COMTRAN (precursor to COBOL) 1957
LISP 1958
ALGOL 58 1958
FACT (forerunner to COBOL) 1959
COBOL 1959
RPG 1959
APL 1962
Simula 1962
SNOBOL 1962
CPL (forerunner to C) 1963
Speakeasy (computational environment) 1964
BASIC 1964
PL/I 1964
JOSS 1966
BCPL (forerunner to C) 1967
C is imperative language and designed to compile in a relatively straightforward manner which provides low-level access to the memory.
With the gradual increase in the popularity of the program, the language and its compiler has become available on a wide range of
platforms from embedded micro-controllers to supercomputers.
With the introduction of K&R C language (which is a new edition of C published in 1978 by Brian Kernighan and Denis Ritchie), several
features have been included in C language.
Some of these features are:
Standard I/O (Input/Output) Library
long int – data type
unsigned int – data type
Compound assignment operators
During the late 1980’s , C was started to use for wide variety of mainframe computers, micro and mini computers which began to get
popular. Gradually C got its superset – i.e. C++ which has added features, but its developed from C with all its initial concepts.
To start learning C programming, you only have to install the C compiler in your system, and nowadays C and C++ both compilers come
as a single integrated package, which serves the purpose of C and C++ both program development.
What is Compiler in C?
A compiler is a computer program that transforms human readable (programming language) source code into another computer
language (binary) code.
In simple terms Compiler takes the code that you write and turned in to the binary code that computer can understand.
C compiler is a software application that transforms human readable C program code to machine readable code. The process of
transforming the code from High Level Language to Machine Level Language is called “Compilation”. Human readable code is the C
program that consists of digits letters, special symbols etc which is understand by human beings. On the other hand, machine language
is dependent on processor and processor understands zeroes and ones (binary) only. All C program execution is based on processor
which is available in the CPU; that is why entire C source code needs to be converted to binary system by the compiler.
This tutorial is written for Windows, Unix / Linux and MAC users. All code has been tested and it works correctly on all three operating
systems.
List of C Compilers for Windows OS
Since there are various compilers available in to the online market, here are the lists of some of the frequently used ones:
CCS C Compiler
Turbo C
Minimalist GNU for Windows (MinGW)
Portable C Compiler
Clang C++
Digital Mars C++ Compiler
Intel C++
IBM C++
Visual C++ : Express Edition
Oracle C++
All of these above compilers for C are free to download, but there are some other paid C compilers also available or programmers can
get it for trial version:
Embarcadero C++
Edison Design Group C++
Green Hills C++
HP C++ for Unix
Intel C++ for Windows, Linux, and some embedded systems.
Microsoft C++
Paradigm C++
C Compiler Installation on Windows
To use C compiler in Windows, you can install any one software mentioned below.
You can download a 90-day trial version of Visual Studio
You can download Dev-C++ IDE to develop C and C++ application.
You can install MinGW
C Compiler Installation on UNIX/Linux
If you are using UNIX / Linux, then most probably C compiler called GCC will already in your system. To check if you have it
installed, you can type cc or gcc at command prompt.
$ gcc -v
If for some reason it is not Installed on your system, you can download it from gcc.gnu.org/install.
C Compiler Installation on MAC
Xcode development environment came with GNU C/C++ compiler, You can install it from Apple’s website.
You can download Xcode from developer.apple.com/technologies/tools.
Basically a C program involves the following sections:
Documentations (Documentation Section)
Preprocessor Statements (Link Section)
Global Declarations (Definition Section)
The main() function
o Local Declarations
o Program Statements & Expressions
User Defined Functions
Let’s look into various parts of the above C program.
/* Comments */ Comments are a way of explaining what makes a program. Comments are
ignored by the compiler and used by others to understand the code.
or
This is a comment block, which is ignored by the compiler. Comment can used
anywhere in program to add info about program or code block, which will be
helpful for developers to easily understand the existing code in the future.
#include<stdio.h stdio is standard for input / output, this allows us to use some commands which
> includes a file called stdio.h.
or
This is a preprocessor command. that notify the compiler to include the header
file stdio.h in the program before compiling the source-code.
int/void main() int/void is a return value, which will be explained in a while.
main() The main() is the main function where program execution begins. Every C
program must contain only one main function.
or
This is a main function, which is the default entry point for every C program and
the void in front of it indicates that it does not return a value.
Braces Two curly brackets “{…}” are used to group all statements together.
or
Curly braces which shows how much the main() function has its scope.
printf() It is a function in C, which prints text on the screen.
or
This is another pre-defined function of C which is used to be displayed text string
in the screen.
return 0 At the end of the main function returns value 0.
Basic Structure of C Program
The example discussed above, illustrate how a simple C program looks like and how the program segment works. A C program may
contain one or more sections which is figured above.
The Documentation section usually contains the collection of comment lines giving the name of the program, author’s or programmer’s
name and few other details. The second part is the link-section which gives instruction to the compiler to connect to the various functions
from system library. The Definition section describes all the symbolic-constants. The global declaration section is used to define those
variables that are used globally within the entire program and is used in more than one function. This section also declares all the user-
defined functions. Then comes the main(). All C programs must have a main() which contains two parts:
Declaration part
Execution part
The declaration part is used to declare all variables that will be used within the program. There needs to be at least one statement in the
executable part and these two parts are declared within the opening and closing curly braces of the main(). The execution of program
begins at the opening brace ‘{‘ and ends with the closing brace ‘}’. Also it has to be noted that all the statements of these two parts
needs to be terminated with semi-colon.
The sub-program section deals with all user defined functions that are called from the main(). These user defined functions are declared
and defined usually after the main() function.
Managing Input/Output
I/O operations are useful for program to interact with users. stdlib is the standard C library for input output operations. While dealing with
input-output operations in C, there are two important streams that play their role. These are:
Standard Input (stdin)
Standard Output (stdout)
Standard input or stdin is used for taking input from devices such as the keyboard as a data stream. Standard output or stdout is used for
giving output to a device such as a monitor. For using I/O functionality, programmers must include stdio header-file within the program.
Reading Character In C
The easiest and simplest of all I/O operations is taking character as input by reading that character from standard input
(keyboard). getchar() function can be used to read a single character. This function is alternate to scanf() function.
Syntax:
var_name = getchar();
Example:
#include<stdio.h>
void main()
char title;
title = getchar();
There is another function to do that task for files: getc which is used to accept a character from standard input.
Syntax:
int getc(FILE *stream);
Writing Character In C
Similar to getchar() there is another function which is used to write characters, but one at a time.
Syntax:
putchar(var_name);
Example:
#include<stdio.h>
void main()
char result = 'P';
putchar(result);
putchar('\n');
}
Similarly, there is another function putc which is used for sending a single character to standard output.
Syntax:
int putc(int c, FILE *stream);
Formatted Input
It refers to an input data which has been arranged in a specific format. This is possible in C using scanf(). We have already encountered
this and familiar with this function.
Syntax:
scanf("control string", arg1, arg2, ..., argn);
The field specification for reading integer inputted number is:
%w sd
Here the % sign denotes the conversion specification; w signifies the integer number that defines the field width of the number to be
read. d defines the number to be read in integer format.
Example:
#include<stdio.h>
void main()
int var1= 60;
int var1= 1234;
scanf("%2d %5d", &var1, &var2);
Input data items should have to be separated by spaces, tabs or new-line and the punctuation marks are not counted as separators.
Reading and Writing Strings in C
There are two popular library functions gets() and puts() provides to deal with strings in C.
gets: The char *gets(char *str) reads a line from stdin and keeps the string pointed to by the str and is terminated when the new line is
read or EOF is reached. The declaration of gets() function is:
Syntax:
char *gets(char *str);
where str is a pointer to an array of characters where C strings are stored.
puts: The function – int puts(const char *str) is used to write a string to stdout but it does not include null characters. A new line character
need to be append to the output. The declaration is:
Syntax:
int puts(const char *str)
where str is the string to be written in C.
Format specifiers can be defined as the operators which are used in association with printf() function for printing the data that is referred
by any object or any variable. When a value is stored within a particular variable then you cannot print the value stored in the
variable straightforwardly without using the format specifiers. You can retrieve the data that are stored in the variables and can print
them onto the console screen by implementing these format specifiers in a printf() function.
Format specifiers start with a percentage % operator and followed by a special character for identifying the type of the data.
There are mostly 6 types of format specifiers that are available in C.
List of format specifiers in C
Format specifier Description
%d Integer Format Specifier
%f Float Format Specifier
%c Character Format Specifier
%s String Format Specifier
%u Unsigned Integer Format Specifier
%ld Long Int Format Specifier
Integer Format Specifier %d
The %d format specifier is implemented for representing integer values. This is used with printf() function for printing the integer value
stored in the variable.
Syntax:
printf("%d",<variable name>);
Float Format Specifier %f
The %f format specifier is implemented for representing fractional values. This is implemented within printf() function for printing the
fractional or floating value stored in the variable. Whenever you need to print any fractional or floating data, you have to use %f format
specifier.
Syntax:
printf("%f", <variable name>);
Character Format Specifier %c
The %c format specifier is implemented for representing characters. This is used with printf() function for printing the character stored in
a variable. When you want to print a character data, you should incorporate the %c format specifier.
Syntax:
printf("%c",<variable name>);
String Format Specifier %s
The %s format specifier is implemented for representing strings. This is used in printf() function for printing a string stored in the
character array variable. When you have to print a string, you should implement the %s format specifier.
Syntax:
printf("%s",<variable name>);
Unsigned Integer Format Specifier %u
The %u format specifier is implemented for fetching values from address of a variable having unsigned decimal integer stored in the
memory. This is used within printf() function for printing the unsigned integer variable.
Syntax:
printf("%u",<variable name>);
Long Int Format Specifier %ld
The %ld format specifier is implemented for representing long integer values. This is implemented with printf() function for printing the
long integer value stored in the variable.
Syntax:
printf("%ld",<variable name>);
Rules for Naming Identifiers
An identifier can only have alphanumeric characters (a-z , A-Z , 0-9) (i.e. letters & digits) and underscore( _ ) symbol.
Identifier names must be unique
The first character must be an alphabet or underscore.
You cannot use a keyword as identifiers.
Only first thirty-one (31) characters are significant.
Must not contain white spaces.
Identifiers are case-sensitive.
C Constants is a most basic and important part of C programming language. Constants in C are the fixed values that are used in a
program, and its value remains the same during the entire execution of the program.
Constants are also called literals.
Constants can be any of the data type.
It is considered best practice to define constants using only upper-case names.
Constant Definition in C
Syntax:
const type constant_name;
const keyword defines a constant in C.
Constant Types in C
Constants is categorized into two basic types and each of these types has own sub types/categories. These are:
Primary Constants
1. Numeric Constants
o Integer Constants
o Real Constants
2. Character Constants
o Single Character Constants
o String Constants
o Backslash Character Constants
Integer Constant
Its refer to sequence of digits. Integers are of three types viz:
1. Decimal Integer
2. Octal Integer
3. Hexadecimal Integer
Example:
15, -265, 0, 99818, +25, 045, 0X6
Real constant
The numbers containing fractional parts like 99.25 are called real or floating points constant.
Single Character Constants
It simply contains a single character enclosed within ‘ and ‘ (pair of single quote). It is to be noted that the character ‘8‘ is not the same
as 8. Character constants have specific set of integer values known as ASCII values (American Standard Code for Information
Interchange).
Example:
‘X’, ‘5’, ‘;’
String Constants
These are sequence of characters enclosed in double quotes and they may include letters, digits, special characters and blank-spaces.
It is again to be noted that “G” and ‘G‘ are different – because “G” represents string as it is enclosed within pair of double quotes
whereas ‘G’ represents a single character.
Example:
“Hello!”, “2015”, “2+1”
Backslash character constant
C supports some character constants having backslash in front of it. The lists of backslash characters have specific meaning which is
known to the compiler. They are also termed as “Escape Sequence”.
For Example:
\t is used to give a tab
\n is used to give new line
Constants Meaning
\a beep sound
\b back space
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\’ single quote
\” double quote
\\ backslash
\0 null
Secondary Constant
Array
Pointer
Structure
Union
Enum
C operators are symbols that is used to perform mathematical or logical manipulations. C programming language is rich with built-in
operators. Operators take part in a program for manipulating data and variables and form a part of the mathematical or logical
expressions.