Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Function
C Development Environment
Phase 1 : Editor Disk Program is created using the Editor and stored on Disk. Preprocessor program processes the code. Compiler creates object code and stores it on Disk. Linker links object code with libraries, creates a.out and stores it on Disk
Phase 2 :
Preprocessor
Disk
Phase 3 :
Compiler
Disk
Phase 4 :
Linker
Disk
: .
CPU takes each instruction and executes it, storing new data values as the program executes.
C Program Structure
An example of simple program in C
#include <stdio.h>
void main(void) { printf(I love programming\n); printf(You will love it too once ); printf(you know the trick\n); }
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
Preprocessor directives
a C program line begins with # provides an instruction to the C preprocessor It is executed before the actual compilation is done. Two most common directives :
#include #define
In our example (#include<stdio.h>) identifies the header file for standard input and output needed by the printf().
Function main
Identify the start of the program Every C program has a main ( ) 'main' is a C keyword. We must not use it for any other variable. 4 common ways of main declaration
int main(void) { return 0; void main(void) { main(void)
main( )
{
Since the opening brace indicates the start of a segment with the closing brace indicating the end of a segment, there must be just as many opening braces as closing braces (this is a common mistake of beginners)
Statement
A specification of an action to be taken by the computer as the program executes. Each statement in C needs to be terminated with semicolon (;) Example: #include <stdio.h>
void main(void) { printf(I love programming\n); printf(You will love it too once ); printf(you know the trick\n); }
Principles of Programming - NI 2005
10
Statement cont
Statement has two parts :
Declaration
The part of the program that tells the compiler the names of memory cells in a program
Executable statements
Program lines that are converted to machine language instructions and executed by the computer
11
C program skeleton
In short, the basic skeleton of a C program looks like this:
#include <stdio.h> void main(void) { Start of segment statement(s); }
End of segment Preprocessor directives
Function main
12
Identifiers
Words used to represent certain program entities (variables, function names, etc). Example:
int my_name;
my_name is an identifier used as a program variable
13
Example
H2o Number1; _area XsquAre my_num R*S+T #@x%!! struct; printf;
14
Variables
Variable: - is a data name that may be used to store a data value. - it may take different values at different times during execution. - Some examples: Average total class_group
15
Declaration of Variables
The syntax for declaring a variable is as follows:
data-type v1,v2,..vn; v1,v2,..vn are the names of variables. Variables are separated by commas. A declaration statement must end with a semicolon. For example, int count; int number, total; double ratio;
Principles of Programming - NI 2005 16
int
used to declare numeric program variables of integer type whole numbers, positive and negative keyword: int int number; number = 12;
Principles of Programming - NI 2005 17
Constants
Entities that appear in the program code as fixed values. Any attempt to modify a CONSTANT will result in error. 4 types of constants:
Integer constants
Positive or negative whole numbers with no fractional part Example:
Constants cont
Character constants
A character enclosed in a single quotation mark Example:
const char letter = n; const char number = 1; printf(%c, S);
Output would be: S
String constants
Example: - char string5[20] = "Hello, world!"; - printf("%s\n", string5); /* Output : Hello, world!
Principles of Programming - NI 2005 21
#define
You may also associate constant using #define preprocessor directive #include <stdio.h> #define pi 3.412 void main(void) { double height, radius, base, volume; printf(Enter the height and radius of the cone:); scanf(%lf %lf,&height,&radius); base = pi * radius * radius; volume = (1.0/3.0) * base * height; printf(\nThe volume of a cone is %f , volume); }
Principles of Programming - NI 2005 23
Input/Output Functions
A C function that performs an input or output operation A few functions that are pre-defined in the header file stdio.h such as :
printf() scanf() getchar() & putchar()
24
25
\n is an escape sequence
moves the cursor to the new line
26
Escape Sequence
Escape Sequence \a \b \f \n \r \t \v \\ \ \o \x \O
Principles of Programming - NI 2005
Effect Beep sound Backspace Formfeed (for printing) New line Carriage return Tab Vertical tab Backslash sign Octal decimal Hexadecimal NULL
27
28
General format:
scanf(Format string, &variable); Notice ampersand (&) operator : C address of operator it passes the address of the variable instead of the variable itself tells the scanf() where to find the variable to store the new value
29
%d %f %f %c %s
%d %f %lf %c %s
30
Example:
float height, weight;
31
32
Usage example: void main (void) { int num = 10; printf (% d,num); }
Principles of Programming - NI 2005 34
35
36
Summary
In this chapter, you have learned the following items:
environment of C language and C programming C language elements
Preprocessor directives, curly braces, main (), semicolon, comments, double quotes
4 basics data type and brief explanation on variable Reserved word, identifier, constant, string literal, punctuators / separators and operators. printf, scanf, getchar and putchar Usage of modifiers : placeholder & escape sequence Common programming errors : syntax error, run-time error and logic error
Principles of Programming - NI 2005 39