C Programming For Dummies
Book image
Explore Book Buy On Amazon
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.

  • The C library holds the definitions for many functions. These are prototyped in header files.

C Language Data Types

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

Type                                       Value                                                                     Range

Void None None
_Bool 0 to 1 %d
Char –128 to 127 %c
unsigned char 0 to 255 %u
short int –32,768 to 32,767 %d
unsigned short int 0 to 65,535 %u
int –2,147,483,648 to 2,147,483,647 %d
unsigned int 0 to 4,294,967,295 %u
long int –2,147,483,648 to 2,147,483,647 %ld
unsigned long int 0 to 4,294,967,295 %lu
long long –9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
%lld
unsigned long long 0 to 18,446,744,073,709,551,615 %llu
float 1.2×10–38 to 3.4×1038 %e, %f, %g
double 2.3×10–308 to 1.7×10308 %e, %f, %g
long double 3.4×10–4932 to 1.1×104932 %e, %f, %g

Keep these C language data type points in mind:

  • Ensure that you choose the proper data 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 data type stores character values, though it can also be used to store tiny integers.
  • Integers, or whole numbers, are stored in the int data types.
  • Any type of value, from the very large to the very small, and any fractional values are stored in the float and double
  • 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 data type. Instead, an array of char variables is used.
  • Other data types include structures, arrays, 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

Common C Conversion Characters

The printf(), scanf() and other 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
%% Percent character (%)
%c 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 Substitution of %f or %e, whichever is shorter (float, double)
%G Substitution of %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, lowercase (short, int, long)
%X Unsigned hexadecimal value, capital letters (short, int long)

Conversion-character formatting

The options available for conversion characters in C are extensive. The printf() man page lists many of them, with some requiring a bit of experimentation to get them correct. Generally speaking, here’s the format for the typical conversion character:

%-pw.dn

Only the first and last characters are required: % is the percent sign that prefixes all conversion characters, and n is the conversion character(s).

The minus sign; works with the w option to right-justify output.

p The padding character, which is either zero or a space, when the w option is used. The padding character is normally a space, in which case the p need not be specified. When p is 0, however, the value is padded on the left with zeroes to match the width set by the w option.

w The width option; sets the minimum number of positions in which the information is displayed. Output is right-justified unless the – prefix is used. Spaces are padded to the left, unless the p value specifies the character 0 (a zero).

.d The dot, followed by a value, d, that describes how many digits to display after the decimal in a floating-point value. If d isn’t specified, only the whole-number portion of the value appears.

n A conversion character, as shown in the table in this appendix. Or it can be the percent sign (%), in which case a % appears in the output.

The Order of Precedence in C

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

Operator(s) Category Description
() [] -> . (period) Expression Function arguments, arrays, pointer members
! ~ – + * & ++ — Unary Logical not, one’s complement, positive, negative, pointer, address-of, increment, decrement; operations 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 Ternary operator, associativity goes right-to-left
= Assignment Variable assignment operator, including += and *= and all assignment operators
, (comma) (None) Separates items in a for statement; precedence is left-to-right
  • The order of precedence goes as shown here and is overridden by using parentheses. C always executes the portion of an equation in parentheses before anything else.
  • Incremented or decremented variables as lvalues (assigned to another value) operate left-to-right. So ++var increments before its value is assigned; var++ increments its value after it’s assigned.
  • Associativity for the assignment operators moves right-to-left. For example, the operation on the right side of += happens first.
  • The order of precedence may also be referred to as the order of operations.

About This Article

This article is from the book:

About the book author:

Dan Gookin wrote the very first For Dummies book in 1991. With more than 11 million copies in print, his books have been translated into 32 languages. PCs For Dummies, now in its 12th edition, is the bestselling beginning PC book in the world. Dan offers tips, games, and fun at www.wambooli.com.

This article can be found in the category: