Download as pdf or txt
Download as pdf or txt
You are on page 1of 57

1.

1 Introduction to C Language

1 Department of CSE
Objectives
• To understand the structure of a C-Language Program
• To write a minimal C program
• To introduce the include preprocessor command
• To be able to create good identifiers for quantities in a program
• To be able to list, describe and use the basic data types in C
• To be able to create and use variables and constants in a C
program

2 Department of CSE
Agenda
• Background of C Language
• Structure of a C program
• C Comments
• Identifiers in C
• Data types in C
• Variables in C
• Constants in C

3 Department of CSE
Background of C
• C is a middle level language it combines the best elements of high-level
languages with the control and flexibility of assembly language
• Like most modern languages, C is also derived from ALGOL 60 (1960)
• Developed by Dennis Ritchie in 1972 using many concepts from its
predecessors – ALGOL,BCPL and B and by adding the concept of data
types
• American National Standards Institute (ANSI) began the standardization of
C in 1983 which was approved in 1989
• In 1990 International Standards Organization (ISO) adopted the ANSI
standard version known as C89
• Minor changes were made to C89 in 1995 which came to be known as
C95
• Much more significant updates were made in 1999 and named as C99
4 Department of CSE
Features of C
• C is a structured programming language which allows compartmentalization
of code and data

• A structured language offers a variety of programming possibilities. For


example, structured languages typically support several loop constructs,
such as while, do-while, and for.

• A structured language allows one to place statements anywhere on a line


and does not require a strict field concept

• In a structured language, the use of goto is either prohibited or


discouraged and is not the common form of program control

5 Department of CSE
Structure of a C Program

Preprocessor Directives To include standard input/output library file in


the program

Global Declarations Describes the data which is visible to all parts


of the program (optional)

• main function is the starting point of every C


int main (parameter list) program
• One and only one function must be named
{ main in a C program
Local Declarations
•Describes the data used in the function
•Visible only inside the function (optional)
Statements
} •Follows the local declaration section
•Contains the instructions for the computer to
do something (e.g., add two numbers)

Other functions as required


Optional functions also called modules similar
to main to accomplish specific tasks

6 Department of CSE
Dissecting a minimal C program
#include <stdio.h> Preprocessor Directive to
int main(void) include stdio.h file
{ main function
printf(“Welcome to Computer Programming”);
return 0; Instruction to print a message
}
Instruction to stop the program

The first C program has


•Preprocessor Commands
• which are placed at the beginning of the program starting with # sign
• can start at any column but traditionally start at column 1
• tells the compiler to include standard input/output library file (stdio.h) in the
program which is needed for printing the welcome message on the screen
• must be typed exactly as shown above without any space between # and include
and the file name is written between < and >.

7 Department of CSE
Dissecting the minimal C program
• General Syntax (writing format) of a Preprocessor Directive is
#include<filename.extension>
Examples:
#include<stdio.h> - Required for using input/ output instructions
#include<string.h> - Required for using pre-defined string functions
#include<math.h> - Required for using pre-defined math functions

• Main function
• Executable part of the program begins with main function
General Syntax:
int main(void)
• The word int before the main indicates that the function will return an integer
value to the operating system
• Since in this program main does not require any parameters, the parameter list
is specified as void
Creating and Compiling the minimal C program
• In the linux environment, locate gedit and open the editor
• Type in the minimal C program exactly with all puncuations as follows
#include <stdio.h>
int main(void)
{
printf(“Welcome to Computer Programming”);
return 0;
}

• After typing the preceding source code, save the program as welcome.c.
• Instead of welcome, any name can be chosen, but the extension must be .c.
• This extension is the common convention in C programs and identifies the
contents of the file as C source code.
• Most compilers will expect the source file to have the extension .c, and if it
doesn’t, the compiler may refuse to process it.
• After saving the program, locate terminal and type cc welcome.c near the $
symbol
Executing the minimal C program
• If the program is present in the correct path and is error free, the
compilation results in a $ being displayed right below the previous
command typed

• If there is a syntax error, open the file again using gedit, locate the
line number indicated by the compiler and do the necessary
corrections

• If the compiler says file not found, change into the folder which
contains the program to be compiled and repeat the compilation step

• Upon successful compilation, type ./a.out at the $ prompt and the


program must print Welcome to Computer Programming
Summary of writing and executing a C program
C Comments
• Placed by the programmer in the code to help the reader to
understand the meaning of sections of code which are not very clear

• Comments are merely internal program documentations

