History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented...

Preview:

Citation preview

History of CHistory of C• 1950 – FORTRAN (Formula

Translator)

• 1959 – COBOL (Common Business Oriented Language)

• 1971 – Pascal

• Between 1970-1980 - Ada

History of CHistory of C• 1967 – BCPL –By Martin Richards–For writing OS and compilers

• 1970 – B –Used to write UNIX OS for DEC PDP-7

• 1972 – C –By Dennis Ritchie–Used to write UNIX OS for DEC PDP-11–Now almost all OS are written in C/C++

History of CHistory of C• C became popular after a book

written by Ritchie and Kernighan– ‘The C Programming Language’

• Many variations came up for different platforms

• ANSI (American National Standards Institute) approved a standard in 1989

Programming ToolsProgramming Tools• Compiler

• Standard Library

• IDE

• Help files & documentations

CompilersCompilers• What does it do?–Parse the code

–Tokenize

–Match syntax

–Find Errors

–Prepare object code

Standard LibraryStandard Library• What does it do?–Provide implementations of some

basic and important functions

–Usually these functions are very efficient

–Programmers should use library functions to improve performance and portability

IDE - Integrated Development IDE - Integrated Development EnvironmentEnvironment

• Helps to Write–Use different color to highlight

different type of code

–Sometimes shows hints

• Helps to Compile–Set environment variables

–Linking with libraries

IDE - Integrated Development IDE - Integrated Development EnvironmentEnvironment

• Helps to Debug–Execute step by step

–Use breaks

–Watch the values of variables

• Helps to Run

Help Files and DocumentationHelp Files and Documentation

• Provide details about –Syntax

–Keywords

–Library functions

–Examples

–Etc.

C Files C Files • Source File– .c

–Contains the code

–A program may use multiple source files

• Object File– .obj

–Compiled source file

C Files C Files • Header File– .h

–Contains codes of library functions

–We can also write header files

• Library File– .lib

–Compiled library function

C Files C Files • Executable File– .exe

–Final program

• Backup File– .bak

• Other Files

Lifecycle of a C ProgramLifecycle of a C Program

SourceFile

User definedHeader File

StandardHeader File

Object File

Compile Compile

Library File

Compile

ExecutableFile

Link

A first look at CA first look at C

# include <stdio.h>

void main(void)

{

printf(“First C Program”);

}

A first look at CA first look at C

# include <stdio.h>

void main(void){

printf(“First C Program”);}

# include• Must be written first

• Means we want to include or use the functions defined in the header

A first look at CA first look at C

# include <stdio.h>

void main(void){

printf(“First C Program”);}

# include• # symbol indicates a preprocessor

• It means it has to be done before compilation

A first look at CA first look at C

# include <stdio.h>void main(void){

printf(“First C Program”);}

<stdio.h>• Name of the header file

• You must know which header you need

• Use help and documentation to find out

A first look at CA first look at C

# include <stdio.h>void main(void){

printf(“First C Program”);}

<stdio.h>• Enclosed in < > (header in default place)

• May be enclosed in “ ” (header is in the same folder as the source)

A first look at CA first look at C# include <stdio.h>

void main(void)

{

printf(“First C Program”);

}

main• Every C program must have a ‘main’

function

• Program starts from the 1st line in ‘main’

A first look at CA first look at C# include <stdio.h>

void main(void)

{

printf(“First C Program”);

}

main• Functions has parameters enclosed in ( )

• Functions may return values

A first look at CA first look at C# include <stdio.h>void main(void)

{printf(“First C Program”);

}

{ }• The curly braces are like containers

• The code between two braces are called a block

A first look at CA first look at C# include <stdio.h>void main(void)

{printf(“First C Program”);

}

{ }• Missing either brace will generate

compile error– “Compound Statement missing”

A first look at CA first look at C# include <stdio.h>

void main(void)

{

printf(“First C Program”);

}

printf• A function given in stdio.h

