1.4 Introduction to C (1)
1.4 Introduction to C (1)
Introduction
C programming is a general-purpose, procedural, imperative computer
2 Introduction to C 2/3/2024
Introduction…
C was invented to write an operating system called UNIX.
Language.
3 Introduction to C 2/3/2024
Applications of C Programming
C was initially used for system development work, particularly the
code that runs nearly as fast as the code written in assembly language.
4 Introduction to C 2/3/2024
Compilation of C Programs
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
5 Introduction to C 2/3/2024
Compilation of C Programs…
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
6 Introduction to C 2/3/2024
Compilation of C Programs…
7 Introduction to C 2/3/2024
C Development Environment…
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
8 Introduction to C 2/3/2024
C Development Environment…
Linker:
9 Introduction to C 2/3/2024
Advantages of C
Quicker compilation:
10 Introduction to C 2/3/2024
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
11 Introduction to C 2/3/2024
C vs C++
C++ extends C to include support for Object Oriented Programming
12 Introduction to C 2/3/2024
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
13 Introduction to C 2/3/2024
Basic Syntax of C…
Example 1:
14 Introduction to C 2/3/2024
Basic Syntax of C…
The first line of the program #include <stdio.h> is a preprocessor
The next line int main() is the main function where the program execution
begins.
The next line /*...*/ will be ignored by the compiler and it has been put
15 Introduction to C 2/3/2024
Basic Syntax of C…
The next line printf(...) is another function available in C which causes the
The next line return 0; terminates the main() function and returns the
value 0.
16 Introduction to C 2/3/2024
Basic Syntax of C…
Example 2:
#include <stdio.h>
int main()
{
printf( “HellowWorld\n" );
getchar();
return 0;
}
17 Introduction to C 2/3/2024
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
18 Introduction to C 2/3/2024
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
19 Introduction to C 2/3/2024
Basic Syntax of C…
An entire line is called a statement.
Escape character (\) indicates that printf should do something out of the
ordinary.
20 Introduction to C 2/3/2024
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
21 Introduction to C 2/3/2024
Basic Syntax of C…
return 0: at the end of the program, we return a value from main to the
22 Introduction to C 2/3/2024
Comments
Comments are used to describe program
23 Introduction to C 2/3/2024
Invisible Characters
Some special characters are not visible directly in the output stream.
\n newline
\t horizontal tab
\a alert bell
\v vertical tab
24 Introduction to C 2/3/2024
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.
25 Introduction to C 2/3/2024
Using Variables
Variables
26 Introduction to C 2/3/2024
Example 3
#include <stdio.h>
int a,b,c;
int main()
{
a=10;
b=20;
c=a+b;
printf("Answer:%d\n\n", c);
}
27 Introduction to C 2/3/2024
Variable declaration
int a,b,c;
int a;
int b;
int c;
or
28 Introduction to C 2/3/2024
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
29 Introduction to C 2/3/2024
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.
30 Introduction to C 2/3/2024
END
31 Introduction to C 2/3/2024