SlideShare a Scribd company logo
Introduction to
C Programming
A Brief History
Created by Dennis Ritchie at AT&T Labs in 1972
Originally created to design and support the Unix
operating system.
There are only 27 keywords in the original version of C.
– for, goto, if, else ……
Easy to build a compiler for C.Easy to build a compiler for C.
– Many people have written C compilers
– C compilers are available for virtually every platform
In 1983 the American National Standards Institute (ANSI)
formed a committee to establish a standard definition.
– Called ANSI Standard C.
– As opposed to K&R C (referring to the general “standards” that
appeared in the first edition of Brian Kernighan and Ritchie’s
influential book: The C Programming Language)
Why use C?
C is intended as a language for programmers
– BASIC was for nonprogrammers to program and solve simple
problems.
– C was created, influenced, and field-tested by working
programmers.
C is powerful and efficient
– You can nearly achieve the efficiency of assembly code.– You can nearly achieve the efficiency of assembly code.
– System calls and pointers allow you do most of the things that
you can do with an assembly language.
C is a structured language
– Code can be written and read much easier.
C is standardized
– Your ANSI C program should work with any ANSI C compiler.
The C Development Cycle
Edit
Program
Source
Code
Compile
Object
Code
Link Object
Code Executable
Library
Files
“Hello World”
Everyone writes this program first
#include <stdio.h>
int main ( )
{
printf ("Hello, World!n");
return 0;
}
Compilation (1)
Compilation translates your source code (in the file
hello.c) into object code (machine dependent
instructions for the particular machine you are on).
– Note the difference with Java:
The javac compiler creates Java byte code from your
Java program.
The byte code is then executed by a Java virtualThe byte code is then executed by a Java virtual
machine, so it’s machine independent.
Linking the object code will generate an executable file.
There are many compilers for C under Unix
– SUN provides the Workshop C Compiler, which you run with
the cc command
– There is also the freeware GNU compiler gcc
Compilation (2)
To compile a program:
Compile the program to object code.
obelix[2] > cc –c hello.c
Link the object code to executable file.
obelix[3] > cc hello.o –o hello
You can do the two steps together by running:You can do the two steps together by running:
obelix[4] > cc hello.c –o hello
To run your program:
obelix[5] > ./hello
Hello World!
If you leave off the
-o, executable goes into
the file a.out
Compilation (3)
Error messages are a little different than you may be
used to but they can be quite descriptive.
Suppose you forgot the semi-colon after the printf
obelix[3] > cc hello.c –o hello
"hello.c", line 5: syntax error before or at: return"hello.c", line 5: syntax error before or at: return
cc: acomp failed for hello.c
Notice that the compiler flags and informs you about
the error at the first inappropriate token.
– In this case, the return statement.
Always try to fix problems starting with the first error
the compiler gives you - the others may disappear too!
Example 1
#include <stdio.h>
int main ()
{
int radius, area;
printf ("Enter radius (i.e. 10) : ");
scanf ( "%d", &radius);
area = 3.14159 * radius * radius;
printf ("nArea = %dnn", area);
return 0;
}
Example 2
#include <stdio.h>
int main ()
{
int i, j;
for (i = 0; i < 10; i++)
{{
printf ("n");
for (j = 0; j < i+1; j++ )
printf ( "A");
}
printf("n");
return 0;
}
Example 3
/* Program to calculate the product of
two numbers */
#include <stdio.h>
int product(int x, int y);
int main ()
{
int a,b,c;
/* Calculate and display the product */
c = product (a, b);
printf ("%d times %d = %d n", a, b, c);
return 0;
}
/* Functions returns the product of its
/* Input the first number */
printf ("Enter a number between 1
and 100: ");
scanf ("%d", &a);
/* Input the second number */
printf ("Enter another number
between 1 and 100: ");
scanf ("%d", &b);
/* Functions returns the product of its
two arguments */
int product (int x, int y)
{
return (x*y);
}

More Related Content

DOCX
Practical no 1
PPT
Introduction to C++,Computer Science
PPTX
Testing lecture after lec 4
PPTX
C program to write c program without using main function
DOCX
C++ programming example
PPT
Lecture 3 c++
PDF
A tutorial on C++ Programming
PPT
Practical no 1
Introduction to C++,Computer Science
Testing lecture after lec 4
C program to write c program without using main function
C++ programming example
Lecture 3 c++
A tutorial on C++ Programming

What's hot (20)

PPT
Win avr presentation_2006
PPTX
CSC – 184 Programming C
PDF
Programs that work in c and not in c
PPT
GCC compiler
PDF
A nice 64-bit error in C
DOCX
Practical no 3
PPT
Gccgdb
PPTX
C++ questions
DOC
Infix to-postfix examples
PPTX
Conversion of Infix to Prefix and Postfix with Stack
PPT
comp1
PPTX
comp2
PPTX
Computer Engineering (Programming Language: Swift)
PDF
C multiple choice questions and answers pdf
PPT
Lập trình C
PPT
History of c++
DOCX
Dev c++using
PPTX
The C show Episode 1
PPTX
C Programming Language Step by Step Part 1
Win avr presentation_2006
CSC – 184 Programming C
Programs that work in c and not in c
GCC compiler
A nice 64-bit error in C
Practical no 3
Gccgdb
C++ questions
Infix to-postfix examples
Conversion of Infix to Prefix and Postfix with Stack
comp1
comp2
Computer Engineering (Programming Language: Swift)
C multiple choice questions and answers pdf
Lập trình C
History of c++
Dev c++using
The C show Episode 1
C Programming Language Step by Step Part 1
Ad

Viewers also liked (10)

PPTX
Filter
PPT
é΄äéä ¹
PPT
Blogging
PPT
Δημιουργία ψηφιακών εκδόσεων
PDF
PDF
PPTX
Unit1 lectbyvrk dynofmachinery
PPTX
Dynamics of Machinery Unit III
PPTX
Gas dynamics and jet propulsion – presentationof problemsanswers
Filter
é΄äéä ¹
Blogging
Δημιουργία ψηφιακών εκδόσεων
Unit1 lectbyvrk dynofmachinery
Dynamics of Machinery Unit III
Gas dynamics and jet propulsion – presentationof problemsanswers
Ad

Similar to 1 c introduction (20)

PDF
88 c-programs
PDF
88 c programs 15184
PDF
88 c-programs
PPT
1_Introduction to C programming - Copy.ppt
DOCX
The basics of c programming
PDF
Introduction of c language
PDF
C class basic programming 1 PPT mayanka (1).pdf
PDF
INTRODUCTION TO C PROGRAMMING MATERIAL.pdf
PDF
C lecture notes new
DOC
Basic c
PDF
Fundamental of Information Technology - UNIT 7
PPTX
Basics of C Lecture 2[16097].pptx
PPTX
COM1407: Introduction to C Programming
DOCX
1 CMPS 12M Introduction to Data Structures Lab La.docx
PDF
Introduction to C programming
PPT
1. over view and history of c
PPTX
C PROGRAMMING AND DATA STRUCTURE _PPT.pptx
PDF
Basics of C Prog Lang.pdf
DOCX
C language industrial training report
88 c-programs
88 c programs 15184
88 c-programs
1_Introduction to C programming - Copy.ppt
The basics of c programming
Introduction of c language
C class basic programming 1 PPT mayanka (1).pdf
INTRODUCTION TO C PROGRAMMING MATERIAL.pdf
C lecture notes new
Basic c
Fundamental of Information Technology - UNIT 7
Basics of C Lecture 2[16097].pptx
COM1407: Introduction to C Programming
1 CMPS 12M Introduction to Data Structures Lab La.docx
Introduction to C programming
1. over view and history of c
C PROGRAMMING AND DATA STRUCTURE _PPT.pptx
Basics of C Prog Lang.pdf
C language industrial training report

1 c introduction

  • 2. A Brief History Created by Dennis Ritchie at AT&T Labs in 1972 Originally created to design and support the Unix operating system. There are only 27 keywords in the original version of C. – for, goto, if, else …… Easy to build a compiler for C.Easy to build a compiler for C. – Many people have written C compilers – C compilers are available for virtually every platform In 1983 the American National Standards Institute (ANSI) formed a committee to establish a standard definition. – Called ANSI Standard C. – As opposed to K&R C (referring to the general “standards” that appeared in the first edition of Brian Kernighan and Ritchie’s influential book: The C Programming Language)
  • 3. Why use C? C is intended as a language for programmers – BASIC was for nonprogrammers to program and solve simple problems. – C was created, influenced, and field-tested by working programmers. C is powerful and efficient – You can nearly achieve the efficiency of assembly code.– You can nearly achieve the efficiency of assembly code. – System calls and pointers allow you do most of the things that you can do with an assembly language. C is a structured language – Code can be written and read much easier. C is standardized – Your ANSI C program should work with any ANSI C compiler.
  • 4. The C Development Cycle Edit Program Source Code Compile Object Code Link Object Code Executable Library Files
  • 5. “Hello World” Everyone writes this program first #include <stdio.h> int main ( ) { printf ("Hello, World!n"); return 0; }
  • 6. Compilation (1) Compilation translates your source code (in the file hello.c) into object code (machine dependent instructions for the particular machine you are on). – Note the difference with Java: The javac compiler creates Java byte code from your Java program. The byte code is then executed by a Java virtualThe byte code is then executed by a Java virtual machine, so it’s machine independent. Linking the object code will generate an executable file. There are many compilers for C under Unix – SUN provides the Workshop C Compiler, which you run with the cc command – There is also the freeware GNU compiler gcc
  • 7. Compilation (2) To compile a program: Compile the program to object code. obelix[2] > cc –c hello.c Link the object code to executable file. obelix[3] > cc hello.o –o hello You can do the two steps together by running:You can do the two steps together by running: obelix[4] > cc hello.c –o hello To run your program: obelix[5] > ./hello Hello World! If you leave off the -o, executable goes into the file a.out
  • 8. Compilation (3) Error messages are a little different than you may be used to but they can be quite descriptive. Suppose you forgot the semi-colon after the printf obelix[3] > cc hello.c –o hello "hello.c", line 5: syntax error before or at: return"hello.c", line 5: syntax error before or at: return cc: acomp failed for hello.c Notice that the compiler flags and informs you about the error at the first inappropriate token. – In this case, the return statement. Always try to fix problems starting with the first error the compiler gives you - the others may disappear too!
  • 9. Example 1 #include <stdio.h> int main () { int radius, area; printf ("Enter radius (i.e. 10) : "); scanf ( "%d", &radius); area = 3.14159 * radius * radius; printf ("nArea = %dnn", area); return 0; }
  • 10. Example 2 #include <stdio.h> int main () { int i, j; for (i = 0; i < 10; i++) {{ printf ("n"); for (j = 0; j < i+1; j++ ) printf ( "A"); } printf("n"); return 0; }
  • 11. Example 3 /* Program to calculate the product of two numbers */ #include <stdio.h> int product(int x, int y); int main () { int a,b,c; /* Calculate and display the product */ c = product (a, b); printf ("%d times %d = %d n", a, b, c); return 0; } /* Functions returns the product of its /* Input the first number */ printf ("Enter a number between 1 and 100: "); scanf ("%d", &a); /* Input the second number */ printf ("Enter another number between 1 and 100: "); scanf ("%d", &b); /* Functions returns the product of its two arguments */ int product (int x, int y) { return (x*y); }