Sie sind auf Seite 1von 19

Introduction to C Programming

Introduction

What is Programming ?

Programming is basically a process of translating

from the language convenient to human beings to the


language convenient to the computer

To write a sequence of coded instructions fed into a computer

To arrange data in a suitable form so that it can be

processed by a computer to feed a program into a


computer.

C History

Developed between 1969 and 1973 along with Unix Due mostly to Dennis Ritchie Designed for systems programming
Operating systems Utility programs Compilers Filters

Evolved from B, which evolved from BCPL(a Low level programming language)

Why C Still Useful?

C provides:

Efficiency, high performance and high quality s/ws flexibility and power many high-level and low-level operations middle level Stability and small size code Provide functionality through rich set of function libraries Gateway for other professional languages like C C++ Java

C is used:

System software Compilers, Editors, embedded systems data compression, graphics and computational geometry, utility programs databases, operating systems, device drivers, system level routines there are zillions of lines of C legacy code Also used in application programs

English and C

Basic Data Types

Five fundamental data types


Character(char) Integer(int) Floating point(float) Double floating point(double) Valueless(Void)

There are Different Format Specifiers for each data type char %c int %d float %f Double %lf Unsigned %u String %s

Rules for writing a C Program


Each statement is a separate instruction Order of the instructions should be same as that of desired o/p To improve readability, programmer can insert blank spaces, but not in between var names and key words. All instructions should be in small case letters. Each statement must end with a ; . The block of statements should be enclosed in the braces { } .

My first C Program
/* Calculation of simple interest */ #include<stdio.h> void main() { int p , n ; float r , si; p=1000; n=3; r=8.5;
si=p*n*r/100; printf( THE VALUE OF INTEREST IS %f, si);

Simple C Program

#include <stdio.h>
As part of compilation, the C compiler runs a program called the C preprocessor. The preprocessor is able to add and remove code from your source file. In this case, the directive #include tells the preprocessor to include code from the file stdio.h. This file contains declarations for functions that the program needs to use. A declaration for the printf function is in this file.

Simple C Program

void main()
This statement declares the main function. A C program can contain many functions but must always have one main function. A function is a self-contained module of code that can accomplish some task. Functions are examined later. The "void" specifies the return type of main. In this case, nothing is returned to the operating system.

Simple C Program

printf(The value of interest is %f ,si);


Printf is a function from a standard C library that is used to print strings to the standard output, normally your screen. The compiler links code from these standard libraries to the code you have written to produce the final executable. The %f" is a special format modifier that tells the printf to put a floating point number at the specified location.

Receiving input
/* Calculation of simple interest */ #include<stdio.h> void main() { int p , n ; float r , si; printf(ENTER VALUE OF p,n,r); scanf(%d %d %d,&p,&n,&r);

si=p*n*r/100; printf( THE VALUE OF INTEREST IS %f, si);


}

Arithmetic Operators and hierarchy

+,-,*,/,% are arithmetic operators

== , < , < , are Comparison operators


++ , -- are increment and decrement operators || (Logical OR), && (Logical AND), ! (Logical NOT)

Quiz
What will be the output of following program 1. main() { int c; c = 5; printf(%d\n, c); printf(%d\n, c++); printf(%d\n\n, c); c = 5; printf(%d\n, c); printf(%d\n, ++c); printf(%d\n, c); return 0;

5 5 6

5 6 6

Quiz
main() { int a=5, b=2; float c,d; int x,y; c=a/b; d=a%b; x=a/b; y=a%b; }

Quiz
main() { int a=5, b=2; float c,d; int x,y; c=a/b; d=a%b; x=a/b; y=a%b; }
2.000000 1.000000 2 1

Examples

Write a program which takes degree Fahrenheit as input and return degree Centigrade as output

Example

A four digit number is input through keyboard. Write a program to calculate sum of digits

Thank You

Das könnte Ihnen auch gefallen