So C is just like batch you can input stuff, output stuff, to use that we include:
STDIO.H // Standard input/output
to output something we put printf("Hello world")
To ask the user for something we use scanf();
now to start a program.
To declare a variable.. there's :
Char, int, float that are mostly used.
a 1 in float is 1.000000
So lets make a simple program that asks for two numbers then does the sum and prints it shall we :) ?
first of all
%d = decimal ( integer )
%s = string
%c = char
%f = float
%.2f = displays two numbers after the comma
%.3f = Displays three numbers after the comma. like 1.123
\n = new line
\t = gives a little space.
#include<stdio.h>
main()
{
int a,b,c; // you can see we use a ; at the end of every line, except some statements
printf("Enter two numbers\n");
scanf("%d%d", &a,&b); The & is used to assign the value entered in %d for the variable.
c=a+b;
printf("%d", c);
}
So that's easy right ?To conceive less memory we can use instead of
int c;
unsigned c;
now to move to other statements.
IF statements :
There are logical operators :
== ( equal )
!= ( NOT equal )
< ( smaller than )
!< ( NOT smaller than )
If(condition) action else if(condition action else actionNow if the action is only 1 line of code we won't need { }
if the first statement is wrong, it checks if the second is true. If none are true it goes to the ELSE.
There's the modulo in C wich is %
fprintf("%d", 10%2);
Will display 0, Why ? well you should know what a modulo is. I'm not that good in english :PSo lets make a statement that checks if a number is dividable by two
if((number%2)==0)
printf("%d is dividable by two", number);
else
printf("%d is not dividable by two", number);
Now we go to loops.
There are 3 kinds of loops, for, while, DO.. while.
using for is like this:
for(i=0; i<10; i++)
{
printf("Hi\n");
}
same thing in while it would be like
while(i<10)
{
printf("HI\n");
i++;
}
You can see a while loop does not add anything automatically. you need to add the variable or else it would get stuck in the loop.now do while is the following:
do{
printf("Hi\n");
i++;
while(i<10);
now what it does is that it doesn't check for the condition untill it does the action Now to move on to Switch Statement:
A switch statement is just like an easier " IF else if else if else if... "
This
#include<stdio.h>
int main()
{
int c;
scanf("%d", &c);
if(c==0)
{
printf("You entered 0");
}
else if(c==1)
{
printf("You entered 1");
}
else if(c==2)
{
printf("You entered 2");
}
}
Can be replaced by
#include<stdio.h>
int main()
{
int c;
scanf("%d", &c);
switch(c)
{
case 0:
printf("You entered 0");
break;
case 1:
printf("You entered 1");
break;
case 2:
printf("You entered 2");
break;
}
}
you can see we used switch(variable)You can consider the variable as something the switch holds and checks the cases.
lets take a virtual example,
lets say if switch case 1 is true, the value is now 1(true)
So now it won't check for others. it will just keep going writing all the other cases.
Thats why we use "Break" statement, it will break the loop,if,switch statement and move on.
If case 1: is false, it won't check whats inside knowing that it won't be needing it!
So now that we know what switch is.. Lets do some examples on all those statements;
A calculator using if statement
#include <stdio.h>
int main()
{
float num1, num2;
char operation;
while (1) {
scanf("%f%c%f", &num1, &operation, &num2);
if (operation == '+')
printf("%f\n", num1+num2);
if (operation == '-')
printf("%f\n", num1-num2);
if (operation == '*')
printf("%f\n", num1*num2);
if (operation == '/')
printf("%f\n", num1/num2);
}
getchar();
return 0;
}
GUEST MADE IT NOT ME.Thats for the if, now that you know how to use switch, you should know how to change all of those if's to switch cases...
Now for using strings..
A string is like uhh a sentence !
How do we call it a string ? Easy !
char str[lengh];
keep in mind that the string will hold a '\0' so if you call the lengh 90, it will only hold 89 letters.
So how to ask the user to input a string ? there are many ways ! But the safest one is
fgets(buffer, buffer_size, stdin);
Why we use this ? because simply this limits the amount of characters to take from the user to prevent buffer overflows ( Zekedragon told me that :P )
To print a string you can either use printf("%s", str);
or puts(str);
keep in mind while using fgets when you press enter it will also grab the enter and put it in the string.
So if we put " hello man [enter] "
The string will say " Hello man \n" taking the crusor to a new line..
To clear it up we use a " LOOP "
for ( i = 0; i < stringsize; i++ )
{
if ( input[i] == '\n' )
{
input[i] = '\0';
break;
}
}
This will eliminate the new line for us :)Now of course our beloved C has made things easier for us by making a library called " string.h "Wich holds some usefull statements like
strcpy strcat strlenstrcat is short for "string concatenate"; concatenate is a fancy word that means to add to the end, or append. It adds the second string to the first string. It returns a pointer to the concatenated string. Beware this function; it assumes that dest is large enough to hold the entire contents of src as well as its own contents.
strcpy is short for string copy, which means it copies the entire contents of src into dest. The contents of dest after strcpy will be exactly the same as src such that strcmp ( dest, src ) will return 0.
strlen will return the length of a string, minus the termating character ('\0'). The size_t is nothing to worry about. Just treat it as an integer that cannot be negative, which is what it actually is. (The type size_t is just a way to indicate that the value is intended for use as a size of something.)
Now a small program to make things easier:
#include <stdio.h> /* stdin, printf, and fgets */
#include <string.h> /* for all the new-fangled string functions */
/* this function is designed to remove the newline from the end of a string
entered using fgets. Note that since we make this into its own function, we
could easily choose a better technique for removing the newline. Aren't
functions great? */
void strip_newline( char *str, int size )
{
int i;
/* remove the null terminator */
for ( i = 0; i < size; ++i )
{
if ( str[i] == '\n' )
{
str[i] = '\0';
/* we're done, so just exit the function by returning */
return;
}
}
/* if we get all the way to here, there must not have been a newline! */
}
int main()
{
char name[50];
char lastname[50];
char fullname[100]; /* Big enough to hold both name and lastname */
printf( "Please enter your name: " );
fgets( name, 50, stdin );
/* see definition above */
strip_newline( name, 50 );
/* strcmp returns zero when the two strings are equal */
if ( strcmp ( name, "Alex" ) == 0 )
{
printf( "That's my name too.\n" );
}
else
{
printf( "That's not my name.\n" );
}
// Find the length of your name
printf( "Your name is %d letters long", strlen ( name ) );
printf( "Enter your last name: " );
fgets( lastname, 50, stdin );
strip_newline( lastname, 50 );
fullname[0] = '\0';
/* strcat will look for the \0 and add the second string starting at
that location */
strcat( fullname, name ); /* Copy name into full name */
strcat( fullname, " " ); /* Separate the names by a space */
strcat( fullname, lastname ); /* Copy lastname onto the end of fullname */
printf( "Your full name is %s\n",fullname );
getchar();
return 0;
}
Credits for C programming.com - Your Resource for C and C++ Programming for the last source and string definitions :)


Sign In
Create Account


Back to top









