History of C
3
1970
1972
TraditionalC
Dennis Ritchie
1978
K & R C
Kernighan & Dennis Ritchie
1989
ANSI C
ANSI Committee
1990
ANSI/ISO C
ISO Committee
1999
C99
Standardization Committee
2000
1980 1990
4.
History of C
4
LanguageYear Developed By
Algol 1960 International Group
BCPL 1967 Martin Richard
B 1970 Ken Thompson
Traditional C 1972 Dennis Ritchie
K & R C 1978 Kernighan & Dennis Ritchie
ANSI C 1989 ANSI Committee
ANSI/ISO C 1990 ISO Committee
C99 1999 Standardization Committee
5.
Introduction to C
5
C is a General-purpose programming language
Originally developed for UNIX
C is mid-level programming language
Structured programming language
C influenced by BCPL (1967, Martin Richard) &
B (1970, Ken Thompson) [type less languages]
6.
Introduction to C
6
C provides variety of data types in various sizes of data [Integers, Floating-
point numbers, and Characters].
C supports derived data types for customized way of handling data
[pointers, arrays, structures & Unions].
C Expressions can be created with operators and operands.
C contains Variety of Statements can be used to construct programs.
C provides control-flow constructions facility to create structured programs
[if…else, switch, while, for, do, break and continue].
C supports in creating function with variety of operational behaviours.
How to runthe program in gcc Compiler
9
#include <stdio.h>
main()
{
printf(“hello, worldn”);
}
1. Write the program
Using text editor / IDE
2. Compile the program
gcc file.c
3. Execute the program
./a.out
Output: hello, world
Compilation of CProgram
14
2. Compiling
- Using inbuilt processor to convert intermediate
file (area.i) to Assembly file (area.s)
15.
Compilation of CProgram
15
3. Assembling
- Converting Assembly file (area.s) to Machine
understandable type Object file (area.o)
16.
Compilation of CProgram
16
4. Linking
•Linking is a process of including the library files into our program.
• Library Files are some predefined files that contain the definition of the functions
in the machine language and these files have an extension of .lib.
•Some unknown statements are written in the object (.o/.obj) file that our
operating system can't understand.
• we use Library Files to give meaning to some unknown statements from
our object file.
17.
Compilation of CProgram
17
// Simple Hello World program in C
#include<stdio.h>
int main()
{
// printf() is a output function which prints
// the passed string in the output console
printf("Hello World!n");
return 0;
}
.file "hello.c"
.def ___main; .scl 2; .type 32;
.endef
.section .rdata,"dr"
LC0:
.ascii "Hello World!0"
.text
.globl _main
.def _main; .scl 2; .type 32;
.endef
_main:
LFB12:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
andl $-16, %esp
subl $16, %esp
call ___main
movl $LC0, (%esp)
call _printf
movl $0, %eax
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE12:
.ident "GCC: (MinGW.org GCC-6.3.0-1) 6.3.0"
.def _printf; .scl 2; .type 32;
.endef
Hello.c
Hello.s
18.
Compilation of CProgram
18
We have a C Program file with an extension of .c i.e. hello.c file.
Step 1 is preprocessing of header files, all the statements starting with # (hash
symbol) and comments are replaced/removed during the pre-processing with
the help of a pre-processor. It generates an intermediate file with .i file
extension i.e. a hello.i file.
Step 2 is a compilation of hello.i file. Compiler software translates the hello.i file
to hello.s with assembly level instructions (low-level code).
Step 3, assembly-level code instructions are converted into machine-
understandable code (binary/hexadecimal form) by the assembler. The file
generated is known as the object file with an extension
of .obj/.o i.e. hello.obj/hello.o file.
Step 4, Linker is used to link the library files with the object file to define the
unknown statements. It generates an executable file with .exe/.out extension
i.e. a hello.exe/hello.out file.
•Next, we can run the hello.exe/hello.out executable file to get the desired
output on our output window, i.e., Hello World!.
19.
Program Explanation
19
Programconsists of functions and
variables.
A function contain statements that
specify the computing operations to be
done.
Variables store values used during the
computation.
#include <stdio.h>
main()
{
printf(“hello, worldn”);
}
20.
Program Explanation
20
Cfunctions are like subroutines.
In this example you can spot main() function
You are free to name functions but main() is
special
All C programs begin executing from main()
function
It means every C program must have main() function
#include <stdio.h>
main()
{
printf(“hello, worldn”);
}
21.
Program Explanation
21
#include <stdio.h>
main()
{
printf(“hello,worldn”);
}
Include
information
about
standard
library
Define a function
named main that
receives no argument
values
Statements
of main are
enclosed in
braces { }
[Body of the
function]
main() calls the library
function printf to print
this sequence of
characters
n in the string is C
notation for newline
character, which when
printed advances the
output to the left
margin on next line
Variables and ArithmeticExpressions
23
#include<stdio.h>
/*print Fahrenheit-Celsius table
for fahr= 0,20,......,300 */
main()
{
int fahr,celsius;
int lower,upper,step;
lower=0; //lower limit of temprature table
upper=300; /* Upper Limit */
step=20; //step size
fahr = lower;
while(fahr <= upper) {
celsius = 5 * (fahr-32) / 9;
printf("%dt%dn",fahr,celsius);
fahr = fahr + step;
}
}
This program prints the Celsius value
to its equivalent Fahrenheit value
using formula
0
C = (5/9)(0
F-32)
F C
0 -17
20 -6
40 4
60 15
80 26
100 37
120 48
140 60
160 71
180 82
200 93
220 104
240 115
260 126
280 137
300 148
24.
Variables and ArithmeticExpressions
24
The line starts from /* and end with */
is called Comment, which will be
ignored by the compiler
Comments are two type
1.Multi-line comment
/* …….
……….
……… */
2.Single-line comment
//………..
Stmts; //………
Variables and ArithmeticExpressions
30
• printf is a general-purpose output formatting
function. Its not part of C.
• The printf function translates internal values to
characters.
• printf converts, formats, and prints its arguments
on the standard output under control of the
format.
• It returns the number of characters printed.
• Format strings contains two types of objects:
1. Ordinary characters, which are copied to the
output stream
2. Conversion specification, each of which causes
conversion and printing of the next successive
argument to printf.
3. Each conversion specification begins with a % and
ends with a conversion character