Tuesday, September 12, 2017

Introduction to C Programming Basic Structure and Fundamentals

The basic form of a simple C program is as shown below. ( Line numbers have been added for future reference. )
 1 /* Filename.c
 2
 3    This program was written by Ima Programmer on some date
 4
 5    This program does something useful . . .
 6
 7 */
 8
 9 #include <stdlib.h>       // For accessing the standard library functions
 10 #include <stdio.h>        // For standard input and output functions
 11
 12
 13 int main( void ) {
 14
 15  // Declare variables to be used
 16
 17  // Explain to the user what the program does
 18
 19  // Get user input ( and check it if possible )
 20
 21  // Perform necessary calculations
 22
 23  // Report results ( in a complete and well-formed format. )
 24
 25  system( "pause" ); // Only needed with certain environments, e.g. Dev C++
 26
 27  return 0;
 28
 29 } // main

Explanation of Specific Lines

  1. The symbols /* and */ enclose a block comment, which may extend over any number of lines, or which may enclose only part of a line.
    • EVERY well-written program should begin with a large block comment specifying the name of the file, the author of the program, when it was written, and a brief description of what the program does and why. In large projects the opening comment may include reference to external documents ( software requirements documentation ), and may include a revision history of who modified the program when and for what purposes.
  2. The #include statement informs the compiler of what libraries are needed.
    • stdlib.h is a header file containing necessary definitions for the use of the standard C library functions. Many systems automatically include this one, but there are exceptions, and it never hurts to include it again, just to be sure.
    • stdio.h contains the definitions of standard input and output functions. It is very rare that a C program would not need this header file.
    • More advanced programs may need to include other libraries, such as cmath for using trigonometric functions.
    • The double slash symbol, //, indicates a comment that extends to the end of the current line.
  3. Line 13 says that we are starting a function named main, which is the starting point for all C programs.
    • The keyword int says that main returns an integer value as its return type. ( See line 27. )
    • The open brace indicates the beginning of a block of code, which must be matched by a closing brace ( on line 29. )
    • The keyword void expressly states that main takes no arguments. In other programs you may see the following alternatives:
      • int main ( ) { - Does not expressly say what arguments main takes.
      • int main( int argc, char *argv[ ] ) { - Beyond the scope of this introduction.
      • int main( int argc, char *argv[ ], char ** envp ) { - Beyond the scope of this introduction.
  4. Some IDEs ( Integrated Development Environments ) execute programs in a separate window, and then immediately close the window when the program is completed. In this case, the system( pause )statement holds the window open so that the results can be seen, until the user presses a key to release it. ( Dev C++ is one of those IDEs. ) Note that CodeLab does not permit the use of system( pause ), so make sure to leave it out when submitting CodeLab solutions.
  5. Every function must have a return statement, which causes the function to end and return control to whoever called it.
    • Because main was declared to have an integer return type in line 13, this function must return an integer.
    • The return value from main is typically interpreted as an error code, with a value of zero indicating successful completion.
    • Negative 1 is a commonly used return value used to indicate an unspecified error of some kind.
  6. It is good practice to comment the closing brace of every function, and any other closing brace that is more than a few lines away from its matching opening brace. This makes it much easier to read more complex programs, which may have MANY sets of nested and consecutive braced blocks.

A Simple Sample:

  • This simple program adds three numbers and reports the total.
 1 /* addThree.c
 2
 3    This program was written by John Bell in January 2013 for CS 107
 4
 5    This program asks the user for two floating point numbers and 
 6    an integer, and reports their total.  Note that one of the floating
 7    point numbers is stored as a double precision data type.
 8
 9 */
 10
 11 #include <stdlib.h>  // For accessing the standard library
 12 #include <stdio.h>  // For standard input and output
 13
 14 int main( void ) {
 15
 16  // Declare variables to be used
 17
 18  int number;                             // The integer
 19  float fpNumber = 0.0f;                  // The floating point number
 20  double dpNumber = 0.0, total = 0.0;     // The double and the total
 21
 22  // Explain to the user what the program does
 23
 24  printf( "This program adds together two floating point numbers\n" );
 25  printf( "and an integer.\n" );
 26  printf( "Written January 2009 by John Bell for CS 107.\n\n" );
 27
 28  // Get user input ( and check it if possible )
 29
 30  printf( "Please enter the first floating point number > " );
 31  scanf( "%f", &fpNumber );
 32
 33  printf( "\nPlease enter the second floating point number > " );
 34  scanf( "%lf", &dpNumber );
 35
 36  printf( "\nPlease enter the integer > " );
 37  scanf( "%d", &number );
 38
 39  // Perform necessary calculations
 40 
 41  total = fpNumber + dpNumber + number;
 42
 43  // Report results ( in a complete and well-formed format. )
 44
 45  printf( "\nThe total of %f plus %f plus %d is %f\n", fpNumber,  
 46    dpNumber, number, total );
 47 
 48  system( "pause" ); // Only with certain environments, e.g. Dev C++
 49
 50  return 0;
 51
 52 } // main

Explanation of Specific Lines

  1. Line 18 declares that this program uses a variable named "number" that is of type "int".
    • All variables in C must be declared befrore they can be used.
      • Traditional C compilers requried that all variable declarations be made before any executable statements.
      • Most C programmers continue to follow this convention, even though the restriction has been relaxed in modern compilers, and there are sometimes good reasons for declaring variables later on in the program.
    • In this example, "number" is not assigned any initial value, so it will start out with an unknown random value.
    • Note the semicolon at the end of the line, which is how C knows that a statement has ended.
  2. Line 19 declares a variable named "fpNumber" of type float, the most basic of floating point data types.
    • This variable has been given an inital value of 0.0.
    • The letter "f" following the 0.0 indicates that this number is to be interepreted to be of type "float", as opposed to the default of "double".
  3. Line 20 declares two variables of type "double", a floating-point data type with twice the numerical precision of the data type "float".
    • Scientific and engineering programs typically use doubles instead of floats, unless there is a specific reason to do otherwise.
    • These variables are also both initialized to zero.
  4. "prntf" is the standard library function for formatted printing
    • A simple text string can be enclosed in double quotes, as shown.
    • The \n indicates a new line character. Without this, all the printout would appear on a single line. Multiple \n characters yield blank lines.
    • Every well-written program should begin by explaining to the user what the program does.
  5. Note that because this line has no "\n" at the end, the curser will be left at the end of the line with the question.
  6. scanf is the standard library function for reading data in from the keyboard.
    • The % symbol is a format specifier, and indicates what type of value to read in. %f specifies to read in a float data type.
    • Note that the variable name must have an ampersand in front of it in a scanf statement. The exact reason for this will be explained in the course section on pointers.
  7. The format specifier for a double precison number is %lf", as opposed to %f". ( A double is essentially a "long" float. )
  8. The format specifier for an integer is "%d". ( "d" stands for "decimal" integer, as opposed to octal or hexadecimal. )
  9. The equals symbol, =, as used here is an assignment, which takes the value from the right side and stores it in the left side.
    • The plus sign, +, peforms addition, and yields the sum of its two arguments.
  10. Format specifiers for printing numbers are similar to those used when reading them in.
    • The first argument to printf when printing numbers is a quoted string, with % format specifiers inserted everywhere that a numerical value is desired.
    • The quoted string is followed by a comma-separated list of values ( variables and/or expressions ) to be printed, one per format specifier.
    • Note that both the float and the double type use "%f" as their format specifier when printing, as opposed to the "%f" for floats and "%lf" for doubles when scanning.
    • Note that the output of the results is a complete sentence, and includes an echoing back of the user's input.
    • Note also that a line of code may span over multiple lines in the file. C/C++ only recognizes the semi-colon as the end of a line.

Variables ( Also covered under Data Types )

  • A variable is a named storage location, where data may be stored and later changed.
  • An identifier is a more general term for a named location, which may contain either data or code.
    • Identifiers must begin with a letter or an underscore, preferable letters for user programs.
    • The remaining characters must be either alphanumeric or underscores.
    • Identifiers may be of any length, but only the first 31 characters are examined in most implementations.
    • Identifiers are case sensitive, so "NUMBER", "number", and "Number" are three different identifiers.
    • By convention ordinary variables begin with a lower case letter, globals with a Single Capital, and constants in ALL CAPS.
      • Multi-word variables may use either underscores or "camel case", such as "new_value" or "newValue".
    • Integers are usually assigned variable names beginning with the letters I, J, K, L, M, or N, and floating point variables are usually assigned names beginning with other letters.
    • Identifiers may not be the same as reserved words. ( See below for a full list. )
  • All variables must be declared before they can be used.
    • In K&R C, all variables must be declared before the first executable statement of the program.
    • Modern C allows variables to be declared any time before they are used, but it is still normally good practice to declare all variables at the beginning of the program, unless there is a very good reason to do otherwise.
      • ( Exceptions: Loop counter variables are often declared as part of the loop structure. Occasionally it is beneficial to declare variables within a reduced scope, to be discussed later. )
  • Variables may be given an initial value at the time they are declared. This is called "initialization", or "initializing the variables".
    • Initialization in C is done using an equals sign:
    • Example: double x1 = 0.0, x2 = 0.0;
    • UNINITIALIZED VARIABLES ARE DANGEROUS, AND SHOULD BE CONSIDERED TO HOLD RANDOM VALUES.
  • Variables may be declared "const", meaning that their values cannot be changed.
    • const variables MUST be initialized at the time they are declared.
    • By convention, const variables are named using ALL CAPS.
    • Examples:
      • const double PI = 3.14159;
      • const int MAXROWS = 100;
    • Note: K&R C did not have the const keyword, and so the #define pre-processor macro was used to define constant values. The const qualifier is a better approach when it is available, because it allows the compiler to perform type checking among other reasons. For CS 107 we will defer the discussion of #define until we get to the chapter on the pre-processor.

Keywords

The following words are reserved, and may not be used as identifiers:
auto
break
case
char
const
continue
default
do
double
else
enum
extern
float
for
goto
if
inline
int
long
register
restrict
return
short
signed
sizeof
static
struct
switch
typedef
union
unsigned
void
volatile
while
_Bool
_Complex
_Imaginary

Naming Conventions

In addition to the variable naming rules imposed by the compiler, there are certain conventions that are commonly followed:
  • Ordinary variables normally begin with lower case letters:
int number, nStudents
  • Global variables ( to be covered later ) normally begin with a single capital letter:
double Coordinate, Salary;
  • Defined constants normally are in all upper case:
const double PI = 3.14159;
const int MAXROWS = 100;
  • Variable names consisting of more than one word typically capitalize successive words. This is known as "camel case":
double totalOld, totalNew, sumOfAllDigits;
  • Alternatively, where camel case is awkward or undesired, underscores may be used to indicate the start of words:
double totalOfX_coordinates, total_of_Y_coordinates
  • Integer numbers typically begin with letters I, J, K, L, M, or N. ( Remeber the first two letters in INteger. )
  • Floating point numbers typically begin with other letters:
int nStucents; // Not just students

double average, total, standardDeviation;
  • In any case, variable names should be meaningful and easily understood. ( Think complete words, not just letters, and make sure the words are meaningful and not just temp1, temp2, temp3, etc. )
 

Another Example ( Enhanced version needs to be converted from C++ to C )

Exercises

  • Write a complete C program to convert a temperature from degrees Centigrade to degrees Kelvin. The conversion is that temperatures in degrees Kelvin are exactly 273.15 higher than in degrees Centigrade.
  • Now write another program to convert from Centigrade to Fahrenheit and/or vice-versa.
  • Write a program to evaluate a quadratic or higher-order polynomial, such as Ax^2 + Bx + C. Your program will have to ask the user for each of the coefficients, as well as the value of X at which to evaluate the function.
  • Try more complex calculations, involving square roots, logarithms, trig functions, etc. ( Requires the use of the math library. #include <math.h> )

2 comments:



  1. If you ever want to change or up your university grades contact cybergolden hacker he'll get it done and show a proof of work done before payment. He's efficient, reliable and affordable. He can also perform all sorts of hacks including text, whatsapp, password decrypt,hack any mobile phone, Escape Bancruptcy, Delete Criminal Records and the rest

    Email: cybergoldenhacker at gmail dot com

    ReplyDelete
  2. Hello, Need hacking services?
    Be warned, most of these so-called hackers here are impostors, I know how real hackers work, they never advertise themselves in such a credulous manner and they are always discrete. I've been ripped off so many times out of desperation trying to get if my ex was cheating on me.finally my friend introduced me to a reliable hacker by the name Aviv Nadav who works with discretion and delivers, he does all sorts of hacks but he helped me;
    -Hacked my cheating ass girlfriend email/facebook,he's also involved with everything hack related.
    -I have made him my permanent hacker and you can as well enjoy his services.
    You can contact him at WIZARDBRIXTON AT GMAIL DOT COM for any hacking services and also endeavor to spread the good news on how he helped you. by doing that, you're stopping the scumbags who call themselves hackers out there. The dude's kinda picky so make sure to make mention of the reference. JAMES referred you. You're welcome
    Contact : WIZARDBRIXTON AT GMAIL DOT COM
    Cell Number : (+) 1807234 0428

    ReplyDelete