0 ratings 0% found this document useful (0 votes) 10 views 13 pages Coding Explanation
The document provides an overview of basic C programming syntax and structure, including examples of simple programs for printing output, taking input, and performing calculations. It explains key concepts such as variable declaration, conditional statements, and the use of functions like printf and scanf. Additionally, it covers commenting in code and the differences between input and output functions.
AI-enhanced title and description
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here .
Available Formats
Download as PDF or read online on Scribd
Go to previous items Go to next items
Save coding explanation For Later @
Basie Struelure and syntax of a ¢ program
# include
int main 0) £
Print (“Hello World !\n") 5
retunn O;
}
Explanation ¢
include
int mainc) §
int number +
Print? (“Enten a number: ") 7
Seant ("%d" &enumben) ;
Print? ("You entered '%d\n”, number) ;
Tetun 0;
j
Explanation:
» #inelude > Qneludes the standard input /output
libaray.
eVanjable Declarations int numben 5 > Declanes an integer
variable to store ineut:
|. output Prompt : print? (“Ehlen anumber : *)i —? Displays a
| message to the user: |
\e4nput * Seant’ (4d, Senumber) ; ynead s an integer ficom She
lusert sThe & operator passes the varlable memory add itess.
|4o seant (0-
|. Display output : printf (Meu etened sd \n", numbee); —»
prints the entened number - |
‘Praga Gnd; reehinn0j ~» End the Program successfully. |ao
waiting and Puming a Simple c program
“include
int main §
| int num 5
| print (“Enter a number : "),'
| Seant ("2d") Boum) 5
Print? (“Square = %d", num* num);
| retun 05
|}
«# include dneludes the standard input/
ouput library fin using print) and seanft).
«int main (§...) >This defines the main functton »-
whieh is the entry point of every ¢ program. Execu-
dion begins herce-
variable deelaration: int num —> Declanes an integer
variable +o store the input value.
prompt fore gnput: Print? (Enter anumber : 9); > displays
1. message asking ‘the . user 4o enter a numben. |
epead Snput : Seanf ("gd &mum), —> Takes the integer
input fiom the userc and stones it in num. The & operat
h(
C)
|
passes the address of num to seant():
scaleulate and Display Culpul + printf‘ squane = Xd") MUM sy,
num); -> Computes the square of the number and prints
it
* pragram End * nelutn 05 —2 Ends the Program success.
fully.
Canlitional Stalements (fee fete)
Example : Girading system:
# ineludestdioshy
int main () $
int manks 5
printf (“Enten youre marks: ”) 5
Seanf («hd & marks);
if (manks > = 80)
“printf (“Gnade ‘ADs
else if (manks > = 60)
print? ( “Girade #8) s
else
printf (“inode sc"),
relutn 0;Sample Output:
Enter youre manks: 85
Gnade : A
Explanation :
eHinelude CStdioch) -» Ineludes the standand input output
tibrany, whieh provides the function Print? () and seanf().
«Variable declaration: int manks + > Declares an integer
Variable named manks to store the usen’s input.
egnput prompt: print? (“Enfen youre mans s "3 > Displays a
message prompting the user to enten thein mans.
ePead Jnputt seanf (%%d" &manks); 7 Reads the input
-freom “the users and stores it inthe variable manks- The
& operator provides the addness of the vaniable for input.
Conditional logic +
> tf Lmanks >= 60) Cheeks PF
tye ; its prints « Grade A”.
>=60) cheeks if the manks ane between
the monks ame 80 on higher IF
else if (marks
60 and 79; if true it prints “Guzade + 8”
5 ebe covers all othert Gases (marks below 69) and: prints
® Grade 1¢"- |
|©
* Outpt Displays The pragram uses Prinif() 4o display
the corresponding grade based on the usen's marks.
program End? Retunn 05 -by Ends the Program
Suecessful execution.
Program 4o calculate the Area ofa Rectangle
#inelude Entec tength of the neclangle : 5:5
Enter width of the rectangles 3-2
Area of the rectangle = 17-60| Explanation : - —______
eH include ZStdion h> > Includes the standard input/
Oubut libary fir using printf and seanf O-
¢ int main 0 §....} — Tis defines the main function,
which Is the entry point of every ¢ program: Execution
begins here.
e Variable declaration float length , width, area 5 ~> declares
here floating - polnt variables to stone the dimesions and
the cdleuloded aned-
+ gnput The Program “Prompts the users {0 enter. the
length and width sepanadely, using print? () and reads
the alues using seanf © with nf FF. mot specifier = |
for Float |
« Coleulation; “The area is computed yy mubiplying the
lergth ond width (area= length * width)
« Output : the aaleulated area is displayed with hoo
deeimal places using prirkf(y and the format specifier |
yok |
+ pragream End: The pragram returns 0; — Ends the
| Program suecessfuly.@
program to check whether a Number is Even on od
A include
int maint) $
int number j
printf ("Enter a numbers "),
Seanf ("%d", *&number);
Hf (number % 2 == 0)
print? ("fd is an even number-\n", number) ,-
else
printf (4d ig an odd number -\n", NuMben),’
return OF
}
Sample oulput:
Enter. amumbert: 24
nh is an even numben -
Anothen Sample output:
eth dn ec a De
Enter. a number. ‘15
45 15 @n Odd Number.| Explanation s
*#include gneludes the standard input/
Output library for using printf) and seanf) -
cint main (> $.-} > This defines the main function,
which is the entry point of every @ PItOgmam -
Execution begins here -
evariable declaration: int number’ —> Declares an integer:
Variable to store the users input-
°4nput : The Program prompts the users to enter a
number using prinif() and reads the input using
sean ()-
Condition check:
>The modulus opercaton % computes the remainder of
number divided by 2 (number 72).
-9 4f the reminden iS O(numbenz 9 == 0), the Program
contludes the number is even and prints a message
accortdirgly-
> otherwise it prints that the numben is od}.
|
|
|
|
Preogicam End: return 0; — End the program successfly,©
Commenting
|e C1 progratnming, a \Commenhis a statement vonitten in
the -sourtee code that is ignored by the icompilery dh does |
‘not ateet the execution of she Pregram « Comments ane
honitten to \ explain the regen and make it easien fort!
ae 4o read and understand. ‘ t ind |
oa: -Line Commenting |
Sa Ranrhnagaee
|A single - line comment, in, ¢ is used to ‘wre ae notes
lin one line. the comer. ignones -evergylhing atten on ||
hat line.
Hemi 4 ya |
| syntas. yw comment text | |
‘Examele: te nl \
| \# } include Va \
Pint inain ee aoa] baer all |
int x=5 5 Zvanigble x is deeloned with | walue Sy oii |
\ |
‘nil #4 “4d; : , : wa me) |
“return 07 tang | (ay dior afl i |
J |
| purpose: ist
Sto explain a’ 'singlé’ statement, |
he make the eode easien + unde
b> To give short notes beside code."Imulli= Tine commenti
A multi-line commenting in @ is Used when
the explana
is long dnd covers more than one. line. The compiler ignores
|
everything. between /* and: 47.
sgntoe
Ze
-~ comment -text line 2
| gomment text Hine 2
vw
Bxomele! :
# indude Zstdio:h> :
int main © §
Pe This program prints
Avthon: Radia Begum
Hello world
|
| ¥
| printf ("Hello world \N") 5
qreturcn 07 aS aa
4 4 Vo¥
Purtpase:
So explain a block of code.
>To give detailed program “information.
27 write author name,
2To temporarily disable multiple lines
data on description .
of code,
\Is:
Wa oe
is syntax of input and output in ¢ mip duomala-se dw |
‘oudput inc: |
—_——_
Output in'c is the process of displaying
‘on the stneen.the function used fore output 's
| gyrrtax:
printf ("format sting’ anguments),
Example:
# include
int main t) &
int age =20 s
Print? ("my age istd's age);
return OF
|
5
output: my %
input in_¢:
4nput in ¢ ig the process of
[during preggream execution: The Lmetion used Fo
seant © -
Ee ("ferret sining’, & orcas) Ki
data on results |
printf() |
2 15 20.
taking data from the user
input isEample:
# include
int main ¢) {
int num ;
Sean ( "2d", Bemus
print? ("you ertened
neturn 07
5
pr ou
You entened +
hpfercences belween prin? ¢
mint PO)
ym jon the. se the seneen:
paint ( seine ( “foremat sticing ”
uments)
Printt ("Enter a vumber : ")
ind", pum) 5
Enterta oes os
5 and scanf
[= Pal
Teed 4 display /oudut deta
sh a Be
seo
| used 40 take /input data
fiom +he | the user:
—\geant (“Pormat string 2” 7 &
variables) 5
4o senech
Sends data “ream Pregream
Takes data fram user 4
PROgIAM -
print “Agee ud”) O90)?
seanf ("Zd"1 & 09¢)
Fon showing mesults,
messages , variables »
Does not requike zx:
(ampensand).
For accepting numbert |
chanactere stings:
lmequives and “befre
variable (except string).