0% found this document useful (0 votes)
6 views

Chapter 2 – Introduction to C Programming

Uploaded by

hauletraders
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Chapter 2 – Introduction to C Programming

Uploaded by

hauletraders
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 63

COMPUTER

PROGRAMMING BASIC
– IN C
MODULE CODE: CS 6224
MODULE NAME: Computer Programming Basic in C
FACILITATOR: Pangahela, R. A
Email: [email protected] Phone: +255 742 926 569
COMPUTER
PROGRAMMING BASIC – IN C
2 – INTRODUCTION TO C PROGRAMMING
Chapter 2 – Introduction to C Programming
2.1 An Example C Program
2.2 Your First C Program
2.3 C Tokens
2.4 Displaying Text
2.4.1 Escape Codes
2.5 Data Types
2.5.1 Primitive Data Types
2.5.2 Modifiers
2.6 Variables
2.6.1 Declaring Variables
2.6.2 Constants
2.7 Displaying Numbers
2.8 Formatted Input
The heart of this program is the function printf,
which actually does the work. The C language is built
from functions like printf that execute different
tasks. The functions must be used in a framework
which starts with the word main( ) , followed by the
block containing the function(s) which is marked by
braces ({ } ). The main( ) is a special function that
is required in every C program.
The first line starting with characters / * and ending
with characters * / is a comment. Comments are
used by programmers to add remarks and
explanations within the program. It is always a
good practice to comment your code whenever
possible. Comments are useful in program
maintenance. Most of the programs that people
write needs to be modified after several months or
years. In such cases they may not remember why
they have written a program in such a manner.
In such cases comments will make a programmer’s
life easy in understanding the source code and the
reasons for writing them. Compiler ignores all the
comments and they do not have any effect on the
executable program.
Comments are of two types; single line comments
and block comments. Single line comments start
with two slashes {/ / } and all the text until the end
of the line is considered a comment. Block
comments start with characters / * and end with
characters * / . Any text between those characters
is
considered a block of comments.
Literals are factual data represented in a language.
Numeric constants are an uninterrupted sequence
of digits (possibly containing a period). Numerical
values such as 123, 10000 and 99.99 are
examples.
C identifiers can be very long and so it allows
descriptive names like “number_of_students” and
“Total_number_of_cars_produced_per_year”.
Sometimes a C compiler may consider only the
first 32 characters in an identifier. While defining
identifiers programmers should follow some of the
naming standards for better readability of the
program. One such standard is the use of
underscores symbol (_) to combine two words
(example: sub_total).
/* Program-2.4 */
#include <stdio.h>
int main()
{
printf("Hi there");
printf("How are you?");
return 0;
}
If you compile this program and run it, the displayed
output is:
Hi thereHow are you?
Now the output is
Hi there
How are you?
2.5 Data Types
A data type in a programming language is a set of
data with values having predefined characteristics
such as integers and characters. The language
usually specifies the range of values for a given
data type, how the values are processed by the
computer and how they are stored. Storage
representations and machine instructions to handle
data types differ form machine to machine.
The variety of data types available allows the
programmer to select the type appropriate to the
needs of the application as well as the machine. C
supports a number of data types; if they are not
enough programmers can also define their own
data types. C supports three classes of data types:
1. Primitive (or basic) data types – these are the
fundamental data types supported by the
language. These can be classified as integer
types, floating point types and character
types.
2. User defined data types – based on the
fundamental data types users can define their
own data types. These include type defined
data types (using typedef keyword) and
enumerated types (using enum keyword).
3. Derived data types – programmers can derive
data types such as arrays, structures, unions
and pointers by combining several data types
together.
2.5.1 Primitive Data Types
The C language supports five primitive data
types; namely integers (int) floating point numbers
(float), double precision floating point numbers
(double), characters (char) and void (void). Many of
these data types can be further extended as long int
and long double. Each of these data types requires
different storage capacities and has different range of
values depending on the hardware (see Table 2.3).
Character (char) type is considered as an integer
type and actual characters are represented based on
their ASCII value.
2.5.2 Modifiers
The basic data types can be modified by adding
special keywords called data type modifiers to produce
new features or new types. The modifiers are signed,
unsigned, long and short. For example short int
represents fairly small integer values and require half the
amount of storage as regular int numbers. Except with
void type, the modifiers can be used with all the basic
data types as shown in Table 2.4.
2.6.1 Declaring Variables
In order to use a variable in C, the programmer
must first declare it specifying the data type. The
most important restriction on using a variable in C
is that they have to be declared at the beginning
of the program. The syntax to declare a new
variable is to first write the data type then followed
by a valid variable identifier as given in the
following examples:
int a;
float total;
unsigned int index_no;
short int number_of_students;
Above set of expressions declared, variables “a”
as an integer, “total” as a floating point number,
“index_ no” as unsigned integer (since there are
no negative index numbers) and “num ber_ of_
students” as a short integer.
Multiple variables belonging to the same data type
can be defined as separate set of expressions or
by listing variable names one after the other
(should be separated by a coma sign (,)).Consider
the following examples:
int a;
int b;
float total;
float sub_total;
previous variables can also be defined as:
int a,b;
float total, sub_total;
After declaring a variable it should be initialized
with a suitable value. In C, an uninitialized variable
can contain any garbage value therefore the
programmer must make sure all the variables are
initialized before using them in any of the
expressions. Initializing a variable can be done
while declaring it, just after declaring it or later
within the code (before accessing/evaluating its
value within an expression).
In initializing a variable any of the following three
approaches are valid.
int a;
a = 10;
or
int a=10;
or
int a;
--------
--------
a = 10;
2.7 Displaying Numbers
When displaying numbers special care must be
given to the data type. Each data type has to be
used with printf function in a specific format. In
order to display the correct values using the
printf function conversion specifiers should be
used. They are used to instruct the compiler
about the type of numbers appearing in the
program, which in turn determines the suitable
memory storage locations.
The conversion specifiers are also referred as format
characters or format specifiers. Table 2.5 summarises
conversion specifies supported by the printf function.
Consider the example given below:
/* Program-2.6 */
#include <stdio.h>
int main()
{
printf("%d\n",128);
printf("%f\n",128.0);
return 0;
}
Executing above program will display the following:
128
128.000000
The first number is of the type integer while the
second number is of the type float. Conversion
specifier %d stands for decimal while %f stands for
float. Incorrect use of format characters would result
in wrong outputs.
Exercise 2.2: Rewrite Program-2.3 and try
swapping the %d and %f. Run your program and
observe the output.
Exercise 2.3: Identify the output of each printf
function call given in Program-2.7. Execute the
program and observe the outputs.