• Prints the text given as the parameter

A first look at CA first look at C# include <stdio.h>

void main(void)

{

printf(“First C Program”);}

“ ; ” (semicolon)• Every C statement must end with a ;

• Otherwise compiler will generate an error– “Statement Missing”

A first look at CA first look at C# include <stdio.h>

void main(void)

{

printf(“First C Program”);

}

Keywords• The texts in White are keywords

• These are words that has special meanings for C

Another ExampleAnother Example/* My second C Program*/

# include <stdio.h>

void main(void)

{

printf(“My Second C Program\n”);

}//end of code

\n

// main function

Another ExampleAnother Example/* My second C Program*/

• Enclosed within /* and */

• This is a multi-line Comment

• It is not part of the code

• Compiler ignores it

• Used for helping the programmer to understand the code better

Another ExampleAnother Example// main function

• This is a single-line Comment

Another ExampleAnother Example\n

• This is an escape sequence

• ‘\’ is escape character

• It indicates that printf should do something different with the character that follows the \

Another ExampleAnother Example\n

• \n = new line (like an ‘enter’ key)

• \t = tab

• \a = alert (the PC speaker gives a beep)

• \\ = \(to print \)

• \” = “ (to print “ or ” )

Yet Another ExampleYet Another Example# include <stdio.h>

void main(void)

{

int a; // declaring a variable

a = 10; // assign a value

printf(“The Value of a is = %d”, a);

}

Yet Another ExampleYet Another Exampleint a;

• ‘int’ is a keyword

• It means that the variable declared here is an integer

• So ‘int’ defines the data type

• C has many data types

Yet Another ExampleYet Another Exampleint a;

• ‘a’ is the variable name

• The statement creates/defines a new variable named ‘a’

• Creating a variable means reserving a memory location for it

• The size of the memory needed depends on the data type

Yet Another ExampleYet Another Examplea = 10;

• This is an assignment statement

• This means the value ‘10’ is written in the memory location reserved for ‘a’

• Before using a variable or assigning a value, we have to define it first– Or a compile error “Unknown identifier”/

“Undefined Symbol” will occur

Yet Another ExampleYet Another Exampleprintf(“The Value of a is = %d”, a);

• This is the way a variable’s value can be outputted

• The first parameter is format control string.–The format and layout of output text is

defined here

Yet Another ExampleYet Another Exampleprintf(“The Value of a is = %d”, a);

• “%d” is a conversion specifier

• It tells the printf function that it has to print the value of an integer variable here

Yet Another ExampleYet Another Exampleprintf(“The Value of a is = %d”, a);

• The second parameter tells the printf function the value that should be printed in place of the “%d”

Today’s Last ExampleToday’s Last Example# include <stdio.h>

void main(void)

{

int a; // declaring a variable

printf(“Please enter value of a:”);

scanf(“%d”, &a); // inputs a value

printf(“\nThe Value of a is = %d”, a);

}

Today’s Last ExampleToday’s Last Examplescanf(“%d”, &a);

• scanf is another function given in stdio.h

• It is used to take inputs from the user

• Syntax is similar to printf

Today’s Last ExampleToday’s Last Examplescanf(“%d”, &a);

• NOTE:–Here the variable name is preceded by

‘&’ symbol.

– It indicates the address of the memory location that was reserved for ‘a’

–Omitting the symbol will cause wrong results

Looking BackLooking Back• We learned about the history

of C

• We learned what tools is needed for software development

• We learned the important files associated with C

Looking BackLooking Back• Every C Code begins with

the #include directive

• Every C Program must contain one and only one function named ‘main’

• A program should have comments to make it easy to understand

Looking BackLooking Back• Variables must be defined

before they can be used

• printf is used to output

• scanf for input

• We learned about escape sequence

• We also learned about conversion specifier

Looking BackLooking Back• We learned about a few

compile errors–Statement missing

–Compound Statement missing

–Unknown identifier

Recommended