Академический Документы
Профессиональный Документы
Культура Документы
Introduction
Programming in C requires a compiler.
A compiler turns the program that you write into an executable file
that your computer can actually understand and run.
Visual Studio provides you with the tools that you need to develop an
application for any platform. Tools include an extensible integrated
development environment and code editors for Mac OS X, Linux, and
Windows.
For this course we use the C++ development environment under Visual
Studio to write and execute both C and C++ programs
2 Introduction to C 6/28/2018
Introduction…
C programming language
Each source file is run through the preprocessor and compiler, resulting in
Object files are tied together by the linker to form a single executable
program
3 Introduction to C 6/28/2018
Introduction…
4 Introduction to C 6/28/2018
Introduction…
A C development environment includes
System libraries and headers: a set of standard libraries and their header
files.
Application Source: application source and header files
module
5 Introduction to C 6/28/2018
Introduction…
Linker:
6 Introduction to C 6/28/2018
Introduction…
Intended use and underlying philosophy:
7 Introduction to C 6/28/2018
Advantages of C
Quicker compilation:
8 Introduction to C 6/28/2018
Advantages of C…
Efficiency: intended for applications where assembly language
had traditionally been used
Portability: hasn’t splintered into incompatible dialects; small
and easily written
Power: large collection of data types and operators
Flexibility: not only for system but also for embedded system
9 Introduction to C 6/28/2018
C vs C++
C++ extends C to include support for Object Oriented Programming
10 Introduction to C 6/28/2018
Basic Syntax of C…
Every full C program begins inside a function called "main".
The main function is always called when the program first executes
From the main function, we can call other functions, whether they be
written by us or by others or use built-in language features
To access the standard functions that comes with your compiler, you need
to include a header with the #include directive.
The header directive takes everything in the header file and paste it into
your program
11 Introduction to C 6/28/2018
Basic Syntax of C…
Example:
#include <stdio.h>
int main()
{
printf( “HellowWorld\n" );
getchar();
return 0;
}
12 Introduction to C 6/28/2018
Basic Syntax of C…
The #include is a "preprocessor" directive that tells the compiler to put
code from the header called stdio.h into our program before actually
creating the executable
By including header files, you can gain access to many different functions.
For example, printf function is included in stdio.h
int main() tells the compiler that there is a function named main, and
that the function returns an integer, hence int
The "curly braces," { and }, signal the beginning and end of functions
and other code blocks
13 Introduction to C 6/28/2018
Basic Syntax of C…
The printf function is the standard C way of displaying output on the
screen.
The quotes tell the compiler that you want to output the literal string as-is
(almost).
The '\n' sequence is actually treated as a single character that stands for a
newline. The actual effect of '\n' is to move the cursor on your screen to
the next line
The semicolon tells the compiler that you're at the end of a command
14 Introduction to C 6/28/2018
Basic Syntax of C…
An entire line is called a statement.
Escape character (\) indicates that printf should do something out of the
ordinary.
15 Introduction to C 6/28/2018
Basic Syntax of C…
getchar(): This is another function call: it reads in a single character and
waits for the user to hit enter before reading the character
new console window, run the program, and then close the window before
you can see the output
This command keeps that window from closing because the program is
not done yet because it waits for you to hit enter. Including that line gives
you time to see the program run
16 Introduction to C 6/28/2018
Basic Syntax of C…
return 0: at the end of the program, we return a value from main to the
17 Introduction to C 6/28/2018
Comments
Comments are used to describe program
18 Introduction to C 6/28/2018
Invisible Characters
Some special characters are not visible directly in the output stream.
\n newline
\t horizontal tab
\a alert bell
\v vertical tab
19 Introduction to C 6/28/2018
Standard Headers
Standard Headers you should know about:
stdio.h – file and console (also a file) IO: perror, printf, open, close, read, write,
scanf, etc.
stdlib.h - common utility functions: malloc, calloc, strtol, atoi, etc
string.h - string and byte manipulation: strlen, strcpy, strcat, memcpy, memset, etc.
20 Introduction to C 6/28/2018
Using Variables
Variables
21 Introduction to C 6/28/2018
Example 2
#include <stdio.h>
int a,b,c;
int main()
{
a=10;
b=20;
c=a+b;
printf("Answer:%d\n\n", c);
}
22 Introduction to C 6/28/2018
Variable declaration
int a,b,c;
int a;
int b;
int c;
or
23 Introduction to C 6/28/2018
Variable Initialization
a=10; b=20; and c=a+b;
printf("Answer:%d\n\n", c);
%d marks the location where the variable c should be printed and also
24 Introduction to C 6/28/2018
Programming Exercise
Write a program to carry out addition, division, subtraction,
and multiplication of two numbers. The program should give
the following output:
The sum of a+b=c
The result of a/b=d
The result of a-b=e
The product a*b=f
Note that a,b,c,d,e,f are variables and should be declared
accordingly.
25 Introduction to C 6/28/2018
END
26 Introduction to C 6/28/2018