Consider Program-2.8 which makes use of


variables. In this program variables “a” and “b”
are initialized with values 10.0 and 3 and the
answer (b/a) is stored in variable “c”.
/* Program-2.7 */
#include <stdio.h>
int main()
{
printf("%d\n",65*2);
printf("%d\n",65/2);
printf("%f\n",65.0/2);
printf("%c\n",65);
printf("%x\n",255);
return 0;
}
/* Program-2.8 */
#include <stdio.h>
int main()
{
int a = 3;
float b = 10.0;
float c;
c = b/a;
printf("A is %d\n",a); //decimal value
printf("B is %d\n",b); //decimal value
printf("Answer is %f\n",c); //floating point value
return 0; }
Executing Program-2.8 will display the following:
A is 3
B is 0
Answer is 3.333333
In Program-2.8 you may wish to see the answer
appearing in a more manageable form like 3.3 or 3.33
rather than 3.333333. This can be achieved by using
modifiers along with the format characters in order to
specify the required field width. The format %.0f will
suppress all the digits to the right of the decimal point,
while the format %.2f will display first two digits after
that decimal point.
Exercise2.4: Modify Program-2.8 so that it displays
an answer which is correct up to 2 decimal points.

Exercise 2.5: Write a program to assign the


number 34.5678 to a variable named “number”
then display the number rounded to the nearest
integer value and next the number rounded to two
decimal places
Program-2.9 illustrates the use of character
variables.
/* Program-2.9 */
#include <stdio.h>
int main()
{ char first_letter;
first_letter = ‘A’;
printf("Character %c\n", first_letter); //display character
printf("ASCII value %d\n", first_letter); //display ASCII
value
return 0; }
Executing Program-2.9 will display the following:
Character A
ASCII value 65
2.8 Formatted Input
We have already used printf function to
display formatted output. The scanf is a similar
function that is used to read data into a program.
The scanf function accepts formatted input from
the keyboard. Program-2.10 demonstrates the use
of the scanf function:
/* Program-2.10 */
#include <stdio.h>
int main()
{
float a,b,sum;
scanf("%f", &a); // read 1st number
scanf("%f", &b); // read 2nd number
sum = a + b; // total
printf("a+b = %f\n", sum); //display answer as
float
return 0;
}
END
THANK YOU

You might also like