0% found this document useful (0 votes)
0 views

1.4 Introduction to C (1)

C programming is a general-purpose language developed in 1972, primarily for system development and the UNIX operating system. It remains widely used due to its efficiency, portability, and powerful features, making it suitable for various applications including operating systems and compilers. The document also covers basic syntax, compilation processes, and variable usage in C programming.

Uploaded by

carllongs1000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

1.4 Introduction to C (1)

C programming is a general-purpose language developed in 1972, primarily for system development and the UNIX operating system. It remains widely used due to its efficiency, portability, and powerful features, making it suitable for various applications including operating systems and compilers. The document also covers basic syntax, compilation processes, and variable usage in C programming.

Uploaded by

carllongs1000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Introduction to C Programming

Introduction
 C programming is a general-purpose, procedural, imperative computer

programming language developed in 1972 by Dennis M. Ritchie at the


BellTelephone Laboratories to develop the UNIX operating system.

 C is the most widely used computer language. It keeps fluctuating at

number one scale of popularity along with Java programming language,


which is also equally popular and most widely used among modern
software programmers.

2 Introduction to C 2/3/2024
Introduction…
 C was invented to write an operating system called UNIX.

 The UNIX OS was totally written in C.

 Today C is the most widely used and popular System Programming

Language.

 Most of the state-of-the-art software have been implemented using C.

3 Introduction to C 2/3/2024
Applications of C Programming
 C was initially used for system development work, particularly the

programs that make-up the operating system.

 C was adopted as a system development language because it produces

code that runs nearly as fast as the code written in assembly language.

 Some examples of the use of C are; Operating Systems, Language

Compilers, Assemblers, Text Editors, Print Spoolers, Network Drivers,


Modern Programs, Databases, Language Interpreters, Utilities, etc.

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

 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

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

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

 Linker: resolves external references and produces the executable

module

8 Introduction to C 2/3/2024
C Development Environment…
 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

9 Introduction to C 2/3/2024
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

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

commercial data processing


 Standard library

 Integration with UNIX

11 Introduction to C 2/3/2024
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

12 Introduction to C 2/3/2024
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

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

command, which tells a C compiler to include stdio.h file before going to


actual compilation.

 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

to add additional comments in the program. So such lines are called


comments in the program.

15 Introduction to C 2/3/2024
Basic Syntax of C…
 The next line printf(...) is another function available in C which causes the

message "Hello,World!" to be displayed on the screen.

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

 All statements must end with a semicolon (;)

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

ordinary.

 \n is the newline character

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

 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

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

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

22 Introduction to C 2/3/2024
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

23 Introduction to C 2/3/2024
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

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.

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

25 Introduction to C 2/3/2024
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

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;

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

28 Introduction to C 2/3/2024
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.

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

You might also like