0% found this document useful (0 votes)
56 views10 pages

Chapter 6: Introduction What Is C?: Section B: Programming in C

C is a programming language developed in 1972 and is still widely used today. It remains popular due to its reliability, simplicity, and performance. While newer languages have emerged, C is still important for tasks like operating system and device driver development where speed is critical. Some key reasons to learn C include that it helps in learning other languages like C++ and Java, it is used extensively in operating systems, and it is suitable for programming small devices and games where response time is important.

Uploaded by

Krishna Maurya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views10 pages

Chapter 6: Introduction What Is C?: Section B: Programming in C

C is a programming language developed in 1972 and is still widely used today. It remains popular due to its reliability, simplicity, and performance. While newer languages have emerged, C is still important for tasks like operating system and device driver development where speed is critical. Some key reasons to learn C include that it helps in learning other languages like C++ and Java, it is used extensively in operating systems, and it is suitable for programming small devices and games where response time is important.

Uploaded by

Krishna Maurya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

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;

Here, int is a keyword that indicates 'money' is a variable of type integer.

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

auto double int struct

break else long switch

case enum register typedef

char extern return union

continue for signed void

2|Page
do if static while

default goto sizeof volatile

const float short unsigned


Along with these keywords, C supports other numerous keywords depending upon the compiler.

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;

Here, money and accountBalance are identifiers.


Also remember, identifier names must be different from keywords. You cannot use int as an identifier
because int is a keyword.

Rules for naming identifiers


1. A valid identifier can have letters (both uppercase and lowercase letters), digits and underscores.
2. The first letter of an identifier should be either a letter or an underscore.
3. There is no rule on how long an identifier can be. However, you may run into problems in some
compilers if identifier is longer than 31 characters.

Good Programming Practice


You can choose any name as an identifier following the rules (excluding keywords). However, give
meaningful name to an identifier (variables, function names etc). It will make your and your fellow
programmers life much easier.

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:

int playerScore = 95;

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';

Rules for naming a variable


1. A variable name can have letters (both uppercase and lowercase letters), digits and underscore
only.
2. The first letter of a variable should be either a letter or an underscore.
3. No commas or blanks or special symbol other than an underscore are allowed within a variable
name.
4. There is no rule on how long a variable name (identifier) can be. However, you may run into
problems in some compilers if variable name is longer than 31 characters.

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:

int number = 5; // integer variable


number = 5.5; // error
double number; // error

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:

const double PI = 3.14;

Notice, we have added keyword const.


Here, PI is a symbolic constant. It's actually a variable however, it's value cannot be changed.

const double PI = 3.14;


PI = 2.9; //Error

Below are the different types of constants you can use in C.

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:

Decimal constants: 0, -9, 22 etc


Octal constants: 021, 077, 033 etc
Hexadecimal constants: 0x7f, 0x2a, 0x521 etc

In C programming, octal starts with a 0, and hexadecimal starts with a 0x.

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

Note: E-5 = 10-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

Escape Sequences Character

\b Backspace

\f Form feed

\n Newline

5|Page
Escape Sequences

Escape Sequences Character

\r Return

\t Horizontal tab

\v Vertical tab

\\ Backslash

\' Single quotation mark

\" Double quotation mark

\? Question mark

\0 Null character

5. String Literals
A string literal is a sequence of characters enclosed in double-quote marks. For example:

"good" //string constant


"" //null string constant
" " //string constant of six white space
"x" //string constant having single character.
"Earth is round\n" //prints string with newline

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.

C Programming Data Types


In C programming, variables (memory location) should be declared before it can be used.
Similarly, a function also needs to be declared before use.
Data types simply refers to the type and size of data associated with variables and functions.
Data type can be either fundamental (provided in C compiler), or derived (derived from
fundamental data types).
Fundamental data types
Commonly used fundamental data types are: int, char and float.
int
Integers are whole numbers that can have both zero, positive and negative values but no decimal
values. Example: 0, -5, 10
We can use int for declaring an integer variable.

int id;

Here, id is a variable of type integer.


You can declare multiple variable at once in C programming. For example:

int id, age;

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:

char test = 'h';

The size of character variable is 1 byte.

float and double


float and double are used to hold real numbers.

7|Page
float salary;
double price;

In C, floating-point numbers can also be represented in exponential. For example:

float normalizationFactor = 22.442e2;

What's the difference between float and double?


The size of float (single precision float data type) is 4 bytes. And the size of double (double
precision float data type) is 8 bytes.

Few other data types:


void
void is an incomplete type. It means "nothing" or "no type". You can think of void as absent.
For example, if a function is not returning anything, its return type should be void.
Note that, you cannot create variables of void type.
bool
Traditionally, there was no boolean type in C. However, C99 defines a standard boolean type
under <stdbool.h> header file. A boolean type can take one of two values, either true or false. For
example:

#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:

enum suit { club, diamonds, hearts, spades};

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;

You can always check size of a variable using sizeof() operator.

#include <stdio.h>
int main() {
short a;
long b;
long long c;
long double d;

printf("size of short = %d bytes\n", sizeof(a));


printf("size of long = %d bytes\n", sizeof(b));
printf("size of long long = %d bytes\n", sizeof(c));
printf("size of long double= %d bytes\n", sizeof(d));
return 0;
}

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

You might also like