Welcome to PlagueFest.com! Log in or Sign up to interact with the Plague Fest community.
  1. Welcome Guest! to interact with the community and gain access to all the site's features.

Your first C program...

Discussion in Programming started by SCIFZOMBIE, Jun 6, 2018

  1. May 28, 2017
    Posts
    C (ISO 99 standard) is a low level programming language, with powerful functionality. Windows, Linux and a variety of operating systems are written largely in C. C is also used to write time-sensitive applications, manage memory and generally undertake actions unavailable in higher level languages.

    To design applications in this language, you will need a compiler. Linux distros, in general, include the GNU C Compiler (GCC). For Windows developers, you can use Visual Studio or MinGW (basically GCC ported to Windows).

    To download MinGW: http://mingw.org/ and follow the installation instructions. If you have any trouble installing the compiler, let me know and I will try to help out.

    OK folks, lets grab a coffee and get to coding. :coffee:

    While you can use an IDE, I personally use notepad when writing C applications. To start, I've created a new text file named "example.c" and opened the blank file in notepad.

    For the next part, I suggest typing the code in your IDE/text editor instead of copy/pasting it. This will give you more hands on experience with the code. IMO, reading/modifying code is a great way to learn the nuts & bolts of a language, as well as familiarize yourself with its all important syntax. (PHP developers will recognize the syntactical similarities between the two languages)

    Code:
    #include <stdio.h>
     
    /* This is a simple application that compares two integers and evaluates them with an IF statement */
    int main(){
    int var1 = 100;
    int var2 = 130;
    if(var1 > var2){
    printf(“%d is greater than %d”,var1,var2);
    }else{
    printf(“%d is less than %d”,var1,var2);
    }
    return(0);
    }
    If you used Visual Studio (vs MinGW + Notepad) compiling is pretty straightforward. Refer to VS documentation for specifics, or feel free to ask questions here and I will do my best to help you. I am very familiar with Visual Studio (with vb.NET and C#) but find MinGW more intuitive when compiling C applications due to my Linux background.

    In MinGW/GCC, you will need to run the following via the Windows command line (start->run->cmd.exe). Please note the path to your MinGW installation, as we will need it for the next steps.

    1. cd C:\mingw32path\bin {ENTER}
    2. gcc C:\Cprojectpath\example.c -o C:\Cprojectpath\example.exe {ENTER}
    3. C:\Cprojectpath\example.exe {ENTER}
    The output in your console will depend on the values specified in your code. Because these values are hard-coded, the utility of this application is pretty limited, right? Lets fix that, and provide our application a means of collecting information from users.

    Code:
    #include <stdio.h>
     
     
     
    /* This is a simple application that compares two user supplied integers and evaluates them with an IF statement */
     
    int main(){
    int var1;
    int var2;
     
    printf(“Please enter the first integer: “); //provide instructions to the user
    scanf(“%d”,&var1); //sets var1 equal to the input integer
    printf(“\n”); //line return
     
    printf(“Please enter the second integer: “); //provide instructions to the user
    scanf(“%d”,&var2); //sets var2 equal to the input integer
    printf(“\n”); //line return
     
    if(var1 > var2){
    printf(“%d is greater than %d\n\n”,var1,var2);
    }elseif(var1 = var2){
    printf(“%d is equal to %d\n\n”,var1,var2);
    }else{
    printf(“%d is less than %d\n\n”,var1,var2);
    }
    return(0);
    }
    Now, following the same steps above, re-compile the code and run your executable. When prompted, provide the application numeric input. For the sake of learning, you can also try providing non-integer input.

    Take a look at the above code, and try to figure out what it is doing and why. If you need any help, let me know! In the mean time, I will continue to introduce my fellow PF members to the art/science of programming.

    For those curious, I am fluent or professionally proficient in several languages/technologies, including: C/99, C#, VB.NET, PHP, SQL, ASM, and others. Looking forward to writing more tutorials on these languages.

    PS: You may edit, re-use, modify, share, sell or otherwise use this code in any way you see fit. I do not require attribution in any way.
    • Wizard! Wizard! x 2
    • Aug 6, 2016
      Posts
      Everything started melting and this was all I could recover...
      • Funny Funny x 2
      • Jun 17, 2014
        Posts
        Code:
        format c: /fs:NTSF
        i is anonymous
        • Zing! Zing! x 1
        • Jul 13, 2008
          Posts
          // pants C++ program
          #include <iostream>
          #include <conio.h>
          using namespace std;

          void main (void) //main program start
          {
          cout << "Hello World!"; //print stuff to screen
          _getch(); //notice the underscore here
          } //end of the code
          compile in visual studio 2017 (download the free student edition)
        • Jun 11, 2012
          Posts
          Jesus I've been doing for so long that a hello world application looks so weird.

          Also grab VSCode and a compiler. Get yourself off Windows and getchu urself a lil VM with a Ubuntu install. Windows got a headache more than @Luffaren with a Haloumi cheese overdose.

          Your life is grander when YOUR NOT CODING ON A SYSTEM THAT STILL USES C:\. WTF IS A ":" for?!?!?!
        • Nov 29, 2010
          Posts
          If you're a student, I wholly recommend using CLion, or any of Jetbrain's IDEs. Totally useful stuff and a lot of really neat built-in features.

          Otherwise, VSCode and Notepad++ are fine alternatives, as long as you're good with compiling using command line arguments

          C is an incredible language and one of my favorite languages to write in (Probably because my prof was fucking amazing) but it's also a major pain in the ass when you get into pointers and memory management. I was enjoying writing C programs to do OS calls for multithreading and the like.
        • Aug 6, 2016
          Posts
          There are "Languages" and multiple ones?....