Professional Documents
Culture Documents
time applications. C programming language was invented by Dennis Ritchie at the Bell Laboratories
in 1972. It was invented for implementing UNIX operating system. C is most widely used
programming language even today. All other programming languages were derived directly or
indirectly from C programming concepts.
. A simple C Program:
Below C program is a very simple and basic program in C programming language. This C
program displays “Hello World!” in the output window. And, all syntax and commands in C
programming are case sensitive. Also, each statement should be ended with semicolon (;)
which is a statement terminator.
C
#include <stdio.h>
int main()
{
/* Our first simple C basic prog
1 #include <stdio.h>
2 int main()
3{
6 getch();
7 return 0;
8}
Output:
Hello World!
1. Create
2. Compile
3. Execute or Run
4. Get the Output
If you want to create, compile and execute C programs by your own, you have to install C
compiler in your machine. Then, you can start to execute your own C programs in your
machine.
You can refer below link for how to install C compiler and compile and execute C programs in
your machine.
Once C compiler is installed in your machine, you can create, compile and execute C programs
as shown in below link.
If you don’t want to install C/C++ compilers in your machine, you can refer online compilers
which will compile and execute C/C++ and many other programming languages online and
display outputs on the screen. Please search for online C/C++ compilers in Google for more
details.
1. Documentation section
2. Link Section
3. Definition Section
4. Global declaration section
5. Function prototype declaration section
6. Main function
7. User defined function definition section
1 /*
2 Documentation section
4 Author: fresh2refresh.com
5 Date : 01/01/2012
6 */
12 {
16 return 0;
17 }
18
20 {
22 }
Output:
This is a C basic program
Sum of two numbers : 2
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
Documentation which are given between “/*” and “*/”, won’t be considered by C
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 program are included in
Link Section
this section
In this section, variables are defined and values are set to these
Definition Section
variables.
Global declaration Global variables are defined in this section. When a variable is to be
section used throughout the program, can be defined in this section.
Function prototype Function prototype gives many information about a function like return
declaration section type, parameter names used inside the function.
Every C program is started from main function and this function
Main function contains two major sections called declaration section and executable
section.
User defined User can define their own functions in this section which perform
function section particular task as per the user requirement.
#include <stdio.h>
int main()
{
char ch = 'A';
1 #include <stdio.h>
2 int main()
3 {
4 char ch = 'A';
7 int no = 150;
16 return 0;
17 }
Output:
Character is A
String is fresh2refresh.com
Float value is 10.234000
Integer value is 150
Double value is 20.123456
Octal value is 226
Hexadecimal value is 96
Then, user enters a string and this value is assigned to the variable “str” and then
displayed.
1 #include <stdio.h>
2 int main()
3 {
4 char ch;
5 char str[100];
6 printf("Enter any character \n");
7 scanf("%c", &ch);
8 printf("Entered character is %c \n", ch);
9 printf("Enter any string ( upto 100 character ) \n");
10 scanf("%s", &str);
11 printf("Entered string is %s \n", str);
12 }
Output :
Enter any character
a
Entered character is a
Enter any string ( upto 100 character )
hai
Entered string is hai
File
Description
handling functions
fgets () fgets () function reads string from a file, one line at a time.
fputchar () function writes a character onto the output screen from keyboard
fputchar ()
input.
SEEK_SET SEEK_SET moves file pointer position to the beginning of the file.
rewind () rewind () function moves file pointer position to the beginning of the file.
getche () getche () function reads character from keyboard and echoes to o/p screen.
Why 4, 6, 8, 9, 10, 12, 14, 15, 16 etc are not prime numbers?
Because, the number 4 can be factored as 2*2 and 1*4. As per the rule of prime number, there
should be 2 factors only. They are 1 and the number itself. But, number 4 has 2*2 also. Like
this, all remaining numbers 6, 8, 9, 10, 12, 14, 15, 16 have factors other than 1 and the number
itself. So, these are not called as prime numbers.
1 #include <stdio.h>
2 int main()
3 {
4 int i, num, p = 0;
5 printf("Please enter a number: \n");
6 scanf("%d", &num);
7 for(i=1; i<=num; i++)
8 {
9 if(num%i==0)
10 {
11 p++;
12 }
13 }
14 if(p==2)
15 {
16 printf("Entered number is %d "\
17 "and it is a prime number.",num);
18 }
19 else
20 {
21 printf("Entered number is %d "\
22 "and it is not a prime number.",num);
23 }
24 }