• Compiler ignores the comments when it translates the program into


executable code

• To identify a comment, C uses two different comment formats:


• Line Comment
• Block Comment
Line Comment
• The Second format, the line comment uses two slashes - // to
identify a comment
• This format does not require an end-of-comment token
• The end of line automatically ends the comment
• Programmers generally use this format for short comments
• The line comment can start anywhere on a line and ends with the
end of that line
• Examples of Line Comment:
//This is a whole line comment
A=5; //This is a partial line comment
Block Comment
• Block comment is used when a comment spans several lines
• It uses opening and closing comment tokens
• Each comment token is made up of two characters without spaces
in between them
• The opening comment token is represented as /*
• The closing comment token is represented as */
• Everything that is placed in between /* and */ is ignored by the
compiler
• The block comments can start in any column and they need not
both be on the same line
• The only restriction is that the opening token /* must precede the
closing token */
Block Comment - Examples
/* This is a block comment that covers one line*/

/* This is a block comment that


covers two lines*/

/*
**Block comments can also be designed with the opening
**comment on a separate line and closing comment on a
**separate line. Some programmers also put at the beginning of
**each line in between to clearly mark the documentation part.
*/
Caution with Comments
• Comments cannot be nested i.e., comments cannot be given inside comments
• Example:
• /* This is an /*inner comment*/outer comment*/

ignored Matches the first /* Left without an


opening /*

• Once the compiler sees an opening block comment token /*, it ignores
everything it sees until it finds a closing token */
• Therefore, the opening token of the nested comment is not recognized and the
closing token of the nested comment matches the outer opening comment
thereby leaving the closing token of the outer comment without a matching
opening token
Minimal C program revisited with comments
/* Welcome to the minimal C program. This program demonstrates a small C
program
Written by: name
Date: program written date
*/
#include <stdio.h> // this is a preprocessor directive
int main(void) // this is the main function
{ //main begins here
//No local declarations needed for this simple program
//Statements
printf(“Welcome to Computer Programming”); //This is a print statement
return 0; //returns the control back to the operating system
} //main ends here
The C Character Set
• Character set specifies the valid symbols that can be used in a C
program
• The C character set consists of the following symbols and escape
sequence characters
Escape Sequence characters
• Escape sequence characters are a ASCII Character Escape representation
combination of two characters but
null character ‘\0’
treated as a single character.
alert (bell) ‘\a’
• Often used in printf statements,they
are not printed explicitly on the backspace ‘\b’
screen but the effect of these horizontal tab ‘\t’
characters can be observed. newline ‘\n’
• The table beside shows some ASCII
vertical tab ‘\v’
characters and their corresponding
escape character format form feed ‘\f’
carriage return ‘\r’
single quote ‘\’’
double quote ‘\”’
backslach ‘\\’
Exercises
• Write a program that will output your name and address using a
separate printf() statement for each line of output
• Write a program to print the pattern of asterisks as shown below

• Write a program to print the following figure using suitable


