Chapter 6: Introduction What Is C?: Section B: Programming in C
Chapter 6: Introduction What Is C?: Section B: Programming in C
Chapter 6: Introduction
What is C?
C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972. It was designed
and written by a man named Dennis Ritchie. In the late seventies C began to replace the more familiar
languages of that time like PL/I, ALGOL, etc. Possibly why C seems so popular is because it is reliable,
simple and easy to use. Moreover, in an industry where newer languages, tools and technologies emerge
and vanish day in and day out, a language that has survived for more than 3 decades has to be really good.
Why C?
An opinion that is often heard today is – “C has been already superceded by languages like C++, C# and
Java, so why bother to learn C today”. There are several reasons for this:
(a) C makes learning other languages (where there are concepts like classes, objects, inheritance,
polymorphism, templates, exception handling, references, etc.) like C++ or JAVA, simple.
(b) Organizing the program in Object Oriented Programming (OOP) like C++, C# or Java becomes
easy if you have a good hold over the language elements of C and the basic programming skills.
(c) Though many C++ and Java based programming tools and frameworks have evolved over the
years the importance of C is still unchallenged because knowingly or unknowingly while using
these frameworks and tools you would be still required to use the core C language elements—
another good reason why one should learn C before C++, C# or Java.
(d) Major parts of popular operating systems like Windows, UNIX, Linux is still written in C. This is
because even today when it comes to performance (speed of execution) nothing beats C.
Moreover, if one is to extend the operating system to work with new devices one needs to write
device driver programs. These programs are exclusively written in C.
(e) While building operating systems and programs for small devices (with limited memory and
processing power) like cellular phones, palmtops, microwave oven, washing machines and digital
cameras, C can be a good choice.
(f) Many popular gaming frameworks (3D games where object needs a real time response, where
essence is speed) have been built using C language. This is where C language scores over other
languages.
(g) At times one is required to very closely interact with the hardware devices. Since C provides
several language elements that make this interaction feasible without compromising the
performance it is the preferred choice of the programmer.
Character set
Character set is a set of alphabets, letters and some special characters that are valid in C language.
Alphabets
Uppercase: A B C ................................... X Y Z
Lowercase: a b c ...................................... x y z
1|Page
C accepts both lowercase and uppercase alphabets as variables and functions.
Digits
0123456789
Special Characters
Special Characters in C Programming
, < > . _
( ) ; $ :
% [ ] # ?
^ ! * / |
- \ ~ +
White space Characters: blank space, new line, horizontal tab, carriage return and form feed
Keywords
Keywords are predefined, reserved words used in programming that have special meanings to the
compiler. Keywords are part of the syntax and they cannot be used as an identifier. For example:
int money;
As C is a case sensitive language, all keywords must be written in lowercase. Here is a list of all
keywords allowed in ANSI C.
Keywords in C Language
2|Page
do if static while
Identifiers
Identifier refers to name given to entities such as variables, functions, structures etc.
Identifier must be unique. They are created to give unique name to an entity to identify it during the
execution of the program. For example:
int money;
double accountBalance;
Variables
In programming, a variable is a container (storage area) to hold data.
To indicate the storage area, each variable should be given a unique name (identifier). Variable names are
just the symbolic representation of a memory location. For example:
Here, playerScore is a variable of integer type. Here, the variable is assigned an integer value 95.
The value of a variable can be changed, hence the name variable.
char ch = 'a';
// some code
3|Page
ch = 'l';
Note: You should always try to give meaningful names to variables. For example: firstNameis a better
variable name than fn.
C is a strongly typed language. This means, variable type cannot be changed once it is declared. For
example:
Here, the type of number variable is int. You cannot assign floating-point (decimal) value 5.5 to this
variable. Also, you cannot redefine the type of the variable to double. By the way, to store decimal values
in C, you need to declare its type to either double or float.
Constants/Literals
A constant is a value (or an identifier) whose value cannot be altered in a program. For example: 1, 2.5,
'c' etc.
Here, 1, 2.5 and 'c' are literal constants. Why? You cannot assign different values to these terms.
You can also create non-modifiable variables in C programming. For example:
1. Integers
An integer is a numeric constant (associated with number) without any fractional or exponential part.
There are three types of integer constants in C programming:
decimal constant(base 10)
4|Page
octal constant(base 8)
hexadecimal constant(base 16)
For example:
2. Floating-point constants
A floating point constant is a numeric constant that has either a fractional form or an exponent
form. For example:
-2.0
0.0000234
-0.22E-5
3. Character constants
A character constant is created by enclosing a single character inside single quotation marks. For
example: 'a', 'm', 'F', '2', '}' etc;
4. Escape Sequences
Sometimes, it is necessary to use characters which cannot be typed or has special meaning in C
programming. For example: newline(enter), tab, question mark etc. In order to use these
characters, escape sequence is used.
For example: \n is used for newline. The backslash \ causes escape from the normal way the
characters are handled by the compiler.
Escape Sequences
\b Backspace
\f Form feed
\n Newline
5|Page
Escape Sequences
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\? Question mark
\0 Null character
5. String Literals
A string literal is a sequence of characters enclosed in double-quote marks. For example:
6. Enumerations
Keyword enum is used to define enumeration types. For example:
6|Page
enum color {yellow, green, black, white};
Here, color is a variable and yellow, green, black and white are the enumeration constants having
value 0, 1, 2 and 3 respectively.
You can also define symbolic constants using #define. We will discuss it in macros.
int id;
The size of int is 2 bytes (16 bits) in Turbo C/C++ compilers. Hence, it can take 216 distinct
states : -2 15
,-214+1, ..., -1, 0, 1, 2, ..., 215-2, 215-1, that is, from -32768 to 32767.
char
Keyword char is used for declaring character type variables. For example:
7|Page
float salary;
double price;
#include <stdio.h>
#include <stdbool.h>
int main() {
bool a = true;
return 0;
}
enum
You can create an enumerated type using enum keyword. An enumeration consists of integral
constants. For example:
Complex types
In ISO C99, support for the complex type was standardized.
If you include complex.h header file in your program, you can use complex as a keyword to create
and work with complex numbers. For example:
#include <stdio.h>
#include <complex.h>
8|Page
int main() {
int complex z = 2 + 1 * I;
}
Modifiers
In c language Data Type Modifiers are keywords used to change the properties of current
properties of data type. Data type modifiers are classified into following types.
long
short
unsigned
signed
long long
Modifiers are prefixed with basic data types to modify (either increase or decrease) the
amount of storage space allocated to a variable.
short and long
If you need to use large number, you can use type specifier long. Here's how:
long a;
long long b;
long double c;
Here variables a and b can store integer values. And, c can store a floating-point number.
If you are sure, only a small integer ([−32,768, +32,767] range) will be used, you can use short.
short d;
#include <stdio.h>
int main() {
short a;
long b;
long long c;
long double d;
9|Page
signed and unsigned
In C, signed and unsigned are type modifiers. You can alter the data storage of a data type by using
them. For example:
unsigned int x;
int y;
Here, the variable x can hold only zero and positive values because we have used unsigned
modifier.
Note:
Rest of the topics e.g. storage classes (automatic, external,register and static) and their use –when
and where , macros, the C pre-processor, will be discussed after Chap10_Functions.
10 | P a g e