CSE101R01 – U1 –L3
Fundamentals of Computer Science
Lecture 3
Introduction to C Programming
Inventor of C Language
2
History of C
3
1970
1972
Traditional C
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
History of C
4
Language Year 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
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]
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.
Getting Started
7
Getting Started
8
#include <stdio.h>
main()
{
printf(“hello, worldn”);
}
How to run the 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
Writing a C Program - IDEs
10
Atom
Compilation of C Program
11
Compilation process in C involves four steps:
1.Preprocessing
2.Compiling
3.Assembling
4.Linking
Compilation of C Program
12
Compilation of C Program
13
Compilation of C Program
14
2. Compiling
- Using inbuilt processor to convert intermediate
file (area.i) to Assembly file (area.s)
Compilation of C Program
15
3. Assembling
- Converting Assembly file (area.s) to Machine
understandable type Object file (area.o)
Compilation of C Program
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.
Compilation of C Program
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
Compilation of C Program
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!.
Program Explanation
19
 Program consists 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”);
}
Program Explanation
20
 C functions 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”);
}
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
Structure of C Program
22
Variables and Arithmetic Expressions
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
Variables and Arithmetic Expressions
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 Arithmetic Expressions
25
Integer data type List of variables of
Integer type
Variables and Arithmetic Expressions
26
Variables and Arithmetic Expressions
27
Variables and Arithmetic Expressions
28
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
Variables and Arithmetic Expressions
29
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
Variables and Arithmetic Expressions
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

1111_U1-T3 Introduction to C_Programming.ppt

  • 1.
    CSE101R01 – U1–L3 Fundamentals of Computer Science Lecture 3 Introduction to C Programming
  • 2.
    Inventor of CLanguage 2
  • 3.
    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.
  • 7.
  • 8.
  • 9.
    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
  • 10.
    Writing a CProgram - IDEs 10 Atom
  • 11.
    Compilation of CProgram 11 Compilation process in C involves four steps: 1.Preprocessing 2.Compiling 3.Assembling 4.Linking
  • 12.
    Compilation of CProgram 12
  • 13.
    Compilation of CProgram 13
  • 14.
    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
  • 22.
    Structure of CProgram 22
  • 23.
    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; //………
  • 25.
    Variables and ArithmeticExpressions 25 Integer data type List of variables of Integer type
  • 26.
  • 27.
  • 28.
    Variables and ArithmeticExpressions 28 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
  • 29.
    Variables and ArithmeticExpressions 29 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
  • 30.
    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