Irishpride<3
[√] Flameworthy
+76|5546|Baile Atha Cliath, Éire
My lecturer is always telling us that we should space out each line using the tab key. I don't really understand what he means. He wants us to make it neater. Is this what he's looking for?

EG

Code:

/*Write a program to compute:
(a) the volume 
(b) the surface area of a box with a height of     10cms, a length of 11.5 cm and a width of 2.5 cm.*/


#include <stdio.h>


int main()
{
    float height, length, width, volume, surfacearea ;
        printf ("Enter the height: ") ;
        scanf ("%f", &height) ;
            printf ("Enter the length: ") ;
            scanf ("%f", &length) ;
                printf ("Enter the width: ") ;
                scanf ("%f", &width) ;
            volume = length*width*height ;
            surfacearea = 2*(length*width+length*height+width*height) ;
        printf ("The volume of the box is %.2f and the surface area of the box is %2.f \n", volume, surfacearea) ;
    return 0 ;
}
Varegg
Support fanatic :-)
+2,206|7056|Nårvei

Kind of ...

You tab the lines accordingly to how "important" they are ... just view the coding of any webpage and compare how tidy they look ...
Wait behind the line ..............................................................
liquidat0r
wtf.
+2,223|6873|UK
In some languages, tab indentations are required (such as in Python). I have no idea if that is the case with C.

Elsewhere, such as in PHP, tab indentations are commonly used to make the code easier to read, such as:

Code:

<?php

$i=1;
while($i<=5)
      {
      echo "The number is " . $i . "<br />";
      $i++;
      }

?>
Especially if you have if statements inside other loops or if statemetns, etc

Python:

Code:

def square_root(x):
    y=x
    while abs((y*y)-x)>0.0000006*x:
        y=0.5*(y+(x/y))
        print y, y*y
    return y

print round(square_root(25),6)
The code doesn't work without correct indentation.
Irishpride<3
[√] Flameworthy
+76|5546|Baile Atha Cliath, Éire

Varegg wrote:

Kind of ...

You tab the lines accordingly to how "important" they are ... just view the coding of any webpage and compare how tidy they look ...
And how do I do that?

Also, what the hell does:

Code:

fflush(stdin);
do? I see it on all my classmates programmes but I've got no idea what it actually does... It normally follows the scanf()
AussieReaper
( ͡° ͜ʖ ͡°)
+5,761|6398|what

Yeah that's exactly what he means.

Just to make things like loops, elseif etc more easy to follow when you read through.

Don't forget to add comments where necessary. You want it to be readable for someone else to come along if they have to modify the code, but at the same time don't go overboard with useless comments. Keep them short and simple.
https://i.imgur.com/maVpUMN.png
liquidat0r
wtf.
+2,223|6873|UK

Irishpride<3 wrote:

And how do I do that?
Right click on the background of any webpage, then "view page source". That'll show you the HTML (and any inline javascript/css)
Varegg
Support fanatic :-)
+2,206|7056|Nårvei

Right click any webpage and you'll get access to the coding ...
Edit: The nerd beat me to it
Wait behind the line ..............................................................
AussieReaper
( ͡° ͜ʖ ͡°)
+5,761|6398|what

Code:

<div id="page-1">
    <div id="page-2">
        <h1 id="page-title">BF2S Forums  <small> C - Tab Key?</small></h1>
            <div id="page-3">
    
                <div id="content"><div id="content-id">

                <div id="punwrap">
                    <div id="punviewtopic" class="pun">

                        <div id="brdheader" class="block">
                            <div class="box">

<div id="brdmenu" class="inbox">
is more readable than

Code:

<div id="page-1">
<div id="page-2">
<h1 id="page-title">BF2S Forums  <small> C - Tab Key?</small></h1>
<div id="page-3">
<div id="content"><div id="content-id">
<div id="punwrap">
<div id="punviewtopic" class="pun">
<div id="brdheader" class="block">
<div class="box">
<div id="brdmenu" class="inbox">
Especially when you have that ^ times 100
https://i.imgur.com/maVpUMN.png
Irishpride<3
[√] Flameworthy
+76|5546|Baile Atha Cliath, Éire
Yeah, I get what you mean... But how do i know when to tab... Looks fairly random IMO.
AussieReaper
( ͡° ͜ʖ ͡°)
+5,761|6398|what

I'll try to use the example I've given already to show that.

Code:

<div id="page-1">
    <div id="page-2">
        <h1 id="page-title">BF2S Forums  <small> C - Tab Key?</small></h1>
            <div id="page-3">
    
                <div id="content"><div id="content-id">

                <div id="punwrap">
                    <div id="punviewtopic" class="pun">

                        <div id="brdheader" class="block">
                            <div class="box">

<div id="brdmenu" class="inbox">
Page 1. Page 2. Heading 1. Page 3. Content on Page 3.


It makes the above ^ easier to find should you go looking for them.
https://i.imgur.com/maVpUMN.png
Microwave
_
+515|6901|Loughborough Uni / Leeds, UK
You tab stuff that's relevant / related.


For example, if you have an if statement, then the action would be followed on a new line and indented. Then if you had an else statement that would be on a new line but inline with the if statement because you read it as  if this is true do this else do this.

Or in html, if you had a div, then the contents of the div would be indented on a new line. And if you had a paragraph tag the contents of the paragraph would be indented on a new line.

Also the close tags of something should be inline with the opening.


Hope that kinda makes sense!
Varegg
Support fanatic :-)
+2,206|7056|Nårvei

Page1
     Headlines
          Subheadlines
               Rabble rabble
Wait behind the line ..............................................................
Finray
Hup! Dos, Tres, Cuatro
+2,629|6034|Catherine Black
It's just to make it easier to read, as said above. In Visual Basic, you get stuff like this

Private Sub
     Code
End Sub

The code is separate from the start/end of the sub.
https://i.imgur.com/qwWEP9F.png
Flaming_Maniac
prince of insufficient light
+2,490|6952|67.222.138.85
You want to tab stuff that is nested inside other stuff. That point has been danced around here, I'm just trying to clarify.

I'm a scrub that learned java, not C, but you should get the point

main
{
      stuff
      more stuff
      function blahblah()
      {
           stuff
           while()
           {
                   do this
                   then this
            }

      if()
      {
            stuff
       }

}


Whenever you logically move down to do some sort of subroutine or loop, indent it. When you exit back to the previous logical level, go back to your original place.

Board footer

Privacy Policy - © 2024 Jeff Minard