Thread: question about basic programming

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    11

    question about basic programming

    Hi,
    I'm learning C programming language at the moment.

    Just out of curiosity what happens if the place holder and the argument is not compatible?
    for example if I write: printf("some text %d, "hello world");
    the output is: some text 4206628
    what does 4206628 means?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    In this case, you're lucky enough to get the decimal representation of the address in memory where "hello world" is stored.

    > Just out of curiosity what happens if the place holder and the argument is not compatible?
    Generally, all bets are off and you're on your own as to what the program may decide to do (or not do).

    Pay attention to the warnings.
    Code:
    $ cat main.c
    #include <stdio.h>
     
    int main()
    {
      printf("some text %d", "hello world");
      return 0;
    }
    $ gcc -Wall main.c
    main.c: In function ‘main’:
    main.c:5:10: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]
       printf("some text %d", "hello world");
              ^
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2016
    Posts
    11
    Quote Originally Posted by Salem View Post
    In this case, you're lucky enough to get the decimal representation of the address in memory where "hello world" is stored.

    > Just out of curiosity what happens if the place holder and the argument is not compatible?
    Generally, all bets are off and you're on your own as to what the program may decide to do (or not do).

    Pay attention to the warnings.
    Code:
    $ cat main.c
    #include <stdio.h>
     
    int main()
    {
      printf("some text %d", "hello world");
      return 0;
    }
    $ gcc -Wall main.c
    main.c: In function ‘main’:
    main.c:5:10: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]
       printf("some text %d", "hello world");
              ^
    Thanks for the explanation

  4. #4
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137
    Hello, Salem already answered your question beautifully. I just want to add one more little tidbit:

    Generally, all bets are off and you're on your own as to what the program may decide to do (or not do).
    Seems to often be called "Undefined behavior" (UB for short) in the community so if you ever hear that phrase come up or need to use it, there ya go! It just means that the C standard provides no definition as to what will happen and what will happen can vary on a case-by-base basis which obviously is a bad thing if you ever want to create usable software. I wanted to tell you because when I was brand new I kept hearing guys say "UB" and I didn't know what they were talking about.
    Last edited by Asymptotic; 12-10-2016 at 04:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 05-03-2014, 06:32 AM
  2. Basic C Programming Question
    By goldeneye007 in forum C Programming
    Replies: 1
    Last Post: 12-06-2012, 06:04 PM
  3. basic bash programming question
    By Jimb0 in forum Linux Programming
    Replies: 4
    Last Post: 05-05-2011, 10:55 PM
  4. A basic math programming question
    By hebali in forum C Programming
    Replies: 38
    Last Post: 02-25-2008, 04:18 PM
  5. Just a basic question about game programming
    By need4speed in forum Game Programming
    Replies: 5
    Last Post: 01-20-2008, 03:24 AM

Tags for this Thread