CSC 203 Week 3 B
CSC 203 Week 3 B
Falana O. J.
falanaoj@funaab.edu.ng
Introduction to C
Programming
language
HISTORY OF C
• In 1972 Dennis Ritchie at Bell Labs writes C and in 1978 the
publication of The C Programming Language by Kernighan
& Ritchie caused a revolution in the computing world
C is used:
system software Compilers, Editors, embedded systems
data compression, graphics and computational geometry,
utility programs
databases, operating systems, device drivers, system level
routines
there are zillions of lines of C legacy code
also used in application programs
Basics of C Environment
• C systems consist of 3 parts
– C development environment
– Language
– C Standard Library
Program edited in
Editor and stored
Phase 1 Editor Disk on disk
Preprocessor
program processes
Phase 2 Preprocessor Disk the code
Primary memory
Puts program in
memory
Phase 5 Loader
Primary memory
Takes each instruction
and executes it storing
Phase 6 CPU new data values
C Compilers, Linkers, Loaders
Few notes on C program…
C is a Structured programming language.
C is case-sensitive
Word, word, WorD, WORD, WOrD, worD, etc are all different
variables / expressions
Eg. sum = 23 + 7
What is the value of Sum after this addition ?
The General Form of a Simple Program
• Simple C programs have the form
directives
int main ()
{
statements
}
• C uses { and } in much the same way that some other languages use words like begin
and end.
• Even the simplest C programs rely on three key language features:
– Directives
– Functions
– Statements
Directives
• Before a C program is compiled, it is first edited by a preprocessor.
• Commands intended for the preprocessor are called directives.
• Example:
#include <stdio.h>
• <stdio.h> is a header containing information about C’s standard I/O
library.
• Directives always begin with a # character.
• By default, directives are one line long; there’s no semicolon or other
special marker at the end.
• Two most common directives :
– #include
– #define
Functions
Every C program consists of one or more modules called FUNCTIONS.
One of the functions must be called main where the execution begins
when the program is run.
A function is a self-contained module of code that can accomplish some
task.
A function is a sub-unit of a program that performs a specific task.
#include <stdio.h>
The main() function is
/* The simplest C Program */ always where your program
int main(int argc, char **argv) starts running.
{
Blocks of code (“lexical
printf(“Hello World\n”); scopes”) are marked by { … }
return 0;
}
__extension__ typedef unsigned long long int __dev_t; In Preprocessing, source code is “expanded” into
__extension__ typedef
__extension__ typedef
unsigned int
unsigned int
__uid_t;
__gid_t; a larger form that is simpler for the compiler to
__extension__ typedef
__extension__ typedef
unsigned long int
unsigned long long int
__ino_t;
__ino64_t; understand. Any line that starts with ‘#’ is a
__extension__ typedef
__extension__ typedef
unsigned int
long int
__nlink_t;
__off_t; line that is interpreted by the Preprocessor.
__extension__ typedef long long int __off64_t;
extern void flockfile (FILE *__stream) ;
extern int ftrylockfile (FILE *__stream)
extern void funlockfile (FILE *__stream)
;
; • Include files are “pasted in” (#include)
int main(int argc, char **argv)
{ • Macros are “expanded” (#define)
printf(“Hello World\n”);
return 0; • Comments are stripped out ( /* */ , // )
}
• Continued lines are joined ( \ )
#include <stdio.h>
void main()
{
printf("Hello World \n");
}
Simple C Program
• Line 1: #include <stdio.h>
• As part of compilation, the C compiler runs a program called the
C preprocessor. The preprocessor is able to add and remove code
from your source file.
• In this case, the directive #include tells the preprocessor to
include code from the file stdio.h.
• This file contains declarations for functions that the program
needs to use. A declaration for the printf function is in this file.
Line 2: void main()
• This statement declares the main function.
• The "void" specifies the return type of main. In this case, nothing
Simple C Program
Line 3: {
This opening bracket denotes the start of the program.
Line 5: }
This closing bracket denotes the end of the program.
C Program Structure
• Another example of simple program in C
#include <stdio.h>
int main()
{
printf(“I love programming\n”);
printf(“You will love it too once
”);
printf(“you know the trick\n”);
return 0;
}
The output
• The previous program will produce the
following output on your screen
I love programming
You will love it too once you know the trick
Input/Output Functions
• A few functions that are pre-defined in the header file
<stdio.h> such as :
– printf()
– scanf()
– getchar() & putchar()
1. The printf function
• Used to send data to the standard output (usually the monitor) to be printed
according to specific format.
• General format:
1.printf(“string literal”);
• A sequence of any number of characters surrounded by double quotation
marks.
• E.g printf(“Thank you\n”); Thank you
The printf function cont…
2.printf(“format string”, variables);
• Format string is a combination of text, format specifier and escape
sequence.
• E.g printf (“Total sum is: %d\n”, sum);
• \n is an escape sequence
– moves the cursor to the new line
Placeholder / FORMAT Specifier
No Format Specifier Output Type Output Example
1 %d Signed decimal integer 76
2 %i Signed decimal integer 76
3 %o Unsigned octal integer 134
4 %u Unsigned decimal integer 76
5 %x Unsigned hexadecimal (small letter) 9c
6 %X Unsigned hexadecimal (capital letter) 9C
7 %f Integer including decimal point 76.0000
8 %e Signed floating point (using e notation) 7.6000e+01
9 %E Signed floating point (using E notation) 7.6000E+01
10 %g The shorter between %f and %e 76
11 %G The shorter between %f and %E 76
12 %c Character ‘7’
13 %s String ‘76'
2. The scanf function
Reads data from the standard input device
(usually keyboard) and stores it in a variable.
General format:
scanf(“format string”, &variable);
E.g scanf (“%d” , &a);
• Example:
Comments
Text surrounded by /* and */ is ignored by computer
Used to describe program
#include <stdio.h>
Preprocessor directive
Tells computer to load contents of a certain file
<stdio.h> allows standard input/output operations
int main()
Parenthesis used to indicate a function
int means that main "returns" an integer value
Braces ({ and }) indicate a block
The bodies of all functions must be contained in braces
return 0;
A way to exit a function
return 0, in this case, means that the program terminated normally
Right brace }
Indicates end of main has been reached
A Simple C Program
1 /* Addition program */
2 #include <stdio.h>
3
4 int main()
5 {
6 int integer1, integer2, sum; /* declaration */
7
8 printf( "Enter first integer\n" ); /* prompt */
9 scanf( "%d", &integer1 ); /* read an integer */
10 printf( "Enter second integer\n" ); /* prompt */
11 scanf( "%d", &integer2 ); /* read an integer */
12 sum = integer1 + integer2; /* assignment of sum */
13 printf( "Sum is %d\n", sum ); /* print sum */
14
15 return 0; /* indicate that program ended successfully */
16 }
OUTPUT
Output: Number is 4
MORE ExampleS
#include <stdio.h>
// program reads and prints the same thing
int main()
{
int number ;
printf (“ Enter a Number: ”);
scanf (“%d”, &number);
printf (“Number is %d\n”, number);
return 0;
}
int main ()
{
const double pi = 3.412;
double height, radius, base, volume;
#include <stdio.h>
#define pi 3.142
int main ()
{
double height, radius, base, volume;