characters
Solutions to Exercises
#include<stdio.h>
int main()
{
printf(“Dhanya M Dhanalakshmy, ");
printf("Asst.Professor, CSE Dept, ");
printf("Amrita School of Engineering, ");
printf("Amrita Vishwa Vidyapeetham, ");
printf("Amrita Nagar Post, ");
printf("Coimbatore 641 112 ");
return 0;
}
Solutions to Exercises
//Prints a triangular pattern
#include<stdio.h>
int main()
{
printf("*\n"); //prints first line
printf("*\t*\n");
printf("*\t*\t*\n");
printf("*\t*\t*\t*\n"); //prints last line
return 0;
}
Solutions to Exercises
//Prints a pattern
#include<stdio.h>
int main()
{
printf("---------- ----------\n"); //prints the top horizontal
bar
printf("| | >>---> | |\n"); /* prints vertical bars*/
printf("| | | |\n");
printf("---------- ----------\n"); //prints the top horizontal
bar
return 0;
}
Identifiers in C – (Recollect Name Bindings in
CTPS)
• Identifiers allow us to name data and other user created components
in a program

• Each identified component is stored at a unique location in the


computer’s memory

• With no identifiers to symbolically represent data locations, we


should know and use the component’s memory address

• Using Identifiers, we simply name the data and let the computer
keep track of where they are physically located in the computer’s
memory
Rules for Identifiers in C
• The only valid alphabets to be used for naming are upper case letters from A to Z and
lower case letters from a to z

• _ (underscore) is the only special symbol that can be used in an identifier

• Numeric symbols from 0 to 9 can be used but the first symbol of an identifier cannot
be a numeric symbol

• The first symbol of an identifier must be an alphabet or an underscore

• Typically application programs do not use underscore as the first symbol because many
of the identifiers in C system libraries start with an underscore

• Duplication of system names for identifiers could be confusing

• Last but not the least, C has 32 keywords also known as reserved words, which have a
predefined meaning and cannot be used as identifier names.
Some Keywords/Reserved words in C
Examples of Valid and Invalid Identifier Names

Valid Names Invalid Names

a //valid but poor style $sum //$ not allowed

student_name //ok 2names //first character should


not be a digit
_aSystemName //valid but similar to Sum-salary //Hyphen not allowed
system identifier pattern
_Bool //Boolean System id Stdnt Nmbr //Spaces not allowed

INT_MIN //System Defined Value int //keyword not allowed


Exercises:
• Identify whether the following identifiers are valid. If not give reason
and correct them
(a) record1 (b) #tax (c) name_and_address (d) 1record (e) name
(f) name-and-address (g)file_3 (h) name and address (i) return
(j) 123-45-6789

• Assume a version of C that can recognize only the first 8 characters


of an identifier name, though the identifier names can be arbitrarily
long. Which of the following pairs of identifier names are considered
to be identical and which are distinct?
(a) name, names (b) address, Address (c) list1, Iist2
(d) char1,char_1 (e) identifier_1, identifier_2 (f) answer, ANSWER
Solution to Exercises
(a) record1 - valid (b) #tax - invalid (#character not allowed)
(c) name_and_address - valid (d) 1record – invalid – first character
cannot be a number (e) name – valid (f) name-and-address –
invalid (hyphen not allowed) (g)file_3 - valid (h) name and
address - invalid (spaces not allowed) (i) return – invalid
(keyword) (j) 123-45-6789 – invalid (contains all numbers)

(a) name, names - distinct (b) address, Address - distinct (c) list1,
Iist2 – distinct (d) char1,char_1 - distinct (e) identifier_1,
identifier_2 - identical (f) answer, ANSWER - distinct
Types in C
• A type defines a set of values and a set of operations that can be
applied on those values

• For example a light switch can be compared to a type


• It can have two values on and off
• Only two operations can be applied to the light switch: turn on
and turn off

• The defined set of types in C can be divided into four general


categories: void, integral, floating-point and derived
Types in C

C Types

void Integral Floating Point Derived

Boolean Character Integer Real Imaginary Complex


Types in C
• Void type:
• Is designated by the key word void
• Has no values and no operations
• Very useful type
• Used in cases where a function has no parameters or return values
and for many other purposes which will be covered in the
forthcoming topics
• Integral type:
• Integral types cannot contain fractional parts i.e., they can contain
only whole number quantities
• C Language has three kinds of integral types: Boolean, Character
and Integer
Types in C
• Boolean:
• Can represent only two values: true or false
• Is referred to by the key word bool
• Boolean type is stored in the memory as 0 or 1
• Character:
• A character data type holds any single symbol from the C character set
• Is referred to by the key word char
• The size of a character is 1 byte or 8 bits
• Integer:
• Is a number without a fraction part (whole numbers)
• C supports four different sizes of integer data types: short int, int, long int and
long long int
• C defines these data types so that they can be organized from smallest to
largest
Types in C
Integer No of Minimum value Maximum Value
Type Bytes that can be stored that can be stored
short int 2 -32,768 32,767

int 4 -2,147,483,648 2,147,483,647

long int 4 -2,147,483,648 2,147,483,647

long long int 8 -9,223,372,036,854,775,807 9,223,372,036,854,775,806

• A short int can also be referred to as short


• A long int can also be referred to as long
• A long long int can also be referred to as long long
• Although the size of a data type is machine dependent, c requires the following relation
ship to be always true:
size of short <= size of int <=size of long <= size of long long
Types in C
• Depending on the usage of sign, each integer size can be of two forms – signed and
unsigned

• If the integer is a signed number, one bit must be used for sign

• Positive sign is indicated by a sign bit with 0 value

• For example, assuming 16 bit representation, the integer number 5 is represented as 0000
0000 0000 0101

• Negative sign is indicated by a sign bit with 1 value

• For example, assuming 16 bit representation, the integer number -5 is represented as


1000 0000 0000 0101

• Unsigned integer can store a positive number that is twice as large as the signed integer of
same size

• However the actual sizes are dependent on the physical hardware


Types in C
• Floating – Point:
• C standard recognizes 3 floating point types: real, imaginary and
complex
• Real:
• Real type holds values that consists of an integral and a fractional
part e.g., 43.32
• Real type can further be classified into three different types
based on size as float, double and long double
• Similar to integer data type, C defines the real types so that they
can be organized from smallest to largest
• Regardless of machine type, C requires the following
relationship to be true:
size of float <= size of double<=size of long double
Types in C

Integer No of Minimum value Maximum Value


Type Bytes that can be stored that can be stored

float 4 3.4 e -38 3.4 e 38

double 8 1.7 e -308 1.7 e 308

long double 12 3.4 e - 4932 1e+493


Types in C
• Imaginary type:
• Imaginary types is one of the components of complex type
• Similar to real type can be of three different sizes: float imaginary,
double imaginary and long imaginary
• Most C implementations do not support the imaginary type yet
and the functions to handle them are not a part of the standard
• Complex Type:
• Implemented by most compilers
• Similar to real type can be of three different sizes: float complex,
double complex and long long complex
• The size needs to be the same in both real and imaginary part
Summary of Types in C

Category Type C Implementation

Void Void void

Integral Boolean bool

Character char

Integer short int, int, long int, long long int

Floating-Point Real float, double, long double

Imaginar float imaginary, double imaginary, long double imaginary


y
Complex float complex, double complex, long double complex
Variables in C (Recall Name bindings in CTPS)
• Variables are named memory locations which have a type such as
integer or character
• The type specified for a variable determines the values that a variable
may contain and the operations that may be used with its values
• Variable Declaration:
• Each variable in a program must be declared and defined
• A C declaration is giving name to a variable
• While creating variables, declaration gives them a symbolic
name
• Generally speaking where a variable is located in memory is not
a programmer’s concern, it is a concern of a compiler
• A Programmer must be able to access the data through their
symbolic names, i.e., identifiers or variables
Variables in C
• Variable Definition:
• Definitions are used to create the variable
• Definition of a C variable assumes that declaration has been done
already or is being done at the same time as definition
• Once defined, variables are used to hold the data required by the
program for its operation
• Definition reserves memory for the variables
Variables in C
• Some Examples of Variable declaration and definition are illustrated below:
char option; Variable’s identifier
int i;
long long natl_debt;
Variable’s type
float payRate;
double pi;

• A variable can be of type except void.

• To create a variable, first specify the type, which automatically specifies its size
(precision) and then its identifier

• For example
float price;

denotes the definition of a real variable named price of type float


Variables in C
• Some examples of different styles of readable variable declarations
and definitions:

bool fact;
short maxItems; //Usage of a capital letter (I) as a word separator
long long pi_value; // usage of underscore as word separator
float payRate; //Usage of a capital letter (I) as a word separator
double tax;
float complex voltage;
int a,b; //Multiple variables of same type declared on same line
char option, kind
Variables in C
• Variable initialization
• When variables are defined they usually contain meaningless values left over in
the memory space from previous use
• Hence there is a need to store data in them by initialization before accessing
the values to avoid unwanted bugs in the program
• With a few exceptions (seen later) variables are not initialized automatically
• A variable is initialized with the prescribed data required when the function
starts
• A variable can be initialized at the time of declaration using an initializer
• An initializer, when present establishes the first value that the variable will
contain
• To initialize a variable when it is defined, the identifier is followed by an = sign
and then the initializer , which is the value the variable is to contain when the
function starts
• An example initialization format is shown below:
int count = 0;
• Every time the function containing count is entered, count is set to zero
Variables in C
• What will be the result of the following initialization?
int count, sum=0;
• Are both count and sum initialized or only sum is initialized?
• Answer:
• The initializer applies only to the variable defined immediately
before it
• Therefore only sum is initialized!
• If both variables need to be initialized, then provide two initializers
as shown below:
int count = 0, sum =0;
Variables in C
• To avoid confusion and error, only one declaration could be specified
per line as follows:
int count = 0;
int sum =0;
• Let us revisit the example of variable declaration and definition along
with initialization and the memory representation
B option
char option = ‘y’;
14 i
int i = 25;
long long natl_debt = 1000000000000; 1000000000000 natl_debt
float payRate = 25.50; 25.50 payRate

double pi = 3.1415926536; 3.1415926536 pi

Program Memory Representation


Constants in C
• Constants are data values that cannot be changed during the
execution of a program
• Like variables, constants also have a type
• Boolean, character, integer, real, complex and string constants are
some types of constants
• Boolean Constants
• A boolean value can have only one of the two values 0 (false) or 1
(true)
• The usage of a boolean constant requires the use of the boolean
library stdbool.h
Constants in C
• Character Constants
• Character constant is a character or symbol from the character set
enclosed between two single quotes e.g., ‘a’
• Escape characters
• A character preceded by a backslash \ enclosed in a pair of single
quotes e.g., ‘\n’
• The backslash is known as an escape character
• It is used when the represented character does not have any
graphic associated with it i.e., when it cannot be printed or
entered from a keyboard
• The escape character signifies that what follows is not a normal
character but something else
• For example ‘\n’ inserts a line feed also known as new-line
character
Constants in C
Examples of representing integer constants

Representation Value Type

+123 123 int

-378 378 int

-32271L -32,371 long int

76542LU 76,542 unsigned long it

12789845LL 12,789,845 long long int


Constants in C
• Real Constants Representation Value Type
• Default form of real constant is
double 0. 0.0 double
• To have the resulting data type to
be float or long double,
.0 0.0 double
corresponding code must be used
to specify the desired data type
• f and F are used for float and l 2.0 2.0 double
and L are used for long double
• Upper case alphabets are 3.1416 3.1416 double
recommended for representing
the codes
-2.0f -2.0 float

3.1415926536L 3.1415926536 long


double
Constants in C
• Complex constants
• Are widely used in engineering
• Coded as two parts – real part and imaginary part, separated by a plus
sign
• Real part is coded using the real format rules
• Imaginary part is coded as a real number times (*) the imaginary constant
(_Complex_I)
• If the complex.h library is included, the imaginary part can be
abbreviated as I
Representation Value Type

12.3 + 14.4 * I 12.3 + 14.4 * (-1)1/2 double complex

14F + 16F * I 14 + 16 * (-1)1/2 float complex

1.4736L + 4.56756L * I 1.4736 + 4.56756 * (-1)1/2 long double


complex
Constants in C
• String Constants
• A string constant is a sequence of zero or more characters enclosed in double
quotes
• Examples of strings
“” // a null string
“h” // a string containing only a single letter h (not a
//character)
“Hello world\n”
“HOW ARE YOU”
“Good Morning!”
• Difference between a null character ‘\0’ and a null string “”
• Null character represents no value
• As a character it is 8 zero bits
• An empty string on the other hand is a string containing nothing
• Single quotes are used for character constants and double quotes are used
for string constants
Constants in C
• Coding constants
• There are three different ways to code constants in programs – literal
constants, defined constants and memory constants

• Literal Constants
• A literal is an unnamed constant used to specify data

• If it is known that the data value cannot be changed, the data value is itself
coded in a statement

• Literals are coded as a part of a statement using the constant format described
previously

• For example, the literal 5 is used in the following statement


a = b + 5;
Constants in C
• Defined Constants
• Another way to designate a constant using the preprocessor
#define
• Like all preprocessor commands, it is prefixed with # (pound
sign)
• The defined constants are usually placed at the beginning of a
program (although they are legal anywhere in the program)
• Placing them at the beginning of the program makes them easy to
find and change
• An example for a typical define command may be
#define SALES_TAX_RATE .0825
The sales tax rate may change more often and it and other similar
constants at the beginning of the program enables one to find and
change them easily
Constants in C
• When the preprocessor formats the program for the language translator, it
replaces each defined name SALES_TAX_RATE with its defined value (.0825)
wherever it is found in the source program

• The preprocessor does not evaluate the definition in any way – it just blindly
makes the substitution

• Memory Constants
• Memory constants use a C type qualifier – const to indicate that the data
cannot be changed

• The syntax or format of a memory constant is

const type identifier = value;


Constants in C
• A variable declaration does nothing more than giving a type and size to a named
object in memory
• A Memory constant – const fixes the contents of the memory location such that
it cannot be changed
• This is similar to the concept of literal but it is named
• The following code creats a memory constant cPi
• To indicate that it is a constant, it is prefixed with c
Example: const float cPi = 3.14159;
• Points to be observed
• The type qualifier comes first
• There must be an initializer – If there is no initializer then cPi would contain
whatever was present in the memory location assigned to cPi
• Finally since cPi is a constant, it cannot be changed
What has been discussed?
• Background of C Language
• Writing, Compiling and Executing C Programs
• Comments in C language
• How to make valid and good identifiers in C
• Basic data types in C
• Declaring and defining variables in C
• Different types of constants in C

You might also like