Chapter 2 - 20 21
Chapter 2 - 20 21
Fundamentals of C
Language
1
Chapter outcomes
• By the end of this chapter, the students will
• get familiar with a C program structure
• know about the fundamentals required to write a C program:
primitive types, syntax rules, etc.
• be able to translate simple algorithms into a C programs
2
Structure of a C program
Library
Function main
Algorithm ADD
Variables declaration
3
Program analysis
• #include: includes a set of functions from the header file
(stdio.h) specified between (<,>).
• main() is a special function which indicates the beginning
of the program.
• Every C program must contain the main() function. The
execution starts from this function.
• The left brace { indicates the beginning of the body of
the main function. A corresponding right brace } must
end the body of the function.
• Each statement in C needs to be terminated with
semicolon (;) 4
C Program Structure
• A C program basically consists of the following parts :
6
• time.h defines date and time handling functions.
Variables
• Variable : is a name associated with a memory cell whose
value can change.
7
Variables
• All variables must be declared before the use of the program.
8
Rules for naming variables
9
Basic Data Types
There are 4 basic data types : Int, float, double, char
int
• used to declare numeric program variables of integer type
• whole numbers, positive and negative
• keyword: int
int number;
number = 12;
float
• fractional parts, positive and negative
• keyword: float
float height;
10
height = 1.72;
Basic Data Types
double
• used to declare floating point variable of higher precision
or higher range of numbers
• exponential numbers, positive and negative
• keyword: double
double valuebig;
valuebig = 12E-3;
char
• Example of characters:
• Numeric digits: 0 – 9, Lowercase/uppercase letters
• Space (blank), Special characters: , . ; ? “ / ( ) [ ] { } * & % ^ < >
etc
• single character
• keyword: char
char my_letter; The declared character must be
my_letter = 'U'; enclosed within a single quote! 11
Constants
Entities that appear in the program code as fixed values.
Any attempt to modify a CONSTANT will result in error.
Examples:
• Integer constants
• const int MAX_NUM = 10;
• Floating-point constants (float or double)
• const double VAL = 0.5877e2; (stands for 0.5877 x 102)
• Character constants
• const char letter = ‘n’
12
Arithmetic operations in C
This table contains useful arithmetic operations in C and examples:
Subtraction - Z-1
Multiplication * X*Y*3
Division / X/2
Modulus % Z%X
(remainder of the integer
division)
13
Relational and Logical operators
This table contains relational and logical operators in C:
== Equal
!= Not Equal
19
The standard Input/Output functions
20
Output Function: printf
• printf : is a subprogram allowing to show its message
on the screen.
printf scanf
int %d %d
float %f %f
double %f or %lf %lf
char %c %c 23
string %s %s
getchar() and putchar()
• getchar() : read a character from standard input
• putchar() : write a character to standard output
Example:
#include<stdio.h>
void main()
{ char my_char;
26