Professional Documents
Culture Documents
First Program of C
First Program of C
First Program of C
C :-
C is a programming language developed at AT & T’s Bell Laboratories of
USA in 1972. It was designed and written by a man named Dennis
Ritchie. In the late seventies C began to replace the more familiar
languages of that time like PL/I, ALGOL, etc. No one pushed C. It wasn’t
made the ‘official’ Bell Labs language.
First program of c -:
#include<stdio.h>
int main(){
return 0;
Output-:
C Character Set -:
The C character set consists of upper and
lowercase alphabets, digits, special
characters and white spaces.
2:
Constants, Variables and Keywords -:
1. Variables: A variable in simple terms is a storage place which has
some memory allocated to it. Example: Height, age, etc.
2. Keywords: Keywords are specific reserved words in C each of which
has a specific feature associated with it. There are a total of 32
keywords in C: auto, break, case, char, const, continue, default, do,
double, else, enum, extern, float, for, goto, if, int, long, register, return,
short, signed, sizeof, static, struct, switch, typedef, union, unsigned,
void, volatile, while
3. Constants: Constants are the fixed values that never change during
the execution of a program. Example, 111, 1234.
ex-
#include<stdio.h>
void main( ){
int p,n;
float r,si;
si=p*n*r/100;
printf("%f",si);
Output-:
3:
Data types -:
Mainly data types are categories into:
(i). Void: As the name suggests, it holds no value and is generally used
for specifying the type of function or what it returns. If the function has
a void type, it means that the function will not return any value.
ex-
int age;
char letter;
(iii). Pointers: These are powerful C features which are used to access
the memory and deal with their addresses.
4:
Data type Format specifier -:
Integer - %d or %I
Long - %ld
Float - %f
Double - %lf
Character %c
String - %s
ex-
#include <stdio.h>
int main(){
int a = 20;
double b = 20.5;
a + b;
Here, first operand is int type and other is of type double. So, as per
rule 2, the variable a will be converted to double. Therefore, the final
answer is double a + b = 40.500000.
2. Explicit Type Conversion: Explicit type conversion rules out the use
of compiler for converting one data type to another instead the user
explicitly defines within the program the datatype of the operands in
the expression. Example:
double da = 4.5;
double db = 4.6;
double dc = 4.9;
C Programming Operators -:
An operator is a symbol that tells the compiler to perform specific
mathematical or logical functions. C language is rich in built-in
operators and provides the following types of operators:
#include <stdio.h>
int main(){
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
7:
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
return 0;
Output-:
#include <stdio.h>
int main(){
8:
int a = 10, b = 100;
return 0;
Output-:
#include <stdio.h>
int main(){
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", result);
result = !(a != b);
printf("!(a == b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);
return 0;
}
11:
Output-:
y=(x>5?3:4);
ASCII code -:
A character variable holds ASCII value (an integer number between 0
and 127) rather than that character itself in C programming. That value
is known as ASCII value
A–Z 65 – 90
a–z 97 – 122
0–9 48 – 57
12:
Control Statements -:
#include<stdio.h>
int a =10;
if (a <20){
printf("value of a is : %d\n",a);
return 0;
}
13:
Output-:
#include<stdio.h>
int a =100;
if (a <20){
else{
printf("value of a is : %d\n",a);
return 0;
Output-:
14:
3. Switch Statement: As switch statement allows a variable to be tested
for equality against a list of values. Each value is called a case, and the
variable being switched on is checked for each switch case.
#include<stdio.h>
switch(grade){
case'A':
printf("Excellent!\n");
break;
case'B':
printf("Well done\n");
break;
case'C':
printf("You passed\n");
break;
case'D':
break;
15:
default:
printf("Invalid grade\n");
return 0;
Output-:
#include<stdio.h>
int a =10;
while(a <20){
printf("%d ",a);
a++;
return 0;
16:
}
Output-:
5. For Loop: A for loop is a repetition control structure that allows you
to efficiently write a loop that needs to execute a specific number of
times.
#include<stdio.h>
printf("%d ",a);
return 0;
Output-:
6. Do...while: Loop Unlike for and while loops, which test the loop
condition at the top of the loop, the do...while loop in C programming
checks its condition at the bottom of the loop. A do...while loop is
similar to a while loop, except the fact that it is guaranteed to execute
at least one time.
17:
#include<stdio.h>
int a =10;
do{
printf("%d ",a);
a =a +1;
while(a <20);
return 0;
Output-:
int a =10;
while(a <20){
printf("%d ",a);
a++;
if(a >15){
break;
return 0;
Output-:
int a =10;
do{
if(a ==15){
a =a +1;
continue;
printf("value of a: %d\n",a);
a++;
while(a <20);
return 0;
Output-:
#include<stdio.h>
int a =10;
LOOP: do{
if(a ==15){
a =a +1;
goto LOOP;
printf("value of a: %d\n",a);
a++;
while(a <20);
return 0;
Output-:
21:
Function -:
A function is a self-contained block of statements that perform a
coherent task of some kind. Every C program can be thought of as a
collection of these functions. A function definition in C programming
consists of a function header and a function body. Here are all the parts
of a function:
Function Name: This is the actual name of the function. The function
name and the parameter list together constitute the function signature.
#include<stdio.h>
void message();
void main( ){
22:
message( ) ;
void message( ){
Output-:
Call by value:
#include <stdio.h>
int a = 100;
int b = 200;
swap(a, b);
return 0;
int t;
t = y;
y = x;
x = t;
}
Output-:
24:
Call by reference:
#include<stdio.h>
void swap(int*x,int*y);
int a =100;
int b =200;
swap(&a,&b);
return 0;
int t;
t = *y;
25:
*y = *x;
*x = t;
}
Output-:
#include<stdio.h>
int a,b;
int c;
a =10;
b =20;
26:
c =a +b;
return 0;
Output-:
#include<stdio.h>
int c:
int a,b;
a =10;
b =20;
c =a +b;
Output-:
Array -:
An array is a collection of similar elements. These similar elements
could be all ints, or all floats, or all chars, etc. Usually, the array of
characters is called a ‘string’, whereas an array of ints or floats is called
simply an array.
#include<stdio.h>
void main( ){
int i ;
int marks[5];
}
28:
for ( i = 0 ; i <= 4 ; i++ )
avg = sum / 5;
Output-:
Pointers -:
#include <stdio.h>
29:
int main(){
int *ptr, q;
q = 50;
ptr = &q;
printf("%d", *ptr);
return 0;
}
Output-:
#include<stdio.h>
void main( ){
int *arr[4] ;
arr[0] = &i ;
30:
arr[1] = &j ;
arr[2] = &k ;
arr[3] = &l ;
Output-:
NULL Pointers -:
It is always a good practice to assign a NULL value to a pointer variable
in case you do not have an exact address to be assigned. This is done at
the time of variable declaration. A pointer that is assigned NULL is
called a null pointer. The NULL pointer is a constant with a value of zero
defined in several standard libraries. Consider the following program:
#include <stdio.h>
return 0;
31:
}
Output-:
Strings -:
The way a group of integers can be stored in an integer array, similarly
a group of characters can be stored in a character array. Character
arrays are many a time also called strings. A string constant is a one-
dimensional array of characters terminated by a null ( ‘\0’ ).
ex-
#include<stdio.h>
char name[ ] = { 'H', 'A', 'E', 'S', 'L', 'E', 'R', '\0' } ;
void main( ){
int i = 0 ;
while ( i <= 7 ){
i++ ;
Output-:
32:
#include<stdio.h>
#include<string.h>
33:
struct Student{
char name[25];
int age;
char branch[10];
char gender;
};
int main(){
struct Student s1;
s1.age = 18;
strcpy(s1.name, "Viraaj");
printf("Name of Student 1: %s\n", s1.name);
printf("Age of Student 1: %d\n", s1.age);
return 0;
}
Output-:
Typedef -:
The C programming language provides a keyword called typedef, which
you can use to give a type, a new name. Following is an example to
define a term BYTE for one-byte numbers: typede funsigned char BYTE;
Typedef vs #define -:
#define is a C-directive which is also used to define the aliases for
various data types similar to typedef but with the following differences:
typedef is limited to giving symbolic names to types only, where as
#define can be used to define alias for values as well, e.g., you can
define 1 as ONE, etc.
typedef interpretation is performed by the compiler where as #define
statements are processed by the preprocessor.
#include<stdio.h>
#defineTRUE 1
#defineFALSE 0
int main(){
printf("Value of TRUE : %d\n",TRUE);
printf("Value of FALSE : %d\n",FALSE);
return 0;
}
35:
Output-:
#include<stdio.h>
void main( ){
36:
float r1 = 6.25, r2 = 2.5, a ;
a = AREA ( r1 ) ;
a = AREA ( r2 ) ;
Output-:
PROGRAMMINGS
int main() {
int num;
scanf("%d", &num);
if(num % 2 == 0)
else
return 0;
Output –
39:
2. C Program to Count the Number of Digits -:
#include <stdio.h>
int main() {
long long n;
int count = 0;
scanf("%lld", &n);
while (n != 0) {
n /= 10;
++count;
Output –
#include <stdio.h>
int main(){
40:
char line[150];
scanf("%[^\n]", line);
line[i]=='O' || line[i]=='U') {
++vowels;
line[i]<='Z')) {
++consonants;
++digits;
printf("Vowels: %d",vowels);
printf("\nConsonants: %d",consonants);
printf("\nDigits: %d",digits);
return 0;
Output –
Numbers -:
#include <stdio.h>
int main() {
42:
double n1, n2, n3;
else
return 0;
Output –
#include <stdio.h>
int main() {
int year;
43:
printf("Enter a year: ");
scanf("%d", &year);
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0)
else
else
else
return 0;
Output –
44:
6. C Program for Factorial of a Number-:
#include <stdio.h>
int main() {
int n, i;
scanf("%d", &n);
if (n < 0)
exist.");
else {
fact *= i;
return 0;
}
45:
Output –
#include <stdio.h>
int main() {
int i, n, t1 = 0, t2 = 1, nextTerm;
scanf("%d", &n);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
return 0;
Output –
46:
#include <stdio.h>
int main() {
while (1) {
min);
break;
++min;
return 0;
}
47:
Output –
#include <stdio.h>
int main() {
scanf("%d", &n);
originalN = n;
while (n != 0) {
remainder = n % 10;
n /= 10;
if (originalN == reversedN)
else
Output –
#include <stdio.h>
int main() {
int n, i, flag = 0;
scanf("%d", &n);
if (n % i == 0) {
flag = 1;
break;
if (n == 1) {
else {
if (flag == 0)
else
return 0;
Output –
#include <stdio.h>
int main() {
scanf("%d", &num);
originalNum = num;
50:
while (originalNum != 0) {
originalNum /= 10;
if (result == num)
else
return 0;
Output –
#include <stdio.h>
int main() {
int i, j, rows;
printf("* ");
printf("\n");
return 0;
Output –
#include<stdio.h>
int main() {
int i, j, rows;
printf("* ");
printf("\n");
return 0;
Output –
#include <stdio.h>
int main() {
printf(" ");
while (k != 2 * i - 1) {
printf("* ");
++k;
printf("\n");
return 0;
Output –
54:
#include <stdio.h>
int main() {
scanf("%d", &rows);
printf(" ");
printf("* ");
printf("* ");
printf("\n");
return 0;
}
55:
Output –
int main() {
scanf("%d", &rows);
printf(" ");
if (j==0||i==0)
coef=1;
else
56:
coef=coef*(i-j+1)/j;
printf("%4d", coef);
printf("\n");
return 0;
Output –
#include <stdio.h>
int main(){
int i, n;
float arr[100];
57:
printf("Enter total number of elements(1 to 100): ");
scanf("%d", &n);
printf("\n");
scanf("%f", &arr[i]);
arr[0] = arr[i];
return 0;
Output –
58:
15. C Program for Reverse a sentence using recursion -:
#include <stdio.h>
void reverseSentence();
int main() {
reverseSentence();
return 0;
void reverseSentence() {
char c;
scanf("%c", &c);
if( c != '\n') {
reverseSentence();
printf("%c",c);
Output –
59:
16. C Program to Find Transpose of a Matrix -:
#include <stdio.h>
int main() {
scanf("%d", &a[i][j]);
transpose[j][i] = a[i][j];
printf("Transpose of Matrix:\n");
if(j==r-1)
printf("\n");
return 0;
Output –
#include <stdio.h>
int main() {
scanf("%s",s1);
s2[i] = s1[i];
s2[i] = '\0';
return 0;
Output –
#include<stdio.h>
int main() {
a=a+b;
62:
b=a-b;
a=a-b;
return 0;
Output –
Output –
#include <stdio.h>
int main() {
scanf("%d", &base);
scanf("%d", &exp);
while (exp != 0) {
result *= base;
--exp;
return 0;
Output-
int main() {
64:
char operator;
scanf("%c", &operator);
switch (operator) {
case '+':
second);
break;
case '-':
second);
break;
case '*':
second);
break;
65:
case '/':
second);
break;
default:
return 0;
Output-
#include <string.h>
int main() {
char s[100];
66:
int i;
gets(s);
return 0;
}
67:
Output-
#define DAYSINWEEK 7
void main() {
scanf("%d",&ndays);
year = ndays/365;
Output-
68:
24. C Program of Binary Search -:
#include <stdio.h>
int main() {
scanf("%d",&n);
scanf("%d",&array[c]);
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
first = middle + 1;
break;
else
last = middle - 1;
search);
return 0;
Output-