0% found this document useful (0 votes)
3 views12 pages

C Notes

The document provides comprehensive notes on the C programming language, covering basic syntax, data types, escape sequences, and input/output functions. It includes examples of printing messages, using comments, and performing arithmetic operations, as well as more advanced topics like conditional statements and switch cases. Additionally, it demonstrates the use of mathematical functions and constants, along with practical exercises for calculating the circumference of a circle and determining voting eligibility based on age.

Uploaded by

riyayy09
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

C Notes

The document provides comprehensive notes on the C programming language, covering basic syntax, data types, escape sequences, and input/output functions. It includes examples of printing messages, using comments, and performing arithmetic operations, as well as more advanced topics like conditional statements and switch cases. Additionally, it demonstrates the use of mathematical functions and constants, along with practical exercises for calculating the circumference of a circle and determining voting eligibility based on age.

Uploaded by

riyayy09
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

C language notes

$Print hello world and hi in next line. Also add comment 2 ways.

#include<stdio.h>

int main(){
printf("Hello world\n");
printf("Hi");
// This is a single line comment
/* This
a
multiline comment*/
return 0;
}

Hello world
Hi

NOTE To go to next line backslash n (\n) is used and for comment frontlash (// & /* … */) is used

ESCAPE SEQUENCE : A combination of backslash and a letter or digits eg. \n for next line, \t for tab
space within a string

#include<stdio.h>

int main(){
printf("1\t2\t3\n4\t5\t6\n7\t8\t9");
return 0;
}

1 2 3
4 5 6
7 8 9
int main(){
printf("\"A qoute\"\n");
printf("\\HELLO\\");
return 0;
}

"A qoute"
\HELLO\

int main(){
// Creating a variable= declaration +
initilization
int x; // declaration
x=3; // initilization
int y=4; //declaration + initilization

char name[]= "RIYA"; //multiple characters


array %s
char grade='C'; //SINGLE CHARACTER %c
int age=18; //%d
float cgpa=1.03; //%f

printf("Hi %s\n",name);
printf("You are %d years old\n",age);
printf ("Your cgpa is %f\n",cgpa);
printf ("Your grade is %c\n",grade);

Hi RIYA
You are 18 years old
Your cgpa is 1.030000
Your grade is C
#include<stdio.h>
#include<stdbool.h>

int main(){
// DATATYPES
char a = 'B'; // Single character %c
char b[] = "Riya"; // Array of character
%s
float c = 2.334567; // 4 bytes (32 bit
precision) 6-7 digits %f
double d = 2.2245965347623; // 8 bytes
(64 bit precision) 15-16 digits %lf (double means
long float)
bool e = true; // 1 byte (t(1) or f(0))
%d
char f = 120; // %c if ASCII alphabet op
is req & %d if the no. 120 is req
// 1 byte(-128 to 127)
unsigned char g = 25; // 1 byte (0 to
255) %d or %c

short h = 32767; // 2 bytes (-32768 to


+32767) %d can also be written as short int h
unsigned short i = 65536U; // 2 bytes (0
to +65535) %d IF 65536 then op will be 0
overflowed
// to avoid overflow warning add U after
number
int j = 23; // 4 bytes (-2 bln to +2 bln)
%d int is by default long int
unsigned int k = 45; // 4 bytes (0 to +4
bln) %u
long long int l = 2635498627363; // 8
bytes (-9 quintillion to +9 quintillion) %lld
unsigned long long int m =
27878888272222; // 8 bytes (0 to +18 quintillion)
%llu
printf("%c\n",a);
printf ("%s\n",b);
printf ("%f\n",c);
printf ("%lf\n",d);
printf ("%d\n",e);
printf ("%c\n",f);
printf ("%d\n",f);
printf ("%c\n",g);
printf ("%d\n",g);
printf ("%d\n",h);
printf ("%d\n",i);
printf ("%d\n",j);
printf ("%u\n",k);
printf ("%lld\n",l);
printf ("%llu\n",m);
return 0;
}

B
Riya
2.334567
2.224597
1
x
120

25
32767
0
23
45
2635498627363
27878888272222

