Вы находитесь на странице: 1из 26

Introduction to C/C++ Programming

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

 Structured and disciplined approach to program design

 A C program consists of source code in one or more files

 Each source file is run through the preprocessor and compiler, resulting in

a file containing object code

 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

 Compiler: converts source to object code for a specific platform

 Linker: resolves external references and produces the executable

module

5 Introduction to C 6/28/2018
Introduction…
 Linker:

 When a function is called, linker locates it in the library and inserts it

into object program


 If function name is misspelled, the linker will produce an error because

it will not be able to find function in the library

6 Introduction to C 6/28/2018
Introduction…
 Intended use and underlying philosophy:

 C is a low-level language: suitable for systems programming

 C is a small language: relies on a “library” of standard functions

 C is a permissive language: it assumes that you know what you’re doing,

so it allows you a wider degree of latitude than many languages. It


doesn’t mandate the detailed error-checking found in other languages

7 Introduction to C 6/28/2018
Advantages of C
 Quicker compilation:

 When modifying a program, a programmer typically edits only a few

source code files at a time


 With separate compilation, only the files that have been edited since the

last compilation need to be recompiled when re-building the program


 For very large programs, this can save a lot of time

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

commercial data processing


 Standard library

 Integration with UNIX

9 Introduction to C 6/28/2018
C vs C++
 C++ extends C to include support for Object Oriented Programming

and other features that facilitate large software development projects

 C is not strictly a subset of C++, but it is possible to write “Clean C” that

conforms to both the C++ and C standards

10 Introduction to C 6/28/2018
Basic Syntax of C…
 Every full C program begins inside a function called "main".

 A function is simply a collection of commands that do "something".

 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.

 All statements must end with a semicolon (;)

 Escape character (\) indicates that printf should do something out of the

ordinary.

 \n is the newline character

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

 getchar() is included because many compiler environments will open a

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

operating system by using the return statement

 This return value is important as it can be used to tell the operating

system whether our program succeeded or not

 A return value of 0 means success

17 Introduction to C 6/28/2018
Comments
 Comments are used to describe program

 Text surrounded by /* and */ or // is ignored by computer

 /* and */ comments a block of text

 // Comments a line of text

18 Introduction to C 6/28/2018
Invisible Characters
 Some special characters are not visible directly in the output stream.

 These all begin with an escape character (\):

 \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.

 ctype.h – character types: isalnum, isprint, isupport, tolower, etc.

 errno.h – defines errno used for reporting system errors

 math.h – math functions: ceil, exp, floor, sqrt, etc.

 signal.h – signal handling facility: raise, signal, etc

 stdint.h – standard integer: intN_t, uintN_t, etc

 time.h – time related facility: asctime, clock, time_t, etc.

20 Introduction to C 6/28/2018
Using Variables
 Variables

 Variable names correspond to locations in the computer's memory

 Every variable has a name, a type, a size and a value

 Whenever a new value is placed into a variable it replaces (and

destroys) the previous value


 Reading variables from memory does not change them

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;

 This declares three variables a, b and c

 The declarations can also be done as:

 int a;

 int b;

 int c;

 or

 int a; int b; int c;

23 Introduction to C 6/28/2018
Variable Initialization
 a=10; b=20; and c=a+b;

 Initializes the three variables by assigning them values

 printf("Answer:%d\n\n", c);

 This prints the value pointed to by the variable c.

 %d marks the location where the variable c should be printed and also

indicates that c is of type integer.

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

Вам также может понравиться