Uploaded by madelojoemer

C programming Fundamentals

advertisement
C programming
Fundamentals
By: JMadz
History
• The C programming language was developed by Bell Laboratories in 1972
• C programs are independent;
• Assembly language uses assembler to convert the symbolic programs to
machine code;
• Whereas, the C programming language uses a compiler to convert the source
code into machine code.
History
• The C language is case sensitive;
• It is common practice to use lowercase for both the code statements and the
comments.
• An editor is used to create a disk file for the source code, which is save with a
.c or a .cpp extension.
• The source is compiled to create an executable file that can be run on a
computer.
Structure of a C program
// this is a single-line comment.
/* this is a single line comment*/
printf(“hello world\n”); //comment for this line of code
/* this is a multiple-line comment
Used to accommodate information
That spans several lines.*/
start
editor
Source code
Compile source
code
Object code
Syntax errors
Link object file
Library files
errors
Execute program
Logic errors
end
HelloWorld.cpp
// hello world 2, a simple C program
#include <stdio.h>
main (){
printf(“hello world\n”);
Return 0;
}
Structure of a C program
• Line 2 is a processor directive used to read another file and include it in the
program.
• Processor directives begin with symbol #.
• It informs the compiler to include at this location in the program that in the
stdio.h file.
• The angle brackets <> indicate that the stdio.h file is located in a specific
machine dependent location.
Structure of a C program
• The stdio.h files are specified by the (ANSI) and include the printf()
function used in this program.
• Line 3 is a separation line.
• Line 4 consist of a function called main(), where the parentheses indicate to
the compiler that it is a function.
• The keyword void can also be included within the parentheses to indicate
that there are no arguments are being passed to the function.
Structure of a C program
• Line 5 contains a left curly barce {, which marks the beginning of the body
of the fuction.
• Left and right curly braces are also used as delimiters to group statements
together.
• Line 6 invokes the printf() function, which displays information on the
monitor screen.
• This function is part of the standard library functions contained in the C
programming environment.
Structure of a C program
• Information pertaining to the printf() function is contained in the stdio.h
header file.
• The data within the parentheses is a series of characters called a string
argument contained within double quotation marks.
• The string is called argument and is displayed on the screen, except for the
symbol (\n), which means a new line character.
• Line 6 is terminated with a semi-colon and declarations and statements are
terminated with a semicolon.
Structure of a C program
• Line 7 indicates that the function returns to calling function main().
• Line 8 is the right closing brace and indicates the end of the program
specified by the function main().
Variables and constants
• Variables stores values in the memory and the type of each variable must be
specified; that is, the variable must be declared as type character, integer an
so forth.
Variables
• A variable is a named memory location and must be declared before it is
used in the program.
• The declaration of a variable with an assigned type permits the compiler to
assign an appropriate amount of storage for the variable.
• A variable is defined by indicating the type of variable followed by the name
of the variable , which can be arrange from a single letter to 31 characters,
Some typical c variables
Type
Keyword
Meaning
Character
Unsigned character
char
unsigned character
Signed character (one byte)
Unsigned character (one byte)
Integer
Unsigned integer
int
unsigned int
Signed integer (two bytes)
Unsigned integer (two bytes)
Float
Double
float
double
Single-precision floating point
numbers
Double-precision floating-point
numbers
typical c variables
Type
Range
Bits required
Character
Unsigned character
-128 to +128
0 to 255
8 bits
8 bits
Integer
Unsigned integer
-32,768 to +32,767
0 to 65,535
16 bits
16 bits
Float
Double
10-38 to10+38
10-308 to 10+308
32bits
64bits
Download