0% found this document useful (0 votes)
37 views28 pages

CLang Lect02

The document provides an introduction to the C programming language including: 1. An example of a "Hello World" C program that prints "Hello World" using printf. 2. An overview of C language structure including common keywords like #include, int main(), and return 0 as well as comments, variables, and loops. 3. A brief history of C and its development from earlier programming languages.

Uploaded by

Thành
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views28 pages

CLang Lect02

The document provides an introduction to the C programming language including: 1. An example of a "Hello World" C program that prints "Hello World" using printf. 2. An overview of C language structure including common keywords like #include, int main(), and return 0 as well as comments, variables, and loops. 3. A brief history of C and its development from earlier programming languages.

Uploaded by

Thành
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Introduction to C

programming language
Department of Information System
SoICT, HUST

1
High level language
10100110 01110110
#include <stdio.h> 00100110 00000000
11111010 11111010
int main() 01001110 10100110
{ 11100110 10010110
printf(“Hello World”); 11001110 00101110
10100110 01001110
11111010 01100110
return 0;
01001110 10000110
}
etc...

Source code Machine code

• Compiler translates a program in high level


programming language into machine language

2
Why C?
• Flexible language
• Structured language
• Low level activities possible
• Standard library exists, allowing portability
• It can produce lean and efficient code
• Wide availability on a variety of computers and
widely used
• It is the foundation for other languages (C++, Java,
Perl, awk)

3
History of C
• CPL Combined Programming Language (Barron et al.,
1963)
• BCPL Basic CPL (Richards, 1969)
• B (Thompson, 1970)
• C K&R C (Ritchie, 1972)
• ANSI C American National Standards Institute C
(X3J11, 1989)
• C99 (JTC1/SC22/WG14, ISO/IEC 9899, 1999)
4
The first C program
Hello World
Algorithm C program
#include <stdio.h>

int main()
output “Hello World!” {
printf(“Hello World!”);

return 0;
}

5
C Language Structure
• #include <stdio.h>
• To declare using the standard I/O library. Other libraries: string, time,
math…
• int main()
• To declare the main() function. An C program must declare only one
main() function. The first line in the main() will implement when the
program starts.
• {…}
• The syntax to open and close a block of codes.
• printf
• the printf() function sends the output to standard output (monitor). This
function will be taught in the next week.
• return 0;
• Stop the program.

6
Syntax of C programs
• A C program is written using:
• Keywords: reserved words for specific meaning in a
program, e.g., main, if, do, while, …
• User’s names: names defined by user to specify a
variable, a function, etc. in a program.
• Specific characters: to represent expressions in a
program and make the program have structure, for
example:
• Create a block of instructions {}
• Create a string “ “

7
Keywords of C
• Flow control (6) – if, else, return, switch,
case, default
• Loops (5) – for, do, while, break, continue
• Common types (5) – int, float, double, char,
void
• structures (3) – struct, typedef, union
• Counting and sizing things (2) – enum, sizeof
• Rare but still useful types (7) – extern, signed,
unsigned, long, short, static, const
• Evil keywords which we avoid (1) – goto
• Weirdies (3) – auto, register, volatile

8
Common characters used in a
program
• {…} create a block of instructions
• “…” create a string to display
• /* … */ create a block of comment in the program
• ; End of an instruction
• other characters for formulas such as +, -, *, /, (), …

9
Identifiers
• When declare a variable or a procedure, we need to
identify it
• Principles:
• Only use alphabetic letters, numbers, underscore _
character to name an identify
• Identify must begin with an alphabetic letter
• Upper case and lower case are different
• Which identities are illegal:
• tong, 2k, trung binh, %totnghiep

10
Example of writing a program
Create the main
print number from 0 to 9 fuction for the
program

int main()
dem = 0
{
while (dem < 10)
do
{
output dem
dem = dem + 1
}
return 0;
}

11
Example …
#include <stdio.h>
print number from 0 to 9

dem = 0 int main() Declare the


while (dem < 10) { standard
do input/output
{ library
output dem
dem = dem + 1
}

return 0;
}

12
Example …
#include <stdio.h>
print number from 0 to 9
/* In tu 0 toi 9 */
dem = 0 int main()
while (dem < 10) { Comment
do
{
output dem
dem = dem + 1
}

return 0;
}

13
Example …
#include <stdio.h>
print number from 0 to 9
/* In tu 0 toi 9 */
dem = 0 int main()
while (dem < 10) {
do int dem;
{
output dem Declare a
dem = dem + 1 variable
}

return 0;
}
14
Example …
#include <stdio.h>
print number from 0 to 9
/* In tu 0 toi 9 */
dem = 0 int main()
while (dem < 10) {
do int dem;
{ dem = 0;
output dem
dem = dem + 1 Assign a
} value to dem

return 0;
}

15
Example …
#include <stdio.h>
print number from 0 to 9
/* In tu 0 toi 9 */
dem = 0 int main()
while (dem < 10) {
do int dem;
{ dem = 0;
output dem while ( dem < 10 )
dem = dem + 1 {
}
Creare a loop

}
return 0;
}
16
Example …
#include <stdio.h>
print number from 0 to 9
/* In tu 0 toi 9 */
int main()
dem = 0 {
while (dem < 10) int dem;
do dem = 0;
{ while ( dem < 10 )
output dem {
dem = dem + 1 printf(“%d\n”, dem);
}
}
return 0;
}

17
Example …
#include <stdio.h>
print number from 0 to 9
/* In tu 0 toi 9 */
int main()
dem = 0 {
while (dem < 10) int dem;
do dem = 0;
{ while ( dem < 10 )
output dem {
dem = dem + 1 printf(“%d\n”, dem);
} dem = dem + 1;
}
return 0;
}

18
What does this program do?
#include <stdio.h>

int main(){
float num;

printf(“Enter a number: “);


scanf(“%f”, &num);

if ( num < 0 ) {
printf(“%f is negative”, num);
} else {
printf(“%f is positive”, num);
}

return 0;
}

19
Algorithm of the program
/* Find the sign of a number */
input num
if (num < 0) then
{
output “negative number”
}
else
{
output “positive number”
}

20
Compile a C program

Compiling phrase Link phrase

Error can appear at compiling phrase or link phrase

21
Compiler
• To translate a program, we need a compiler, for
example: gcc
• The compiler of C always supports parameter to
perform two phrases of the compiling process. For
example, gcc – c to do the compiling phrase, gcc –o
to do the link phrase.
• If your program is written in only one file, a single
Unix command can help to make an executable
program from the source code.
• $ gcc -o program-name filename
• Ex: $gcc -o hello hello.c

22
IDE: Integrated Development
Environment
• Programming is a process that repeatly carries out operations:
source code editing, compiling, debugging.
• These operations can carry out independently by different
tools. For example, edit by emacs, compile by gcc.
• However, a more convenient way is to integrate all
programming tools to an unique environment to support the
programming. This environment is called IDE.
• IDE - an environment of 3 in 1: editor, compiler, debugger

23
Products of IDE
• On Linux:
• KDevelop
• On Window:
• Dev-C++,
• Turbo C++,
• Visual C++,
• etc.

24
KDevelop

25
Dev-C++

26
Visual C++

27
Thank you
for your
attentions!

You might also like