Basics of C

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

COMPUTER PROGRAMMING

Introduction &basics o f C

Md. Istiaque Zahur


Lecturer
WHAT IS C ?

o C is a high level, general purpose & structured programming language in which the sequences of
instruction are for a computer to interpret & execute and is ideal for developing firmware or
portable applications.

o Its instruction consists of terms that resembles algebraic expressions, augmented by certain
English keywords.

o C is a particularly popular language for personal computer programmers because it is


relatively small -- it requires less memory than other languages. The first major program written
in C was the UNIX operating system, and for many years C was considered to be inextricably
linked with UNIX.

Department of Mechanical Engineering


W HY IS IT CALLED C ?
o C was developed by Dennis Ritchie at Bell TelephoneLaboratory
in 1972.
o It is an outgrowth of two earlier languages called BPCL and B
which were also developed in the same laboratory.That is why it is
called C.
o American National Standard Institute (ANSI) developed the new
standardized “ANSI C” in1989.

o C++ is an object-oriented programming developed in 1983, itwas


renamed from C with Classes to C++ (++ being the incremental
operator in C)

o Fortran dates back to 1950s.


Dennis Ritchie

Department of Mechanical Engineering


FEATURES OF C
Fixed number of keywords, including a set of control primitives, such as if, for, while, switch and dowhile.
Multiple logical and mathematical operators, including bit manipulators.
Multiple assignments may be applied in a single statement.
Function return values are not always required and may be ignored if unneeded.
Typing is static.All data has type but may be implicitly converted.
Basic form of modularity, as files may be separately compiled and linked.
Control of function and object visibility to other files via extern and static attributes
 Common embedded systems language

 Can be fast, efficient (most cited reason)

Department of Mechanical Engineering


DISADVANTAGES OF C (A FLEXIBLE LANGUAGE)

Flexible language allows programmers to commit many sins without warning


Hard to debug, fix code
Speed depends on programmer as much as the language
Managing memory can be dangerous, difficult, painful
Lacks modern features (OOP, exceptions,etc.)

Department of Mechanical Engineering


APPLICATIONS OF C
• Robotics
• Controller Area Networks
• Automotive
• Aircraft

• Video Gaming (GPUs)


• High End Computing (database systems) All modern micro
possessors are designed
• Adaptive Controls & Remote Sensing
to be programmed via C.
• Digitally Controlled Adaptive Filters Industry Standard!

Department of Mechanical Engineering


WRITING A C PROGRAM

A programmer uses a text editor to create or modify files containing C code.

Code is also known as source code.

A file containing source code is called a source file.

After a C source file has been created, the programmer must invoke the C
compiler before the program can be executed (run).

Department of Mechanical Engineering


LETS START PROGRAMMING
#include <stdio.h>
Preprocessor: Includestatement
#include <stdlib.h>
main()
Main function
{
printf ("Lets begin");
Main Program

return 0;
}
Output
Lets begin
Department of Mechanical Engineering
MEANINGS OF STATEMENTS://#INCLUDE<STDIO.H>//

• Include statement: In 'C' programming language all library functions are included in
different header files in different categories with ".h" extension.
• # is called as "preprocessor". Means it will include the header file before compiling
the code. "include" gives command to include something
• "stdio" stands for "standard input output". As it is a header file so (.h) extension is
there. It contains some standard functions related to input and output such as
"printf", "scanf".
• For example "math.h" includes all mathematical library functions.

Department of Mechanical Engineering


MEANINGS OF STATEMENTS: //BODY //

• Every C program that can be run must have a main function.When the system starts
the executable, it runs that function by default

• A left brace (curly bracket) -- { -- begins the body of every function. A


corresponding right brace -- } -- ends the function body.
• Because function main() returns an integer value, there must be a statement that
indicates what this value is.

• The statement return 0 indicates that main() returns a value of zero to the operating
system.

Department of Mechanical Engineering


WHAT IS HAPPENING
3 Stages of Compilation
Stage 1: Preprocessing
• Performed by a program called the preprocessor
• Modifies the source code (in RAM) according to preprocessor directives (preprocessor
commands) embedded in the source code
• Strips comments and white space from the code
• The source code as stored on disk is not modified.

Stage II: Compilation

• Performed by a program called the compiler


• Translates the preprocessor-modified source code into object code (machine code)
• Checks for syntax errors and warnings
• Saves the object code to a disk file, if instructed to do so.
Department of Mechanical Engineering
WHAT IS HAPPENING (CONT’D)

Stage 3: Linking
o Combines the program object code with other object code to produce the executable
file.
o The other object code can come from the Run-Time Library, other libraries, or
object files that you have created.
o Saves the executable code to a disk file. On the Linux system, that file is called a.out.

Department of Mechanical Engineering


THE NEXT STEP TO C PROGRAMMING

#include <stdio.h>
#include <stdlib.h>

main() Output
{ X=5
int x; The value of x is= 5
scanf ("%d",&x);
printf ("The value of x is= %d", x);
return 0;
}

Department of Mechanical Engineering


THANK YOU

Department of Mechanical Engineering

You might also like