Sunteți pe pagina 1din 2

C Reference Sheet

Getting Started:

Conditions (continued):

Complete C Program:

Compound Conditions:

comment

/* A Complete C Program */

A
false
false
true
true

directive

#include <stdio.h>

function
main()
{
printf("\nC you later\n");
}

int
float
char

1, 0, 7, -15
1.0, -12.56, 3.14
'A', 'a', '\n', '7'

switch (cResponse)
{
case 'a':
case 'A':
printf("You selected a or A\n");
break;
case 'b':
case 'B':
printf("You selected b or B\n");
break;
case 'c':
case 'C':
printf("You selected c or C\n");
break;
}

Conversion
Specifier
%d
%f
%c

Declaring Variables and Constants:


float myGuess = 0.0;
const int DAYS = 5;
Printing Values:
printf("DAYS equals %d \n", DAYS);

Loops:

Keyboard Input:
printf("Enter a number: ");
scanf("%f", &myGuess);

condition

Operators (grouped by precedence):


increment, decrement
multiply, divide, modulus
add, subtract
relational comparisons
equality comparisons
and
or
assignment

A&&B
false
false
false
true

must be an
integral type

switch Structures:

Primary Data Types:


Examples

A||B
false
true
true
true

statement

Escape Sequences: \n \t \" \\

Data Types

B
false
true
false
true

++, -*, /, %
+, >, >=, <, <=
==, !=
&&
||
=, +=, -=, *=, /=, %=

executed when
condition is
true

while (balance < TARGET)


{
year++;
balance = balance * (1 + rate / 100);
}
initialization
condition
update

for (i = 0; i < 10; i++)


{
printf("%d ", i);
}

Conditions:
if Structures:

do
{

condition

if (floor >= 13)


executed when
{
condition is
actualFloor = floor - 1;
true
}
else if (floor >= 0)
{
second condition
actualFloor = floor;
(optional)
}
else
{
printf("Floor negative \n");
}

executed at
least once

printf("Enter a positive integer: ");


scanf("%d", &input);
}
while (input <= 0);
condition

executed when
all conditions are
false (optional)

Developed for Mississippi State University's CSE1233 course


Download the latest version from http://cse.msstate.edu/~crumpton/reference

December 9, 2009

Functions:

Strings:

Function Prototypes (at beginning of the program):


function name
parameter types

return
type

int addTwoNumbers(int, int);


pass by value

void printBalance(int);
pass by reference

int userInput(float &);


void displayMenu();

C Strings are character arrays:


char fname[30];
char lname[30] = "Sawyer";

leave room for the


NULL character

Input / Output:
allows entry of a string
scanf("%s", fname);
that contains spaces
gets(lname);
printf("Hi %s %s \n", fname, lname);

String Functions ( #include <string.h> ):


s and s1 are C Strings, c is a char
strlen(s)
length of s
strcpy(s,
copy s1 to s
strcat(s,
concatenate s1 after s
strcmp(s,
compare s to s1
strchr(s,
pointer to first c in s
strstr(s,
pointer to first s1 in s

Sample Calls (in main() or another function):


displayMenu();
int answer;
answer = addTwoNumbers(3, 5);
Function Definition:
int addTwoNumbers(int a, int b)
{
int sum = 0;
sum = a + b;
return sum;
}

Arrays:
Declaration and initialization:

s1)
s1)
s1)
c)
s1)

Character Functions ( #include <ctype.h> ):


c is a char
alphanumeric?
alphabetic?
decimal digit?
whitespace?
convert to lower case
convert to upper case

isalnum(c)
isalpha(c)
isdigit(c)
isspace(c)
tolower(c)
toupper(c)

indexes 0 through 99

int dollars[100];
float values[15] = {1.1, 2.2, 3.1, -1};
Accessing individual elements:

Data Structures:
Declaring a struct:

dollars[3] = 17;

typedef struct {
int x;
int y;
} point;

for (i=0; i<15; i++)


printf("%f ", values[i]);

Declaring a variable and accessing members:

Pointers:

point first;
first.x = 1;
first.y = 4;
printf("(%d, %d) \n", first.x, first.y);

Declaration and initialization:


int a = 14;
int b = 15;
int * iPtr;
iPtr = &a;

address of operator

int * anotherPtr = &b;

File Input / Output:


Declaring a FILE pointer:
FILE * inputFile;
FILE * outputFile;

Accessing pointers and values:


// assign an address to another pointer
anotherPtr = iPtr;
// change the value stored in the memory
// location being pointed to
*iPtr = 3;
// print the address held be a pointer
printf("%x \n", iPtr);
// print the value being pointed to
printf("%d \n", *iPtr);
indirection (or dereference)
operator

Opening a file:

r for read

inputFile = fopen("file1.txt", "r");


outputFile = fopen("file2.txt", "w");
Input / Output:
fscanf(inputFile, "%d", &x);
fprintf(outputFile, "%f \n", 3.14);
Closing a file:
fclose(inputFile);
fclose(outputFile);

w for write
a for append

S-ar putea să vă placă și