-
Notifications
You must be signed in to change notification settings - Fork 3
/
C Language
406 lines (290 loc) · 12.6 KB
/
C Language
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
Let us start Our C.
First of all I will tell you that C is a high level language,
which developed by Dennis Ritchie, in 1972, in AT and T's bell laboratory California
Now I will tell you about levels of language----->>
We generally have two level of language-->
LOW LAVEL and HIGH LAVEL
A low level language->
To code in a low level language we have a good knowledge of hardwares
because it is a Machine dependendent language.
without a knowledge of hardware we coud't use a low level language.
High level language->
High level language is that language which does not depend on any hardware.
or we can say that a user does not need any knowledge of hardware.
because a high level language is developed that any user can use that without any knowledge of hardware.
I hope you understand what would I want to say by using these words
That's the different thing that a low level language is easily understood by computer.
Or we can say the machine but a high level language is easily understand by us
that is why all the language which are consider in programming are in high level.
Now I will tell you something about some word which are already fixed for some purpose are known as keyword we have 32 keywords in C.
Auto double int struct break elss
Long switch case enuam register typedef
Char extern return union const float
Short unsigned continue four signed void
Default goto Sizeof Volatile do If
Static while
Now I will tell you a lot talking about the characters of c
Alphabets A,B,C ......Z or a, b, c........z
Digits .....-9,-8,-7,-6,-5,-4,-3,-2-1,0, 1, 2, 3, 4, 5, 6, 8, 9,......
Symbols ! @ # % ^ & * () _ + - = ; : ' " .......
NOTE:->
we can we can use any kind of symbol in C language except with Dollar sign ($) this sign is not Allow in C language.
Now I will tell you where we can write C program-->
I advise you to use code block either in Windows and Linux turbo C also used as compiler in which we can write our c program.
But if you are using Linux then builder in the best choice or we can say a best platform to write your program.
In Linux we use a command gcc name of programc.c and a command ./a.out to run our program.
structure of a program==>>
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("hello word");
getch();
}
This program arising a lot of questions in your mind
well these questions are arise in my mind also that
what is #include ?
and all the things written in the program.
Now I will tell you everything about it clearly==>>
The symbol (#) known as hash or called a preprocessor in c.
And the written word (include) with symble is called an derivative.
When both include and hash(#) combine they form a preprocessor derivative.
which connects a library to our programe. And the library name which we connect to a program is <stdio.h>.
Now I will tell you what is a C librarie==>
A c librari in actual it is a file contains some function of c which are used by us in a program.
we even create our own c library by using the extension .h.
This library will contain the
printf("") functions These function are contained by this library.
and the function void main is also from the same library.
The first function printf() ==>
This function is used while we want to print something on the screen As our first program that we print hello world.
we write hello world into function printf() to print hello.
One more thing about the program the semi colon (;) which is used in the last of each and every line
in a program is known as Terminator in C language it is used at the end of every statement.
while we finish our program or start our program we use a pair of curly brackets {} affter main function.
this pare which tell that our main
function is being start and at end it tell that our function is over.
Now you will see that one more library is connected by us that is <conio.h> this library with
contain the clrscr() and getch() function
now I will tell you the use of these function.
The function clrscr() is use to clear the screen.
If you don't understand what is this clear screen then don't get nervous because I will tell you about it in other programs.
And this the last function getch()==>
Is used to store our result if we do not use this function then our .exe extension of program does not so any result.
I hope now you will understand everything about our first program the hello world.
You still you have any problem then you can contact us.
I am writing some questions you have to go to code them.
Questions
1 write a program to show i love my country.
2 write a program which show early to bed early to rise makes a man healthy wealthy and wise.
3 write a program which show my name is vipin
i belong to india
answers
1
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("i love my country ");
getch();
}
2
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("early to bed early to rise makes a man healthy wealthy and wise.");
getch();
}
3
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("my name is vipin \n i belong to india");
getch();
}
OR
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("my name is vipin");
printf("i belong to india");
getch();
}
As you can see in 3rd question I use \n what's this combination is used to print something from a new line.
Is combination are called escape sequence Many more combination are there like.
\t
\v
\b
\a
You can use any sequence
to make your program better or to give a special effect to your program you can try all these by you yourself.
Using variables.
C variable is nothing but it is just a name is given to any data type.
It also have some rules - ---->
It does not contain any other character except underscore.
It should not be a keyword of c.
it should not be larger than 32 words.
NOTE:===>> name of variable shoud be of your choice.
now i will tell you what a data type is ===>
A data type is nothing it is just a type of data like if integer character decimal number and Many other kind of data.
Let us discuss diffrent type of data types.......
PRIMARY DATA TYPE AND SECONDARY DATA TYPE
PRIMARY DATA TYPE ======>>>>
Integer --->
It is Kind of data type which tell that a integer is being used in a program
in easy language we can say that a number is use in our program.
Character data type --->
It is a kind of data type which tell that a character
even that is symbol for a letter or an alphabet is used in our program.
Double data type---- >
This data type tell us that a decimal number is being used in our program
for printing for taking a decimal number from user
the same work is being done by float data type but
float data type round off our number and double data type does not round orf number.
Float data type --- >
It is also act as double data type it also tell that it is a molecule your program
but it round off that decimal number.
SECONDARY DATA TYPE =====>
This data type use while we learn moduler progreaming
in c they form using primary data types.
Let us start learning using of data types=====>
Simple C program which accept a integer--
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a;
printf(" enter a number \n ");
scanf("%d",&a);
getch();
}
As you can see we use another keyword of c name int.
Have you remember that why we use int ?
Because we are using an integer in a program.
How to define a variable in C--->
Simply we write the keyword for the data type of variable and write the name of variable as we have done in a program.
int a; =====> hear we defin a variable name a;
Haven't I tell you about scanf() function till now .
Now you will learn use of scanf() function--->
It is a function which we use while taking input from user it has a specific format
First of all we have to write scanf("") with invited coma ("") and in invited comma
THen We have to use
%d for integer
%c for character
%f for float
%lf for double
As you can see we are using an integer program that is why we use %d
And then we use a (,) affter invited comma (scanf("%d",)) and then we use this symbol (&)
scanf("%d",&)
This symbol will locate our variable into the memory of computer or simply we can say provide address to our variable.
scanf("%d",&a) HEAR a IS THE NAME OF VAREABLE GIVEN BY US.
now I will tell complit function again
in Starting we write scanf() then use invited commas then we use symbol (&) and then use comma
and then write the name of our variable which we define above.
Now we will start how to take a number and print in a program .
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a;
printf(" enter a number \n ");
scanf("%d",&a);
printf(" entered number is %d \n ",a);
getch();
}
As you can see we again use %d in our program in function printf("%d",a) ;
One more time you will notice that we does not use the symbol (&) hear
because here the function will automatically
find the location of a and print the number on the screen.
Now we will start how to take a float and print a float in a program .
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
float a;
printf(" enter a decimal number \n ");
scanf("%f",&a);
printf(" entered float is %f \n ",a);
getch();
}
As you can see we again use %f in our program in function printf("%f",a) ;
One more time you will notice that we does not use the symbol (&) hear because here the function will automatically find the location of a and print the chracter on the screen.
Now we will start how to take a character and print a character in a program .
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char a;
printf(" enter a chracter \n ");
scanf("%c",&a);
printf(" entered character is %c \n ",a);
getch();
}
As you can see we again use %c in our program in function printf("%c",a).
Now we will start how to take a double number and print a double number in a program .
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
double a;
printf(" enter a double number \n ");
scanf("%lf",&a);
printf(" entered double number is %lf \n ",a);
getch();
}
As you can see we again use %lf in our program in function printf("%lf",a) ;
And now we will discuss about operators in C.
Generally we have three types of operator=====>>>>>
1 Unary operator
2 Binary operator
3 Ternary operator or conditional operator
Unary operator ----->
Unary operators are used only single operands
For example (), [] , {} etc.
Binary operator---->
Binary operator need two operands
For example addition ,subtraction, multiplication etc.
Ternary operator--->
This operator need three operands to work one is condiction
and two are expresion. We will deeply study while we make some programs
on it.
Arithmetic operaters--->
Arithmetic operators are you to perform any mathematical operation
Like
+ adding
- subtracting
* multiply
/ divide
% finding remainder.{10%3=1(remainder)}
ther all are arthmatic operater type of binary operater.
Relational operators===>>
From it's come in the name of operator this operator show any
relation between two operands.
< less then
> greater then
== equal to
<= less than equal to
>= greater than equal to
!= no equal to
They are also binary operator
Logical operator=====>
We have three kind of logical operator in C.
A && and operator
B || or operator
C ! not operater
Sizeof() operator =====>
This operator tell us the size of a data type.
We will discuss it later while we may program on operator.