c notes
c notes
by Dennis Ritchie at AT&T Bell labs, USA between 1969 and 1973.
Database Systems
Language Interpreters
Compilers and Assemblers
Operating Systems
Network Drivers
Word Processors
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.
Autocode 1952
LISP 1958
ALGOL 58 1958
COBOL 1959
RPG 1959
APL 1962
Simula 1962
SNOBOL 1962
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
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
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
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.
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
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++
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.
/* 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
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
Braces Two curly brackets “{…}” are used to group all statements together.
or
Curly braces which shows how much the main() function has its scope.
or
in the screen.
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
The sub-program section deals with all user defined functions that are called from the main(). These user defined functions are declared
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 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()
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
%w sd
Here the % sign denotes the conversion specification; w signifies the integer number that defines the field width of the number to be
#include<stdio.h>
void main()
Input data items should have to be separated by spaces, tabs or new-line and the punctuation marks are not counted as separators.
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
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
Format specifiers start with a percentage % operator and followed by a special character for identifying the type of the data.
The %d format specifier is implemented for representing integer values. This is used with printf() function for printing the integer value
printf("%d",<variable name>);
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:
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>);
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>);
The %ld format specifier is implemented for representing long integer values. This is implemented with printf() function for printing the
printf("%ld",<variable name>);
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.
Constant Definition in C
Syntax:
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
1. Decimal Integer
2. Octal Integer
3. Hexadecimal Integer
Example:
Real constant
The numbers containing fractional parts like 99.25 are called real or floating points constant.
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:
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
C supports some character constants having backslash in front of it. The lists of backslash characters have specific meaning which is
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.