Professional Documents
Culture Documents
Chapter three: Fundamentals of C programming such as variables, constants as well as operators supported by
3.1 Chapter objectives C. The chapter also aims to familiarize the learners on how to
Therefore, by the end of this chapter, the learners should be declare constants and variables in C language. We shall also
able to: discuss the various data types supported by C programming
Differentiate between identifiers and keywords Language. Finally, we provide a summary for the chapter and
Declare and use variables in C language to store some self-testing exercise for the learners
information 3.3 Identifiers
They are name given to various program elements such as
Understand the different types of variable scopes
variables, constants, functions and arrays. Rules for naming
Understand different data types supported by C identifiers
language i). Can consists of letters and digits in any order
Understand how and when to use comments in C ii). The first character must be a letter or an underscore
programming language. symbol.
Understand the different data types supported by C ++
iii). Identifier names cannot have space
programming language
iv). Both upper and lower case letters are permitted
3.2 Introduction
This chapter aims at introducing learners to fundamental
concepts of C programming. The chapter will look at concepts
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
CMT 110: PROGRAMMING METHODOLOGY
v). The only special character that can be used in identifier 3.7 Comments in C++
definition is the underscore (_) and can be included C supports single line and multi-line comments. Comments
often in the middle of an identifier help in improving programming readability as well as serve as
documentation for others so that they can understand the
3.4 Key words
program pretty easily. All characters available inside any
These are words that have a standard predefined meaning in C
comment are ignored by C compiler. C multiple comments
programming language. E.g. main is a word that has a
predefined meaning in C language so no identifier can have start with /* and end with */. e.g.
3.5 Statements in C++ /* C++ comments can also span multiple lines */
A statement is meant to cause the computer to carry out some
For single comment, we use the double fowardslash (//). All
action. In C, all statements are usually terminated with a
statements preceded by the backslash shall be ignored by the
semicolon except for comments and control structure
compiler. e.g.
statements.
#include <stdio.h>
3.6 Include <headerfile.h>
C has some predefined header files. Any program can include Int main ()
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
CMT 110: PROGRAMMING METHODOLOGY
3.8 Data types Floating Float 4 bytes-32 bits +/- 3.4e +/- 38
Before storing values in C programming, we need to declare point (~7 digits)
variable (s) to hold their values. Variables will have different Double Double 8 bytes-64 bits +/- 1.7e +/- 308
data type depending on the kind of data they will store. While (~15 digits)
float point
declaring Identifiers, the programmer must specify a data type
Void
to help to reserve memory.
Char
As a programming language, C offers the programmer a rich
collection of built-in as well as user defined data types. We can modify several of the basic types using one or more
Following table lists down seven basic (primitive) data types of these type modifiers signed (1), unsigned (2), short (3) and
long (4) as depicted in the table that follows
Data type Keyword Typical bit Typical range
Data type Typical bit Typical range
width
width
Boolean bool I byte- 8 bits
unsigned 1byte 0 to 255
Character Char 1 byte-8 bits -127 to 127 or 0 char
to signed 1byte -127 to 127
Integer Int 4 bytes-32 bits 2147483648 to char
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
CMT 110: PROGRAMMING METHODOLOGY
signed int 4bytes -2147483648 to The sizes of variables might be different from those shown in
2147483647 the above table, depending on the compiler and the computer
short int 2bytes -32768 to 32767 you are using.
unsigned Range 0 to 65,535 3.9 Variables
short int While programming using any programming language, we may
signed Range -32768 to 32767 need to use various variables to store various information.
long int 4bytes -2,147,483,647 to values. They are identifiers whose value is allowed to change
signed 4bytes same as long int Before using a variable, we must declare it first. By declaring
long int a variable, we are simply requesting the operating system for
unsigned 4bytes 0 to 4,294,967,295 a piece of memory. This piece of memory you give a name and
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
CMT 110: PROGRAMMING METHODOLOGY
When we declare a variable, we are telling the compiler where E.g. float sum=0.0;
and how much to create the storage for the variable. A variable
3.9.2 Variable scope
definition specifies a data type, and contains a list of one or
more variables of that type as shown in the following syntax: A variable scope means the extent to which a variable is
available in a program. There are three variable scopes namely:
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
CMT 110: PROGRAMMING METHODOLOGY
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
CMT 110: PROGRAMMING METHODOLOGY
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
CMT 110: PROGRAMMING METHODOLOGY
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
CMT 110: PROGRAMMING METHODOLOGY
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
CMT 110: PROGRAMMING METHODOLOGY
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
CMT 110: PROGRAMMING METHODOLOGY
value of right operand, if yes the value of right operand, if yes true
then condition becomes true. then condition becomes true.
< Checks if the value of left (X<Y) is not true 3.12.3 Logical Operators
operand is less than the value There are following logical operators supported by C language.
of right operand, if yes then The result of logical operators can only be a Boolean value; true
condition becomes true. or false. Assume variable x holds 1 and variable y holds 0, then:
>= Checks if the value of left (X>=Y) is true
operand is greater than or equal
to the value of right operand, if
yes then condition becomes
true.
>= Checks if the value of left (X>=Y) is true
operand is greater than or equal
to the value of right operand, if
yes then condition becomes
true.
<= Checks if the value of left (X<=Y) is not
operand is less than or equal to
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
CMT 110: PROGRAMMING METHODOLOGY
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
CMT 110: PROGRAMMING METHODOLOGY
integer or float will return 4. of the table, those with the lowest appear at the bottom.
Operator precedence determines the grouping of terms in an
Condition? X : Conditional operator. If Condition is true? then it
expression, higher precedence operators will be evaluated first.
Y returns value X : or else value Y
expression. This affects how an expression is evaluated.
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
CMT 110: PROGRAMMING METHODOLOGY
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
CMT 110: PROGRAMMING METHODOLOGY
For the assignment to work successfully, the type of the variable Printf (“the area of the circle is %d\n”, area);
on the left hand side should be the same as the type returned “The area of the circle is %d\n”, is referred as control string
by the expression. while area is the variable to be outputted
%d is a conversion specifier indicating that the type of the
The general format is as follows corresponding variable to be printed is integer
Variable=expression;
3.17 Input in c
Where expression is the value we are assigning to the variable. scanf ( ) is used to read values from the keyboard then assign
A good example is presented below whereby the programs them to specified variable(s). E.g. scan("%d", &x); read a
takes the values of a and b, sums them together and assigns decimal integer from the keyboard and store the value in the
the result to a variable known as total i.e. total = a + b; memory address of the variable x
Anytime a variable appears in an expression, it represents the 3.18 scanf( ) Control String Conversion Characters
scanf( ) uses the same format specifier as the printf statement.
value currently stored in that memory location. When an
Arguments of scanf must be pointers to memory address hence
assignment statement is executed, a new value is dropped into
the use of the ampersand sign (&)
the memory location overwriting it with the new one
Character Form of output Expected argument
3.16 Output in C
c Character Pointer to char
To print a value on the console, we always use the function
printf which is defined in the standard library stdio.h in c. An d Decimal integer Pointer to int
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
CMT 110: PROGRAMMING METHODOLOGY
o Octal integer Pointer to int The d, x,o,u or i conversion characters should be preceded by
u Unsigned integer Pointer to int l if the argument is a pointer to a long rather than an a pointer
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
CMT 110: PROGRAMMING METHODOLOGY
void main()
{
int a; a = getchar(); /* read a character and assign to c */
putchar(c); /* print c on the screen */
}
3.12.6
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author