Professional Documents
Culture Documents
1.1 Introduction To C
1.1 Introduction To C
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
5 Department of CSE
Structure of a C Program
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
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
/*
**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*/
• 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
• 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
• Numeric symbols from 0 to 9 can be used but the first symbol of an identifier cannot
be a numeric symbol
• Typically application programs do not use underscore as the first symbol because many
of the identifiers in C system libraries start with an underscore
• 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
(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
C Types
• If the integer is a signed number, one bit must be used for sign
• For example, assuming 16 bit representation, the integer number 5 is represented as 0000
0000 0000 0101
• Unsigned integer can store a positive number that is twice as large as the signed integer of
same size
Character char
• To create a variable, first specify the type, which automatically specifies its size
(precision) and then its identifier
• For example
float price;
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
• 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
• 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