MIJ 1 C Prog 01
MIJ 1 C Prog 01
C Program Structure
Data Types
Concept of Variables
Variable Naming Conventions
C Reserve Words
Introduction
The C programming language was designed by Dennis Ritchie at Bell
Laboratories in the early 1970s
Influenced by
ALGOL 60 (1960),
CPL (Cambridge, 1963),
BCPL (Martin Richard, 1967),
B (Ken Thompson, 1970)
Traditional C:
The C Programming Language, by Brian Kernighan and Dennis Ritchie,
2nd Edition, Prentice Hall
Referred to as K&R
Standard C
Standardized in 1989 by ANSI (American National Standards Institute)
known as ANSI C
International standard (ISO) in 1990 which was adopted by ANSI and is
known as C89
As part of the normal evolution process the standard was updated in 1995
(C95) and 1999 (C99)
C++ and C
• C++ extends C to include support for Object Oriented
Programming and other features that facilitate large
software development projects
• C is not strictly a subset of C++, but it is possible to write “Clean C”
that conforms to both the C++ and C standards.
c
Elements of a C Program
A C development environment includes
System libraries and headers: a set of standard libraries and their header
files. For example see /usr/include and glibc.
Application Source: application source and header files
Compiler: converts source to object code for a specific platform
Linker: resolves external references and produces the executable module
User program structure
There must be one main function where execution begins when the program
is run. This function is called main
int main (void) { ... },
int main (int argc, char *argv[]) { ... }
int main (int argc, char *argv[], char *envp[])
additional local and external functions and variables
C Program Structure
C programs are composed of one or more functions and at least one function must have
name main().
If there is only one function and that should be main().
C program start its execution from the function main().
Function must have
Function heading with () symbol and all arguments are in the brackets ()
Arguments must have declared before use
Function have all of its actions in the function body.
• Examples:
• a7, sum, average, product, quotient,
increment, for9
Integer Constants
No comma, no blank space are in the numbers
Only negative sign (-) can be place in front of the
constant
Value must be within the defined upper and lower value
String Constants
Texts or numbers written within the double quotation sign (“) pair.
Compound Statement:
{
int a,b,x;
a=32;
b=43;
x=a+b;
printf(“\nSum = %d”, x);
}
Some C Programs
1. C Programs
Problem: A C Program to display the name address with a message.
/*program 01: to display the name and address*/
#include<stdio.h>
int main()
{
printf("Jamal Uddin Russel\n");
printf("Department of Computer Science and Engineering\n");
printf("Daffodil International University\n");
printf("Bangladesh\n");
printf("Programming is fun\n");
printf("Welcome to Programming World\n");
return 0;
}
2. C Programs
Problem: A C program that finds the sum, subtract and product of two
given numbers.
/*program 02: to find the sum, subtract, product of given two numbers*/
#include<stdio.h>
int main()
{
int i,j,sum,subtract,product;
i=250;
j=100;
sum=i+j;
subtract=i-j;
product=i*j;
printf("\n\nSum = %d",sum);
printf("\n\nSubtract = %d",subtract);
printf("\n\nProduct = %d",product);
return 0;
}
3. C Programs
Problem: A C program that enters two numbers from the keyboard and
finds the sum, subtract and product of them
/*program 03: to find the sum, subtract, product of two numbers where numbers are
inputted from the keyboard*/
#include<stdio.h>
int main()
{
int i,j,sum,subtract,product;
printf("\nEnter first number: ");
scanf("%d",&i);
printf("\nEnter second number: ");
scanf("%d",&j);
sum=i+j;
subtract=i-j;
product=i*j;
printf("\n\nSum = %d",sum);
printf("\n\nSubtract = %d",subtract);
printf("\n\nProduct = %d",product);
return 0;
}
4. C Programs
Problem: A C program that enters two numbers from the keyboard and
finds the sum, subtract and product of them with formatted output.
/*program 04: to find the sum, subtract, product of given two numbers where numbers
are inputted from the keyboard*/
#include<stdio.h>
int main()
{
int i,j,sum,subtract,product;
printf("\nEnter first number: ");
scanf("%d",&i);
printf("\nEnter second number: ");
scanf("%d",&j);
sum=i+j;
subtract=i-j;
product=i*j;
printf("\n\nSum of %d and %d is = %d",i,j,sum);
printf("\n\nSubtract of %d and %d is = %d",i,j,subtract);
printf("\n\nProduct of %d and %d is = %d",i,j,product);
return 0;
}
More Programs
Program 1: A C program that display the following message
Programming is fun
C programming is also very fun
Welcome to the World of C
Programming is fun
C programming is also very fun
Welcome to the World of C
Programming is fun
C programming is also very fun
Welcome to the World of C */
#include<stdio.h>
void main()
{
printf(“\nProgramming is fun");
printf(“\nC Programming is also very fun”);
printf(“\nWelcome to the world of C”);
}
Program 03: A C program that finds the sum of two given integer values.
/*program 5 to display the sum and the numbers with proper format*/
#include<stdio.h>
void main()
{
int a,b,s;
a=120, b=210;
printf("\na = %d",a);
printf("\nb = %d",b);
printf("\nSum = %d",s);
}
Program 6: A C program to display the sum of two numbers with the two
numbers as specified format
/*program 6 to display the sum of two numbers with the two numbers as specified
format*/
#include<stdio.h>
void main()
{
int a,b,s;
a=120, b=210;
s=a+b;
printf("\nFirst Number = %d",a);
printf("\nSecond Number = %d",b);
printf("\nSum = %d",s);
}
Program 7: A C Program that display the sum, subtract and product with
the given numbers and specified format
/*program 8 to display the sum, subtract, product of the two numbers taken from
the keyboard with the format*/
#include<stdio.h>
void main()
{
int a,b,s,sb,pr;
printf("\nEnter First Number = %d",a);
scanf(“%d”,&a);
printf("\nSecond Number = %d",b);
scanf(“%d”,&b);
s=a+b;
sb=a-b;
pr=a*b;
printf("\nFirst Number = %d",a);
printf("\nSecond Number = %d",b);
printf("\nSum = %d",s);
printf("\nSubtract = %d",sb);
printf("\nProduct = %d",pr);
}
Thanks