Вы находитесь на странице: 1из 27

Amity School of Engineering & Technology

Module-2

STARTING OF C
Amity School of Engineering & Technology

HISTORY OF C
• The C programming language is a
structure oriented programming
language, developed at Bell
Laboratories in 1972 by Dennis Ritchie. 
• C programming is most widely used
programming language even today.
• C programming is the basis for all
programming languages.
Amity School of Engineering & Technology

• In 1978, Dennis Ritchie and Brian


Kernighan published the first edition  “The
C Programming Language” and commonly
known as K&R C
• In 1983, the American National Standards
Institute (ANSI) established a committee to
provide a modern, comprehensive
definition of C.
• The resulting definition, the ANSI standard,
or “ANSI C”, was completed late 1988.
Amity School of Engineering & Technology

• All other programming languages were


derived directly or indirectly from C
programming concepts.
• In this module we explain all basic
concepts in C like history of C language,
data types, keywords, constants,
variables, operators, expressions, control
statements, array, pointer, string, library
functions, structures and unions etc.
Amity School of Engineering & Technology

Structured Oriented
• In this type of language, large programs are
divided into small programs called functions
• Prime focus is on functions and procedures
that operate on the data
• Data moves freely around the systems from
one function to another
• Program structure follows “Top Down
Approach”
Amity School of Engineering & Technology

#include <stdio.h>
int main()
{
/* Our first simple C basic program */
printf(“Hello World! ”);
getch();
return 0;
}
Amity School of Engineering & Technology

S.no Command Explanation


This is a preprocessor command that includes
#include
1 standard input output header file(stdio.h) from
<stdio.h>
the C library before compiling a C program
This is the main function from where execution
2 int main()
of any C program begins.
This indicates the beginning of the main
3 {
function.
/ whatever is given inside the command “/*   */”
4 *_some_com in any C program, won’t be considered for
ments_*/ compilation and execution.
printf(“Hello_ printf command prints the output onto the
5
World! “); screen.
This command waits for any character input
6 getch();
from keyboard.
This command terminates C program (main
7 return 0;
function) and returns 0.
8 } This indicates the end of the main function.
Amity School of Engineering & Technology

Steps to write C programs and


get the output:
Amity School of Engineering & Technology

Basic structure of C program:


• Structure of C program is defined by set of
rules called protocol, to be followed by
programmer while writing C program.
• All C programs are having sections/parts which
are mentioned  below.
• Documentation section
• Link Section
• Global declaration section
• Main function
Amity School of Engineering & Technology

/* 1-Documentation section
Sum of two nos
*/
#include <stdio.h> /* 2-Link section */

int total = 0; /* 3-Global declaration and


definition section */
Amity School of Engineering & Technology

int main () /* 4-Main function */


{
int a,b,total=0;
printf (“This is a C basic program \n”);
printf(“Enter 2 nos”);
scanf(“%d %d”,&a,&b);
total = a+b;
printf (“Sum of two numbers : %d \n”, total);
return 0;
}
Amity School of Engineering & Technology

S.No Sections Description

We can give comments about the program, creation


or modified date, author name etc in this section. The
characters or words or anything which are given
Documentation between “/*” and “*/”, won’t be considered by C
1
section compiler for compilation process. These will be
ignored by C compiler during compilation.
Example : /* comment line1 comment line2 comment
3 */

Header files that are required to execute a C


2 Link Section
program are included in this section
Global Global variables are defined in this section. When a
3 declaration variable is to be used throughout the program, can
section be defined in this section.
Every C program is started from main function and
4 Main function this function contains two major sections called
declaration section and executable section.
Amity School of Engineering & Technology

printf()
• printf() function is used to print the “character,
string, float, integer, octal and hexadecimal
values” onto the output screen.
• We use printf() function with %d format specifier
to display the value of an integer variable.
• Similarly %c is used to display character, %f for
float variable, %s for string variable, %lf for
double and %x for hexadecimal variable.
• To generate a new line, we use “\n” in printf()
statement.
Amity School of Engineering & Technology

scanf()
• scanf() function is used to read character,
string, numeric data from keyboard.
• The format specifier %d is used in scanf()
statement. So that, the value entered is
received as an integer and %s for string.
• Ampersand is used before variable name
“ch” in scanf() statement as &ch.
Amity School of Engineering & Technology

Note:
• C language is case sensitive. For
example, printf() and scanf() are different
from Printf() and Scanf().
• All characters in printf() and scanf()
functions must be in lower case.
Amity School of Engineering & Technology

Standard Headers you should know


about
– stdio.h – file and console (also a file) IO: perror, printf,
open, close, read, write, scanf, etc.
– conio.h – clrscr(),getch() 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
Amity School of Engineering & Technology

32 KEYWORDS
• auto double int struct
• break else long switch
• case enum register typedef
• char extern return union
• const float short unsigned
• continue for signed void
• default goto sizeof volatile
• do if static while
Amity School of Engineering & Technology

VARIABLES
• They are used to store the values.
• They must start with a alphabet and then forward
to any number of alphabets and numbers.
• No special character is used to declare a variable
name except underscore symbol.
• E.g. x1,xyz, x_1 all are correct but 1x, 8y are
incorrect.
• No keyword is also used for variable name like
int float etc.
Amity School of Engineering & Technology

DATA TYPES
Amity School of Engineering & Technology

• One printf can print several lines by using


additional newline characters.

• Each time the \n (newline) escape sequence is


encountered, output continues at the beginning
of the next line
Amity School of Engineering & Technology

• #include<stdio.h>
• #include<conio.h>
• void main()
• {
• printf(“Hello class \n”);
• getch();
• }
• o/p=Hello class
_
Amity School of Engineering & Technology

Constants and Variables


• Constant
– A data element that never changes in your program
• 5, 62.37, 4.219E-6
• i = j + 7; /* which one is the constant? */
• first_letter = ‘a’; /* which one is the constant? */

• Variable
– A data element that can take on different values
• Its name represents a location (address) in memory 
– i = j + 7; /* which are variables? */
– second_letter = ‘b’; /* which is the variable? */
• Values are ‘assigned’ using a single equal sign ( = )
– Read the statement: i = j + 7;
Amity School of Engineering & Technology

• In maths we write
• x+y=10; so here x and y are variables and
10 is constant.

• What are the possible values for x and y??


Amity School of Engineering & Technology
Addition of two nos
• #include<stdio.h>
• #include<conio.h>
• void main()
• {
• int a,b,c;
• printf(“enter two nos:”);
• scanf(“%d %d”,&a,&b);
• c=a+b;
• printf(“sum is =%d\n”,c);
• getch();
• }
Amity School of Engineering & Technology

• We can also write the printf statement as


• printf(“sum of %d and %d is %d ”, a,b,c);

• One more way to write this is;


• printf(“ %d + %d is %d \n” ,a,b,c);
Amity School of Engineering & Technology

Wap to calc square and cube


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf(“enter a no:”);
scanf(“%d ”,&a);
b=a*a;
printf(“square is=%d\n”,b);
b=a*b;
printf(“Cube is=%d\n”,b);
getch();
}
Amity School of Engineering & Technology

Assignment of values
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b,c; //here abc are variables and 10 is
constant
printf(“enter a no:”);
scanf(“%d ”,&b);
a=b;
c=a;
printf(“%d %d %d\n”a,b,c);
getch(); }

Вам также может понравиться