Cheat Sheet

Beginning C Programming For Dummies

The best way to learn programming is to start with a fundamental language like C. Nearly every other popular language today borrows from C. Whether you’re curious about programming, need to pass a college course, or want to start your own app business, learning C is the right place to begin.

Understanding the C Language Skeleton

Most coding starts with a C language structure. This skeleton includes the basic bones upon which most programs are written. Use this simple skeleton to get started:

#include <stdio.h>
int main()
{
    return(0);
}

Traditionally, the program begins with preprocessor directives plus prototypes. The #include statements bring in header files, such as stdio.h, the standard input/output header file.

The primary function in all C code is main(), which is the first function that's run when the program starts. The main() function is an int function, so it must return an integer value. All the function's statements are enclosed in curly brackets, or braces.

C Language Keywords

The C language keywords represent the core of the language. With the C11 revision to the language, several new keywords have been added. They're shown with leading underscores in the following table:

_Alignas break float signed
_Alignof case for sizeof
_Atomic char goto static
_Bool const if struct
_Complex continue inline switch
_Generic default int typedef
_Imaginary do long union
_Noreturn double register unsigned
_Static_assert else restrict void
_Thread_local enum return volatile
auto extern short while

Keep the following points in mind as you start programming in C:

  • Do not name any function or variable the same as a keyword.

  • You use only a few of the C language keywords in your code. Some of them, you'll probably never use.

  • Most of the work in your code is done by functions, not by keywords.

C Language Variable Types

Rather than make all your variables floats, it’s more efficient to examine the type of data that’s stored and then choose an appropriate C variable type.

Type Value Range
_Bool 0 to 1
char –28 to 127
unsigned char 0 to255
short int –32,768 to 32,767
unsigned short int 0 to 65,535
int –2,147,483,648 to 2,147,483,647
unsigned int 0 to 4,294,967,295
long int –2,147,483,648 to 2,147,483,647
unsigned long int 0 to 4,294,967,295
float 1.17×10–38 to 3.40×1038
double 2.22×10–308 to 1.79×10308

Keep these C language variable type points in mind:

  • Ensure that you choose the proper variable type for the values you need to store.

  • The _Bool type stores only two values, 0 and 1, which can represent TRUE or FALSE or On or Off or any binary condition.

  • The char variable type stores character values, though it can also be used to store tiny integers.

  • Integers, or whole numbers, are stored in the int variable types.

  • Any type of value, from the very large to the very small, and any fractional values are stored in the float and double types.

  • Remember to use int values for functions that generate integers, such as getchar(). It’s easy to assume that the function returns a char value because of the function’s name.

  • C lacks a string variable type. Instead, an array of char variables is used.

  • Other variable types include structures and pointers.

Common C Escape Sequences

When you cannot type characters into your string, use the escape sequences to insert nonprintable characters into text strings, char variables, and arrays. Here are common C escape sequences:

Characters What It Represents or Displays
\a Bell ("beep!")
\b Backspace, non-erasing
\f Form feed or clear the screen
\n Newline
\r Carriage return
\t Tab
\v Vertical tab
\\ Backslash character
\? Question mark
\' Single quote
\" Double quote
\xnn Hexadecimal character code nn
\onn Octal character code nn
\nn Octal character code nn

Using escape sequences is the secret way to embed characters you cannot type into a string, or to test for those characters in a conditional statement or for regular input. Even printable characters can be coded by using an escape sequence. The \x hexadecimal and \nn sequences can represent any ASCII character.

Common C Conversion Characters

The printf() and scanf() functions use conversion characters as placeholders for various values. Conversion characters are used to indicate a value when the function runs in the final program.

Conversion Character What It Displays
%% The percent character (%)
%c A single character (char)
%d Integer value (short, int)
%e Floating-point value in scientific notation using a little E (float, double)
%E Floating-point value in scientific notation using a big E (float, double)
%f Floating-point value in decimal notation (float, double)
%g Substitutes %f or %e, whichever is shorter (float, double)
%G Substitutes %f or %E, whichever is shorter (float, double)
%i Integer value (short, int)
%ld Long integer value (long int)
%o Unsigned octal value, no leading zero
%p Memory location in hexadecimal (*pointer)
%s String (char *)
%u Unsigned integer (unsigned short, unsigned int, unsigned long)
%x Unsigned hexadecimal value, lower case (short, int, long)
%X Unsigned hexadecimal value, capital letters (short, int long)

The Order of Precedence in C

The order of precedence determines which operators act upon a value first. When crafting statements, know the order of precedence to ensure that the program does what you intend.

Operator(s) Category Description
! Unary Logical not; associativity goes right to left
++ -- Unary Increment, decrement, read from right to left
* / % Math Multiplication, division, modulo
+ – Math Addition, subtraction
<< >> Binary Shift left, shift right
< > <= >= Comparison Less than, greater than, less than or equal to, greater than or equal to
== != Comparison Is equal to, not equal to
& Binary And
^ Binary Exclusive or (XOR)
| Binary Or
&& Logical And
|| Logical Or
?: Comparison Weird if thing; associativity goes right to left
= Assignment Variable assignment operator, including the +=, *=, and all assignment operators
, (None) The comma separates items in a for statement; precedence from left to right

The order of precedence can be overridden by using parentheses. Simply enclose within a set of parentheses the part of the equation that you want executed first. That part is executed first no matter what the priority is.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com