int main(){
/*format specifiers
%c = single char
%s = array
%d = int
%f = float
%lf = double

%.2 decimal precision


%7. = minimum feild length
%- = left align*/

float weight1 = 60.423;


float weight2 = 67.3;
float weight3 = 70.8;

printf ("First weight is %.2f\n", weight1);


printf ("Second weight is %8.3f\n", weight2);
printf ("Third weight is %-8.1f\n", weight3);
return 0;

First weight is 60.42


Second weight is 67.300
Third weight is 70.8

int main(){
// constant - a fixed value that cannot be
altered by the program during its execution
const float PI = 3.14159;
printf ("%.5f\n",PI);
/* ARITHMETIC OPERATORS
+ - * / % ++ -- */
int x = 5;
int y = 2;
int z = x/y; // division of 2 integers yeilds
a integer
float p = x/ (float) y;
printf ("%f\n",p);
printf ("%d\n",z);
x++,
y--;
printf ("%d\n",x);
printf ("%d\n",y);
return 0;
}

3.14159
2.500000
2
6
1

int main(){
int x = 5;
x+=2; // x = x+2
printf ("%d",x);
}

7
int main(){
// scanf stops reading user input when and
where a white space is encountered
// we can use fgets function to read white
space too
char name[20];
printf ("What is your name? ");
fgets(name,20,stdin);

int age;
printf ("What is your age? ");
scanf("%d",&age);

printf ("Your name is %s", name);


printf ("You are %d years old.", age);
return 0;
}

What is your name? Riya Garg


What is your age? 18
Your name is Riya Garg
You are 18 years old.

int main(){
// math functions
double a = round(7.7);
double b = ceil(2.3);
double c = floor(3.9);
int d = fabs(-20);
double e = sin(45);
int f = pow(2,3);
double g = log(2);
int h = sqrt(16);
printf ("%f\n",a);
printf ("%f\n",b);
printf ("%f\n",c);
printf ("%d\n",d);
printf ("%lf\n",e);
printf ("%d\n",f);
printf ("%lf\n",g);
printf ("%d\n",h);
return 0;
}

8.000000
3.000000
3.000000
20
0.850904
8
0.693147
4

Q WAP to calculate the circumference and area of


a circle.

int main(){
double radius;
printf ("Enter radius: ");
scanf("%lf",&radius);
double circum;
const double PI = 3.14159;
circum = 2*PI*radius;

double area;
area = PI * pow(radius,2);

printf ("Circumference of the circle is %lf\


n", circum);
printf ("Area of the circle is %lf", area);
return 0;
}

Enter radius: 2.5


Circumference of the circle is 15.707950
Area of the circle is 19.634937

Q. WAP to find the hypotenuse of a right


triangle.

#include<stdio.h>
#include<math.h>

int main(){
double base;
double height;
double hypo1;
double hypo;

printf ("Enter base: ");


scanf ("%lf",&base);
printf ("Enter height: ");
scanf ("%lf",&height);
hypo1=pow(base,2)+pow(height,2);
hypo=sqrt(hypo1);

printf ("The hypotenuse is %lf", hypo);


return 0;
}

Enter base: 3
Enter height: 4
The hypotenuse is 5.000000

Q WAP to input age and tell if the person can


vote.

#include<stdio.h>

int main(){
int age;
printf ("What is your age :");
scanf ("%d",&age);
if(age>=18){
printf ("You are elegible to vote!");
}
else if(age<0){
printf ("Invalid age!");
}
else{
printf ("You are not elegible to vote!");
}
return 0;
}

What is your age :15


You are not elegible to vote!

Q Switch statements
#include<stdio.h>

int main(){
char grade;
printf("Enter your grade: ");
scanf("%c",&grade);

switch(grade){
case 'A':
printf ("Excellent");
break;
case 'B':
printf ("Good");
break;
case 'C':
printf ("Can do better");
break;
case 'D':
printf ("Dog");
break;
case 'E':
printf ("Very poor");
break;
default:
printf ("Enter valid grades");
}
}

Enter your grade: A


Excellent

You might also like